@govuk-react/primitives 0.0.0-stage → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/README.md +271 -2
  2. package/dist/accordion/element.d.ts +14 -0
  3. package/dist/accordion/element.js +302 -0
  4. package/dist/accordion/element.js.map +1 -0
  5. package/dist/accordion/index.d.ts +74 -0
  6. package/dist/accordion/index.js +138 -0
  7. package/dist/accordion/index.js.map +1 -0
  8. package/dist/auto-focus/element.d.ts +1 -0
  9. package/dist/auto-focus/element.js +75 -0
  10. package/dist/auto-focus/element.js.map +1 -0
  11. package/dist/auto-focus/index.d.ts +297 -0
  12. package/dist/auto-focus/index.js +29 -0
  13. package/dist/auto-focus/index.js.map +1 -0
  14. package/dist/autocomplete/element.d.ts +27 -0
  15. package/dist/autocomplete/element.js +597 -0
  16. package/dist/autocomplete/element.js.map +1 -0
  17. package/dist/autocomplete/index.d.ts +350 -0
  18. package/dist/autocomplete/index.js +108 -0
  19. package/dist/autocomplete/index.js.map +1 -0
  20. package/dist/button/element.d.ts +1 -0
  21. package/dist/button/element.js +59 -0
  22. package/dist/button/element.js.map +1 -0
  23. package/dist/button/index.d.ts +10 -0
  24. package/dist/button/index.js +9 -0
  25. package/dist/button/index.js.map +1 -0
  26. package/dist/character-count/element.d.ts +7 -0
  27. package/dist/character-count/element.js +287 -0
  28. package/dist/character-count/element.js.map +1 -0
  29. package/dist/character-count/index.d.ts +57 -0
  30. package/dist/character-count/index.js +79 -0
  31. package/dist/character-count/index.js.map +1 -0
  32. package/dist/character-count/messages.d.ts +20 -0
  33. package/dist/character-count/messages.js +54 -0
  34. package/dist/character-count/messages.js.map +1 -0
  35. package/dist/choices/element.d.ts +5 -0
  36. package/dist/choices/element.js +202 -0
  37. package/dist/choices/element.js.map +1 -0
  38. package/dist/choices/index.d.ts +309 -0
  39. package/dist/choices/index.js +37 -0
  40. package/dist/choices/index.js.map +1 -0
  41. package/dist/disclosure/element.d.ts +14 -0
  42. package/dist/disclosure/element.js +123 -0
  43. package/dist/disclosure/element.js.map +1 -0
  44. package/dist/disclosure/index.d.ts +307 -0
  45. package/dist/disclosure/index.js +66 -0
  46. package/dist/disclosure/index.js.map +1 -0
  47. package/dist/exit-this-page/element.d.ts +19 -0
  48. package/dist/exit-this-page/element.js +201 -0
  49. package/dist/exit-this-page/element.js.map +1 -0
  50. package/dist/exit-this-page/index.d.ts +330 -0
  51. package/dist/exit-this-page/index.js +54 -0
  52. package/dist/exit-this-page/index.js.map +1 -0
  53. package/dist/file-upload/element.d.ts +23 -0
  54. package/dist/file-upload/element.js +270 -0
  55. package/dist/file-upload/element.js.map +1 -0
  56. package/dist/file-upload/index.d.ts +46 -0
  57. package/dist/file-upload/index.js +65 -0
  58. package/dist/file-upload/index.js.map +1 -0
  59. package/dist/focus-links/element.d.ts +1 -0
  60. package/dist/focus-links/element.js +90 -0
  61. package/dist/focus-links/element.js.map +1 -0
  62. package/dist/focus-links/index.d.ts +288 -0
  63. package/dist/focus-links/index.js +7 -0
  64. package/dist/focus-links/index.js.map +1 -0
  65. package/dist/index.d.ts +13 -0
  66. package/dist/index.js +14 -0
  67. package/dist/index.js.map +1 -0
  68. package/dist/inline/accordion-element.js +1 -0
  69. package/dist/inline/auto-focus-element.js +1 -0
  70. package/dist/inline/autocomplete-element.js +2 -0
  71. package/dist/inline/button-element.js +1 -0
  72. package/dist/inline/character-count-element.js +1 -0
  73. package/dist/inline/choices-element.js +1 -0
  74. package/dist/inline/disclosure-element.js +1 -0
  75. package/dist/inline/exit-this-page-element.js +1 -0
  76. package/dist/inline/file-upload-element.js +1 -0
  77. package/dist/inline/focus-links-element.js +1 -0
  78. package/dist/inline/modal-element.js +1 -0
  79. package/dist/inline/password-input-element.js +1 -0
  80. package/dist/inline/tabs-element.js +1 -0
  81. package/dist/inline-script/index.d.ts +26 -0
  82. package/dist/inline-script/index.js +31 -0
  83. package/dist/inline-script/index.js.map +1 -0
  84. package/dist/modal/element.d.ts +10 -0
  85. package/dist/modal/element.js +114 -0
  86. package/dist/modal/element.js.map +1 -0
  87. package/dist/modal/index.d.ts +36 -0
  88. package/dist/modal/index.js +109 -0
  89. package/dist/modal/index.js.map +1 -0
  90. package/dist/password-input/element.d.ts +1 -0
  91. package/dist/password-input/element.js +134 -0
  92. package/dist/password-input/element.js.map +1 -0
  93. package/dist/password-input/index.d.ts +318 -0
  94. package/dist/password-input/index.js +35 -0
  95. package/dist/password-input/index.js.map +1 -0
  96. package/dist/tabs/element.d.ts +19 -0
  97. package/dist/tabs/element.js +416 -0
  98. package/dist/tabs/element.js.map +1 -0
  99. package/dist/tabs/index.d.ts +83 -0
  100. package/dist/tabs/index.js +138 -0
  101. package/dist/tabs/index.js.map +1 -0
  102. package/dist/utils/dom.d.ts +9 -0
  103. package/dist/utils/dom.js +27 -0
  104. package/dist/utils/dom.js.map +1 -0
  105. package/dist/utils/on-property-set.d.ts +9 -0
  106. package/dist/utils/on-property-set.js +38 -0
  107. package/dist/utils/on-property-set.js.map +1 -0
  108. package/dist/utils/set-focus.d.ts +11 -0
  109. package/dist/utils/set-focus.js +38 -0
  110. package/dist/utils/set-focus.js.map +1 -0
  111. package/dist/utils/set-up-when-ready.d.ts +6 -0
  112. package/dist/utils/set-up-when-ready.js +16 -0
  113. package/dist/utils/set-up-when-ready.js.map +1 -0
  114. package/dist/utils/slot.d.ts +13 -0
  115. package/dist/utils/slot.js +40 -0
  116. package/dist/utils/slot.js.map +1 -0
  117. package/dist/utils/translation.d.ts +13 -0
  118. package/dist/utils/translation.js +57 -0
  119. package/dist/utils/translation.js.map +1 -0
  120. package/licences/accessible-autocomplete.txt +20 -0
  121. package/package.json +49 -4
