@govuk-react/primitives 0.2.1 → 0.4.0

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 (95) hide show
  1. package/README.md +109 -25
  2. package/dist/accordion/element.d.ts +3 -0
  3. package/dist/accordion/element.js.map +1 -1
  4. package/dist/accordion/index.d.ts +3 -7
  5. package/dist/accordion/index.js +35 -33
  6. package/dist/accordion/index.js.map +1 -1
  7. package/dist/auto-focus/index.d.ts +3 -285
  8. package/dist/auto-focus/index.js +13 -3
  9. package/dist/auto-focus/index.js.map +1 -1
  10. package/dist/autocomplete/element.d.ts +7 -4
  11. package/dist/autocomplete/element.js +11 -11
  12. package/dist/autocomplete/element.js.map +1 -1
  13. package/dist/autocomplete/index.d.ts +7 -7
  14. package/dist/autocomplete/index.js +17 -25
  15. package/dist/autocomplete/index.js.map +1 -1
  16. package/dist/character-count/element.d.ts +5 -0
  17. package/dist/character-count/element.js +5 -5
  18. package/dist/character-count/element.js.map +1 -1
  19. package/dist/character-count/index.d.ts +8 -8
  20. package/dist/character-count/index.js +13 -14
  21. package/dist/character-count/index.js.map +1 -1
  22. package/dist/character-count/messages.d.ts +1 -1
  23. package/dist/character-count/messages.js +13 -13
  24. package/dist/character-count/messages.js.map +1 -1
  25. package/dist/character-count-text/index.d.ts +1 -0
  26. package/dist/character-count-text/index.js +6 -0
  27. package/dist/character-count-text/index.js.map +1 -0
  28. package/dist/choices/element.d.ts +5 -0
  29. package/dist/choices/element.js +6 -31
  30. package/dist/choices/element.js.map +1 -1
  31. package/dist/disclosure/element.d.ts +3 -0
  32. package/dist/disclosure/element.js +21 -12
  33. package/dist/disclosure/element.js.map +1 -1
  34. package/dist/disclosure/index.d.ts +6 -5
  35. package/dist/disclosure/index.js +21 -11
  36. package/dist/disclosure/index.js.map +1 -1
  37. package/dist/exit-this-page/element.d.ts +1 -1
  38. package/dist/exit-this-page/element.js +20 -21
  39. package/dist/exit-this-page/element.js.map +1 -1
  40. package/dist/exit-this-page/index.d.ts +11 -11
  41. package/dist/exit-this-page/index.js +7 -7
  42. package/dist/exit-this-page/index.js.map +1 -1
  43. package/dist/file-upload/element.d.ts +11 -6
  44. package/dist/file-upload/element.js +15 -15
  45. package/dist/file-upload/element.js.map +1 -1
  46. package/dist/file-upload/index.d.ts +9 -9
  47. package/dist/file-upload/index.js +11 -11
  48. package/dist/file-upload/index.js.map +1 -1
  49. package/dist/index.d.ts +1 -0
  50. package/dist/index.js +1 -0
  51. package/dist/index.js.map +1 -1
  52. package/dist/inline/accordion-element.js +1 -1
  53. package/dist/inline/auto-focus-element.js +1 -1
  54. package/dist/inline/autocomplete-element.js +1 -1
  55. package/dist/inline/character-count-element.js +1 -1
  56. package/dist/inline/choices-element.js +1 -1
  57. package/dist/inline/disclosure-element.js +1 -1
  58. package/dist/inline/exit-this-page-element.js +1 -1
  59. package/dist/inline/file-upload-element.js +1 -1
  60. package/dist/inline/focus-links-element.js +1 -1
  61. package/dist/inline/modal-element.js +1 -1
  62. package/dist/inline/password-input-element.js +1 -1
  63. package/dist/inline/tabs-element.js +1 -1
  64. package/dist/inline-script/index.js +12 -12
  65. package/dist/modal/element.js +126 -11
  66. package/dist/modal/element.js.map +1 -1
  67. package/dist/modal/index.d.ts +8 -3
  68. package/dist/modal/index.js +56 -20
  69. package/dist/modal/index.js.map +1 -1
  70. package/dist/password-input/element.js +27 -11
  71. package/dist/password-input/element.js.map +1 -1
  72. package/dist/password-input/index.d.ts +3 -3
  73. package/dist/password-input/index.js +4 -2
  74. package/dist/password-input/index.js.map +1 -1
  75. package/dist/styles.css +11 -0
  76. package/dist/tabs/element.d.ts +3 -0
  77. package/dist/tabs/element.js +17 -10
  78. package/dist/tabs/element.js.map +1 -1
  79. package/dist/tabs/index.d.ts +9 -18
  80. package/dist/tabs/index.js +48 -44
  81. package/dist/tabs/index.js.map +1 -1
  82. package/dist/utils/change-details.d.ts +11 -0
  83. package/dist/utils/change-details.js +3 -0
  84. package/dist/utils/change-details.js.map +1 -0
  85. package/dist/utils/on-property-change.d.ts +7 -0
  86. package/dist/utils/on-property-change.js +19 -0
  87. package/dist/utils/on-property-change.js.map +1 -0
  88. package/dist/utils/set-focus.js.map +1 -1
  89. package/dist/utils/slot.d.ts +5 -3
  90. package/dist/utils/slot.js +24 -2
  91. package/dist/utils/slot.js.map +1 -1
  92. package/package.json +8 -2
  93. package/dist/utils/on-property-set.d.ts +0 -9
  94. package/dist/utils/on-property-set.js +0 -38
  95. package/dist/utils/on-property-set.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tabs/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE5E,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,OAAO,EAAoB,MAAM,cAAc,CAAC;AAEzD;;;;;;;;;;;;;GAaG;AAEH,MAAM,WAAW,GAAG,aAAa,CAI9B,EAAE,CAAC,CAAC;AAEP;;;GAGG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,GAAG,OAAO,qHAAqH,CAAC;AAEjK,MAAM,OAAO,GAAG,CAAC,IAAY,EAAE,KAAa,EAAE,EAAE,CAAC,GAAG,IAAI,IAAI,KAAK,EAAE,CAAC;AA+BpE,oGAAoG;AACpG,MAAM,UAAU,IAAI,CAAC,KAAgB;IACnC,OAAO,oBAAC,QAAQ,IAAC,QAAQ,EAAC,OAAO,KAAK,KAAK,GAAI,CAAC;AAClD,CAAC;AAMD;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,KAAsB;IAC/C,OAAO,oBAAC,QAAQ,IAAC,QAAQ,EAAC,QAAQ,KAAK,KAAK,GAAI,CAAC;AACnD,CAAC;AAED,SAAS,QAAQ,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7D,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"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tabs/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,O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aAAa,EAAE,GAAG,SAAS,CAAC,EAAE,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,EAAE,IAAI,CAAC,GAC5C,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, useReducer, useRef } from 'react';\nimport type { ComponentProps, ReactElement, ReactNode } from 'react';\nimport { early, live, type ChangeDetails } from '../utils/change-details.js';\nimport { Slot } from '../utils/slot.js';\nimport { tagName, type TabsElement } from './element.js';\n\nexport type { ChangeDetails };\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 `RadiosTrigger` and `RadiosContent` 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, details: ChangeDetails) => void;\n}>({});\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, details: ChangeDetails) => void;\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 `RadiosTrigger`\n * and `RadiosContent` 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 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 // `value` is set once hydrated, so the server markup is uncontrolled and a choice made before\n // hydration still switches tab. That choice fired no onChange that React saw, so it is reported\n // once.\n const synced = useRef(false);\n const [, render] = useReducer((count: number) => count + 1, 0);\n useEffect(() => {\n const node = ref.current;\n if (!node) return;\n if (!synced.current) {\n synced.current = true;\n const picked = node.pickedValue;\n if (picked !== undefined) {\n onValueChange?.(picked, early);\n if (value !== undefined && picked !== value) {\n // The pick shows until the next render, which adopts it or goes back to `value`.\n render();\n return;\n }\n }\n }\n if (value === undefined) node.removeAttribute('value');\n else if (node.getAttribute('value') !== value) node.setAttribute('value', value);\n });\n\n return (\n <TabsContext.Provider value={{ selected: value ?? defaultValue, name: groupName, onValueChange }}>\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 'default-value': value ?? defaultValue,\n fallback,\n orientation,\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 RadiosTriggerProps = TriggerProps & IslandProps;\n\n/** A tab in a `RadiosRoot`: a label for its panel's radio. */\nexport function RadiosTrigger({ asChild, value, name: nameProp, ...props }: RadiosTriggerProps) {\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 RadiosContentProps = ContentProps;\n\n/** A panel in a `RadiosRoot`, shown without JavaScript only while its radio is checked. Its id is its `value`. */\nexport function RadiosContent(props: RadiosContentProps) {\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, live)}\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,11 @@
