Files
supabase/apps/design-system/components/component-preview.tsx
hasithasandun a0a17e17e8 fix(design-system): improve responsiveness of copywriting components (#46034)
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

## What kind of change does this PR introduce?

Bug fix

## What is the current behavior?

On mobile devices, the component previews in the Design System
documentation site (`/apps/design-system`) overflow horizontally and
clip off the screen. This is caused by rigid `p-10` padding on the
preview container and hardcoded widths (`w-[400px]`, `w-[300px]`) and
inflexible flex layouts (`flex-row gap-16`) within the example
components.

## What is the new behavior?

The component previews are now fully responsive and render correctly on
mobile viewports:
- Updated the main preview container padding to be responsive (`p-4
sm:p-10`) in `component-preview.tsx` to maximize available space on
mobile.
- Replaced fixed widths with fluid max-widths (`w-full max-w-[400px]`)
in various copywriting component examples.
- Updated flex layouts that previously forced single rows to wrap
(`flex-wrap`) and scale down gaps on smaller screens (`gap-8
sm:gap-16`).
- Added `shrink-0` to icons in alerts to prevent them from squishing
when text wraps on narrow screens.

Affected components:
- `component-preview.tsx`
- `copy-button-verbs.tsx`
- `copy-confirmations.tsx`
- `copy-error-messages.tsx`
- `copy-form-labels.tsx`
- `copy-loading-states.tsx`
- `copy-success-messages.tsx`
- `copy-tooltips.tsx`

## Screenshots
### Mobile (Layout Bug - Before)
<!-- Add your broken mobile layout screenshot here -->
<img width="383" height="673" alt="Image"
src="https://github.com/user-attachments/assets/faeba844-c137-4ab2-aa2c-2299e5ae5bf0"
/>

<img width="377" height="667" alt="Image"
src="https://github.com/user-attachments/assets/e995289c-8c4c-4ba2-8fac-f36bfe869cd7"
/>

<img width="376" height="671" alt="Image"
src="https://github.com/user-attachments/assets/b08afcfc-cc03-4087-9bdc-6082ed4a0c0a"
/>

### Mobile (Responsive - After)
<!-- Add your fixed mobile layout screenshot here -->
<img width="375" height="672" alt="Image"
src="https://github.com/user-attachments/assets/54f68165-1e42-4f96-b9b8-cc6928909cbf"
/>

<img width="378" height="673" alt="Image"
src="https://github.com/user-attachments/assets/816e7103-3184-4593-9847-896e3e4736ff"
/>

<img width="372" height="665" alt="Image"
src="https://github.com/user-attachments/assets/97ba8f70-5340-4d15-92ee-1a032ee7d935"
/>

## Additional context

This addresses the layout bugs on the Design System Copywriting
documentation page.

Fixes #46033


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Style**
* Improved responsiveness across the design system with updated layout
and spacing utilities.
* Replaced several fixed-width examples with flexible full-width
containers constrained by max-widths for consistent sizing.
* Adjusted preview padding to use responsive values for better mobile
presentation.
  * Enabled flex wrapping and refined gap sizes for multi-row layouts.
* Prevented icons from shrinking in flex rows to preserve alignment and
visual consistency.

<!-- review_stack_entry_start -->

[![Review Change
Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/46034?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack)

<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-11-17 11:49:55 +11:00

197 lines
6.3 KiB
TypeScript

'use client'
import { ChevronRight, Expand } from 'lucide-react'
import * as React from 'react'
import { Button, cn, Collapsible, CollapsibleContent, CollapsibleTrigger, FloatingPlate } 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
hideCode?: boolean
padded?: boolean
squared?: boolean
}
export function ComponentPreview({
name,
children,
className,
extractClassname,
extractedClassNames,
align = 'center',
peekCode = false,
showGrid = false,
showDottedGrid = true,
wide = false,
hideCode = false,
padded = true,
squared = 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 previewClassName = className
const Preview = React.useMemo(() => {
const Component = Index[config.style][name]?.component
if (!Component) {
return (
<p className="text-sm text-muted-foreground">
Component <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-[256px] w-full justify-center',
padded && 'p-4 sm:p-10',
{
'items-center': align === 'center',
'items-start': align === 'start',
'items-end': align === 'end',
},
previewClassName
)}
>
<React.Suspense
fallback={
<div className="flex items-center text-sm text-muted-foreground">Loading...</div>
}
>
{Preview}
</React.Suspense>
</div>
</>
)
}, [Preview, align, padded, previewClassName])
const wideClasses = wide ? '2xl:-ml-20 2xl:-mr-20' : ''
if (peekCode) {
return (
<div className={cn('@container mt-4 mb-12', wideClasses)}>
<div
className={cn(
'relative overflow-hidden border-t border-l border-r bg-studio',
!squared && 'rounded-tl-md rounded-tr-md'
)}
>
{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 className="preview-grid-background"></div> */}
</div>
<div className="flex flex-col space-y-4">
<div
className={cn(
'relative',
'w-full rounded-md [&_pre]:my-0',
expand
? '[&_pre]:overflow-auto'
: 'inset-0 [&_pre]:max-h-[196px] [&_pre]:overflow-hidden',
'[&_pre]:rounded-tr-none [&_pre]:rounded-tl-none'
)}
>
{Code}
<div className="absolute bottom-0 w-full flex justify-center mb-4">
<FloatingPlate rounded="full">
<Button
className="rounded-full"
onClick={() => setExpandState(!expand)}
icon={<Expand className="text-foreground-lighter" />}
>
{expand ? 'Collapse code' : 'Expand code'}
</Button>
</FloatingPlate>
</div>
</div>
</div>
</div>
)
}
return (
<div className={cn('mt-4 mb-12', wideClasses)}>
<div
className={cn('relative overflow-hidden bg-studio', {
'rounded-tl-md rounded-tr-md border-t border-l border-r': !hideCode,
'rounded-md border': hideCode,
})}
>
{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>
{!hideCode && (
<Collapsible>
<CollapsibleTrigger
className={`
flex
gap-3 items-center
w-full
font-mono
text-xs
text-foreground-light
px-4 py-4
border border-r
group
data-closed:rounded-bl-md data-closed:rounded-br-md
`}
>
<ChevronRight
className="transition-all group-data-open:rotate-90 text-foreground-lighter"
size={14}
/>
View code
</CollapsibleTrigger>
<CollapsibleContent className="transition-all">
<div
className={cn(
'relative',
'w-full rounded-md [&_pre]:my-0',
'[&_pre]:overflow-auto',
'[&_pre]:max-h-[320px]',
'[&_pre]:rounded-tr-none [&_pre]:rounded-tl-none [&_pre]:border-t-transparent'
)}
>
{Code}
</div>
</CollapsibleContent>
</Collapsible>
)}
</div>
)
}