Restructure the repo into npm workspaces so the component kit publishes as its own package, like MUI and shadcn/ui: - packages/ui: @openkhalix/tansy-ui (published), with components, stories, tests, tokens and the library build - packages/brand: private logo and catalogue data shared by both apps - apps/site: the landing page - apps/docs: Storybook The package: - MIT licence (copyright OpenKhalix) - per-component ES modules and type declarations - "use client" on component modules so they import into Next.js Server Components; helpers stay server-safe - tokens.css for Tailwind v4 apps, and a prebuilt styles.css for apps without Tailwind - lucide-react 1.x, whose types compile against current React 19 types The apps resolve the package through a "source" export condition, so they hot-reload component edits, while outside users get dist/. scripts/test-package.mjs packs the package and installs the tarball into Vite + Tailwind (strict types), plain React and Next.js apps; each must build and render correctly. Gitea workflows run CI on push and publish to npm when a vX.Y.Z tag matching the package version is pushed.
246 lines
8.0 KiB
TypeScript
246 lines
8.0 KiB
TypeScript
import { Dialog as BaseDialog } from '@base-ui/react/dialog'
|
||
import { X } from 'lucide-react'
|
||
import {
|
||
useCallback,
|
||
useEffect,
|
||
useState,
|
||
type ComponentProps,
|
||
type ReactElement,
|
||
type ReactNode,
|
||
} from 'react'
|
||
import { cn } from '../lib/cn'
|
||
import { focusRing } from '../lib/styles'
|
||
import { IconButton } from './Button'
|
||
import {
|
||
DIALOG_SIZES,
|
||
backdropClass,
|
||
dialogDescriptionClass,
|
||
dialogPanelClass,
|
||
dialogTitleClass,
|
||
type DialogSize,
|
||
} from './dialog.tokens'
|
||
|
||
type OpenProps = {
|
||
/** Controlled open state. Pair with `onOpenChange`. */
|
||
open?: boolean
|
||
/** Initial open state when uncontrolled. */
|
||
defaultOpen?: boolean
|
||
/** Fires with the next open state: trigger press, Escape, outside click or a close button. */
|
||
onOpenChange?: (open: boolean) => void
|
||
}
|
||
|
||
export type DialogRootProps = OpenProps & {
|
||
/** Close when the backdrop is clicked. Escape always closes. */
|
||
closeOnOutsideClick?: boolean
|
||
/** `false` lets the page behind stay interactive; the default traps focus and locks scroll. */
|
||
modal?: boolean
|
||
children?: ReactNode
|
||
}
|
||
|
||
/** Holds the open state. Wrap Trigger and Content in it for a custom layout. */
|
||
function DialogRoot({ closeOnOutsideClick = true, modal = true, onOpenChange, children, ...props }: DialogRootProps) {
|
||
return (
|
||
<BaseDialog.Root
|
||
{...props}
|
||
modal={modal}
|
||
disablePointerDismissal={!closeOnOutsideClick}
|
||
onOpenChange={onOpenChange ? (open) => onOpenChange(open) : undefined}
|
||
>
|
||
{children}
|
||
</BaseDialog.Root>
|
||
)
|
||
}
|
||
|
||
/**
|
||
* Opens the dialog. Pass your button as `render` (`<Dialog.Trigger render={<Button>Edit</Button>} />`)
|
||
* so it keeps the Tansy styles.
|
||
*/
|
||
function DialogTrigger(props: BaseDialog.Trigger.Props) {
|
||
return <BaseDialog.Trigger {...props} />
|
||
}
|
||
|
||
export type DialogContentProps = Omit<BaseDialog.Popup.Props, 'className'> & {
|
||
/** Panel width. `full` fills the screen. */
|
||
size?: DialogSize
|
||
/** Hide the × button in the corner. Escape still closes. */
|
||
hideCloseButton?: boolean
|
||
className?: string
|
||
}
|
||
|
||
/** Portal, backdrop and the panel. Put Header, Body and Footer inside. */
|
||
function DialogContent({ size = 'md', hideCloseButton = false, className, children, ...props }: DialogContentProps) {
|
||
return (
|
||
<BaseDialog.Portal>
|
||
<BaseDialog.Backdrop className={cn(backdropClass, 'z-50')} />
|
||
<BaseDialog.Viewport
|
||
className={cn(
|
||
'fixed inset-0 z-50 flex items-center justify-center',
|
||
size === 'full' ? 'p-0 sm:p-6' : 'p-4 sm:p-6',
|
||
)}
|
||
>
|
||
<BaseDialog.Popup className={cn(dialogPanelClass, DIALOG_SIZES[size], className)} {...props}>
|
||
{children}
|
||
{!hideCloseButton && (
|
||
<BaseDialog.Close
|
||
render={<IconButton aria-label="Close" size="sm" icon={<X aria-hidden="true" />} />}
|
||
className="absolute right-3 top-3 sm:right-4 sm:top-4"
|
||
/>
|
||
)}
|
||
</BaseDialog.Popup>
|
||
</BaseDialog.Viewport>
|
||
</BaseDialog.Portal>
|
||
)
|
||
}
|
||
|
||
/** Title and description block at the top. It stays put while the body scrolls. */
|
||
export function DialogHeader({ className, ...props }: ComponentProps<'div'>) {
|
||
return <div className={cn('flex shrink-0 flex-col gap-1 px-5 pt-5 pr-14 sm:px-6 sm:pt-6 sm:pr-16', className)} {...props} />
|
||
}
|
||
|
||
/** The dialog's accessible name. Renders an `<h2>`. */
|
||
function DialogTitle({ className, ...props }: Omit<BaseDialog.Title.Props, 'className'> & { className?: string }) {
|
||
return <BaseDialog.Title className={cn(dialogTitleClass, className)} {...props} />
|
||
}
|
||
|
||
/** Supporting text, announced with the title. */
|
||
function DialogDescription({
|
||
className,
|
||
...props
|
||
}: Omit<BaseDialog.Description.Props, 'className'> & { className?: string }) {
|
||
return <BaseDialog.Description className={cn(dialogDescriptionClass, className)} {...props} />
|
||
}
|
||
|
||
/**
|
||
* The scrolling middle of a dialog or drawer. When its content overflows it
|
||
* becomes keyboard-focusable (so it can be scrolled without a mouse) and
|
||
* draws a rule under the header.
|
||
*/
|
||
export function DialogBody({ className, ref, ...props }: ComponentProps<'div'>) {
|
||
const [node, setNode] = useState<HTMLDivElement | null>(null)
|
||
const [scrollable, setScrollable] = useState(false)
|
||
|
||
const setRefs = useCallback(
|
||
(element: HTMLDivElement | null) => {
|
||
setNode(element)
|
||
if (typeof ref === 'function') ref(element)
|
||
else if (ref) ref.current = element
|
||
},
|
||
[ref],
|
||
)
|
||
|
||
useEffect(() => {
|
||
if (!node) return
|
||
const measure = () => setScrollable(node.scrollHeight > node.clientHeight + 1)
|
||
measure()
|
||
const observer = new ResizeObserver(measure)
|
||
observer.observe(node)
|
||
for (const child of Array.from(node.children)) observer.observe(child)
|
||
return () => observer.disconnect()
|
||
}, [node])
|
||
|
||
return (
|
||
<div
|
||
ref={setRefs}
|
||
tabIndex={scrollable ? 0 : undefined}
|
||
data-scrollable={scrollable || undefined}
|
||
className={cn(
|
||
'min-h-0 flex-1 overflow-y-auto overscroll-contain px-5 pb-5 pt-4 text-sm leading-6 text-fg first:pt-5 sm:px-6 sm:pb-6 sm:first:pt-6',
|
||
'data-scrollable:mt-4 data-scrollable:border-t data-scrollable:border-border data-scrollable:first:mt-0 data-scrollable:first:border-t-0',
|
||
focusRing,
|
||
'focus-visible:-outline-offset-2',
|
||
className,
|
||
)}
|
||
{...props}
|
||
/>
|
||
)
|
||
}
|
||
|
||
/** Actions at the bottom, right-aligned. Stacks full-width on phones, primary action on top. */
|
||
export function DialogFooter({ className, ...props }: ComponentProps<'div'>) {
|
||
return (
|
||
<div
|
||
className={cn(
|
||
'flex shrink-0 flex-col-reverse gap-2 border-t border-border bg-surface-muted px-5 py-4 sm:flex-row sm:items-center sm:justify-end sm:gap-3 sm:px-6',
|
||
className,
|
||
)}
|
||
{...props}
|
||
/>
|
||
)
|
||
}
|
||
|
||
/** Closes the dialog. Pass a Button as `render`. */
|
||
function DialogClose(props: BaseDialog.Close.Props) {
|
||
return <BaseDialog.Close {...props} />
|
||
}
|
||
|
||
export type DialogProps = DialogRootProps & {
|
||
/** Element that opens the dialog, usually a `<Button>`. Leave out when you control `open` yourself. */
|
||
trigger?: ReactElement
|
||
/** Heading; also the dialog's accessible name. */
|
||
title?: ReactNode
|
||
/** Supporting text under the title. */
|
||
description?: ReactNode
|
||
/** Panel width. `full` fills the screen. */
|
||
size?: DialogSize
|
||
/** Action buttons, shown in a bar under the body. */
|
||
footer?: ReactNode
|
||
/** Hide the × button in the corner. Escape still closes. */
|
||
hideCloseButton?: boolean
|
||
/** Accessible name when there is no `title`. */
|
||
'aria-label'?: string
|
||
/** Classes for the panel. */
|
||
className?: string
|
||
/** The body. Scrolls when it's taller than the screen; header and footer stay visible. */
|
||
children?: ReactNode
|
||
}
|
||
|
||
/**
|
||
* A modal dialog (Base UI Dialog). Pass `title`, `children` and `footer` for
|
||
* the common case, or build a custom layout from `Dialog.Root`,
|
||
* `Dialog.Trigger`, `Dialog.Content`, `Dialog.Header`, `Dialog.Title`,
|
||
* `Dialog.Description`, `Dialog.Body`, `Dialog.Footer` and `Dialog.Close`.
|
||
*/
|
||
function DialogComponent({
|
||
trigger,
|
||
title,
|
||
description,
|
||
size = 'md',
|
||
footer,
|
||
hideCloseButton,
|
||
className,
|
||
children,
|
||
'aria-label': ariaLabel,
|
||
...rootProps
|
||
}: DialogProps) {
|
||
return (
|
||
<DialogRoot {...rootProps}>
|
||
{trigger && <DialogTrigger render={trigger} />}
|
||
<DialogContent size={size} hideCloseButton={hideCloseButton} className={className} aria-label={ariaLabel}>
|
||
{(title != null || description != null) && (
|
||
<DialogHeader>
|
||
{title != null && <DialogTitle>{title}</DialogTitle>}
|
||
{description != null && <DialogDescription>{description}</DialogDescription>}
|
||
</DialogHeader>
|
||
)}
|
||
{children != null && <DialogBody>{children}</DialogBody>}
|
||
{footer != null && <DialogFooter>{footer}</DialogFooter>}
|
||
</DialogContent>
|
||
</DialogRoot>
|
||
)
|
||
}
|
||
|
||
export const Dialog = Object.assign(DialogComponent, {
|
||
Root: DialogRoot,
|
||
Trigger: DialogTrigger,
|
||
Content: DialogContent,
|
||
Header: DialogHeader,
|
||
Title: DialogTitle,
|
||
Description: DialogDescription,
|
||
Body: DialogBody,
|
||
Footer: DialogFooter,
|
||
Close: DialogClose,
|
||
})
|
||
|
||
/** Alias of `Dialog`, for teams that call it a modal. */
|
||
export const Modal = Dialog
|