Sheet
overlaysGratisMITEt panel, der glider op fra bunden og kan trækkes ned for at lukke.
npx shadcn@latest add https://mikkelmanniche.dk/lab/r/sheet.jsonPreview
Live demo i det mørke tema.
Kode
Installér
npx shadcn@latest add https://mikkelmanniche.dk/lab/r/sheet.jsonMed registret tilføjet (components.json)
npx shadcn@latest add @manniche/sheetBrug
import { useState } from 'react'
import { Sheet } from '@/components/sheet'
export default function SheetDemo() {
const [open, setOpen] = useState(false)
return (
<>
<button type="button" className="min-h-11 rounded-xl border bg-card px-4 text-sm font-medium" onClick={() => setOpen(true)}>
Open delivery options
</button>
<Sheet open={open} onOpenChange={setOpen} title="Delivery options">
<p>Standard delivery takes 3 to 5 days and is free.</p>
<button
type="button"
onClick={() => setOpen(false)}
className="mt-4 min-h-11 w-full rounded-xl bg-primary text-sm font-medium text-primary-foreground"
>
Done
</button>
</Sheet>
</>
)
}Kilde
import { X } from 'lucide-react'
import { useEffect, useRef, type PointerEvent, type ReactNode } from 'react'
import { cn } from '@/lib/utils'
export type SheetProps = {
/** Whether the sheet is open. */
open: boolean
/** Called with the new open state when the sheet should close: the close button, Esc, a click outside or a drag down. */
onOpenChange: (open: boolean) => void
/** Accessible name, and the heading at the top of the sheet. */
title: string
/** The content below the title. */
children: ReactNode
/** Visible text and screen reader text. Key: `close` (the name of the close button). */
labels?: { close?: string }
/** Classes for the dialog. */
className?: string
}
/**
* A panel that slides up from the bottom. Close it with the button in the corner, drag it down, press Esc or tap outside.
* Built on the native <dialog>, so focus is trapped and returns to the opener on close.
*/
export function Sheet({ open, onOpenChange, title, children, labels = {}, className }: SheetProps) {
const { close = 'Close' } = labels
const dialog = useRef<HTMLDialogElement>(null)
const drag = useRef<{ y: number; id: number } | null>(null)
useEffect(() => {
const d = dialog.current
if (!d) return
if (open && !d.open) {
d.style.transform = ''
d.showModal()
} else if (!open && d.open) d.close()
}, [open])
function down(e: PointerEvent<HTMLDivElement>) {
drag.current = { y: e.clientY, id: e.pointerId }
e.currentTarget.setPointerCapture(e.pointerId)
dialog.current!.style.transition = 'none'
}
function move(e: PointerEvent<HTMLDivElement>) {
if (!drag.current) return
dialog.current!.style.transform = `translateY(${Math.max(0, e.clientY - drag.current.y)}px)`
}
function up(e: PointerEvent<HTMLDivElement>) {
if (!drag.current) return
const d = dialog.current!
const dy = e.clientY - drag.current.y
drag.current = null
d.style.transition = ''
if (dy > d.offsetHeight * 0.3) onOpenChange(false)
else d.style.transform = ''
}
return (
<dialog
ref={dialog}
onClose={() => onOpenChange(false)}
onClick={(e) => e.target === dialog.current && onOpenChange(false)}
aria-label={title}
className={cn(
'mx-auto mt-auto mb-0 w-full max-w-xl rounded-t-3xl border border-b-0 bg-card p-0 text-card-foreground shadow-2xl',
'max-h-[85dvh] overflow-hidden transition-transform duration-300 ease-out-quint motion-reduce:transition-none',
'backdrop:bg-black/40 open:animate-[manniche-sheet-in_380ms_cubic-bezier(0.23,1,0.32,1)] motion-reduce:open:animate-none',
className,
)}
>
<style href="manniche-sheet" precedence="default">
{'@keyframes manniche-sheet-in { from { transform: translateY(100%) } }'}
</style>
<div
onPointerDown={down}
onPointerMove={move}
onPointerUp={up}
onPointerCancel={up}
className="cursor-grab touch-none px-6 pt-3 pr-16 pb-2 active:cursor-grabbing"
>
<div className="mx-auto mb-4 h-1.5 w-11 rounded-full bg-border" aria-hidden />
<h2 className="text-lg font-medium">{title}</h2>
</div>
<button
type="button"
onClick={() => onOpenChange(false)}
aria-label={close}
className="absolute top-2 right-3 grid size-11 place-items-center rounded-full text-muted-foreground transition-colors duration-150 hover:bg-muted hover:text-foreground motion-reduce:transition-none"
>
<X className="size-5" aria-hidden />
</button>
<div className="max-h-[calc(85dvh-5rem)] overflow-y-auto px-6 pb-[max(2rem,env(safe-area-inset-bottom))]">{children}</div>
</dialog>
)
}Props
SheetProps
| Prop | Type | Standard | Hvad den gør |
|---|---|---|---|
open påkrævet | boolean | – | Whether the sheet is open. |
onOpenChange påkrævet | (open: boolean) => void | – | Called with the new open state when the sheet should close: the close button, Esc, a click outside or a drag down. |
title påkrævet | string | – | Accessible name, and the heading at the top of the sheet. |
children påkrævet | ReactNode | – | The content below the title. |
labels | { close?: string } | {} | Visible text and screen reader text. Key: close (the name of the close button). |
className | string | – | Classes for the dialog. |
A11y
Tastatur
Esc closes it, because it is a native modal dialog that traps focus and returns it to the opener on close. A visible 44 pixel Close button at the top right does the same with Enter or Space, and a click on the backdrop closes it too.
Skærmlæser
The dialog is labelled by title, which is also the h2 at the top. The Close button is named by labels.close. The grab handle is aria-hidden.
Mindre bevægelse
Under reduced motion the slide-in animation and the button transition are off.
Andet
Dragging the top area (handle and title) down by more than 30% of the sheet's height closes it, and a shorter drag springs back. The drag is an extra for pointer users: the Close button, Esc and a click on the backdrop do the same by keyboard.