ShtukbaseStandard
包文件→ .shtukbase/packages/shtukbase-ui-react/library/lib/define.ts

library/lib/define.ts

66 行, 3.2 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // The component contract: meta and states. They are written by the file `<id>.states.tsx` next to the component:
  2. //
  3. // export const meta = defineComponent({ id: "chip", name: "Chip", level: "primitive" });
  4. // export const states = defineStates<ChipProps>(meta, {
  5. // default: { children: "Norms", value: 14 }, // the state names are the ones of the header (and of the HTML templates)
  6. // hover: { children: "Norms", value: 14, "data-hover": "" },
  7. // pair: () => <><Chip>A</Chip><Chip>B</Chip></>, // a state that is not one element: a function that draws it
  8. // });
  9. //
  10. // The render test of the package draws every state and compares it with the HTML template of the same name, so the
  11. // state names are the ones of the component header: `states:` and `status:`.
  12. import type { ComponentType, ReactNode } from "react";
  13. export const LEVELS = ["primitive", "composite", "block", "shell", "page"] as const;
  14. export type ComponentLevel = (typeof LEVELS)[number];
  15. const PASCAL_CASE = /^[A-Z][A-Za-z0-9]*$/;
  16. const KEBAB_CASE = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/;
  17. export type ComponentMeta = Readonly<{
  18. /** The id of the header and of the HTML library: kebab-case. */
  19. id: string;
  20. /** The React export: PascalCase. */
  21. name: string;
  22. level: ComponentLevel;
  23. }>;
  24. /** The description of a component. A mistake in it is an exception when the file is imported. */
  25. export function defineComponent<const M extends ComponentMeta>(meta: M): Readonly<M> {
  26. const where = `defineComponent(${JSON.stringify(meta.name)})`;
  27. if (!PASCAL_CASE.test(meta.name)) throw new Error(`${where}: the component name is PascalCase`);
  28. if (!KEBAB_CASE.test(meta.id)) throw new Error(`${where}: the id ${meta.id} is kebab-case`);
  29. if (!LEVELS.includes(meta.level)) throw new Error(`${where}: unknown level ${String(meta.level)}`);
  30. return Object.freeze(meta);
  31. }
  32. /** One state: the props that draw it on demo data, or a function that draws it (a state of several elements). */
  33. export type StateEntry<P> = P | (() => ReactNode);
  34. /** State name (as in the header) -> its entry. */
  35. export type StatesInput<P> = { default: StateEntry<P> } & { [state: string]: StateEntry<P> };
  36. export type StatesDefinition<P> = {
  37. readonly meta: ComponentMeta;
  38. /** State names in the order of declaration. */
  39. readonly names: readonly string[];
  40. readonly entries: Readonly<Record<string, StateEntry<P>>>;
  41. };
  42. export function defineStates<P>(meta: ComponentMeta, input: StatesInput<P>): StatesDefinition<P> {
  43. const where = `defineStates(${JSON.stringify(meta.name)})`;
  44. const names = Object.keys(input);
  45. for (const name of names) if (!KEBAB_CASE.test(name)) throw new Error(`${where}: the state name ${name} must be kebab-case`);
  46. if (!names.includes("default")) throw new Error(`${where}: no default state`);
  47. return Object.freeze({ meta, names: Object.freeze(names), entries: Object.freeze({ ...input }) });
  48. }
  49. /** A registry entry: the component, its meta and states. */
  50. export type RegistryEntry = {
  51. // biome-ignore lint/suspicious/noExplicitAny: components in the registry have different props
  52. readonly component: ComponentType<any>;
  53. readonly meta: ComponentMeta;
  54. // biome-ignore lint/suspicious/noExplicitAny: the same: the props of the states differ
  55. readonly states: StatesDefinition<any>;
  56. };