179 lines · 8.6 KBRaw
| 1 | import { Dialog as BaseDialog } from "@base-ui/react/dialog"; |
| 2 | import { Menu } from "@base-ui/react/menu"; |
| 3 | import { Popover as BasePopover } from "@base-ui/react/popover"; |
| 4 | import { PreviewCard } from "@base-ui/react/preview-card"; |
| 5 | import { Tooltip as BaseTooltip } from "@base-ui/react/tooltip"; |
| 6 | import { CheckIcon } from "../icons"; |
| 7 | import { useRef, type ComponentProps, type ReactElement, type ReactNode } from "react"; |
| 8 | import { Link } from "../provider"; |
| 9 | import { cn } from "./cn"; |
| 10 | |
| 11 | const SURFACE = "material-popup rounded-panel text-ink shadow-surface"; |
| 12 | const SHEET = "rounded-sheet bg-raised text-ink shadow-surface"; |
| 13 | const ROW = "flex w-full cursor-pointer items-center gap-2 rounded-control px-2 py-1.5 pointer-coarse:py-2.5 text-left text-compact text-ink outline-none select-none data-[highlighted]:bg-hover data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50 [&_svg]:size-4 [&_svg]:shrink-0 [&_svg]:text-muted"; |
| 14 | |
| 15 | type Align = "start" | "center" | "end"; |
| 16 | |
| 17 | function MenuRoot({ children, onOpenChange }: { children: ReactNode; onOpenChange?: (open: boolean) => void }) { |
| 18 | return <Menu.Root onOpenChange={(open) => onOpenChange?.(open)}>{children}</Menu.Root>; |
| 19 | } |
| 20 | |
| 21 | function MenuTrigger({ render, children, ...props }: { render?: ReactElement; children?: ReactNode } & Omit<ComponentProps<typeof Menu.Trigger>, "render">) { |
| 22 | return ( |
| 23 | <Menu.Trigger render={render} {...props}> |
| 24 | {children} |
| 25 | </Menu.Trigger> |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | function MenuContent({ align = "start", side = "bottom", className, children }: { align?: Align; side?: "top" | "bottom"; className?: string; children: ReactNode }) { |
| 30 | return ( |
| 31 | <Menu.Portal> |
| 32 | <Menu.Positioner side={side} align={align} sideOffset={6} className="z-50"> |
| 33 | <Menu.Popup className={cn("popup min-w-44 p-1 outline-none", SURFACE, className)}>{children}</Menu.Popup> |
| 34 | </Menu.Positioner> |
| 35 | </Menu.Portal> |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | function MenuItem({ icon, variant, className, children, ...props }: { icon?: ReactNode; variant?: "danger" } & ComponentProps<typeof Menu.Item>) { |
| 40 | return ( |
| 41 | <Menu.Item className={cn(ROW, variant === "danger" && "text-danger [&_svg]:text-danger", className as string)} {...props}> |
| 42 | {icon} |
| 43 | {children} |
| 44 | </Menu.Item> |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | function MenuLinkItem({ href, icon, className, children }: { href: string; icon?: ReactNode; className?: string; children: ReactNode }) { |
| 49 | return ( |
| 50 | <Menu.LinkItem render={<Link href={href} />} className={cn(ROW, className)}> |
| 51 | {icon} |
| 52 | {children} |
| 53 | </Menu.LinkItem> |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | function MenuSeparator() { |
| 58 | return <Menu.Separator className="mx-1 my-1 h-px bg-hairline" />; |
| 59 | } |
| 60 | |
| 61 | function MenuCheckboxItem({ checked, onCheckedChange, closeOnClick = false, children }: { checked: boolean; onCheckedChange: (checked: boolean) => void; closeOnClick?: boolean; children: ReactNode }) { |
| 62 | return ( |
| 63 | <Menu.CheckboxItem checked={checked} onCheckedChange={(next) => onCheckedChange(next)} closeOnClick={closeOnClick} className={cn(ROW, "pl-7 relative")}> |
| 64 | <Menu.CheckboxItemIndicator className="absolute left-2 flex [&_svg]:text-ink"> |
| 65 | <CheckIcon /> |
| 66 | </Menu.CheckboxItemIndicator> |
| 67 | {children} |
| 68 | </Menu.CheckboxItem> |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | function MenuRadioGroup({ value, onValueChange, children }: { value: string; onValueChange: (value: string) => void; children: ReactNode }) { |
| 73 | return ( |
| 74 | <Menu.RadioGroup value={value} onValueChange={(next) => onValueChange(next as string)}> |
| 75 | {children} |
| 76 | </Menu.RadioGroup> |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | function MenuRadioItem({ value, children }: { value: string; children: ReactNode }) { |
| 81 | return ( |
| 82 | <Menu.RadioItem value={value} className={ROW}> |
| 83 | {children} |
| 84 | <Menu.RadioItemIndicator className="ml-auto flex [&_svg]:text-ink"> |
| 85 | <CheckIcon weight="bold" /> |
| 86 | </Menu.RadioItemIndicator> |
| 87 | </Menu.RadioItem> |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | function MenuLabel({ children }: { children: ReactNode }) { |
| 92 | return <div className="caption px-2 pt-2 pb-1 text-faint">{children}</div>; |
| 93 | } |
| 94 | |
| 95 | /** A menu of actions or links that opens from its trigger. */ |
| 96 | export const DropdownMenu = Object.assign(MenuRoot, { |
| 97 | Trigger: MenuTrigger, |
| 98 | Content: MenuContent, |
| 99 | Item: MenuItem, |
| 100 | LinkItem: MenuLinkItem, |
| 101 | Separator: MenuSeparator, |
| 102 | CheckboxItem: MenuCheckboxItem, |
| 103 | RadioGroup: MenuRadioGroup, |
| 104 | RadioItem: MenuRadioItem, |
| 105 | Label: MenuLabel, |
| 106 | }); |
| 107 | |
| 108 | const DIALOG_SIZE = { sm: "w-[min(26rem,calc(100vw-2rem))]", base: "w-[min(32rem,calc(100vw-2rem))]", lg: "w-[min(40rem,calc(100vw-2rem))]" }; |
| 109 | |
| 110 | function DialogPopup({ size = "base", className, children }: { size?: keyof typeof DIALOG_SIZE; className?: string; children: ReactNode }) { |
| 111 | const popup = useRef<HTMLDivElement>(null); |
| 112 | /** No button is the default, so Return cannot confirm or cancel by accident. A field takes focus when there is one. */ |
| 113 | const firstFocus = () => popup.current?.querySelector<HTMLElement>("input:not([type='hidden']), textarea") ?? popup.current; |
| 114 | return ( |
| 115 | <BaseDialog.Portal> |
| 116 | <BaseDialog.Backdrop className="backdrop fixed inset-0 z-50" /> |
| 117 | <BaseDialog.Popup ref={popup} initialFocus={firstFocus} className={cn("modal fixed top-[12vh] left-1/2 z-50 -translate-x-1/2 overflow-hidden outline-none max-sm:top-auto max-sm:bottom-0 max-sm:left-0 max-sm:max-h-[88svh] max-sm:w-full max-sm:translate-x-0 max-sm:overflow-y-auto max-sm:rounded-b-none max-sm:pb-[env(safe-area-inset-bottom)]", SHEET, DIALOG_SIZE[size], className)}>{children}</BaseDialog.Popup> |
| 118 | </BaseDialog.Portal> |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | /** A modal that asks for a deliberate decision, such as deleting a repository. On a phone it is a bottom sheet. */ |
| 123 | export const Dialog = Object.assign(DialogPopup, { |
| 124 | Root: BaseDialog.Root, |
| 125 | Trigger: BaseDialog.Trigger, |
| 126 | Close: BaseDialog.Close, |
| 127 | Title: ({ className, ...props }: ComponentProps<typeof BaseDialog.Title>) => <BaseDialog.Title className={cn("m-0 text-base font-semibold", className as string)} {...props} />, |
| 128 | Description: ({ className, ...props }: ComponentProps<typeof BaseDialog.Description>) => <BaseDialog.Description className={cn("m-0 text-compact text-muted", className as string)} {...props} />, |
| 129 | }); |
| 130 | |
| 131 | function PopoverContent({ align = "center", side = "bottom", className, children }: { align?: Align; side?: "top" | "bottom"; className?: string; children: ReactNode }) { |
| 132 | return ( |
| 133 | <BasePopover.Portal> |
| 134 | <BasePopover.Positioner side={side} align={align} sideOffset={8} className="z-50"> |
| 135 | <BasePopover.Popup className={cn("popup p-4 outline-none", SURFACE, className)}>{children}</BasePopover.Popup> |
| 136 | </BasePopover.Positioner> |
| 137 | </BasePopover.Portal> |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | /** Non-modal content anchored to a trigger, such as clone instructions. */ |
| 142 | export const Popover = Object.assign(BasePopover.Root, { |
| 143 | Trigger: BasePopover.Trigger, |
| 144 | Content: PopoverContent, |
| 145 | Title: ({ className, ...props }: ComponentProps<typeof BasePopover.Title>) => <BasePopover.Title className={cn("m-0 text-compact font-semibold", className as string)} {...props} />, |
| 146 | Description: ({ className, ...props }: ComponentProps<typeof BasePopover.Description>) => <BasePopover.Description className={cn("m-0 text-xs text-muted", className as string)} {...props} />, |
| 147 | }); |
| 148 | |
| 149 | /** A short label on hover or focus. A tooltip opened next to another one appears at once. */ |
| 150 | export function Tooltip({ content, side = "top", align = "center", render, className, children }: { content: ReactNode; side?: "top" | "bottom" | "left" | "right"; align?: Align; render?: ReactElement; className?: string; children?: ReactNode }) { |
| 151 | return ( |
| 152 | <BaseTooltip.Root> |
| 153 | <BaseTooltip.Trigger render={render} className={className}> |
| 154 | {children} |
| 155 | </BaseTooltip.Trigger> |
| 156 | <BaseTooltip.Portal> |
| 157 | <BaseTooltip.Positioner side={side} align={align} sideOffset={6} className="z-50"> |
| 158 | <BaseTooltip.Popup className="popup tooltip max-w-72 rounded-chip bg-primary px-2 py-1 text-xs font-medium text-on-primary shadow-[0_4px_12px_-4px_rgb(0_0_0/0.35)]">{content}</BaseTooltip.Popup> |
| 159 | </BaseTooltip.Positioner> |
| 160 | </BaseTooltip.Portal> |
| 161 | </BaseTooltip.Root> |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | export const TooltipProvider = BaseTooltip.Provider; |
| 166 | |
| 167 | /** A card that opens when the pointer rests on `render` or the keyboard focuses it. It previews what the link opens. */ |
| 168 | export function HoverCard({ render, children, className }: { render: ReactElement; children: ReactNode; className?: string }) { |
| 169 | return ( |
| 170 | <PreviewCard.Root> |
| 171 | <PreviewCard.Trigger render={render} delay={400} closeDelay={150} /> |
| 172 | <PreviewCard.Portal> |
| 173 | <PreviewCard.Positioner side="bottom" align="start" sideOffset={8} className="z-50"> |
| 174 | <PreviewCard.Popup className={cn("popup w-72 max-w-[calc(100vw-2rem)] p-4 outline-none", SURFACE, className)}>{children}</PreviewCard.Popup> |
| 175 | </PreviewCard.Positioner> |
| 176 | </PreviewCard.Portal> |
| 177 | </PreviewCard.Root> |
| 178 | ); |
| 179 | } |