1
+ /** What a change handler is told about a change, as its second argument. */
2
+ export type ChangeDetails = {
3
+ /**
4
+ * The user made this change before React hydrated, so it's reported once hydrated, with only
5
+ * the state they left and not each step. When controlled, the primitive goes back to the prop
6
+ * unless you adopt the change.
7
+ */
8
+ beforeHydration: boolean;
9
+ };
10
+ export declare const live: ChangeDetails;
11
+ export declare const early: ChangeDetails;
@@ -0,0 +1,3 @@
1
+ export const live = { beforeHydration: false };
2
+ export const early = { beforeHydration: true };
3
+ //# sourceMappingURL=change-details.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"change-details.js","sourceRoot":"","sources":["../../src/utils/change-details.ts"],"names":[],"mappings":"AAUA,MAAM,CAAC,MAAM,IAAI,GAAkB,EAAE,eAAe,EAAE,KAAK,EAAE,CAAC;AAC9D,MAAM,CAAC,MAAM,KAAK,GAAkB,EAAE,eAAe,EAAE,IAAI,EAAE,CAAC","sourcesContent":["/** What a change handler is told about a change, as its second argument. */\nexport type ChangeDetails = {\n /**\n * The user made this change before React hydrated, so it's reported once hydrated, with only\n * the state they left and not each step. When controlled, the primitive goes back to the prop\n * unless you adopt the change.\n */\n beforeHydration: boolean;\n};\n\nexport const live: ChangeDetails = { beforeHydration: false };\nexport const early: ChangeDetails = { beforeHydration: true };\n"]}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Calls `onChange` when `read` gives a new value. A script that sets a field's `value` or
3
+ * `checked`, such as React Hook Form's `setValue`, fires no event and changes no attribute, so
4
+ * the value is read on an interval, as govuk-frontend's character count and
5
+ * accessible-autocomplete do. Returns a function that stops watching.
6
+ */
7
+ export declare function onPropertyChange<T>(read: () => T, onChange: () => void): () => void;
@@ -0,0 +1,19 @@
1
+ const intervalMs = 100;
2
+ /**
3
+ * Calls `onChange` when `read` gives a new value. A script that sets a field's `value` or
4
+ * `checked`, such as React Hook Form's `setValue`, fires no event and changes no attribute, so
5
+ * the value is read on an interval, as govuk-frontend's character count and
6
+ * accessible-autocomplete do. Returns a function that stops watching.
7
+ */
8
+ export function onPropertyChange(read, onChange) {
9
+ let last = read();
10
+ const interval = window.setInterval(() => {
11
+ const next = read();
12
+ if (next === last)
13
+ return;
14
+ last = next;
15
+ onChange();
16
+ }, intervalMs);
17
+ return () => window.clearInterval(interval);
18
+ }
19
+ //# sourceMappingURL=on-property-change.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"on-property-change.js","sourceRoot":"","sources":["../../src/utils/on-property-change.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,GAAG,GAAG,CAAC;AAEvB;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAI,IAAa,EAAE,QAAoB;IACrE,IAAI,IAAI,GAAG,IAAI,EAAE,CAAC;IAClB,MAAM,QAAQ,GAAG,MAAM,CAAC,WAAW,CAAC,GAAG,EAAE;QACvC,MAAM,IAAI,GAAG,IAAI,EAAE,CAAC;QACpB,IAAI,IAAI,KAAK,IAAI;YAAE,OAAO;QAC1B,IAAI,GAAG,IAAI,CAAC;QACZ,QAAQ,EAAE,CAAC;IACb,CAAC,EAAE,UAAU,CAAC,CAAC;IACf,OAAO,GAAG,EAAE,CAAC,MAAM,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;AAC9C,CAAC","sourcesContent":["const intervalMs = 100;\n\n/**\n * Calls `onChange` when `read` gives a new value. A script that sets a field's `value` or\n * `checked`, such as React Hook Form's `setValue`, fires no event and changes no attribute, so\n * the value is read on an interval, as govuk-frontend's character count and\n * accessible-autocomplete do. Returns a function that stops watching.\n */\nexport function onPropertyChange<T>(read: () => T, onChange: () => void) {\n let last = read();\n const interval = window.setInterval(() => {\n const next = read();\n if (next === last) return;\n last = next;\n onChange();\n }, intervalMs);\n return () => window.clearInterval(interval);\n}\n"]}
@@ -1 +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;AAE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unction 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"]}
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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AC,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(\n element: HTMLElement,\n { focusedClasses = [], preventScroll = false }: { focusedClasses?: string[]; preventScroll?: boolean } = {}\n) {\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"]}
@@ -1,13 +1,15 @@
1
- import type { ReactNode } from 'react';
1
+ import type { ReactNode, Ref } from 'react';
2
2
  type Props = Record<string, unknown>;
3
3
  export type SlotProps<P extends object> = P & {
4
4
  children?: ReactNode;
5
5
  };
6
+ /** One ref that sets every ref given, and clears them again. */
7
+ export declare function composeRefs<T>(...refs: (Ref<T> | undefined)[]): (node: T | null) => () => void;
6
8
  /**
7
9
  * Renders its only child, merging its own props onto it, for parts that
8
10
  * 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
+ * handlers both run (the child's first), and so do refs. For any other prop,
12
+ * the child's value wins. Undefined props are ignored.
11
13
  */
12
14
  export declare function Slot<P extends object>({ children, ...slotProps }: SlotProps<P>): import("react").ReactElement<Props, string | import("react").JSXElementConstructor<any>>;
13
15
  export {};
@@ -1,10 +1,29 @@
1
1
  import { Children, cloneElement, isValidElement } from 'react';
2
2
  import { clsx } from 'clsx';
3
+ /** One ref that sets every ref given, and clears them again. */
4
+ export function composeRefs(...refs) {
5
+ return (node) => {
6
+ const cleanups = refs.map((ref) => {
7
+ if (typeof ref === 'function') {
8
+ const cleanup = ref(node);
9
+ return typeof cleanup === 'function' ? cleanup : () => ref(null);
10
+ }
11
+ if (!ref)
12
+ return () => { };
13
+ ref.current = node;
14
+ return () => {
15
+ ref.current = null;
16
+ };
17
+ });
18
+ return () => cleanups.forEach((cleanup) => cleanup());
19
+ };
20
+ }
21
+ const isRef = (value) => typeof value === 'function' || (typeof value === 'object' && value !== null && 'current' in value);
3
22
  /**
4
23
  * Renders its only child, merging its own props onto it, for parts that
5
24
  * 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.
25
+ * handlers both run (the child's first), and so do refs. For any other prop,
26
+ * the child's value wins. Undefined props are ignored.
8
27
  */
9
28
  export function Slot({ children, ...slotProps }) {
10
29
  const child = Children.only(children);
@@ -19,6 +38,9 @@ export function Slot({ children, ...slotProps }) {
19
38
  if (childValue === undefined) {
20
39
  merged[key] = slotValue;
21
40
  }
41
+ else if (key === 'ref' && isRef(slotValue) && isRef(childValue)) {
42
+ merged[key] = composeRefs(childValue, slotValue);
43
+ }
22
44
  else if (key === 'className' && typeof slotValue === 'string' && typeof childValue === 'string') {
23
45
  merged[key] = clsx(slotValue, childValue);
24
46
  }
@@ -1 +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,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;IACzC,KAAK,MAAM,CAAC,GAAG,EAAE,SAAS,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;QACzD,8EAA8E;QAC9E,IAAI,SAAS,KAAK,SAAS;YAAE,SAAS;QACtC,MAAM,UAAU,GAAY,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC7C,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;YAC7B,MAAM,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;QAC1B,CAAC;aAAM,IAAI,GAAG,KAAK,WAAW,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE,CAAC;YAClG,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;QAC5C,CAAC;aAAM,IAAI,GAAG,KAAK,OAAO,IAAI,QAAQ,CAAC,SAAS,CAAC,IAAI,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;YAC1E,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,SAAS,EAAE,GAAG,UAAU,EAAE,CAAC;QAChD,CAAC;aAAM,IAAI,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,OAAO,SAAS,KAAK,UAAU,IAAI,OAAO,UAAU,KAAK,UAAU,EAAE,CAAC;YACvG,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAe,EAAE,EAAE;gBACnC,UAAU,CAAC,GAAG,IAAI,CAAC,CAAC;gBACpB,SAAS,CAAC,GAAG,IAAI,CAAC,CAAC;YACrB,CAAC,CAAC;QACJ,CAAC;IACH,CAAC;IAED,OAAO,YAAY,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AACrC,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,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"]}
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,gEAAgE;AAChE,MAAM,UAAU,WAAW,CAAI,GAAG,IAA4B;IAC5D,OAAO,CAAC,IAAc,EAAE,EAAE;QACxB,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;YAChC,IAAI,OAAO,GAAG,KAAK,UAAU,EAAE,CAAC;gBAC9B,MAAM,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,CAAC;gBAC1B,OAAO,OAAO,OAAO,KAAK,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YACnE,CAAC;YACD,IAAI,CAAC,GAAG;gBAAE,OAAO,GAAG,EAAE,GAAE,CAAC,CAAC;YAC1B,GAAG,CAAC,OAAO,GAAG,IAAI,CAAC;YACnB,OAAO,GAAG,EAAE;gBACV,GAAG,CAAC,OAAO,GAAG,IAAI,CAAC;YACrB,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;QACH,OAAO,GAAG,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC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E,UAAU,CAAC,CAAC;QAC5C,CAAC;aAAM,IAAI,GAAG,KAAK,OAAO,IAAI,QAAQ,CAAC,SAAS,CAAC,IAAI,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;YAC1E,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,SAAS,EAAE,GAAG,UAAU,EAAE,CAAC;QAChD,CAAC;aAAM,IAAI,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,OAAO,SAAS,KAAK,UAAU,IAAI,OAAO,UAAU,KAAK,UAAU,EAAE,CAAC;YACvG,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAe,EAAE,EAAE;gBACnC,UAAU,CAAC,GAAG,IAAI,CAAC,CAAC;gBACpB,SAAS,CAAC,GAAG,IAAI,CAAC,CAAC;YACrB,CAAC,CAAC;QACJ,CAAC;IACH,CAAC;IAED,OAAO,YAAY,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AACrC,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,CAAC;AACrD,CAAC","sourcesContent":["import { Children, cloneElement, isValidElement } from 'react';\nimport type { ReactNode, Ref } from 'react';\nimport { clsx } from 'clsx';\n\ntype Props = Record<string, unknown>;\n\nexport type SlotProps<P extends object> = P & { children?: ReactNode };\n\n/** One ref that sets every ref given, and clears them again. */\nexport function composeRefs<T>(...refs: (Ref<T> | undefined)[]) {\n return (node: T | null) => {\n const cleanups = refs.map((ref) => {\n if (typeof ref === 'function') {\n const cleanup = ref(node);\n return typeof cleanup === 'function' ? cleanup : () => ref(null);\n }\n if (!ref) return () => {};\n ref.current = node;\n return () => {\n ref.current = null;\n };\n });\n return () => cleanups.forEach((cleanup) => cleanup());\n };\n}\n\nconst isRef = (value: unknown): value is Ref<unknown> =>\n typeof value === 'function' || (typeof value === 'object' && value !== null && 'current' in value);\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), and so do refs. For any other prop,\n * the child's 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 === 'ref' && isRef(slotValue) && isRef(childValue)) {\n merged[key] = composeRefs(childValue, 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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@govuk-react/primitives",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "Headless, progressively enhanced behaviour for design system components: custom elements with React parts over them.",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
@@ -9,6 +9,7 @@
9
9
  "types": "./dist/index.d.ts",
10
10
  "default": "./dist/index.js"
11
11
  },
12
+ "./styles.css": "./dist/styles.css",
12
13
  "./inline/*": "./dist/inline/*",
13
14
  "./*-element": {
14
15
  "types": "./dist/*/element.d.ts",
@@ -20,12 +21,17 @@
20
21
  },
21
22
  "./package.json": "./package.json"
22
23
  },
24
+ "sideEffects": [
25
+ "./dist/*/element.js",
26
+ "./dist/inline/*.js",
27
+ "./dist/styles.css"
28
+ ],
23
29
  "files": [
24
30
  "dist",
25
31
  "licences"
26
32
  ],
27
33
  "scripts": {
28
- "build": "rm -rf dist && tsc -p tsconfig.build.json && node build-inline-scripts.mjs"
34
+ "build": "rm -rf dist && tsc -p tsconfig.build.json && node build-inline-scripts.mjs && cp src/styles.css dist/styles.css"
29
35
  },
30
36
  "dependencies": {
31
37
  "clsx": "^2.1.1"
@@ -1,9 +0,0 @@
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 {};
@@ -1,38 +0,0 @@
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
@@ -1 +0,0 @@
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"]}