Galerie aller shadcn/ui-Komponenten auf der Startseite
- Alle 63 Komponenten aus dem shadcn-Register installiert (Stil base-luma) - Galerie unter components/galerie mit 7 Kategorien und Sprungnavigation - Inhaltsbreite auf max-w-6xl erweitert - shadcn-Hauptfarbe auf E&P-Tiefblau, größter Radius eckig - App-Titel "UI-Galerie" Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
57d65f0f08
commit
78c8b702f0
79 files changed
+11234
-14
No files matched your search
@@ -0,0 +1,75 @@
|
||||
import * as React from "react"
|
||||
|
||||
import { loadHighlighter } from "@/lib/highlighter"
|
||||
|
||||
export function useHighlightedCode(value: string, language: string) {
|
||||
// A trailing newline would render as an extra empty line.
|
||||
const code = value.endsWith("\n") ? value.slice(0, -1) : value
|
||||
const [loaded, setLoaded] = React.useState<{
|
||||
language: string
|
||||
highlighter: Awaited<ReturnType<typeof loadHighlighter>>
|
||||
} | null>(null)
|
||||
const highlighter = loaded?.language === language ? loaded.highlighter : null
|
||||
|
||||
React.useEffect(() => {
|
||||
let cancelled = false
|
||||
|
||||
loadHighlighter(language)
|
||||
.then((next) => {
|
||||
if (!cancelled) {
|
||||
setLoaded({ language, highlighter: next })
|
||||
}
|
||||
})
|
||||
.catch(() => {})
|
||||
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [language])
|
||||
|
||||
// The complete lines, highlighted. While the code streams, only the new
|
||||
// lines are tokenized, starting from the grammar state of the last one.
|
||||
const [done, setDone] = React.useState<{
|
||||
highlighter: typeof highlighter
|
||||
text: string
|
||||
lines: React.ReactNode[]
|
||||
state?: Parameters<NonNullable<typeof highlighter>["highlight"]>[1]
|
||||
}>({ highlighter: null, text: "", lines: [] })
|
||||
|
||||
let next = done
|
||||
|
||||
if (next.highlighter !== highlighter || !code.startsWith(next.text)) {
|
||||
next = { highlighter, text: "", lines: [] }
|
||||
}
|
||||
|
||||
const end = code.lastIndexOf("\n") + 1
|
||||
|
||||
if (highlighter && end > next.text.length) {
|
||||
const result = highlighter.highlight(
|
||||
code.slice(next.text.length, end - 1),
|
||||
next.state
|
||||
)
|
||||
|
||||
next = {
|
||||
highlighter,
|
||||
text: code.slice(0, end),
|
||||
lines: [...next.lines, ...result.lines],
|
||||
state: result.state,
|
||||
}
|
||||
}
|
||||
|
||||
if (next !== done) {
|
||||
setDone(next)
|
||||
}
|
||||
|
||||
// The last line is still being written, so it is tokenized on every render.
|
||||
const tail = code.slice(end)
|
||||
|
||||
return React.useMemo(() => {
|
||||
if (!highlighter) {
|
||||
return code.split("\n")
|
||||
}
|
||||
|
||||
return [...next.lines, highlighter.highlight(tail, next.state).lines[0]]
|
||||
}, [code, highlighter, next, tail])
|
||||
}
|
||||
Reference in new issue
Block a user