跳至正文
获取 Pro
基础原语U.19

抽屉面板

FreeRegistry
components/linyi/sheet.tsx
@base-ui/reactclass-variance-authoritylucide-react
"use client";
 
import * as React from "react";
import { Dialog as BaseDialog } from "@base-ui/react/dialog";
import { cva, type VariantProps } from "class-variance-authority";
import { X } from "lucide-react";
import { cn } from "@/lib/cn";
 
export const Sheet = BaseDialog.Root;
export const SheetTrigger = BaseDialog.Trigger;
export const SheetClose = BaseDialog.Close;
 
export const sheetContentVariants = cva(
[
"fixed z-[var(--z-modal)] flex flex-col bg-surface text-fg border-line-strong shadow-dialog outline-none",
"transition-[transform,opacity] duration-220 ease-[cubic-bezier(0.16,1,0.3,1)] motion-reduce:transition-none",
].join(" "),
{
variants: {
side: {
right: [
"inset-y-0 right-0 h-full w-full max-w-md border-l p-5 pt-[max(1.25rem,env(safe-area-inset-top))] pb-[max(1.25rem,env(safe-area-inset-bottom))] pr-[max(1.25rem,env(safe-area-inset-right))]",
"data-[starting-style]:translate-x-full data-[starting-style]:opacity-0",
"data-[ending-style]:translate-x-full data-[ending-style]:opacity-0",
].join(" "),
left: [
"inset-y-0 left-0 h-full w-full max-w-md border-r p-5 pt-[max(1.25rem,env(safe-area-inset-top))] pb-[max(1.25rem,env(safe-area-inset-bottom))] pl-[max(1.25rem,env(safe-area-inset-left))]",
"data-[starting-style]:-translate-x-full data-[starting-style]:opacity-0",
"data-[ending-style]:-translate-x-full data-[ending-style]:opacity-0",
].join(" "),
bottom: [
"inset-x-0 bottom-0 max-h-[85vh] w-full rounded-t-panel border-t px-5 pt-4 pb-[max(1.25rem,env(safe-area-inset-bottom))]",
"data-[starting-style]:translate-y-full data-[starting-style]:opacity-0",
"data-[ending-style]:translate-y-full data-[ending-style]:opacity-0",
].join(" "),
top: [
"inset-x-0 top-0 max-h-[85vh] w-full rounded-b-panel border-b px-5 pb-5 pt-[max(1.25rem,env(safe-area-inset-top))]",
"data-[starting-style]:-translate-y-full data-[starting-style]:opacity-0",
"data-[ending-style]:-translate-y-full data-[ending-style]:opacity-0",
].join(" "),
},
},
defaultVariants: {
side: "right",
},
}
);
 
export interface SheetContentProps
extends Omit<
React.ComponentPropsWithRef<typeof BaseDialog.Popup>,
"className"
>,
VariantProps<typeof sheetContentVariants> {
className?: string;
showClose?: boolean;
}
 
export function SheetContent({
className,
side = "right",
showClose = true,
children,
...props
}: SheetContentProps) {
return (
<BaseDialog.Portal>
<BaseDialog.Backdrop className="fixed inset-0 z-[var(--z-modal)] bg-[var(--scrim-backdrop)] backdrop-blur-[4px] transition-opacity duration-200 motion-reduce:transition-none data-[starting-style]:opacity-0 data-[ending-style]:opacity-0" />
<BaseDialog.Viewport className="fixed inset-0 z-[var(--z-modal)] pointer-events-none">
<BaseDialog.Popup
className={cn(
"pointer-events-auto",
sheetContentVariants({ side }),
className
)}
{...props}
>
{side === "bottom" && (
<div
aria-hidden="true"
className="mx-auto mb-3 h-1 w-10 shrink-0 rounded-pill bg-line-strong"
/>
)}
{children}
{showClose && (
<BaseDialog.Close
aria-label="关闭抽屉"
className="absolute top-3.5 right-3.5 inline-flex size-8 items-center justify-center rounded-pill text-fg-secondary hover:text-fg hover:bg-well transition-colors cursor-pointer outline-none focus-visible:ring-2 focus-visible:ring-accent"
>
<X className="size-4" aria-hidden="true" />
</BaseDialog.Close>
)}
</BaseDialog.Popup>
</BaseDialog.Viewport>
</BaseDialog.Portal>
);
}
 
export function SheetHeader({
className,
...props
}: React.ComponentPropsWithRef<"div">) {
return <div className={cn("space-y-1.5 pr-8 shrink-0", className)} {...props} />;
}
 
export function SheetTitle({
className,
...props
}: Omit<React.ComponentPropsWithRef<typeof BaseDialog.Title>, "className"> & {
className?: string;
}) {
return (
<BaseDialog.Title
className={cn("text-sm font-semibold tracking-tight text-fg", className)}
{...props}
/>
);
}
 
export function SheetDescription({
className,
...props
}: Omit<
React.ComponentPropsWithRef<typeof BaseDialog.Description>,
"className"
> & {
className?: string;
}) {
return (
<BaseDialog.Description
className={cn("text-xs leading-relaxed text-fg-secondary", className)}
{...props}
/>
);
}
 
export function SheetBody({
className,
...props
}: React.ComponentPropsWithRef<"div">) {
return (
<div
className={cn("mt-4 flex-1 overflow-y-auto space-y-3", className)}
{...props}
/>
);
}
 
export function SheetFooter({
className,
...props
}: React.ComponentPropsWithRef<"div">) {
return (
<div
className={cn(
"mt-5 flex shrink-0 items-center justify-end gap-2 border-t border-line pt-3",
className
)}
{...props}
/>
);
}