@cerberus-design/react
Version:
The Cerberus Design React component library.
1 lines • 18.5 kB
Source Map (JSON)
{"version":3,"sources":["../../../../src/components/table/index.ts","../../../../src/components/table/primitives.tsx","../../../../src/system/primitive-factory.tsx","../../../../src/system/index.ts","../../../../src/components/table/parts.ts","../../../../src/components/show/show.tsx","../../../../src/components/table/table.tsx"],"sourcesContent":["export * from './parts'\nexport * from './primitives'\nexport * from './table'\n","import { ark, type HTMLArkProps } from '@ark-ui/react/factory'\nimport { table, type TableVariantProps } from 'styled-system/recipes'\nimport {\n createCerberusPrimitive,\n type CerberusPrimitiveProps,\n} from '../../system/index'\n\n/**\n * This module contains the primitives of the Table component.\n * @module 'table/primitives'\n */\n\nconst { withSlotRecipe } = createCerberusPrimitive(table)\n\n// Root\n\nexport type TableRootProps = CerberusPrimitiveProps<\n HTMLArkProps<'div'> & TableVariantProps\n>\nexport const TableRoot = withSlotRecipe<TableRootProps>(ark.div, 'root', {\n defaultProps: {\n 'data-scope': 'table',\n 'data-part': 'root',\n },\n})\n\n// TableEl\n\nexport type TableElProps = CerberusPrimitiveProps<HTMLArkProps<'table'>>\nexport const TableEl = withSlotRecipe<TableElProps>(ark.table, 'table', {\n defaultProps: {\n 'data-scope': 'table',\n 'data-part': 'table',\n },\n})\n\n// Caption\n\nexport type CaptionProps = CerberusPrimitiveProps<HTMLArkProps<'caption'>>\nexport const Caption = withSlotRecipe<CaptionProps>(ark.caption, 'caption', {\n defaultProps: {\n 'data-scope': 'table',\n 'data-part': 'caption',\n },\n})\n\n// Thead\n\nexport type TheadProps = CerberusPrimitiveProps<HTMLArkProps<'thead'>>\nexport const Thead = withSlotRecipe<TheadProps>(ark.thead, 'thead', {\n defaultProps: {\n 'data-scope': 'table',\n 'data-part': 'header',\n },\n})\n\n// Th\n\nexport type ThProps = CerberusPrimitiveProps<HTMLArkProps<'th'>>\nexport const Th = withSlotRecipe<ThProps>(ark.th, 'th', {\n defaultProps: {\n 'data-scope': 'table',\n 'data-part': 'header-col',\n },\n})\n\n// Tbody\n\nexport type TbodyProps = CerberusPrimitiveProps<HTMLArkProps<'tbody'>>\nexport const Tbody = withSlotRecipe<TbodyProps>(ark.tbody, 'tbody', {\n defaultProps: {\n 'data-scope': 'table',\n 'data-part': 'body',\n },\n})\n\n// Tr\nexport type TrProps = CerberusPrimitiveProps<HTMLArkProps<'tr'>>\nexport const Tr = withSlotRecipe<TrProps>(ark.tr, 'tr', {\n defaultProps: {\n 'data-scope': 'table',\n 'data-part': 'row',\n },\n})\n\n// Td\n\nexport type TdProps = CerberusPrimitiveProps<HTMLArkProps<'td'>>\nexport const Td = withSlotRecipe<TdProps>(ark.td, 'td', {\n defaultProps: {\n 'data-scope': 'table',\n 'data-part': 'cell',\n },\n})\n\n// Tfoot\n\nexport type TfootProps = CerberusPrimitiveProps<HTMLArkProps<'tfoot'>>\nexport const Tfoot = withSlotRecipe<TfootProps>(ark.tfoot, 'tfoot', {\n defaultProps: {\n 'data-scope': 'table',\n 'data-part': 'footer',\n },\n})\n\n// TableTrigger\n\nexport type TableTriggerProps = CerberusPrimitiveProps<HTMLArkProps<'button'>>\nexport const TableTrigger = withSlotRecipe<TableTriggerProps>(\n ark.button,\n 'trigger',\n {\n defaultProps: {\n 'data-scope': 'table',\n 'data-part': 'trigger',\n },\n },\n)\n","import { css, cx } from 'styled-system/css'\nimport type { RecipeVariantRecord } from 'styled-system/types'\nimport {\n type ComponentType,\n type ElementType,\n type HTMLAttributes,\n type PropsWithChildren,\n} from 'react'\nimport type { WithCss } from '../types'\nimport type {\n CerberusPrimitiveEl,\n CerberusPrimitiveRecipe,\n CerberusRecipe,\n CerberusSlotRecipe,\n WithRecipeOptions,\n} from './types'\n\n/**\n * This module contains a factory for creating Cerberus primitives.\n * @module @cerberus/core/system/factory\n */\n\nexport class CerberusPrimitive {\n recipe: CerberusPrimitiveRecipe | null\n\n constructor(recipe?: CerberusPrimitiveRecipe) {\n this.recipe = recipe ?? null\n }\n\n private hasStyles(styles: string | undefined): Record<string, unknown> {\n if (styles) {\n return {\n className: styles,\n }\n }\n return {}\n }\n\n private validateComponent<P extends HTMLAttributes<unknown>>(\n Component: ComponentType<P> | string,\n ) {\n if (typeof Component !== 'function' && typeof Component !== 'object') {\n return false\n }\n return true\n }\n\n /**\n * Creates a Cerberus component with bare features and no recipe.\n * @param Component - The React component to enhance with Cerberus features.\n * Can be a string or a component reference.\n * @returns A new React component that applies Cerberus features to the\n * original component.\n *\n * @example\n * ```ts\n * const { withNoRecipe } = createCerberusPrimitive(buttonRecipe)\n * const Button = withNoRecipe('button')\n * ```\n */\n withNoRecipe = <P extends HTMLAttributes<unknown>>(\n Component: ComponentType<P> | string,\n options?: WithRecipeOptions,\n ): CerberusPrimitiveEl<P> => {\n const { defaultProps } = options || {}\n const El = Component as ComponentType<P> | ElementType\n\n const CerbComponent = (props: PropsWithChildren<P> & WithCss) => {\n const { css: customCss, className, ...nativeProps } = props\n const styles = this.hasStyles(cx(className, css(customCss)))\n return <El {...defaultProps} {...styles} {...(nativeProps as P)} />\n }\n\n if (this.validateComponent(El)) {\n const ElName = typeof El === 'string' ? El : El.displayName || El.name\n CerbComponent.displayName = ElName\n }\n\n return CerbComponent\n }\n\n /**\n * Creates a Cerberus component with the given recipe.\n * @param Component - The React component to enhance with the recipe.\n * @param options - Options for the recipe.\n * @returns A new React component that applies the recipe to the original\n * component.\n */\n withRecipe = <P extends HTMLAttributes<unknown>>(\n Component: ComponentType<P> | string,\n options?: WithRecipeOptions,\n ): CerberusPrimitiveEl<P & WithRecipeOptions['defaultProps']> => {\n const { defaultProps } = options || {}\n const El = Component as ComponentType<P> | ElementType\n\n const recipe = this.recipe as CerberusRecipe\n\n const CerbComponent = (internalProps: PropsWithChildren<P> & WithCss) => {\n const {\n css: customCss,\n className,\n ...restOfInternalProps\n } = internalProps\n\n const [variantOptions, nativeProps] =\n recipe.splitVariantProps(restOfInternalProps)\n const recipeStyles = recipe(variantOptions)\n\n return (\n <Component\n {...defaultProps}\n {...(nativeProps as P)}\n className={cx(className, recipeStyles, css(customCss))}\n />\n )\n }\n\n if (this.validateComponent(El)) {\n const ElName = typeof El === 'string' ? El : El.displayName || El.name\n CerbComponent.displayName = ElName\n }\n\n return CerbComponent\n }\n\n /**\n * Creates a Cerberus component with a slot recipe applied.\n * @param Component - The React component to enhance with Cerberus features.\n * @param recipe - The slot recipe to apply to the component.\n * @returns A new React component that applies Cerberus features and the\n * specified slot recipe to the original component.\n * @example\n * ```typescript\n * const { withSlotRecipe } = createCerberusPrimitive(field)\n * const Field = withSlotRecipe(RawField, field)\n * ```\n */\n withSlotRecipe = <P extends HTMLAttributes<unknown>>(\n Component: ComponentType<P> | string,\n slot: keyof RecipeVariantRecord,\n options?: WithRecipeOptions,\n ) => {\n const { defaultProps } = options || {}\n const El = Component as ComponentType<P> | ElementType\n\n const recipe = this.recipe as CerberusSlotRecipe<typeof slot>\n\n const CerbComponent = (internalProps: PropsWithChildren<P> & WithCss) => {\n const {\n css: customCss,\n className,\n ...restOfInternalProps\n } = internalProps\n\n const [variantOptions, nativeProps] =\n recipe.splitVariantProps(restOfInternalProps)\n const styles = recipe(variantOptions)\n const slotStyles = styles[slot as keyof typeof styles]\n\n return (\n <Component\n {...defaultProps}\n {...(nativeProps as P)}\n className={cx(className, slotStyles, css(customCss))}\n />\n )\n }\n\n if (this.validateComponent(El)) {\n const ElName = typeof El === 'string' ? El : El.displayName || El.name\n CerbComponent.displayName = ElName\n }\n\n return CerbComponent\n }\n}\n","import { CerberusPrimitive } from './primitive-factory'\nimport type { CerberusFactory, CerberusPrimitiveRecipe } from './types'\nimport { cerberusFactoryProxy } from './factory'\n\n/**\n * This module contains the user interface for creating Cerberus primitives.\n * @module @cerberus/core/system/create-cerb-primitive\n */\n\n/**\n * A factory function that creates a Cerberus primitive instance with the given\n * recipe.\n * @param recipe\n * @returns An object with three methods: `withNoRecipe`, `withRecipe`, and `withSlotRecipe` that\n * apply the recipes and special Cerberus helpers like `css`.\n *\n * @example\n * ```tsx\n * const { withRecipe } = createCerberusPrimitive(myCustomRecipe);\n * export const Button = withRecipe(MyCustomButton)\n * ```\n */\nexport function createCerberusPrimitive<T extends CerberusPrimitiveRecipe>(\n recipe?: T,\n) {\n return new CerberusPrimitive(recipe)\n}\n\n/**\n * A utility function to access Cerberus components by their name.\n * @param component - The name of the Cerberus component to access.\n * @returns The Cerberus component corresponding to the provided name.\n * @throws An error if the component name is not valid.\n *\n * @example\n * ```tsx\n * import { cerberus } from '@cerberus/react'\n * const Button = cerberus('button')\n *\n * <Button css={{ color: 'blue' }} asChild>\n * <Link href=\"/some-page\">Click me</Link>\n * </Button>\n * ```\n */\nexport const cerberus = cerberusFactoryProxy as CerberusFactory\n\nexport * from './types'\n","import type { ElementType } from 'react'\nimport {\n TableRoot,\n Thead,\n Th,\n Tbody,\n Tr,\n Td,\n Tfoot,\n TableEl,\n Caption,\n TableTrigger,\n} from './primitives'\n\n/**\n * This module contains the parts of the Table parts.\n * @module 'table/parts'\n */\n\nexport interface TablePartsValue {\n /**\n * The container of the table.\n */\n Root: ElementType\n /**\n * The table element.\n */\n Table: ElementType\n /**\n * The table caption element.\n */\n Caption: ElementType\n /**\n * The table head element.\n */\n Header: ElementType\n /**\n * The table header cell element.\n */\n HeaderCol: ElementType\n /**\n * The table body element.\n */\n Body: ElementType\n /**\n * The table row element.\n */\n Row: ElementType\n /**\n * The table cell element.\n */\n Cell: ElementType\n /**\n * The table footer element.\n */\n Footer: ElementType\n /**\n * The table trigger element.\n */\n Trigger: ElementType\n}\n\n/**\n * An Object containing the parts of the Table component. For users that\n * prefer Object component syntax.\n *\n * @remarks\n *\n * When using object component syntax, you import the TableParts object and\n * the entire family of components vs. only what you use.\n */\nexport const TableParts: TablePartsValue = {\n Root: TableRoot,\n Table: TableEl,\n Caption: Caption,\n Header: Thead,\n HeaderCol: Th,\n Body: Tbody,\n Row: Tr,\n Cell: Td,\n Footer: Tfoot,\n Trigger: TableTrigger,\n}\n","import { type PropsWithChildren, type ReactNode } from 'react'\n\n/**\n * This module contains the Show component.\n * @module\n */\n\nexport interface ShowProps<T> {\n /**\n * The condition to render memoized children or the fallback content.\n */\n when: T | boolean | null | undefined\n /**\n * The children to render when the condition is false.\n */\n fallback?: ReactNode\n}\n\n/**\n * Conditionally render a memoized version of the children or optional fallback\n * content.\n * @see https://cerberus.digitalu.design/react/show\n * @example\n * ```tsx\n * <Show when={isLoggedIn} fallback={<Navigate to=\"/login\" />}>\n * <Dashboard />\n * </Show>\n */\nexport function Show<T>(props: PropsWithChildren<ShowProps<T>>) {\n const { when, children, fallback } = props\n\n if (when) {\n return <>{children}</>\n }\n\n if (fallback) {\n return <>{fallback}</>\n }\n\n return null\n}\n","import type { TablePartsValue } from './parts'\nimport {\n Caption,\n TableEl,\n TableRoot,\n TableTrigger,\n Tbody,\n Td,\n Tfoot,\n Th,\n Thead,\n Tr,\n type TableRootProps,\n} from './primitives'\nimport { Show } from '../show/index'\n\nexport interface TableRootElProps extends TableRootProps {\n /**\n * An easy to understand description of the table. Required for accessibility.\n */\n caption: string\n /**\n * If true, the table header will be sticky.\n *\n * @default false\n */\n sticky?: boolean\n}\n\n/**\n * An abstraction over the TableRoot component that adds a sticky attribute.\n * @description [Cerberus Docs](https://cerberus.digitalu.design/react/table)\n *\n * @remarks\n *\n * This component is used to create a table with an optional sticky header.\n * It is a wrapper around the TableRoot component and the TableEl primitive.\n */\nfunction TableRootEl(props: TableRootElProps) {\n const { sticky, caption, ...rootProps } = props\n return (\n <TableRoot {...rootProps} data-sticky={sticky ?? false}>\n <TableEl>\n <Show when={caption}>\n <Caption>{caption}</Caption>\n </Show>\n\n {rootProps.children}\n </TableEl>\n </TableRoot>\n )\n}\n\n/**\n * An Object containing the parts of the Table component. For users that\n * prefer Object component syntax.\n *\n * @remarks\n *\n * When using object component syntax, you import the TableParts object and\n * the entire family of components vs. only what you use.\n */\nexport const Table: Omit<TablePartsValue, 'Table'> = {\n Root: TableRootEl,\n Caption: Caption,\n Header: Thead,\n HeaderCol: Th,\n Body: Tbody,\n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