'use client'
import Link from 'next/link'
import { type CSSProperties, type PropsWithChildren, type ReactNode } from 'react'
import {
cn,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
IconGitHubSolid,
} from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
interface CodeSampleWrapperProps {
source: string | URL | (string | URL)[]
}
interface MultipleSourcesProps {
sources: (string | URL)[]
}
interface SingleSourceProps {
source: string | URL
}
interface SourceFrameProps {
footer: ReactNode
notchWidth?: number
}
export const SOURCE_FOOTER_CLASSES = cn(
'not-prose absolute right-0 bottom-0 z-1 flex h-8 w-(--source-notch-width) items-center gap-2 px-3 whitespace-nowrap',
'text-xs text-foreground-lighter no-underline transition-colors hover:text-foreground',
'focus-inset rounded-md'
)
const DEFAULT_NOTCH_WIDTH = 117
const getNotchOutline = (width: number): string =>
`M0 39.5A7.5 7.5 0 0 0 7.5 32V16A8.5 8.5 0 0 1 16 7.5H${width}A7.5 7.5 0 0 0 ${width + 7.5} 0`
const getNotchFocusRing = (width: number): string =>
`M0 41A9 9 0 0 0 9 32V16A7 7 0 0 1 16 9H${width}A9 9 0 0 0 ${width + 9} 0`
const getNotchMask = (width: number): string =>
`url("data:image/svg+xml,${encodeURIComponent(
``
)}")`
const getSourcePath = (source: string | URL): string => {
const [, org, repo, , , ...path] = new URL(source).pathname.split('/')
return `${org}/${repo}/${path.join('/')}`
}
export function CodeSampleDummy() {
return (
The $CodeSample directive with external repos is not supported in local
development because it relies on a GitHub API key. Please check the preview site to see the
final UI.
)
}
export function CodeSampleWrapper({
children,
/**
* A GitHub URL to the source code file.
*/
source: _source,
}: PropsWithChildren) {
const source = Array.isArray(_source) ? _source : [_source]
if (source.length === 1) {
return {children}
}
if (source.length > 1) {
return {children}
}
return <>{children}>
}
function MultipleSources({ children, sources }: PropsWithChildren) {
return (
{sources.map((source) => (
window.open(source.toString(), '_blank', 'noopener noreferrer')}
>
{getSourcePath(source)}
))}
}
>
{children}
)
}
function SingleSource({ children, source }: PropsWithChildren) {
return (
View source
}
>
{children}
)
}
export function SourceFrame({
children,
footer,
notchWidth = DEFAULT_NOTCH_WIDTH,
}: PropsWithChildren) {
const outline = getNotchOutline(notchWidth)
const notchStyle = {
'--source-notch-width': `${notchWidth}px`,
'--source-notch-mask': getNotchMask(notchWidth),
'--source-notch-mask-width': `${notchWidth + 10}px`,
} as CSSProperties
return (
)
}