archivo del paquete→ .shtukbase/packages/shtukbase-ui-react/library/lib/define.ts
library/lib/define.ts
66 líneas, 3.2 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // The component contract: meta and states. They are written by the file `<id>.states.tsx` next to the component:
- //
- // export const meta = defineComponent({ id: "chip", name: "Chip", level: "primitive" });
- // export const states = defineStates<ChipProps>(meta, {
- // default: { children: "Norms", value: 14 }, // the state names are the ones of the header (and of the HTML templates)
- // hover: { children: "Norms", value: 14, "data-hover": "" },
- // pair: () => <><Chip>A</Chip><Chip>B</Chip></>, // a state that is not one element: a function that draws it
- // });
- //
- // The render test of the package draws every state and compares it with the HTML template of the same name, so the
- // state names are the ones of the component header: `states:` and `status:`.
- import type { ComponentType, ReactNode } from "react";
- export const LEVELS = ["primitive", "composite", "block", "shell", "page"] as const;
- export type ComponentLevel = (typeof LEVELS)[number];
- const PASCAL_CASE = /^[A-Z][A-Za-z0-9]*$/;
- const KEBAB_CASE = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/;
- export type ComponentMeta = Readonly<{
- /** The id of the header and of the HTML library: kebab-case. */
- id: string;
- /** The React export: PascalCase. */
- name: string;
- level: ComponentLevel;
- }>;
- /** The description of a component. A mistake in it is an exception when the file is imported. */
- export function defineComponent<const M extends ComponentMeta>(meta: M): Readonly<M> {
- const where = `defineComponent(${JSON.stringify(meta.name)})`;
- if (!PASCAL_CASE.test(meta.name)) throw new Error(`${where}: the component name is PascalCase`);
- if (!KEBAB_CASE.test(meta.id)) throw new Error(`${where}: the id ${meta.id} is kebab-case`);
- if (!LEVELS.includes(meta.level)) throw new Error(`${where}: unknown level ${String(meta.level)}`);
- return Object.freeze(meta);
- }
- /** One state: the props that draw it on demo data, or a function that draws it (a state of several elements). */
- export type StateEntry<P> = P | (() => ReactNode);
- /** State name (as in the header) -> its entry. */
- export type StatesInput<P> = { default: StateEntry<P> } & { [state: string]: StateEntry<P> };
- export type StatesDefinition<P> = {
- readonly meta: ComponentMeta;
- /** State names in the order of declaration. */
- readonly names: readonly string[];
- readonly entries: Readonly<Record<string, StateEntry<P>>>;
- };
- export function defineStates<P>(meta: ComponentMeta, input: StatesInput<P>): StatesDefinition<P> {
- const where = `defineStates(${JSON.stringify(meta.name)})`;
- const names = Object.keys(input);
- for (const name of names) if (!KEBAB_CASE.test(name)) throw new Error(`${where}: the state name ${name} must be kebab-case`);
- if (!names.includes("default")) throw new Error(`${where}: no default state`);
- return Object.freeze({ meta, names: Object.freeze(names), entries: Object.freeze({ ...input }) });
- }
- /** A registry entry: the component, its meta and states. */
- export type RegistryEntry = {
- // biome-ignore lint/suspicious/noExplicitAny: components in the registry have different props
- readonly component: ComponentType<any>;
- readonly meta: ComponentMeta;
- // biome-ignore lint/suspicious/noExplicitAny: the same: the props of the states differ
- readonly states: StatesDefinition<any>;
- };