/** * Mailpuff's design kit: the names the CRM screens take from `ui`, built on * the shadcn copies in ./ui and Bridle's tokens (src/admin/styles/theme.css). * * The props follow what the screens pass, which is what Saddle's kit * (@plugpress/ui) took before: only those are supported. Looks follow * Mailpuff's tokens (theme.css): ink primary buttons, quiet secondary ones, * segmented tabs on a faint track, white 8px surfaces with barely-there * edges on the grey page, WordPress's 2px controls, lucide at 16px. Fills that * must read on the page and on a surface alike (tracks, chips, ghost hovers) * are translucent ink rather than a muted token. */ import { forwardRef, useId, useRef, useState } from 'react'; import { Slot } from 'radix-ui'; import { Check, ChevronDown, CircleAlert, CircleCheck, CircleHelp, Info, Loader2, Search, TriangleAlert, X, } from 'lucide-react'; import { __, sprintf } from '@wordpress/i18n'; import { cn } from 'cn'; import { buttonVariants } from './ui/button'; import { Input as InputBase } from './ui/input'; import { Textarea as TextareaBase } from './ui/textarea'; import { Switch as SwitchBase } from './ui/switch'; import { CheckboxMark } from './ui/checkbox'; import { Skeleton as SkeletonBase } from './ui/skeleton'; import { Tooltip, TooltipContent, TooltipTrigger } from './ui/tooltip'; import { Dialog as DialogRoot, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from './ui/dialog'; import { DropdownMenu as DropdownRoot, DropdownMenuContent, DropdownMenuItem, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuShortcut, DropdownMenuTrigger, } from './ui/dropdown-menu'; import { Popover as PopoverRoot, PopoverContent, PopoverTrigger, } from './ui/popover'; import { BottomBar } from './bottom-bar'; import { Collapsible as CollapsibleRoot, CollapsibleContent, CollapsibleTrigger, } from './ui/collapsible'; /* ---------------------------------------------------------------- Button */ const BUTTON_VARIANT = { primary: 'default', secondary: 'outline', ghost: 'ghost', danger: 'destructive', link: 'link', }; const BUTTON_SIZE = { sm: 'sm', md: 'default', lg: 'lg' }; const BUTTON_ICON_SIZE = { sm: 'icon-sm', md: 'icon', lg: 'icon-lg' }; /** * Button: variant primary | secondary | ghost | danger | link; size sm | md | * lg; `icon` makes it square; `loading` shows a spinner and disables it; * `as` / `href` / `asChild` change the element. */ export const Button = forwardRef( function Button( { variant = 'primary', size = 'md', icon = false, loading = false, asChild = false, as, className, children, type, disabled, ...props }, ref ) { const Comp = asChild ? Slot.Root : as || ( props.href ? 'a' : 'button' ); const isButton = Comp === 'button'; const classes = cn( buttonVariants( { variant: BUTTON_VARIANT[ variant ] || 'default', size: ( icon ? BUTTON_ICON_SIZE : BUTTON_SIZE )[ size ] || 'default', } ), ( disabled || loading ) && ! isButton && 'pointer-events-none opacity-50', className ); return ( { asChild ? ( children ) : ( <> { loading && ( ); } ); /** IconButton: a square ghost button; needs an aria-label. */ export const IconButton = forwardRef( function IconButton( { variant = 'ghost', ...props }, ref ) { return ); return ( { onClick ? ( ) : ( children ) } { remove } ); } ); /* ------------------------------------------------------------------ Card */ /** Card: the one surface. Children bring their own padding (CardHeader, CardContent). */ export const Card = forwardRef( function Card( { className, ...props }, ref ) { return (
); } ); export const CardHeader = forwardRef( function CardHeader( { className, title, description, actions, children, ...props }, ref ) { return (
{ title && (

{ title }

) } { description && (

{ description }

) } { children }
{ actions && (
{ actions }
) }
); } ); /** * CardContent: a card's body. After a CardHeader it starts right under it * (the header brings its own bottom space); as the card's first child it * gets top padding of its own, so a card without a header is not flush to * its top edge. Override that with `first:pt-*`, not `pt-*`. */ export const CardContent = forwardRef( function CardContent( { className, ...props }, ref ) { return (
); } ); /** StatCard: a label over a number. `flush` drops the surface so it can sit in a StatGrid. */ export const StatCard = forwardRef( function StatCard( { label, value, delta, sub, icon, flush = false, className, ...props }, ref ) { return (
{ icon && ( ) }
{ label }
{ value }
{ has( delta ) && (
{ delta }
) } { has( sub ) && (
{ sub }
) }
); } ); /** StatGrid: StatCards in `columns` equal columns; `divided` puts hairlines between them (inside one surface). */ export const StatGrid = forwardRef( function StatGrid( { columns, min = 180, divided = false, className, style, ...props }, ref ) { return (
*]:min-w-0', className ) } style={ { gridTemplateColumns: columns ? `repeat(${ columns }, minmax(0, 1fr))` : `repeat(auto-fit, minmax(${ min }px, 1fr))`, ...style, } } { ...props } /> ); } ); /* ------------------------------------------------------------- HelpTip */ /** * HelpTip: a small "?" that shows one or two sentences of help on hover or * focus. Help goes here, not in a line under the field; longer guidance * belongs in a docs link. */ export const HelpTip = forwardRef( function HelpTip( { children, label, className, ...props }, ref ) { return ( { children } ); } ); /* ---------------------------------------------------------- Field, inputs */ /** * Field: a label (with an optional `tip`, a HelpTip beside it), the control, * a hint and an error. Prefer `tip` to `hint`. `children` can be a * function that receives { id, aria-describedby, aria-invalid, aria-required, * error } to spread on the control, so the label points at it. */ export const Field = forwardRef( function Field( { label, tip, hint, error, required = false, className, children, ...props }, ref ) { const id = useId(); const hintId = hint ? `${ id }-hint` : undefined; const errorId = error ? `${ id }-error` : undefined; const a11y = { id, 'aria-describedby': [ hintId, errorId ].filter( Boolean ).join( ' ' ) || undefined, 'aria-invalid': error ? true : undefined, 'aria-required': required || undefined, error: Boolean( error ), }; return (
{ label && (
{ tip && { tip } }
) } { typeof children === 'function' ? children( a11y ) : children } { hint && ! error && (

{ hint }

) } { error && typeof error !== 'boolean' && ( ) }
); } ); /** Input: a text field; `error` marks it invalid. */ export const Input = forwardRef( function Input( { error = false, className, ...props }, ref ) { return ( ); } ); export const Textarea = forwardRef( function Textarea( { error = false, className, ...props }, ref ) { return ( ); } ); /** NativeSelect: the browser's select with Bridle's field look; options are its children. */ export const NativeSelect = forwardRef( function NativeSelect( { error = false, className, children, ...props }, ref ) { return ( ); } ); /** SearchInput: a field with a magnifier; `wrapperClassName` styles the box around it. */ export const SearchInput = forwardRef( function SearchInput( { className, wrapperClassName, ...props }, ref ) { return ( ); } ); /** * Checkbox: a native checkbox drawn as Bridle's ink box, with an optional * label. onChange gets the change event, as for any input. */ export const Checkbox = forwardRef( function Checkbox( { label, checked, defaultChecked, indeterminate = false, error = false, onChange, disabled, className, ...props }, ref ) { const [ internal, setInternal ] = useState( Boolean( defaultChecked ) ); const controlled = checked !== undefined; const on = controlled ? Boolean( checked ) : internal; let state = on ? 'checked' : 'unchecked'; if ( indeterminate ) { state = 'mixed'; } return ( // eslint-disable-next-line jsx-a11y/label-has-associated-control -- the input is nested. ); } ); /** Switch: onChange gets the next boolean. */ export const Switch = forwardRef( function Switch( { checked, defaultChecked, onChange, size = 'md', ...props }, ref ) { return ( ); } ); /* ----------------------------------------------------------------- Tabs */ const has = ( value ) => value !== undefined && value !== null; const hasValue = ( value, items ) => items.some( ( item ) => item.value === value ); /** * FilterTabs: segmented tabs on a faint track, a white current one and * optional muted counts. * items: [ { value, label, count?, disabled? } ]. */ export const FilterTabs = forwardRef( function FilterTabs( { items = [], value, defaultValue, onChange, className, 'aria-label': ariaLabel, ...props }, ref ) { const [ internal, setInternal ] = useState( defaultValue ); const current = value !== undefined ? value : internal; const select = ( next ) => { if ( value === undefined ) { setInternal( next ); } if ( onChange ) { onChange( next ); } }; return (
{ items.map( ( item ) => { const selected = hasValue( current, items ) && item.value === current; return ( ); } ) }
); } ); /** * FilterSelect: a list's one-of filter as a quiet menu button ("Status: * All"), each choice with an optional muted count. It keeps a list's * toolbar to one row however many choices there are. * items: [ { value, label, count?, icon?, disabled? } ], or `{ heading }` * to start a section (Subscribers' view menu: Segments, Tags); `label` * names the filter. `actions` ( [ { label, icon?, onSelect } ] ) go under * the choices, after a hairline, such as New segment. */ export function FilterSelect( { label, items = [], value, onChange, actions = [], className, 'aria-label': ariaLabel, } ) { const choices = items.filter( ( item ) => ! item.heading ); const current = choices.find( ( item ) => item.value === value ) || choices[ 0 ]; return ( 0 ? 'min-w-60' : 'min-w-52' } > onChange && onChange( next ) } > { items.map( ( item, i ) => item.heading ? (
{ item.heading }
) : ( { item.label } { item.icon } { has( item.count ) && ( { item.count } ) } ) ) }
{ actions.length > 0 && ( <> { actions.map( ( action ) => ( { action.icon } { action.label } ) ) } ) }
); } /** SegmentedControl: a small switch between a few options. options: [ { value, label, disabled? } ]. */ export const SegmentedControl = forwardRef( function SegmentedControl( { options = [], value, defaultValue, onChange, className, 'aria-label': ariaLabel, ...props }, ref ) { const [ internal, setInternal ] = useState( defaultValue ); const current = value !== undefined ? value : internal; return (
{ options.map( ( option ) => { const selected = option.value === current; return ( ); } ) }
); } ); /* ------------------------------------------------------------ Lists, rows */ /** RowList: one surface with hairlines between its Rows. Inside a Card it drops its own edge. */ export const RowList = forwardRef( function RowList( { className, header, loading, loadingRows = 3, children, ...props }, ref ) { return (
{ header &&
{ header }
} { loading ? Array.from( { length: loadingRows } ).map( ( _, i ) => (
) ) : children }
); } ); /** Row: a title and second line on the left, quick actions on the right. onClick makes the row pressable. */ export const Row = forwardRef( function Row( { icon, title, description, actions, onClick, className, children, ...props }, ref ) { const activatable = Boolean( onClick ); const nested = activatable && Boolean( actions ); const pressable = activatable ? { role: 'button', tabIndex: 0, onKeyDown: ( e ) => { if ( e.key === 'Enter' || e.key === ' ' ) { e.preventDefault(); onClick( e ); } }, } : {}; return ( // eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions -- keyboard and role are added when pressable.
{ icon && ( ) }
{ children || ( <> { title && ( { title } ) } { description && ( { description } ) } ) }
{ actions && ( // eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions -- only stops the row click.
e.stopPropagation() } > { actions }
) }
); } ); /** KeyValueList: label and value pairs. items: [ { label, value } ]. */ export const KeyValueList = forwardRef( function KeyValueList( { items = [], layout = 'row', className, ...props }, ref ) { return (
{ items.map( ( item, i ) => (
{ item.label }
{ item.value }
) ) }
); } ); /** * ChecklistItem: a step with a marker. status todo | active | done | warning | * danger. `hint` (how to fix it, why it matters) sits in a `?` beside the * label, so a list of steps stays one line each. */ export const ChecklistItem = forwardRef( function ChecklistItem( { status = 'todo', label, hint, trailing, className, children, ...props }, ref ) { const done = status === 'done'; const marker = { done: 'border-primary bg-primary text-primary-foreground', active: 'border-brand bg-brand-soft', warning: 'border-amber bg-note', danger: 'border-destructive bg-destructive/10', todo: 'border-input bg-card', }[ status ]; const words = { done: __( 'Done', 'mailpuff' ), active: __( 'In progress', 'mailpuff' ), warning: __( 'Needs a look', 'mailpuff' ), danger: __( 'Failed', 'mailpuff' ), todo: __( 'To do', 'mailpuff' ), }[ status ]; return (
{ `${ words }: ` } { label } { hint && { hint } } { children }
{ has( trailing ) && (
{ trailing }
) }
); } ); /* ------------------------------------------------------------- DataTable */ /** * DataTable: a table on a surface. columns: [ { key, header, align?, render? * (row) } ]; rows; `rowId` (default row.id); `selectable` with `selected` (an * array of ids) and `onSelect( ids )`; `onRowClick( row )`; `rowActions( row )` * shows on hover or focus; `loading`; `emptyTitle` / `emptyDescription` / * `emptyIcon` / `empty`. * * `lead( row )` is a row's picture, such as a subscriber's avatar. It sits * where the select box goes and turns into the box on hover or focus, and * every row's does once anything is selected, as in Gmail, so no column is * left blank. Without a lead the select boxes stay out of sight until they * are wanted: a row's on hover or focus, the select-all box while the table * is hovered, all of them once anything is selected. Touch screens always * show them. Shift-click selects every row between two clicks. * * `bulk` ( `{ count, actions, onClear }` ) turns the header row into the * selection's bar while `count` is more than 0: "N selected", the actions * and a way to clear it, in place of the column names, so nothing floats * over the rows. Selected rows carry a soft accent. */ export function DataTable( { columns = [], rows = [], rowId = ( row ) => row.id, selectable = false, selected = [], onSelect, onRowClick, rowActions, loading = false, loadingRows = 5, empty, emptyTitle, emptyDescription, emptyIcon, footer, lead, bulk, className, } ) { // The row last ticked, and whether Shift was down, for a range. const lastPick = useRef( null ); const shift = useRef( false ); const ids = rows.map( rowId ); const picked = ids.filter( ( id ) => selected.includes( id ) ); let allState = 'unchecked'; if ( ids.length && picked.length === ids.length ) { allState = 'checked'; } else if ( picked.length ) { allState = 'mixed'; } const toggleAll = () => onSelect && onSelect( allState === 'checked' ? selected.filter( ( id ) => ! ids.includes( id ) ) : [ ...new Set( [ ...selected, ...ids ] ) ] ); const toggleOne = ( id, index ) => { if ( ! onSelect ) { return; } const on = ! selected.includes( id ); let range = [ id ]; if ( shift.current && lastPick.current !== null ) { const from = Math.min( lastPick.current, index ); const to = Math.max( lastPick.current, index ); range = ids.slice( from, to + 1 ); } lastPick.current = index; onSelect( on ? [ ...new Set( [ ...selected, ...range ] ) ] : selected.filter( ( s ) => ! range.includes( s ) ) ); }; const align = ( a ) => ( { right: 'text-right', center: 'text-center', } )[ a ] || 'text-left'; const span = columns.length + ( selectable ? 1 : 0 ) + ( rowActions ? 1 : 0 ); // Once anything is picked, every box shows, so the choice stays in view. const selecting = selected.length > 0 || Number( bulk?.count ) > 0; const barOn = selectable && Number( bulk?.count ) > 0; const selectAll = ( ); const quiet = ( reveal ) => cn( 'inline-flex transition-opacity duration-150 focus-within:opacity-100 [@media(hover:none)]:opacity-100', selecting ? 'opacity-100' : cn( 'opacity-0', reveal ) ); if ( ! loading && ! rows.length ) { return (
{ empty || ( ) }
); } return ( // Relative, so screen-reader text in the cells scrolls with the table // instead of widening the page on a phone.
{ barOn ? ( ) : ( { selectable && ( ) } { columns.map( ( col ) => ( ) ) } { rowActions && ( ) } ) } { loading ? Array.from( { length: loadingRows } ).map( ( _, r ) => ( ) ) : rows.map( ( row, index ) => { const id = rowId( row ); const isSelected = selected.includes( id ); return ( onRowClick( row ) : undefined } onKeyDown={ onRowClick ? ( e ) => { if ( e.target === e.currentTarget && ( e.key === 'Enter' || e.key === ' ' ) ) { e.preventDefault(); onRowClick( row ); } } : undefined } className={ cn( 'group/row transition-colors outline-none focus-visible:bg-secondary', onRowClick && 'cursor-pointer', isSelected ? 'bg-brand-soft/60 hover:bg-brand-soft' : onRowClick && 'hover:bg-secondary' ) } > { selectable && ( ) } { columns.map( ( col ) => ( ) ) } { rowActions && ( ) } ); } ) }
{ selectAll }
{ sprintf( /* translators: %s: number of selected items. */ __( '%s selected', 'mailpuff' ), bulk.count ) } { bulk.actions }
{ selectAll } { col.header } { __( 'Actions', 'mailpuff' ) }
{ shift.current = e.shiftKey; } } onKeyDownCapture={ () => { shift.current = false; } } onClick={ ( e ) => e.stopPropagation() } > { lead && ( { lead( row ) } ) } toggleOne( id, index ) } /> { col.render ? col.render( row ) : row[ col.key ] } e.stopPropagation() } >
{ rowActions( row ) }
{ footer && (
{ footer }
) }
); } const STATUS_DOTS = { neutral: 'bg-muted-foreground/40', accent: 'bg-brand', info: 'bg-brand', success: 'bg-success', warning: 'bg-amber', danger: 'bg-destructive', }; /** * Status: a coloured dot and a word, how a row says where something stands. * Lists use it rather than pills, so a row carries one kind of mark. * tone neutral | info | success | warning | danger. */ export function Status( { tone = 'neutral', children, className } ) { return ( ); } /** Nothing yet: a muted dash for a cell with no value. */ export function Dash() { return ( — ); } /** Pagination: previous and next with the page count. Renders nothing for a single page. */ export function Pagination( { page = 1, pageCount, onChange, className } ) { const current = page; if ( ! pageCount || pageCount <= 1 ) { return null; } const go = ( next ) => onChange && onChange( next ); return ( ); } /** BulkBar: the bottom bar while something is selected: the count, the actions, a clear. */ export function BulkBar( { count, children, onClear, className } ) { if ( ! count ) { return null; } return ( { sprintf( /* translators: %s: number of selected items. */ __( '%s selected', 'mailpuff' ), count ) } { children } ); } /* ------------------------------------------------------ Feedback, status */ const NOTICE = { info: { box: 'border-brand/15 bg-brand-soft', Icon: Info, icon: 'text-brand-ink', }, accent: { box: 'border-brand/15 bg-brand-soft', Icon: Info, icon: 'text-brand-ink', }, success: { box: 'border-border/60 bg-card', Icon: CircleCheck, icon: 'text-foreground', }, warning: { box: 'border-note-border bg-note text-note-foreground', Icon: TriangleAlert, icon: 'text-note-foreground', }, danger: { box: 'border-destructive/15 bg-destructive/5', Icon: CircleAlert, icon: 'text-destructive', }, neutral: { box: 'border-border/60 bg-card', Icon: Info, icon: 'text-muted-foreground', }, }; /** Notice: a message in the page. tone info | success | warning | danger | neutral | accent. */ export const Notice = forwardRef( function Notice( { tone = 'info', title, onDismiss, className, children, ...props }, ref ) { const t = NOTICE[ tone ] || NOTICE.info; const urgent = tone === 'danger' || tone === 'warning'; return (
); } ); /** EmptyState: nothing here yet. icon, title, description and actions are all optional. */ export const EmptyState = forwardRef( function EmptyState( { icon, illustration, title, description, actions, className, ...props }, ref ) { const art = illustration || icon; return (
{ art && ( ) } { title && (

{ title }

) } { description && (

{ description }

) } { actions && (
{ actions }
) }
); } ); /** ProgressBar: value of max. tone neutral | info | success | warning | danger; `indeterminate` for unknown. */ export const ProgressBar = forwardRef( function ProgressBar( { value = 0, max = 100, tone = 'neutral', label, showValue = false, indeterminate = false, className, ...props }, ref ) { const pct = max > 0 ? Math.min( 100, Math.max( 0, ( value / max ) * 100 ) ) : 0; const fill = { neutral: 'bg-primary', success: 'bg-primary', info: 'bg-brand', accent: 'bg-brand', warning: 'bg-amber', danger: 'bg-destructive', }[ tone ] || 'bg-primary'; return (
{ ( label || showValue ) && (
{ label } { showValue && ! indeterminate && ( { `${ Math.round( pct ) }%` } ) }
) }
); } ); /** LiveIndicator: a dot, pulsing while work is going, and a short word. */ export const LiveIndicator = forwardRef( function LiveIndicator( { tone = 'success', pulse = false, className, children, ...props }, ref ) { return (