mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-06-24 21:25:55 +00:00
* feat: add base and radix docs * feat: transform code for display * fix * fix * fix * fix * fix * chore: remove claude files * fix * fix * fix * chore: run format:write * fix * feat: add more examples * fix * feat: add aspect-ratio * feat: add avatar * feat: add badge * feat: add breadcrumb * fix * feat: add button * fix * fix * fix * feat: add calendar and card * feat: add carousel * fix: chart * feat: add checkbox * feat: add collapsible * feat: add combobox * feat: add command * feat: add context menu * feat: add data-table dialog and drawer * feat: dropdown-menu * feat: add date-picker * feat: add empty * feat: add field and hover-card * fix: input * feat: add input * feat: add input-group * feat: add input-otp * feat: add item * feat: add kbd and label * feat: add menubar * feat: add native-select * feat: add more components * feat: more components * feat: more components * feat: add skeleton, slider and sonner * feat: add spinner and switch * feat: add more components * fix: tabs * fix: tabs * feat: add docs for sidebar * fix * fix * fi * docs: update * fix: create page * fix * fix * chore: add changelog * fix
110 lines
2.6 KiB
TypeScript
110 lines
2.6 KiB
TypeScript
import fs from "node:fs/promises"
|
|
import path from "node:path"
|
|
import * as React from "react"
|
|
|
|
import { highlightCode } from "@/lib/highlight-code"
|
|
import { getDemoItem, getRegistryItem } from "@/lib/registry"
|
|
import { formatCode } from "@/lib/rehype"
|
|
import { cn } from "@/lib/utils"
|
|
import { CodeCollapsibleWrapper } from "@/components/code-collapsible-wrapper"
|
|
import { CopyButton } from "@/components/copy-button"
|
|
import { getIconForLanguageExtension } from "@/components/icons"
|
|
|
|
export async function ComponentSource({
|
|
name,
|
|
src,
|
|
title,
|
|
language,
|
|
collapsible = true,
|
|
className,
|
|
styleName = "new-york-v4",
|
|
}: React.ComponentProps<"div"> & {
|
|
name?: string
|
|
src?: string
|
|
title?: string
|
|
language?: string
|
|
collapsible?: boolean
|
|
styleName?: string
|
|
}) {
|
|
if (!name && !src) {
|
|
return null
|
|
}
|
|
|
|
let code: string | undefined
|
|
|
|
if (name) {
|
|
const item =
|
|
(await getDemoItem(name, styleName)) ??
|
|
(await getRegistryItem(name, styleName))
|
|
code = item?.files?.[0]?.content
|
|
}
|
|
|
|
if (src) {
|
|
const file = await fs.readFile(path.join(process.cwd(), src), "utf-8")
|
|
code = file
|
|
}
|
|
|
|
if (!code) {
|
|
return null
|
|
}
|
|
|
|
code = await formatCode(code, styleName)
|
|
code = code.replaceAll("/* eslint-disable react/no-children-prop */\n", "")
|
|
|
|
const lang = language ?? title?.split(".").pop() ?? "tsx"
|
|
const highlightedCode = await highlightCode(code, lang)
|
|
|
|
if (!collapsible) {
|
|
return (
|
|
<div className={cn("relative", className)}>
|
|
<ComponentCode
|
|
code={code}
|
|
highlightedCode={highlightedCode}
|
|
language={lang}
|
|
title={title}
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<CodeCollapsibleWrapper className={className}>
|
|
<ComponentCode
|
|
code={code}
|
|
highlightedCode={highlightedCode}
|
|
language={lang}
|
|
title={title}
|
|
/>
|
|
</CodeCollapsibleWrapper>
|
|
)
|
|
}
|
|
|
|
function ComponentCode({
|
|
code,
|
|
highlightedCode,
|
|
language,
|
|
title,
|
|
}: {
|
|
code: string
|
|
highlightedCode: string
|
|
language: string
|
|
title: string | undefined
|
|
}) {
|
|
return (
|
|
<figure data-rehype-pretty-code-figure="" className="[&>pre]:max-h-96">
|
|
{title && (
|
|
<figcaption
|
|
data-rehype-pretty-code-title=""
|
|
className="text-code-foreground [&_svg]:text-code-foreground flex items-center gap-2 [&_svg]:size-4 [&_svg]:opacity-70"
|
|
data-language={language}
|
|
>
|
|
{getIconForLanguageExtension(language)}
|
|
{title}
|
|
</figcaption>
|
|
)}
|
|
<CopyButton value={code} />
|
|
<div dangerouslySetInnerHTML={{ __html: highlightedCode }} />
|
|
</figure>
|
|
)
|
|
}
|