Files
supabase/apps/design-system/components/code-fragment.tsx
Danny White 9bd86bee93 feat(design-system): document shared inline code defaults (#51984)
## Problem

The design system repeats inline-code styles and does not document the
new shared defaults.

## Solution

Import the shared styles from #51980 and document how sizing, colour,
weight and wrapping work. Add examples of bare and linked code, tables,
long values, Tailwind overrides and `data-code-style="plain"`. Remove
repeated styles from existing examples.

Highlighted code blocks keep an explicit `text-sm` size so removing
inline-code classes does not enlarge them.

## Review instructions

This PR changes the design-system app only.

1. Open the [Layout
page](https://design-system-git-dnywh-depr-707-code-design-system-supabase.vercel.app/design-system/docs/ui-patterns/layout).
Check the inline-code examples at narrow and desktop widths in both
themes.
2. Check code in headings, long values, explicit size overrides and the
plain display example. Code should use the default foreground colour and
follow the surrounding weight unless a class overrides it.
3. Check the [chart palette
example](https://design-system-git-dnywh-depr-707-code-design-system-supabase.vercel.app/design-system/docs/ui-patterns/charts).
CSS variable names should remain `<code>` elements with their own
display styles.

| Before | After |
| --- | --- |
| <img width="1666" height="770" alt="CleanShot 2026-10-20 at 16 37
20@2x"
src="https://github.com/user-attachments/assets/2ac87442-12a9-49ab-8e4f-f82436308567"
/> | <img width="1680" height="782" alt="CleanShot 2026-10-20 at 16 36
12@2x"
src="https://github.com/user-attachments/assets/b0074844-3431-4fb9-827b-c6fd0de889fe"
/> |
| _Layout
([live](https://supabase.com/design-system/docs/ui-patterns/layout))_ |
_Layout
([preview](https://design-system-git-dnywh-depr-707-code-design-system-supabase.vercel.app/design-system/docs/ui-patterns/layout))_
|
| <img width="1072" height="476" alt="CleanShot 2026-10-20 at 16 35
14@2x"
src="https://github.com/user-attachments/assets/242bf850-58fa-4b29-a978-bd0e296c2e8b"
/> | <img width="1072" height="484" alt="CleanShot 2026-10-20 at 16 34
47@2x"
src="https://github.com/user-attachments/assets/962adef0-05b7-47ed-8fdc-5229a3a332e1"
/> |
| _Charts
([live](https://supabase.com/design-system/docs/ui-patterns/charts))_ |
_Charts
([preview](https://design-system-git-dnywh-depr-707-code-design-system-supabase.vercel.app/design-system/docs/ui-patterns/charts)):
no visual changes as `data-code-style="plain"` has been used_ |
2026-10-22 00:48:57 +00:00

103 lines
3.0 KiB
TypeScript

'use client'
import * as React from 'react'
import { cn } from 'ui'
import { Index } from '@/__registry__'
import { useConfig } from '@/hooks/use-config'
import { styles } from '@/registry/styles'
interface ComponentPreviewProps extends React.HTMLAttributes<HTMLDivElement> {
name: string
extractClassname?: boolean
extractedClassNames?: string
align?: 'center' | 'start' | 'end'
peekCode?: boolean
showGrid?: boolean
showDottedGrid?: boolean
wide?: boolean
}
export function CodeFragment({
name,
children,
className,
extractClassname,
extractedClassNames,
align = 'center',
peekCode = false,
showGrid = false,
showDottedGrid = true,
wide = false,
...props
}: ComponentPreviewProps) {
const [config] = useConfig()
const index = styles.findIndex((style) => style.name === config.style)
const Codes = React.Children.toArray(children) as React.ReactElement[]
const Code = Codes[index]
const [expand, setExpandState] = React.useState(false)
const Preview = React.useMemo(() => {
// console.log('Index', Index)
// console.log('name', name)
// console.log('config.style', config.style)
const Component = Index[config.style][name]?.component
// const Component = Index[name]?.component
if (!Component) {
return (
<p className="text-sm text-muted-foreground">
Code fragment <code>{name}</code> not found in registry.
</p>
)
}
return <Component />
}, [name, config.style])
const ComponentPreview = React.useMemo(() => {
return (
<>
<div
className={cn('preview flex min-h-[350px] w-full justify-center p-10', {
'items-center': align === 'center',
'items-start': align === 'start',
'items-end': align === 'end',
})}
>
<React.Suspense
fallback={
<div className="flex items-center text-sm text-muted-foreground">Loading...</div>
}
>
{Preview}
</React.Suspense>
</div>
</>
)
}, [Preview, align])
const wideClasses = wide ? '2xl:-ml-12 2xl:-mr-12' : ''
return (
<div className={cn('mt-4 mb-12', wideClasses)}>
<div
className={cn(
'relative rounded-md border-t border-l border-r border-b bg-studio overflow-hidden'
)}
>
{showGrid && (
<div className="pointer-events-none absolute h-full w-full bg-[linear-gradient(to_right,oklch(from_var(--foreground-default)_l_c_h_/_0.02)_1px,transparent_1px),linear-gradient(to_bottom,#80808012_1px,transparent_1px)] bg-size-[24px_24px]"></div>
)}
{showDottedGrid && (
<div className="z-0 pointer-events-none absolute h-full w-full bg-[radial-gradient(oklch(from_var(--foreground-default)_l_c_h_/_0.02)_1px,transparent_1px)] bg-size-[16px_16px] mask-[radial-gradient(ellipse_50%_50%_at_50%_50%,#000_70%,transparent_100%)]"></div>
)}
<div className="z-10 relative">{ComponentPreview}</div>
</div>
</div>
)
}