Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

code.tsx 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. 'use client'
  2. import {
  3. Children,
  4. createContext,
  5. useContext,
  6. useEffect,
  7. useRef,
  8. useState,
  9. } from 'react'
  10. import { Tab, TabGroup, TabList, TabPanel, TabPanels } from '@headlessui/react'
  11. import { Tag } from './tag'
  12. import classNames from '@/utils/classnames'
  13. import { writeTextToClipboard } from '@/utils/clipboard'
  14. import type { PropsWithChildren, ReactElement, ReactNode } from 'react'
  15. const languageNames = {
  16. js: 'JavaScript',
  17. ts: 'TypeScript',
  18. javascript: 'JavaScript',
  19. typescript: 'TypeScript',
  20. php: 'PHP',
  21. python: 'Python',
  22. ruby: 'Ruby',
  23. go: 'Go',
  24. } as { [key: string]: string }
  25. type IChildrenProps = {
  26. children: React.ReactNode
  27. [key: string]: any
  28. }
  29. function ClipboardIcon(props: any) {
  30. return (
  31. <svg viewBox="0 0 20 20" aria-hidden="true" {...props}>
  32. <path
  33. strokeWidth="0"
  34. d="M5.5 13.5v-5a2 2 0 0 1 2-2l.447-.894A2 2 0 0 1 9.737 4.5h.527a2 2 0 0 1 1.789 1.106l.447.894a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2h-5a2 2 0 0 1-2-2Z"
  35. />
  36. <path
  37. fill="none"
  38. strokeLinejoin="round"
  39. d="M12.5 6.5a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2h-5a2 2 0 0 1-2-2v-5a2 2 0 0 1 2-2m5 0-.447-.894a2 2 0 0 0-1.79-1.106h-.527a2 2 0 0 0-1.789 1.106L7.5 6.5m5 0-1 1h-3l-1-1"
  40. />
  41. </svg>
  42. )
  43. }
  44. function CopyButton({ code }: { code: string }) {
  45. const [copyCount, setCopyCount] = useState(0)
  46. const copied = copyCount > 0
  47. useEffect(() => {
  48. if (copyCount > 0) {
  49. const timeout = setTimeout(() => setCopyCount(0), 1000)
  50. return () => {
  51. clearTimeout(timeout)
  52. }
  53. }
  54. }, [copyCount])
  55. return (
  56. <button
  57. type="button"
  58. className={classNames(
  59. 'group/button absolute right-4 top-1.5 overflow-hidden rounded-full py-1 pl-2 pr-3 text-2xs font-medium opacity-0 backdrop-blur transition focus:opacity-100 group-hover:opacity-100',
  60. copied
  61. ? 'bg-emerald-400/10 ring-1 ring-inset ring-emerald-400/20'
  62. : 'hover:bg-white/7.5 dark:bg-white/2.5 bg-white/5 dark:hover:bg-white/5',
  63. )}
  64. onClick={() => {
  65. writeTextToClipboard(code).then(() => {
  66. setCopyCount(count => count + 1)
  67. })
  68. }}
  69. >
  70. <span
  71. aria-hidden={copied}
  72. className={classNames(
  73. 'pointer-events-none flex items-center gap-0.5 text-zinc-400 transition duration-300',
  74. copied && '-translate-y-1.5 opacity-0',
  75. )}
  76. >
  77. <ClipboardIcon className="h-5 w-5 fill-zinc-500/20 stroke-zinc-500 transition-colors group-hover/button:stroke-zinc-400" />
  78. Copy
  79. </span>
  80. <span
  81. aria-hidden={!copied}
  82. className={classNames(
  83. 'pointer-events-none absolute inset-0 flex items-center justify-center text-emerald-400 transition duration-300',
  84. !copied && 'translate-y-1.5 opacity-0',
  85. )}
  86. >
  87. Copied!
  88. </span>
  89. </button>
  90. )
  91. }
  92. function CodePanelHeader({ tag, label }: { tag?: string; label?: string }) {
  93. if (!tag && !label)
  94. return null
  95. return (
  96. <div className="border-b-white/7.5 bg-white/2.5 dark:bg-white/1 flex h-9 items-center gap-2 border-y border-t-transparent bg-zinc-900 px-4 dark:border-b-white/5">
  97. {tag && (
  98. <div className="dark flex">
  99. <Tag variant="small">{tag}</Tag>
  100. </div>
  101. )}
  102. {tag && label && (
  103. <span className="h-0.5 w-0.5 rounded-full bg-zinc-500" />
  104. )}
  105. {label && (
  106. <span className="font-mono text-xs text-zinc-400">{label}</span>
  107. )}
  108. </div>
  109. )
  110. }
  111. type CodeExample = {
  112. title?: string
  113. tag?: string
  114. code: string
  115. }
  116. type ICodePanelProps = {
  117. children?: React.ReactNode
  118. tag?: string
  119. label?: string
  120. code?: string
  121. title?: string
  122. targetCode?: CodeExample
  123. }
  124. function CodePanel({ tag, label, children, targetCode }: ICodePanelProps) {
  125. const child = Children.toArray(children)[0] as ReactElement<any>
  126. return (
  127. <div className="dark:bg-white/2.5 group">
  128. <CodePanelHeader
  129. tag={tag}
  130. label={label}
  131. />
  132. <div className="relative">
  133. {/* <pre className="p-4 overflow-x-auto text-xs text-white">{children}</pre> */}
  134. {/* <CopyButton code={child.props.code ?? code} /> */}
  135. {/* <CopyButton code={child.props.children.props.children} /> */}
  136. <pre className="overflow-x-auto p-4 text-xs text-white">
  137. {targetCode?.code ? (
  138. <code>{targetCode?.code}</code>
  139. ) : (
  140. child
  141. )}
  142. </pre>
  143. <CopyButton code={targetCode?.code ?? child.props.children.props.children} />
  144. </div>
  145. </div>
  146. )
  147. }
  148. type CodeGroupHeaderProps = {
  149. title?: string
  150. tabTitles?: string[]
  151. selectedIndex?: number
  152. }
  153. function CodeGroupHeader({ title, tabTitles, selectedIndex }: CodeGroupHeaderProps) {
  154. const hasTabs = (tabTitles?.length ?? 0) > 1
  155. return (
  156. <div className="flex min-h-[calc(theme(spacing.12)+1px)] flex-wrap items-start gap-x-4 border-b border-zinc-700 bg-zinc-800 px-4 dark:border-zinc-800 dark:bg-transparent">
  157. {title && (
  158. <h3 className="mr-auto pt-3 text-xs font-semibold text-white">
  159. {title}
  160. </h3>
  161. )}
  162. {hasTabs && (
  163. <TabList className="-mb-px flex gap-4 text-xs font-medium">
  164. {tabTitles!.map((tabTitle, tabIndex) => (
  165. <Tab
  166. key={tabIndex}
  167. className={classNames(
  168. 'border-b py-3 transition focus:[&:not(:focus-visible)]:outline-none',
  169. tabIndex === selectedIndex
  170. ? 'border-emerald-500 text-emerald-400'
  171. : 'border-transparent text-zinc-400 hover:text-zinc-300',
  172. )}
  173. >
  174. {tabTitle}
  175. </Tab>
  176. ))}
  177. </TabList>
  178. )}
  179. </div>
  180. )
  181. }
  182. type ICodeGroupPanelsProps = PropsWithChildren<{
  183. targetCode?: CodeExample[]
  184. [key: string]: any
  185. }>
  186. function CodeGroupPanels({ children, targetCode, ...props }: ICodeGroupPanelsProps) {
  187. if ((targetCode?.length ?? 0) > 1) {
  188. return (
  189. <TabPanels>
  190. {targetCode!.map(code => (
  191. <TabPanel>
  192. <CodePanel {...props} targetCode={code} />
  193. </TabPanel>
  194. ))}
  195. </TabPanels>
  196. )
  197. }
  198. return <CodePanel {...props} targetCode={targetCode?.[0]}>{children}</CodePanel>
  199. }
  200. function usePreventLayoutShift() {
  201. const positionRef = useRef<any>()
  202. const rafRef = useRef<any>()
  203. useEffect(() => {
  204. return () => {
  205. window.cancelAnimationFrame(rafRef.current)
  206. }
  207. }, [])
  208. return {
  209. positionRef,
  210. preventLayoutShift(callback: () => {}) {
  211. const initialTop = positionRef.current.getBoundingClientRect().top
  212. callback()
  213. rafRef.current = window.requestAnimationFrame(() => {
  214. const newTop = positionRef.current.getBoundingClientRect().top
  215. window.scrollBy(0, newTop - initialTop)
  216. })
  217. },
  218. }
  219. }
  220. function useTabGroupProps(availableLanguages: string[]) {
  221. const [preferredLanguages, addPreferredLanguage] = useState<any>([])
  222. const [selectedIndex, setSelectedIndex] = useState(0)
  223. const activeLanguage = [...(availableLanguages || [])].sort(
  224. (a, z) => preferredLanguages.indexOf(z) - preferredLanguages.indexOf(a),
  225. )[0]
  226. const languageIndex = availableLanguages?.indexOf(activeLanguage) || 0
  227. const newSelectedIndex = languageIndex === -1 ? selectedIndex : languageIndex
  228. if (newSelectedIndex !== selectedIndex)
  229. setSelectedIndex(newSelectedIndex)
  230. const { positionRef, preventLayoutShift } = usePreventLayoutShift()
  231. return {
  232. as: 'div',
  233. ref: positionRef,
  234. selectedIndex,
  235. onChange: (newSelectedIndex: number) => {
  236. preventLayoutShift(() =>
  237. (addPreferredLanguage(availableLanguages[newSelectedIndex]) as any),
  238. )
  239. },
  240. }
  241. }
  242. const CodeGroupContext = createContext(false)
  243. type CodeGroupProps = PropsWithChildren<{
  244. /** Code example(s) to display */
  245. targetCode?: string | CodeExample[]
  246. /** Example block title */
  247. title?: string
  248. /** HTTP method tag, e.g. GET, POST */
  249. tag?: string
  250. /** API path */
  251. label?: string
  252. }>
  253. export function CodeGroup({ children, title, targetCode, ...props }: CodeGroupProps) {
  254. const examples = typeof targetCode === 'string' ? [{ code: targetCode }] as CodeExample[] : targetCode
  255. const tabTitles = examples?.map(({ title }) => title || 'Code') || []
  256. const tabGroupProps = useTabGroupProps(tabTitles)
  257. const hasTabs = tabTitles.length > 1
  258. const Container = hasTabs ? TabGroup : 'div'
  259. const containerProps = hasTabs ? tabGroupProps : {}
  260. const headerProps = hasTabs
  261. ? { selectedIndex: tabGroupProps.selectedIndex, tabTitles }
  262. : {}
  263. return (
  264. <CodeGroupContext.Provider value={true}>
  265. <Container
  266. {...containerProps}
  267. className="not-prose my-6 overflow-hidden rounded-2xl bg-zinc-900 shadow-md dark:ring-1 dark:ring-white/10"
  268. >
  269. <CodeGroupHeader title={title} {...headerProps} />
  270. <CodeGroupPanels {...props} targetCode={examples}>{children}</CodeGroupPanels>
  271. </Container>
  272. </CodeGroupContext.Provider>
  273. )
  274. }
  275. type IChildProps = {
  276. children: ReactNode
  277. [key: string]: any
  278. }
  279. export function Code({ children, ...props }: IChildProps) {
  280. return <code {...props}>{children}</code>
  281. }
  282. export function Pre({ children, ...props }: IChildrenProps) {
  283. const isGrouped = useContext(CodeGroupContext)
  284. if (isGrouped)
  285. return children
  286. return <CodeGroup {...props}>{children}</CodeGroup>
  287. }
  288. export function Embed({ value, ...props }: IChildrenProps) {
  289. return <span {...props}>{value}</span>
  290. }