@@ -0,0 +1,138 @@
1
+ 'use client';
2
+ import * as React from 'react';
3
+ import { createContext, useContext, useEffect, useId, useRef } from 'react';
4
+ import { Slot } from '../utils/slot.js';
5
+ import { tagName } from './element.js';
6
+ /*
7
+ * Headless tabs: unstyled parts over the <govuk-react-tabs> element (./element.ts), which adds
8
+ * the tab behaviour on upgrade. Every part except Root takes `asChild`.
9
+ *
10
+ * Each panel holds a hidden radio, and the radio group is the source of truth. A user's choice
11
+ * fires the radio's native change, which React reports through the radio's `onChange`.
12
+ *
13
+ * Without JavaScript, `Root`'s tabs are links to every panel, and `RadiosRoot`'s are labels for the
14
+ * panels' radios, so they switch panels without JavaScript too. Use `Trigger` and `Content` in a
15
+ * `Root`, and `RadioTrigger` and `RadioContent` in a `RadiosRoot`.
16
+ *
17
+ * Context only fills in defaults: in an island without a Root, pass `name` and `defaultSelected`
18
+ * to the parts. Each `value` is also its panel's id, so must be unique.
19
+ */
20
+ const TabsContext = createContext({});
21
+ /**
22
+ * Shows only the checked radio's panel until the element upgrades, and below its `media`.
23
+ * Root renders it inline; under a strict CSP, add it to a stylesheet instead.
24
+ */
25
+ export const radiosFallbackCss = `${tagName}:not([data-upgraded]) [data-tabs-content]:has(> input[data-tabs-fallback]):not(:has(> input:checked)){display:none}`;
26
+ const radioId = (name, value) => `${name}-${value}`;
27
+ /** Tabs that are links to every panel without JavaScript. Use `Trigger` and `Content` inside it. */
28
+ export function Root(props) {
29
+ return React.createElement(TabsRoot, { fallback: "links", ...props });
30
+ }
31
+ /**
32
+ * Tabs that switch panels without JavaScript too, through the panels' radios. Use `RadioTrigger`
33
+ * and `RadioContent` inside it.
34
+ */
35
+ export function RadiosRoot(props) {
36
+ return React.createElement(TabsRoot, { fallback: "radios", ...props });
37
+ }
38
+ function TabsRoot({ children, value, defaultValue, onValueChange, preHydration = 'ignore', orientation, fallback, name, media, upgradedClass, activeItemClass, hiddenContentClass, className, ...props }) {
39
+ const generatedName = useId();
40
+ const groupName = name !== null && name !== void 0 ? name : generatedName;
41
+ const ref = useRef(null);
42
+ // A tab the user picked before hydration fired no onChange that React saw, so report it once.
43
+ // When controlled with "ignore", `value` wins, so there is nothing to report.
44
+ const reported = useRef(false);
45
+ const initial = useRef({ value, preHydration, onValueChange });
46
+ useEffect(() => {
47
+ var _a, _b;
48
+ const picked = (_a = ref.current) === null || _a === void 0 ? void 0 : _a.pickedValue;
49
+ if (reported.current || picked === undefined)
50
+ return;
51
+ reported.current = true;
52
+ const start = initial.current;
53
+ if (start.value === undefined || start.preHydration === 'report')
54
+ (_b = start.onValueChange) === null || _b === void 0 ? void 0 : _b.call(start, picked);
55
+ }, []);
56
+ return (React.createElement(TabsContext.Provider, { value: { selected: value !== null && value !== void 0 ? value : defaultValue, name: groupName, onValueChange } },
57
+ fallback === 'radios' && (React.createElement("style", { href: `${tagName}-radios-fallback`, precedence: "default" }, radiosFallbackCss)),
58
+ React.createElement(tagName, {
59
+ ...props,
60
+ ref,
61
+ // The radios fallback switches tabs before upgrade, so it is styled as tabs from the start.
62
+ className: [className, fallback === 'radios' && upgradedClass].filter(Boolean).join(' ') || undefined,
63
+ value,
64
+ 'default-value': defaultValue,
65
+ fallback,
66
+ orientation,
67
+ 'prefer-pick': preHydration === 'report' ? '' : undefined,
68
+ media,
69
+ 'upgraded-class': upgradedClass,
70
+ 'active-item-class': activeItemClass,
71
+ 'hidden-content-class': hiddenContentClass,
72
+ // The element adds attributes on upgrade, so they can differ from the server's.
73
+ suppressHydrationWarning: true,
74
+ }, children)));
75
+ }
76
+ export function List({ asChild, ...props }) {
77
+ const Element = asChild ? Slot : 'div';
78
+ return React.createElement(Element, { "data-tabs-list": "", suppressHydrationWarning: true, ...props });
79
+ }
80
+ /** An optional wrapper around a tab, such as a list item. */
81
+ export function Item({ asChild, value, ...props }) {
82
+ const Element = asChild ? Slot : 'div';
83
+ return React.createElement(Element, { "data-tabs-item": "", "data-value": value, suppressHydrationWarning: true, ...props });
84
+ }
85
+ function useIsland({ name }) {
86
+ var _a;
87
+ const context = useContext(TabsContext);
88
+ return {
89
+ name: (_a = name !== null && name !== void 0 ? name : context.name) !== null && _a !== void 0 ? _a : tagName,
90
+ selected: context.selected,
91
+ onValueChange: context.onValueChange,
92
+ };
93
+ }
94
+ const triggerData = (value) => ({
95
+ 'data-tabs-trigger': '',
96
+ 'data-value': value,
97
+ suppressHydrationWarning: true,
98
+ });
99
+ /** A tab in a `Root`: a link to its panel. */
100
+ export function Trigger({ asChild, value, ...props }) {
101
+ const Element = asChild ? Slot : 'a';
102
+ return React.createElement(Element, { ...triggerData(value), href: `#${value}`, ...props });
103
+ }
104
+ /** A tab in a `RadiosRoot`: a label for its panel's radio. */
105
+ export function RadioTrigger({ asChild, value, name: nameProp, ...props }) {
106
+ const { name } = useIsland({ name: nameProp });
107
+ const Element = asChild ? Slot : 'label';
108
+ return React.createElement(Element, { ...triggerData(value), htmlFor: radioId(name, value), ...props });
109
+ }
110
+ /** A panel in a `Root`. Its id is its `value`. */
111
+ export function Content(props) {
112
+ return React.createElement(Panel, { ...props });
113
+ }
114
+ /** A panel in a `RadiosRoot`, shown without JavaScript only while its radio is checked. Its id is its `value`. */
115
+ export function RadioContent(props) {
116
+ return React.createElement(Panel, { radios: true, ...props });
117
+ }
118
+ function Panel({ asChild, value, children, name: nameProp, defaultSelected, radios = false, ...props }) {
119
+ const { name, selected, onValueChange } = useIsland({ name: nameProp });
120
+ const radio = (
121
+ // The selection, and in a RadiosRoot the no-JS state. `form` names no form, so the radio is
122
+ // never submitted with a form around it.
123
+ React.createElement("input", { type: "radio", name: name, id: radioId(name, value), form: `${name}-none`, defaultChecked: defaultSelected !== null && defaultSelected !== void 0 ? defaultSelected : value === selected, hidden: true, "data-tabs-fallback": radios ? '' : undefined, suppressHydrationWarning: true, onChange: () => onValueChange === null || onValueChange === void 0 ? void 0 : onValueChange(value) }));
124
+ const shared = {
125
+ id: value,
126
+ 'data-tabs-content': '',
127
+ 'data-value': value,
128
+ suppressHydrationWarning: true,
129
+ };
130
+ if (asChild && React.isValidElement(children)) {
131
+ const child = children;
132
+ return (React.createElement(Slot, { ...shared, ...props }, React.cloneElement(child, undefined, radio, child.props.children)));
133
+ }
134
+ return (React.createElement("div", { ...shared, ...props },
135
+ radio,
136
+ children));
137
+ }
138
+ //# sourceMappingURL=index.js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AAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC;IACxE,MAAM,KAAK,GAAG;IACZ,4FAA4F;IAC5F,yCAAyC;IACzC,+BACE,IAAI,EAAC,OAAO,EACZ,IAAI,EAAE,IAAI,EACV,EAAE,EAAE,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,EACxB,IAAI,EAAE,GAAG,IAAI,OAAO,EACpB,cAAc,EAAE,eAAe,aAAf,eAAe,cAAf,eAAe,GAAI,KAAK,KAAK,QAAQ,EACrD,MAAM,8BACc,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,EAC3C,wBAAwB,QACxB,QAAQ,EAAE,GAAG,EAAE,CAAC,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAG,KAAK,CAAC,GACtC,CACH,CAAC;IACF,MAAM,MAAM,GAAG;QACb,EAAE,EAAE,KAAK;QACT,mBAAmB,EAAE,EAAE;QACvB,YAAY,EAAE,KAAK;QACnB,wBAAwB,EAAE,IAAI;KAC/B,CAAC;IACF,IAAI,OAAO,IAAI,KAAK,CAAC,cAAc,CAA2B,QAAQ,CAAC,EAAE,CAAC;QACxE,MAAM,KAAK,GAA2C,QAAQ,CAAC;QAC/D,OAAO,CACL,oBAAC,IAAI,OAAK,MAAM,KAAM,KAAK,IACxB,KAAK,CAAC,YAAY,CAAC,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,QAAQ,CAAC,CAC7D,CACR,CAAC;IACJ,CAAC;IACD,OAAO,CACL,gCAAS,MAAM,KAAM,KAAK;QACvB,KAAK;QACL,QAAQ,CACL,CACP,CAAC;AACJ,CAAC","sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { createContext, useContext, useEffect, useId, useRef } from 'react';\nimport type { ComponentProps, ReactElement, ReactNode } from 'react';\nimport { Slot } from '../utils/slot.js';\nimport { tagName, type TabsElement } from './element.js';\n\n/*\n * Headless tabs: unstyled parts over the <govuk-react-tabs> element (./element.ts), which adds\n * the tab behaviour on upgrade. Every part except Root takes `asChild`.\n *\n * Each panel holds a hidden radio, and the radio group is the source of truth. A user's choice\n * fires the radio's native change, which React reports through the radio's `onChange`.\n *\n * Without JavaScript, `Root`'s tabs are links to every panel, and `RadiosRoot`'s are labels for the\n * panels' radios, so they switch panels without JavaScript too. Use `Trigger` and `Content` in a\n * `Root`, and `RadioTrigger` and `RadioContent` in a `RadiosRoot`.\n *\n * Context only fills in defaults: in an island without a Root, pass `name` and `defaultSelected`\n * to the parts. Each `value` is also its panel's id, so must be unique.\n */\n\nconst TabsContext = createContext<{\n selected?: string;\n name?: string;\n onValueChange?: (value: string) => void;\n}>({});\n\n/**\n * Shows only the checked radio's panel until the element upgrades, and below its `media`.\n * Root renders it inline; under a strict CSP, add it to a stylesheet instead.\n */\nexport const radiosFallbackCss = `${tagName}:not([data-upgraded]) [data-tabs-content]:has(> input[data-tabs-fallback]):not(:has(> input:checked)){display:none}`;\n\nconst radioId = (name: string, value: string) => `${name}-${value}`;\n\ntype BaseRootProps = {\n children?: ReactNode;\n /** The selected tab's value, to control the tabs. */\n value?: string;\n /** The tab selected at first, when uncontrolled. Defaults to the first tab. */\n defaultValue?: string;\n /** Called when the user selects a tab. */\n onValueChange?: (value: string) => void;\n /**\n * When controlled, what happens to a tab the user picked before hydration: \"ignore\" keeps\n * `value`, \"report\" calls `onValueChange` with the pick so you can adopt it.\n */\n preHydration?: 'ignore' | 'report';\n /** The axis of the arrow keys. */\n orientation?: 'horizontal' | 'vertical';\n /** The panels' radio group name. Defaults to a generated one. */\n name?: string;\n /** Only behave as tabs when this media query matches. */\n media?: string;\n /** Classes added to the element when it upgrades. */\n upgradedClass?: string;\n /** Classes added to the active item. */\n activeItemClass?: string;\n /** Classes added to inactive panels. */\n hiddenContentClass?: string;\n} & Omit<React.HTMLAttributes<HTMLElement>, 'defaultValue' | 'onChange'>;\n\nexport type RootProps = BaseRootProps;\n\n/** Tabs that are links to every panel without JavaScript. Use `Trigger` and `Content` inside it. */\nexport function Root(props: RootProps) {\n return <TabsRoot fallback=\"links\" {...props} />;\n}\n\nexport type RadiosRootProps = BaseRootProps &\n // Without JavaScript only the checked radio's panel shows, so one must be checked from the start.\n ({ value: string } | { defaultValue: string });\n\n/**\n * Tabs that switch panels without JavaScript too, through the panels' radios. Use `RadioTrigger`\n * and `RadioContent` inside it.\n */\nexport function RadiosRoot(props: RadiosRootProps) {\n return <TabsRoot fallback=\"radios\" {...props} />;\n}\n\nfunction TabsRoot({\n children,\n value,\n defaultValue,\n onValueChange,\n preHydration = 'ignore',\n orientation,\n fallback,\n name,\n media,\n upgradedClass,\n activeItemClass,\n hiddenContentClass,\n className,\n ...props\n}: BaseRootProps & { fallback: 'links' | 'radios' }) {\n const generatedName = useId();\n const groupName = name ?? generatedName;\n const ref = useRef<TabsElement>(null);\n\n // A tab the user picked before hydration fired no onChange that React saw, so report it once.\n // When controlled with \"ignore\", `value` wins, so there is nothing to report.\n const reported = useRef(false);\n const initial = useRef({ value, preHydration, onValueChange });\n useEffect(() => {\n const picked = ref.current?.pickedValue;\n if (reported.current || picked === undefined) return;\n reported.current = true;\n const start = initial.current;\n if (start.value === undefined || start.preHydration === 'report') start.onValueChange?.(picked);\n }, []);\n\n return (\n <TabsContext.Provider value={{ selected: value ?? defaultValue, name: groupName, onValueChange }}>\n {fallback === 'radios' && (\n <style href={`${tagName}-radios-fallback`} precedence=\"default\">\n {radiosFallbackCss}\n </style>\n )}\n {React.createElement(\n tagName,\n {\n ...props,\n ref,\n // The radios fallback switches tabs before upgrade, so it is styled as tabs from the start.\n className: [className, fallback === 'radios' && upgradedClass].filter(Boolean).join(' ') || undefined,\n value,\n 'default-value': defaultValue,\n fallback,\n orientation,\n 'prefer-pick': preHydration === 'report' ? '' : undefined,\n media,\n 'upgraded-class': upgradedClass,\n 'active-item-class': activeItemClass,\n 'hidden-content-class': hiddenContentClass,\n // The element adds attributes on upgrade, so they can differ from the server's.\n suppressHydrationWarning: true,\n },\n children\n )}\n </TabsContext.Provider>\n );\n}\n\ntype PartProps<E extends keyof React.JSX.IntrinsicElements> = ComponentProps<E> & {\n /** Render the only child instead, with this part's props merged onto it. */\n asChild?: boolean;\n};\n\nexport type ListProps = PartProps<'div'>;\n\nexport function List({ asChild, ...props }: ListProps) {\n const Element = asChild ? Slot : 'div';\n return <Element data-tabs-list=\"\" suppressHydrationWarning {...props} />;\n}\n\nexport type ItemProps = PartProps<'div'> & { value: string };\n\n/** An optional wrapper around a tab, such as a list item. */\nexport function Item({ asChild, value, ...props }: ItemProps) {\n const Element = asChild ? Slot : 'div';\n return <Element data-tabs-item=\"\" data-value={value} suppressHydrationWarning {...props} />;\n}\n\n/** Set by Root. Pass it to a part rendered without a Root, such as in an island. */\ntype IslandProps = {\n /** The panels' radio group name. */\n name?: string;\n};\n\nfunction useIsland({ name }: IslandProps) {\n const context = useContext(TabsContext);\n return {\n name: name ?? context.name ?? tagName,\n selected: context.selected,\n onValueChange: context.onValueChange,\n };\n}\n\nexport type TriggerProps = React.HTMLAttributes<HTMLElement> & {\n /** Render the only child instead, with this part's props merged onto it. */\n asChild?: boolean;\n value: string;\n};\n\nconst triggerData = (value: string) => ({\n 'data-tabs-trigger': '',\n 'data-value': value,\n suppressHydrationWarning: true,\n});\n\n/** A tab in a `Root`: a link to its panel. */\nexport function Trigger({ asChild, value, ...props }: TriggerProps) {\n const Element = asChild ? Slot : 'a';\n return <Element {...triggerData(value)} href={`#${value}`} {...props} />;\n}\n\nexport type RadioTriggerProps = TriggerProps & IslandProps;\n\n/** A tab in a `RadiosRoot`: a label for its panel's radio. */\nexport function RadioTrigger({ asChild, value, name: nameProp, ...props }: RadioTriggerProps) {\n const { name } = useIsland({ name: nameProp });\n const Element = asChild ? Slot : 'label';\n return <Element {...triggerData(value)} htmlFor={radioId(name, value)} {...props} />;\n}\n\nexport type ContentProps = PartProps<'div'> &\n IslandProps & {\n value: string;\n /** Whether this panel's radio is checked at first. Defaults to Root's value. */\n defaultSelected?: boolean;\n };\n\n/** A panel in a `Root`. Its id is its `value`. */\nexport function Content(props: ContentProps) {\n return <Panel {...props} />;\n}\n\nexport type RadioContentProps = ContentProps;\n\n/** A panel in a `RadiosRoot`, shown without JavaScript only while its radio is checked. Its id is its `value`. */\nexport function RadioContent(props: RadioContentProps) {\n return <Panel radios {...props} />;\n}\n\nfunction Panel({\n asChild,\n value,\n children,\n name: nameProp,\n defaultSelected,\n radios = false,\n ...props\n}: ContentProps & { radios?: boolean }) {\n const { name, selected, onValueChange } = useIsland({ name: nameProp });\n const radio = (\n // The selection, and in a RadiosRoot the no-JS state. `form` names no form, so the radio is\n // never submitted with a form around it.\n <input\n type=\"radio\"\n name={name}\n id={radioId(name, value)}\n form={`${name}-none`}\n defaultChecked={defaultSelected ?? value === selected}\n hidden\n data-tabs-fallback={radios ? '' : undefined}\n suppressHydrationWarning\n onChange={() => onValueChange?.(value)}\n />\n );\n const shared = {\n id: value,\n 'data-tabs-content': '',\n 'data-value': value,\n suppressHydrationWarning: true,\n };\n if (asChild && React.isValidElement<{ children?: ReactNode }>(children)) {\n const child: ReactElement<{ children?: ReactNode }> = children;\n return (\n <Slot {...shared} {...props}>\n {React.cloneElement(child, undefined, radio, child.props.children)}\n </Slot>\n );\n }\n return (\n <div {...shared} {...props}>\n {radio}\n {children}\n </div>\n );\n}\n"]}
@@ -0,0 +1,9 @@
1
+ /** The class names in a space-separated attribute, such as `upgraded-class`. */
2
+ export declare const classesOf: (attribute: string | null) => string[];
3
+ /**
4
+ * Sets an element's text. Keeps a lone text node, such as one React rendered, rather than
5
+ * replacing it, so React can still update it after hydration.
6
+ */
7
+ export declare function setText(element: Element, text: string): void;
8
+ /** Sets an attribute, or removes it for `null`. */
9
+ export declare function setAttribute(element: Element, name: string, value: string | null): void;
@@ -0,0 +1,27 @@
1
+ /*
2
+ * DOM writes for elements that watch their own subtree with a MutationObserver. Each write is
3
+ * skipped when nothing would change, because even an unchanged write wakes the observer again.
4
+ */
5
+ /** The class names in a space-separated attribute, such as `upgraded-class`. */
6
+ export const classesOf = (attribute) => { var _a; return (_a = attribute === null || attribute === void 0 ? void 0 : attribute.split(/\s+/).filter(Boolean)) !== null && _a !== void 0 ? _a : []; };
7
+ /**
8
+ * Sets an element's text. Keeps a lone text node, such as one React rendered, rather than
9
+ * replacing it, so React can still update it after hydration.
10
+ */
11
+ export function setText(element, text) {
12
+ if (element.textContent === text)
13
+ return;
14
+ const { firstChild } = element;
15
+ if (firstChild instanceof Text && element.childNodes.length === 1)
16
+ firstChild.nodeValue = text;
17
+ else
18
+ element.textContent = text;
19
+ }
20
+ /** Sets an attribute, or removes it for `null`. */
21
+ export function setAttribute(element, name, value) {
22
+ if (value === null)
23
+ element.removeAttribute(name);
24
+ else if (element.getAttribute(name) !== value)
25
+ element.setAttribute(name, value);
26
+ }
27
+ //# sourceMappingURL=dom.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dom.js","sourceRoot":"","sources":["../../src/utils/dom.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,gFAAgF;AAChF,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,SAAwB,EAAE,EAAE,WAAC,OAAA,MAAA,SAAS,aAAT,SAAS,uBAAT,SAAS,CAAE,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,mCAAI,EAAE,CAAA,EAAA,CAAC;AAErG;;;GAGG;AACH,MAAM,UAAU,OAAO,CAAC,OAAgB,EAAE,IAAY;IACpD,IAAI,OAAO,CAAC,WAAW,KAAK,IAAI;QAAE,OAAO;IACzC,MAAM,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC;IAC/B,IAAI,UAAU,YAAY,IAAI,IAAI,OAAO,CAAC,UAAU,CAAC,MAAM,KAAK,CAAC;QAAE,UAAU,CAAC,SAAS,GAAG,IAAI,CAAC;;QAC1F,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;AAClC,CAAC;AAED,mDAAmD;AACnD,MAAM,UAAU,YAAY,CAAC,OAAgB,EAAE,IAAY,EAAE,KAAoB;IAC/E,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;SAC7C,IAAI,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,KAAK;QAAE,OAAO,CAAC,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AACnF,CAAC","sourcesContent":["/*\n * DOM writes for elements that watch their own subtree with a MutationObserver. Each write is\n * skipped when nothing would change, because even an unchanged write wakes the observer again.\n */\n\n/** The class names in a space-separated attribute, such as `upgraded-class`. */\nexport const classesOf = (attribute: string | null) => attribute?.split(/\\s+/).filter(Boolean) ?? [];\n\n/**\n * Sets an element's text. Keeps a lone text node, such as one React rendered, rather than\n * replacing it, so React can still update it after hydration.\n */\nexport function setText(element: Element, text: string) {\n if (element.textContent === text) return;\n const { firstChild } = element;\n if (firstChild instanceof Text && element.childNodes.length === 1) firstChild.nodeValue = text;\n else element.textContent = text;\n}\n\n/** Sets an attribute, or removes it for `null`. */\nexport function setAttribute(element: Element, name: string, value: string | null) {\n if (value === null) element.removeAttribute(name);\n else if (element.getAttribute(name) !== value) element.setAttribute(name, value);\n}\n"]}
@@ -0,0 +1,9 @@
1
+ type Field = HTMLInputElement | HTMLTextAreaElement;
2
+ type Property = 'value' | 'checked' | 'type';
3
+ /**
4
+ * Calls `onSet` after a script sets a field's `value`, `checked` or `type`, which fires no event,
5
+ * such as React Hook Form's `setValue`, or React updating an input. Wraps the setter the field
6
+ * has, which can be React's own, so React still tracks the value. Returns a function that puts the setter back.
7
+ */
8
+ export declare function onPropertySet(field: Field, property: Property, onSet: () => void): () => void;
9
+ export {};
@@ -0,0 +1,38 @@
1
+ const descriptorOf = (field, property) => {
2
+ for (let target = field; target; target = Object.getPrototypeOf(target)) {
3
+ const descriptor = Object.getOwnPropertyDescriptor(target, property);
4
+ if (descriptor)
5
+ return { descriptor, own: target === field };
6
+ }
7
+ return undefined;
8
+ };
9
+ /**
10
+ * Calls `onSet` after a script sets a field's `value`, `checked` or `type`, which fires no event,
11
+ * such as React Hook Form's `setValue`, or React updating an input. Wraps the setter the field
12
+ * has, which can be React's own, so React still tracks the value. Returns a function that puts the setter back.
13
+ */
14
+ export function onPropertySet(field, property, onSet) {
15
+ const found = descriptorOf(field, property);
16
+ const get = found === null || found === void 0 ? void 0 : found.descriptor.get;
17
+ const set = found === null || found === void 0 ? void 0 : found.descriptor.set;
18
+ if (!found || !get || !set)
19
+ return () => { };
20
+ Object.defineProperty(field, property, {
21
+ configurable: true,
22
+ enumerable: found.descriptor.enumerable,
23
+ get() {
24
+ return get.call(this);
25
+ },
26
+ set(value) {
27
+ set.call(this, value);
28
+ onSet();
29
+ },
30
+ });
31
+ return () => {
32
+ if (found.own)
33
+ Object.defineProperty(field, property, found.descriptor);
34
+ else
35
+ Reflect.deleteProperty(field, property);
36
+ };
37
+ }
38
+ //# sourceMappingURL=on-property-set.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"on-property-set.js","sourceRoot":"","sources":["../../src/utils/on-property-set.ts"],"names":[],"mappings":"AAIA,MAAM,YAAY,GAAG,CAAC,KAAY,EAAE,QAAkB,EAAE,EAAE;IACxD,KAAK,IAAI,MAAM,GAAkB,KAAK,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,CAAC;QACvF,MAAM,UAAU,GAAG,MAAM,CAAC,wBAAwB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QACrE,IAAI,UAAU;YAAE,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,KAAK,KAAK,EAAE,CAAC;IAC/D,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,KAAY,EAAE,QAAkB,EAAE,KAAiB;IAC/E,MAAM,KAAK,GAAG,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IAC5C,MAAM,GAAG,GAAG,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,UAAU,CAAC,GAAG,CAAC;IAClC,MAAM,GAAG,GAAG,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,UAAU,CAAC,GAAG,CAAC;IAClC,IAAI,CAAC,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG;QAAE,OAAO,GAAG,EAAE,GAAE,CAAC,CAAC;IAC5C,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,QAAQ,EAAE;QACrC,YAAY,EAAE,IAAI;QAClB,UAAU,EAAE,KAAK,CAAC,UAAU,CAAC,UAAU;QACvC,GAAG;YACD,OAAO,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACxB,CAAC;QACD,GAAG,CAAC,KAAK;YACP,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YACtB,KAAK,EAAE,CAAC;QACV,CAAC;KACF,CAAC,CAAC;IACH,OAAO,GAAG,EAAE;QACV,IAAI,KAAK,CAAC,GAAG;YAAE,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,QAAQ,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC;;YACnE,OAAO,CAAC,cAAc,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IAC/C,CAAC,CAAC;AACJ,CAAC","sourcesContent":["type Field = HTMLInputElement | HTMLTextAreaElement;\n\ntype Property = 'value' | 'checked' | 'type';\n\nconst descriptorOf = (field: Field, property: Property) => {\n for (let target: object | null = field; target; target = Object.getPrototypeOf(target)) {\n const descriptor = Object.getOwnPropertyDescriptor(target, property);\n if (descriptor) return { descriptor, own: target === field };\n }\n return undefined;\n};\n\n/**\n * Calls `onSet` after a script sets a field's `value`, `checked` or `type`, which fires no event,\n * such as React Hook Form's `setValue`, or React updating an input. Wraps the setter the field\n * has, which can be React's own, so React still tracks the value. Returns a function that puts the setter back.\n */\nexport function onPropertySet(field: Field, property: Property, onSet: () => void) {\n const found = descriptorOf(field, property);\n const get = found?.descriptor.get;\n const set = found?.descriptor.set;\n if (!found || !get || !set) return () => {};\n Object.defineProperty(field, property, {\n configurable: true,\n enumerable: found.descriptor.enumerable,\n get() {\n return get.call(this);\n },\n set(value) {\n set.call(this, value);\n onSet();\n },\n });\n return () => {\n if (found.own) Object.defineProperty(field, property, found.descriptor);\n else Reflect.deleteProperty(field, property);\n };\n}\n"]}
@@ -0,0 +1,11 @@
1
+ /** Removes a server-rendered `autofocus` and its `tabindex`, when the target won't take focus. */
2
+ export declare function dropAutoFocus(element: HTMLElement): void;
3
+ /**
4
+ * Moves focus to an element that may not take focus, as govuk-frontend's `setFocus` does: it gets
5
+ * `tabindex="-1"` until it loses focus again, and `focusedClasses` while it has it. Says whether it
6
+ * took focus.
7
+ */
8
+ export declare function setFocus(element: HTMLElement, { focusedClasses, preventScroll }?: {
9
+ focusedClasses?: string[];
10
+ preventScroll?: boolean;
11
+ }): boolean;
@@ -0,0 +1,38 @@
1
+ /** Removes what was added so a target could take focus. */
2
+ function cleanUp(element, added, focusedClasses) {
3
+ focusedClasses.forEach((name) => element.classList.remove(name));
4
+ if (added)
5
+ element.removeAttribute('tabindex');
6
+ element.removeAttribute('data-focus-tabindex');
7
+ element.removeAttribute('autofocus');
8
+ }
9
+ /** Whether a `tabindex` is there only to take focus: added here, or by the server for `autofocus`. */
10
+ const tabIndexAdded = (element) => !element.hasAttribute('tabindex') ||
11
+ (element.hasAttribute('data-focus-tabindex') && element.getAttribute('tabindex') === '-1');
12
+ /** Removes a server-rendered `autofocus` and its `tabindex`, when the target won't take focus. */
13
+ export function dropAutoFocus(element) {
14
+ cleanUp(element, element.hasAttribute('tabindex') && tabIndexAdded(element), []);
15
+ }
16
+ /**
17
+ * Moves focus to an element that may not take focus, as govuk-frontend's `setFocus` does: it gets
18
+ * `tabindex="-1"` until it loses focus again, and `focusedClasses` while it has it. Says whether it
19
+ * took focus.
20
+ */
21
+ export function setFocus(element, { focusedClasses = [], preventScroll = false } = {}) {
22
+ const added = tabIndexAdded(element);
23
+ if (!element.hasAttribute('tabindex'))
24
+ element.setAttribute('tabindex', '-1');
25
+ if (document.activeElement !== element) {
26
+ focusedClasses.forEach((name) => element.classList.add(name));
27
+ element.focus({ preventScroll });
28
+ }
29
+ if (document.activeElement !== element) {
30
+ // Hidden, or not yet in place.
31
+ cleanUp(element, added, focusedClasses);
32
+ return false;
33
+ }
34
+ focusedClasses.forEach((name) => element.classList.add(name));
35
+ element.addEventListener('blur', () => cleanUp(element, added, focusedClasses), { once: true });
36
+ return true;
37
+ }
38
+ //# sourceMappingURL=set-focus.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set-focus.js","sourceRoot":"","sources":["../../src/utils/set-focus.ts"],"names":[],"mappings":"AAAA,2DAA2D;AAC3D,SAAS,OAAO,CAAC,OAAoB,EAAE,KAAc,EAAE,cAAwB;IAC7E,cAAc,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;IACjE,IAAI,KAAK;QAAE,OAAO,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC;IAC/C,OAAO,CAAC,eAAe,CAAC,qBAAqB,CAAC,CAAC;IAC/C,OAAO,CAAC,eAAe,CAAC,WAAW,CAAC,CAAC;AACvC,CAAC;AAED,sGAAsG;AACtG,MAAM,aAAa,GAAG,CAAC,OAAoB,EAAE,EAAE,CAC7C,CAAC,OAAO,CAAC,YAAY,CAAC,UAAU,CAAC;IACjC,CAAC,OAAO,CAAC,YAAY,CAAC,qBAAqB,CAAC,IAAI,OAAO,CAAC,YAAY,CAAC,UAAU,CAAC,KAAK,IAAI,CAAC,CAAC;AAE7F,kGAAkG;AAClG,MAAM,UAAU,aAAa,CAAC,OAAoB;IAChD,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC,UAAU,CAAC,IAAI,aAAa,CAAC,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC;AACnF,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,OAAoB,EAAE,EAAE,cAAc,GAAG,EAAc,EAAE,aAAa,GAAG,KAAK,KAAK,EAAE;IAC5G,MAAM,KAAK,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC;IACrC,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,UAAU,CAAC;QAAE,OAAO,CAAC,YAAY,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;IAC9E,IAAI,QAAQ,CAAC,aAAa,KAAK,OAAO,EAAE,CAAC;QACvC,cAAc,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;QAC9D,OAAO,CAAC,KAAK,CAAC,EAAE,aAAa,EAAE,CAAC,CAAC;IACnC,CAAC;IACD,IAAI,QAAQ,CAAC,aAAa,KAAK,OAAO,EAAE,CAAC;QACvC,+BAA+B;QAC/B,OAAO,CAAC,OAAO,EAAE,KAAK,EAAE,cAAc,CAAC,CAAC;QACxC,OAAO,KAAK,CAAC;IACf,CAAC;IACD,cAAc,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;IAC9D,OAAO,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,KAAK,EAAE,cAAc,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IAChG,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["/** Removes what was added so a target could take focus. */\nfunction cleanUp(element: HTMLElement, added: boolean, focusedClasses: string[]) {\n focusedClasses.forEach((name) => element.classList.remove(name));\n if (added) element.removeAttribute('tabindex');\n element.removeAttribute('data-focus-tabindex');\n element.removeAttribute('autofocus');\n}\n\n/** Whether a `tabindex` is there only to take focus: added here, or by the server for `autofocus`. */\nconst tabIndexAdded = (element: HTMLElement) =>\n !element.hasAttribute('tabindex') ||\n (element.hasAttribute('data-focus-tabindex') && element.getAttribute('tabindex') === '-1');\n\n/** Removes a server-rendered `autofocus` and its `tabindex`, when the target won't take focus. */\nexport function dropAutoFocus(element: HTMLElement) {\n cleanUp(element, element.hasAttribute('tabindex') && tabIndexAdded(element), []);\n}\n\n/**\n * Moves focus to an element that may not take focus, as govuk-frontend's `setFocus` does: it gets\n * `tabindex=\"-1\"` until it loses focus again, and `focusedClasses` while it has it. Says whether it\n * took focus.\n */\nexport function setFocus(element: HTMLElement, { focusedClasses = [] as string[], preventScroll = false } = {}) {\n const added = tabIndexAdded(element);\n if (!element.hasAttribute('tabindex')) element.setAttribute('tabindex', '-1');\n if (document.activeElement !== element) {\n focusedClasses.forEach((name) => element.classList.add(name));\n element.focus({ preventScroll });\n }\n if (document.activeElement !== element) {\n // Hidden, or not yet in place.\n cleanUp(element, added, focusedClasses);\n return false;\n }\n focusedClasses.forEach((name) => element.classList.add(name));\n element.addEventListener('blur', () => cleanUp(element, added, focusedClasses), { once: true });\n return true;\n}\n"]}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Runs `setUp` now, and again whenever the element's children change until it succeeds, for parts
3
+ * that are parsed or rendered after the element connects. Returns the observer to disconnect, if
4
+ * `setUp` didn't succeed straight away.
5
+ */
6
+ export declare function setUpWhenReady(element: Element, setUp: () => boolean): MutationObserver;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Runs `setUp` now, and again whenever the element's children change until it succeeds, for parts
3
+ * that are parsed or rendered after the element connects. Returns the observer to disconnect, if
4
+ * `setUp` didn't succeed straight away.
5
+ */
6
+ export function setUpWhenReady(element, setUp) {
7
+ if (setUp())
8
+ return undefined;
9
+ const observer = new MutationObserver(() => {
10
+ if (setUp())
11
+ observer.disconnect();
12
+ });
13
+ observer.observe(element, { childList: true, subtree: true });
14
+ return observer;
15
+ }
16
+ //# sourceMappingURL=set-up-when-ready.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set-up-when-ready.js","sourceRoot":"","sources":["../../src/utils/set-up-when-ready.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,OAAgB,EAAE,KAAoB;IACnE,IAAI,KAAK,EAAE;QAAE,OAAO,SAAS,CAAC;IAC9B,MAAM,QAAQ,GAAG,IAAI,gBAAgB,CAAC,GAAG,EAAE;QACzC,IAAI,KAAK,EAAE;YAAE,QAAQ,CAAC,UAAU,EAAE,CAAC;IACrC,CAAC,CAAC,CAAC;IACH,QAAQ,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;IAC9D,OAAO,QAAQ,CAAC;AAClB,CAAC","sourcesContent":["/**\n * Runs `setUp` now, and again whenever the element's children change until it succeeds, for parts\n * that are parsed or rendered after the element connects. Returns the observer to disconnect, if\n * `setUp` didn't succeed straight away.\n */\nexport function setUpWhenReady(element: Element, setUp: () => boolean) {\n if (setUp()) return undefined;\n const observer = new MutationObserver(() => {\n if (setUp()) observer.disconnect();\n });\n observer.observe(element, { childList: true, subtree: true });\n return observer;\n}\n"]}
@@ -0,0 +1,13 @@
1
+ import type { ReactNode } from 'react';
2
+ type Props = Record<string, unknown>;
3
+ export type SlotProps<P extends object> = P & {
4
+ children?: ReactNode;
5
+ };
6
+ /**
7
+ * Renders its only child, merging its own props onto it, for parts that
8
+ * take `asChild`. Class names are joined, styles are merged and event
9
+ * handlers both run (the child's first). For any other prop, the child's
10
+ * value wins. Undefined props are ignored.
11
+ */
12
+ export declare function Slot<P extends object>({ children, ...slotProps }: SlotProps<P>): import("react").ReactElement<Props, string | import("react").JSXElementConstructor<any>>;
13
+ export {};
@@ -0,0 +1,40 @@
1
+ import { Children, cloneElement, isValidElement } from 'react';
2
+ import { clsx } from 'clsx';
3
+ /**
4
+ * Renders its only child, merging its own props onto it, for parts that
5
+ * take `asChild`. Class names are joined, styles are merged and event
6
+ * handlers both run (the child's first). For any other prop, the child's
7
+ * value wins. Undefined props are ignored.
8
+ */
9
+ export function Slot({ children, ...slotProps }) {
10
+ const child = Children.only(children);
11
+ if (!isValidElement(child))
12
+ return null;
13
+ const merged = { ...child.props };
14
+ for (const [key, slotValue] of Object.entries(slotProps)) {
15
+ // A prop left out stays left out, and doesn't hide one the child sets itself.
16
+ if (slotValue === undefined)
17
+ continue;
18
+ const childValue = child.props[key];
19
+ if (childValue === undefined) {
20
+ merged[key] = slotValue;
21
+ }
22
+ else if (key === 'className' && typeof slotValue === 'string' && typeof childValue === 'string') {
23
+ merged[key] = clsx(slotValue, childValue);
24
+ }
25
+ else if (key === 'style' && isObject(slotValue) && isObject(childValue)) {
26
+ merged[key] = { ...slotValue, ...childValue };
27
+ }
28
+ else if (/^on[A-Z]/.test(key) && typeof slotValue === 'function' && typeof childValue === 'function') {
29
+ merged[key] = (...args) => {
30
+ childValue(...args);
31
+ slotValue(...args);
32
+ };
33
+ }
34
+ }
35
+ return cloneElement(child, merged);
36
+ }
37
+ function isObject(value) {
38
+ return typeof value === 'object' && value !== null;
39
+ }
40
+ //# sourceMappingURL=slot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"slot.js","sourceRoot":"","sources":["../../src/utils/slot.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAE/D,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAM5B;;;;;GAKG;AACH,MAAM,UAAU,IAAI,CAAmB,EAAE,QAAQ,EAAE,GAAG,SAAS,EAAgB;IAC7E,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACtC,IAAI,CAAC,cAAc,CAAQ,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IAE/C,MAAM,MAAM,GAAU,EAAE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,IAAI,CAAC;AACrD,CAAC","sourcesContent":["import { Children, cloneElement, isValidElement } from 'react';\nimport type { ReactNode } from 'react';\nimport { clsx } from 'clsx';\n\ntype Props = Record<string, unknown>;\n\nexport type SlotProps<P extends object> = P & { children?: ReactNode };\n\n/**\n * Renders its only child, merging its own props onto it, for parts that\n * take `asChild`. Class names are joined, styles are merged and event\n * handlers both run (the child's first). For any other prop, the child's\n * value wins. Undefined props are ignored.\n */\nexport function Slot<P extends object>({ children, ...slotProps }: SlotProps<P>) {\n const child = Children.only(children);\n if (!isValidElement<Props>(child)) return null;\n\n const merged: Props = { ...child.props };\n for (const [key, slotValue] of Object.entries(slotProps)) {\n // A prop left out stays left out, and doesn't hide one the child sets itself.\n if (slotValue === undefined) continue;\n const childValue: unknown = child.props[key];\n if (childValue === undefined) {\n merged[key] = slotValue;\n } else if (key === 'className' && typeof slotValue === 'string' && typeof childValue === 'string') {\n merged[key] = clsx(slotValue, childValue);\n } else if (key === 'style' && isObject(slotValue) && isObject(childValue)) {\n merged[key] = { ...slotValue, ...childValue };\n } else if (/^on[A-Z]/.test(key) && typeof slotValue === 'function' && typeof childValue === 'function') {\n merged[key] = (...args: unknown[]) => {\n childValue(...args);\n slotValue(...args);\n };\n }\n }\n\n return cloneElement(child, merged);\n}\n\nfunction isObject(value: unknown): value is object {\n return typeof value === 'object' && value !== null;\n}\n"]}
@@ -0,0 +1,13 @@
1
+ /** A message, or one message for each plural form, as in govuk-frontend's localisation. */
2
+ export type Translation = string | Partial<Record<Intl.LDMLPluralRule, string>>;
3
+ /** The attributes for a message: the message itself, or one attribute for each plural form. */
4
+ export declare function translationAttributes(name: string, message: Translation | undefined): {
5
+ [x: string]: string;
6
+ };
7
+ /** Every attribute `translationAttributes` can write for a message, to observe. */
8
+ export declare const translationAttributeNames: (name: string) => string[];
9
+ /** Reads a message back from attributes written by `translationAttributes`. */
10
+ export declare function translationFromAttributes(element: Element, name: string): Translation | undefined;
11
+ export declare function withLocale<T>(locale: string | undefined, create: (locale: string | undefined) => T): T;
12
+ /** The message for `count`, in its plural form for the locale, with `%{count}` replaced. */
13
+ export declare function translate(message: Translation, count: number, locale?: string): string;
@@ -0,0 +1,57 @@
1
+ /*
2
+ * Messages as in govuk-frontend's localisation: one message, or one for each plural form, with
3
+ * `%{count}` for the number. Elements read them from attributes, which React parts write.
4
+ */
5
+ const pluralForms = ['zero', 'one', 'two', 'few', 'many', 'other'];
6
+ /** The attributes for a message: the message itself, or one attribute for each plural form. */
7
+ export function translationAttributes(name, message) {
8
+ if (message === undefined)
9
+ return {};
10
+ if (typeof message === 'string')
11
+ return { [name]: message };
12
+ return Object.fromEntries(pluralForms.filter((form) => message[form] !== undefined).map((form) => [`${name}-${form}`, message[form]]));
13
+ }
14
+ /** Every attribute `translationAttributes` can write for a message, to observe. */
15
+ export const translationAttributeNames = (name) => [name, ...pluralForms.map((form) => `${name}-${form}`)];
16
+ /** Reads a message back from attributes written by `translationAttributes`. */
17
+ export function translationFromAttributes(element, name) {
18
+ const message = element.getAttribute(name);
19
+ if (message !== null)
20
+ return message;
21
+ const forms = pluralForms.flatMap((form) => {
22
+ const text = element.getAttribute(`${name}-${form}`);
23
+ return text === null ? [] : [[form, text]];
24
+ });
25
+ return forms.length ? Object.fromEntries(forms) : undefined;
26
+ }
27
+ // An invalid `lang` makes Intl throw.
28
+ function safely(create) {
29
+ try {
30
+ return create(undefined);
31
+ }
32
+ catch (_a) {
33
+ return create('en');
34
+ }
35
+ }
36
+ export function withLocale(locale, create) {
37
+ try {
38
+ return create(locale);
39
+ }
40
+ catch (_a) {
41
+ return safely(create);
42
+ }
43
+ }
44
+ /** The message for `count`, in its plural form for the locale, with `%{count}` replaced. */
45
+ export function translate(message, count, locale) {
46
+ var _a;
47
+ let text;
48
+ if (typeof message === 'string')
49
+ text = message;
50
+ else {
51
+ const form = withLocale(locale, (l) => new Intl.PluralRules(l)).select(count);
52
+ text = (_a = message[form]) !== null && _a !== void 0 ? _a : message.other;
53
+ }
54
+ const number = withLocale(locale, (l) => new Intl.NumberFormat(l)).format(count);
55
+ return (text !== null && text !== void 0 ? text : '').replaceAll('%{count}', number);
56
+ }
57
+ //# sourceMappingURL=translation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"translation.js","sourceRoot":"","sources":["../../src/utils/translation.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,MAAM,WAAW,GAA0B,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;AAE1F,+FAA+F;AAC/F,MAAM,UAAU,qBAAqB,CAAC,IAAY,EAAE,OAAgC;IAClF,IAAI,OAAO,KAAK,SAAS;QAAE,OAAO,EAAE,CAAC;IACrC,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,OAAO,EAAE,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,CAAC;IAC5D,OAAO,MAAM,CAAC,WAAW,CACvB,WAAW,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,SAAS,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,GAAG,IAAI,IAAI,IAAI,EAAE,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAC5G,CAAC;AACJ,CAAC;AAED,mFAAmF;AACnF,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,IAAI,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC;AAEnH,+EAA+E;AAC/E,MAAM,UAAU,yBAAyB,CAAC,OAAgB,EAAE,IAAY;IACtE,MAAM,OAAO,GAAG,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IAC3C,IAAI,OAAO,KAAK,IAAI;QAAE,OAAO,OAAO,CAAC;IAC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AAI,OAAO,CAAC,KAAK,CAAC;IACxC,CAAC;IACD,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACjF,OAAO,CAAC,IAAI,aAAJ,IAAI,cAAJ,IAAI,GAAI,EAAE,CAAC,CAAC,UAAU,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;AACrD,CAAC","sourcesContent":["/*\n * Messages as in govuk-frontend's localisation: one message, or one for each plural form, with\n * `%{count}` for the number. Elements read them from attributes, which React parts write.\n */\n\n/** A message, or one message for each plural form, as in govuk-frontend's localisation. */\nexport type Translation = string | Partial<Record<Intl.LDMLPluralRule, string>>;\n\nconst pluralForms: Intl.LDMLPluralRule[] = ['zero', 'one', 'two', 'few', 'many', 'other'];\n\n/** The attributes for a message: the message itself, or one attribute for each plural form. */\nexport function translationAttributes(name: string, message: Translation | undefined) {\n if (message === undefined) return {};\n if (typeof message === 'string') return { [name]: message };\n return Object.fromEntries(\n pluralForms.filter((form) => message[form] !== undefined).map((form) => [`${name}-${form}`, message[form]])\n );\n}\n\n/** Every attribute `translationAttributes` can write for a message, to observe. */\nexport const translationAttributeNames = (name: string) => [name, ...pluralForms.map((form) => `${name}-${form}`)];\n\n/** Reads a message back from attributes written by `translationAttributes`. */\nexport function translationFromAttributes(element: Element, name: string): Translation | undefined {\n const message = element.getAttribute(name);\n if (message !== null) return message;\n const forms = pluralForms.flatMap((form) => {\n const text = element.getAttribute(`${name}-${form}`);\n return text === null ? [] : [[form, text] as const];\n });\n return forms.length ? Object.fromEntries(forms) : undefined;\n}\n\n// An invalid `lang` makes Intl throw.\nfunction safely<T>(create: (locale: string | undefined) => T): T {\n try {\n return create(undefined);\n } catch {\n return create('en');\n }\n}\n\nexport function withLocale<T>(locale: string | undefined, create: (locale: string | undefined) => T): T {\n try {\n return create(locale);\n } catch {\n return safely(create);\n }\n}\n\n/** The message for `count`, in its plural form for the locale, with `%{count}` replaced. */\nexport function translate(message: Translation, count: number, locale?: string) {\n let text: string | undefined;\n if (typeof message === 'string') text = message;\n else {\n const form = withLocale(locale, (l) => new Intl.PluralRules(l)).select(count);\n text = message[form] ?? message.other;\n }\n const number = withLocale(locale, (l) => new Intl.NumberFormat(l)).format(count);\n return (text ?? '').replaceAll('%{count}', number);\n}\n"]}
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Crown Copyright (Government Digital Service)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json CHANGED
@@ -1,6 +1,51 @@
1
1
  {
2
2
  "name": "@govuk-react/primitives",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.1",
4
+ "description": "Headless, progressively enhanced behaviour for design system components: custom elements with React parts over them.",
5
+ "type": "module",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ },
12
+ "./inline/*": "./dist/inline/*",
13
+ "./*-element": {
14
+ "types": "./dist/*/element.d.ts",
15
+ "default": "./dist/*/element.js"
16
+ },
17
+ "./*": {
18
+ "types": "./dist/*/index.d.ts",
19
+ "default": "./dist/*/index.js"
20
+ },
21
+ "./package.json": "./package.json"
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "licences"
26
+ ],
27
+ "scripts": {
28
+ "build": "rm -rf dist && tsc -p tsconfig.build.json && node build-inline-scripts.mjs"
29
+ },
30
+ "dependencies": {
31
+ "clsx": "^2.1.1"
32
+ },
33
+ "peerDependencies": {
34
+ "react": ">=19",
35
+ "react-dom": ">=19"
36
+ },
37
+ "author": "Alasdair McLeay",
38
+ "license": "MIT",
39
+ "homepage": "https://github.com/govuk-react/govuk-react#readme",
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "git+https://github.com/govuk-react/govuk-react.git",
43
+ "directory": "packages/primitives"
44
+ },
45
+ "publishConfig": {
46
+ "access": "public"
47
+ },
48
+ "devDependencies": {
49
+ "esbuild": "^0.28.2"
50
+ }
51
+ }