Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 495da1c

Browse files
committed
install nitric component
1 parent 56fe0e7 commit 495da1c

File tree

5 files changed

+108
-44
lines changed

5 files changed

+108
-44
lines changed

src/components/InstallNitric.tsx

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React, { Suspense } from 'react'
2+
import { highlight } from 'codehike/code'
3+
import { Schema } from '../code/CodeWithTabs'
4+
import { parseProps } from 'codehike/blocks'
5+
import CODE_THEME from '@/components/code/theme'
6+
import { InstallNitricTabs } from './InstallNitricTabs.client'
7+
import Pre from '../code/Pre'
8+
import CodeContainer from '../code/CodeContainer'
9+
10+
export const InstallNitric: React.FC = async (props) => {
11+
const { tabs } = parseProps(props, Schema)
12+
13+
const highlighted = await Promise.all(
14+
tabs.map((tab) => highlight(tab, CODE_THEME)),
15+
)
16+
17+
return (
18+
<CodeContainer>
19+
<Suspense fallback={<Pre highlighted={highlighted[0]} {...props} />}>
20+
<InstallNitricTabs highlighted={highlighted} tabs={tabs} />
21+
</Suspense>
22+
</CodeContainer>
23+
)
24+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use client'
2+
3+
import React, { useEffect, useState } from 'react'
4+
import { HighlightedCode, RawCode } from 'codehike/code'
5+
import { Tabs, TabsContent, TabsList, TabsTrigger } from '../ui/tabs'
6+
import Pre from '../code/Pre'
7+
8+
interface InstallNitricProps {
9+
highlighted: HighlightedCode[]
10+
tabs: RawCode[]
11+
}
12+
13+
// this function detects the operating system of the user and returns the name that will be used to sort the instructions
14+
function detectOS() {
15+
const userAgent = window.navigator.userAgent
16+
if (userAgent.includes('Mac')) {
17+
return 'macOS'
18+
} else if (userAgent.includes('Win')) {
19+
return 'Windows'
20+
}
21+
22+
return 'Linux'
23+
}
24+
25+
export const InstallNitricTabs: React.FC<InstallNitricProps> = ({
26+
highlighted,
27+
tabs,
28+
}) => {
29+
const [value, setValue] = useState('')
30+
31+
useEffect(() => {
32+
// this will only run on the client side
33+
const OS = detectOS()
34+
35+
highlighted.some((code) => {
36+
// check based on the title prop of the children with the detected OS
37+
if (code.meta === OS) {
38+
setValue(code.meta)
39+
return true
40+
}
41+
})
42+
}, [])
43+
44+
if (!value) return null
45+
46+
return (
47+
<Tabs value={value} onValueChange={setValue}>
48+
<TabsList className="mt-auto h-10 bg-transparent">
49+
{tabs.map((tab) => (
50+
<TabsTrigger
51+
key={tab.meta}
52+
value={tab.meta}
53+
className="group/tab relative hover:text-zinc-200 data-[state=active]:bg-transparent data-[state=active]:text-primary-300"
54+
>
55+
{tab.meta}
56+
<div className="absolute inset-x-2 -bottom-[4.5px] h-px bg-primary-300 opacity-0 transition-opacity group-data-[state=active]/tab:opacity-100" />
57+
</TabsTrigger>
58+
))}
59+
</TabsList>
60+
{tabs.map((tab, i) => (
61+
<TabsContent
62+
key={tab.meta}
63+
value={tab.meta}
64+
className="m-0 border-t border-zinc-300/10"
65+
>
66+
<Pre highlighted={highlighted[i]} />
67+
</TabsContent>
68+
))}
69+
</Tabs>
70+
)
71+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { InstallNitric } from './InstallNitric'

src/components/code/CodeWithTabs.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import React from 'react'
22
import { Block, CodeBlock, parseProps } from 'codehike/blocks'
33
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
4-
import { highlight, RawCode } from 'codehike/code'
4+
import { highlight, HighlightedCode, RawCode } from 'codehike/code'
55
import { z } from 'zod'
66
import Pre from './Pre'
77
import CodeContainer from './CodeContainer'
88
import CODE_THEME from './theme'
99

10-
const Schema = Block.extend({ tabs: z.array(CodeBlock) })
10+
export const Schema = Block.extend({ tabs: z.array(CodeBlock) })
1111
export async function CodeWithTabs(props: unknown) {
1212
const { tabs } = parseProps(props, Schema)
13-
return <CodeTabs tabs={tabs} />
14-
}
1513

16-
export async function CodeTabs(props: { tabs: RawCode[] }) {
17-
const { tabs } = props
1814
const highlighted = await Promise.all(
1915
tabs.map((tab) => highlight(tab, CODE_THEME)),
2016
)
17+
18+
return <CodeTabs tabs={tabs} highlighted={highlighted} />
19+
}
20+
21+
export function CodeTabs(props: {
22+
tabs: RawCode[]
23+
highlighted: HighlightedCode[]
24+
}) {
25+
const { tabs, highlighted } = props
26+
2127
return (
2228
<CodeContainer>
2329
<Tabs defaultValue={tabs[0]?.meta}>

0 commit comments

Comments
 (0)