@govuk-react/primitives 0.3.0 → 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.
package/README.md CHANGED
@@ -203,6 +203,15 @@ Without JavaScript the `Description` is shown and both statuses are hidden. On u
203
203
 
204
204
  `countType` is "length" (the default, as `maxlength` counts), "characters" (as users see them, such as an emoji as one) or "words". The messages are English by default, and take a message for each plural form. The statuses get `data-state` ("under", "at" or "over") for styling, or pass class names such as `overLimitClass`. The field's value is the state, so a value React sets is counted too.
205
205
 
206
+ `countOf` and `countMessage` count text and build the message as the element does. They are in their own entry point, which has no `'use client'`, so a server component can call them:
207
+
208
+ ```tsx
209
+ import { countOf, countMessage } from '@govuk-react/primitives/character-count-text';
210
+
211
+ const remaining = 200 - countOf(value, 'characters');
212
+ const { state, text } = countMessage(remaining, 'characters', {});
213
+ ```
214
+
206
215
  ## Autocomplete
207
216
 
208
217
  ```tsx
@@ -1,7 +1,7 @@
1
1
  import * as React from 'react';
2
2
  import type { ComponentProps } from 'react';
3
3
  import { type CountType, type Translation } from './messages.js';
4
- export { countOf, countMessage, type CountType, type Translation } from './messages.js';
4
+ export type { CountType, Translation } from './messages.js';
5
5
  export type RootProps = React.HTMLAttributes<HTMLElement> & {
6
6
  /** The most characters, or words, the user should enter. Without it there's no count. */
7
7
  maxLength?: number;
@@ -4,7 +4,6 @@ import { createContext, useContext, useEffect, useRef, useState } from 'react';
4
4
  import { Slot } from '../utils/slot.js';
5
5
  import { tagName } from './element.js';
6
6
  import { countMessage, messageAttributes } from './messages.js';
7
- export { countOf, countMessage } from './messages.js';
8
7
  // What the counts start with, so the element only changes text React rendered.
9
8
  const InitialMessage = createContext(undefined);
10
9
  const isCharacterCount = (element) => 'update' in element;
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/character-count/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,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, useState } from 'react';\nimport type { ComponentProps } from 'react';\nimport { Slot } from '../utils/slot.js';\nimport { tagName, type CharacterCountElement } from './element.js';\nimport { countMessage, messageAttributes, type CountType, type Translation } from './messages.js';\n\nexport { countOf, countMessage, type CountType, type Translation } from './messages.js';\n\n/*\n * Headless character count: unstyled parts over the <govuk-react-character-count> element\n * (./element.ts), which shows the count on upgrade and keeps it up to date. Every part except Root\n * takes `asChild`.\n *\n * The field's value is the state, so there is nothing else to control. Without JavaScript the\n * description is shown and the counts are hidden.\n */\n\nexport type RootProps = React.HTMLAttributes<HTMLElement> & {\n /** The most characters, or words, the user should enter. Without it there's no count. */\n maxLength?: number;\n /**\n * What to count: \"length\" (the default) counts UTF-16 code units, as `maxlength` does;\n * \"characters\" counts characters as users see them, such as an emoji as one; \"words\" counts words.\n */\n countType?: CountType;\n /** The percentage of the limit from which the count is shown. */\n threshold?: number;\n /** Shown while under the limit. `%{count}` is the number left. */\n charactersUnderLimitText?: Translation;\n /** Shown at the limit. */\n charactersAtLimitText?: Translation;\n /** Shown while over the limit. `%{count}` is the number too many. */\n charactersOverLimitText?: Translation;\n /** Shown while under the limit when counting words. `%{count}` is the number left. */\n wordsUnderLimitText?: Translation;\n /** Shown at the limit when counting words. */\n wordsAtLimitText?: Translation;\n /** Shown while over the limit when counting words. `%{count}` is the number too many. */\n wordsOverLimitText?: Translation;\n /** Classes that visually hide the description once upgraded. Without them, inline styles do. */\n hiddenDescriptionClass?: string;\n /** Classes for the visible count while within the limit. */\n withinLimitClass?: string;\n /** Classes for the visible count while over the limit. */\n overLimitClass?: string;\n /** Classes for the visible count while below `threshold`. */\n belowThresholdClass?: string;\n /** Classes for the field while over the limit. */\n inputOverLimitClass?: string;\n};\n\n// What the counts start with, so the element only changes text React rendered.\nconst InitialMessage = createContext<string | undefined>(undefined);\n\nconst isCharacterCount = (element: HTMLElement): element is CharacterCountElement => 'update' in element;\n\nexport function Root({\n maxLength,\n countType = 'length',\n threshold,\n charactersUnderLimitText,\n charactersAtLimitText,\n charactersOverLimitText,\n wordsUnderLimitText,\n wordsAtLimitText,\n wordsOverLimitText,\n hiddenDescriptionClass,\n withinLimitClass,\n overLimitClass,\n belowThresholdClass,\n inputOverLimitClass,\n ...props\n}: RootProps) {\n const element = useRef<HTMLElement>(null);\n const messages = {\n 'characters-under-limit-text': charactersUnderLimitText,\n 'characters-at-limit-text': charactersAtLimitText,\n 'characters-over-limit-text': charactersOverLimitText,\n 'words-under-limit-text': wordsUnderLimitText,\n 'words-at-limit-text': wordsAtLimitText,\n 'words-over-limit-text': wordsOverLimitText,\n };\n\n // A value set by React, rather than typed, fires no `input` event.\n useEffect(() => {\n const node = element.current;\n if (node && isCharacterCount(node)) node.update();\n });\n\n // Rendered once and then left to the element, so a re-render never puts back a stale count.\n const [initial] = useState(() =>\n maxLength === undefined ? undefined : countMessage(maxLength, countType, messages, props.lang || 'en').text\n );\n\n return (\n <InitialMessage.Provider value={initial}>\n {React.createElement(tagName, {\n ...props,\n ref: element,\n // The element owns `data-upgraded`.\n suppressHydrationWarning: true,\n maxlength: maxLength,\n 'count-type': countType,\n threshold,\n ...messageAttributes('characters-under-limit-text', charactersUnderLimitText),\n ...messageAttributes('characters-at-limit-text', charactersAtLimitText),\n ...messageAttributes('characters-over-limit-text', charactersOverLimitText),\n ...messageAttributes('words-under-limit-text', wordsUnderLimitText),\n ...messageAttributes('words-at-limit-text', wordsAtLimitText),\n ...messageAttributes('words-over-limit-text', wordsOverLimitText),\n 'hidden-description-class': hiddenDescriptionClass,\n 'within-limit-class': withinLimitClass,\n 'over-limit-class': overLimitClass,\n 'below-threshold-class': belowThresholdClass,\n 'input-over-limit-class': inputOverLimitClass,\n })}\n </InitialMessage.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 InputProps = PartProps<'textarea'>;\n\n/** The field: a `textarea`, or an `input` with `asChild`. Its `maxlength` is removed on upgrade. */\nexport function Input({ asChild, ...props }: InputProps) {\n const Element = asChild ? Slot : 'textarea';\n // The element owns `maxlength` and its over-limit classes after upgrade.\n return <Element data-character-count-input=\"\" suppressHydrationWarning {...props} />;\n}\n\nexport type DescriptionProps = PartProps<'div'>;\n\n/**\n * The description, such as \"You can enter up to 200 characters\", shown without JavaScript and\n * visually hidden on upgrade. Give it an id and add that to the field's `aria-describedby`.\n */\nexport function Description({ asChild, ...props }: DescriptionProps) {\n const Element = asChild ? Slot : 'div';\n // The element owns its hiding styles after upgrade.\n return <Element data-character-count-description=\"\" suppressHydrationWarning {...props} />;\n}\n\nexport type StatusProps = PartProps<'div'>;\n\n/** The visible count, hidden from assistive technology. Hidden until the element upgrades. */\nexport function Status({ asChild, children, ...props }: StatusProps) {\n const initial = useContext(InitialMessage);\n const Element = asChild ? Slot : 'div';\n // The element owns `hidden`, its text and its classes after upgrade.\n return (\n <Element data-character-count-status=\"\" hidden aria-hidden=\"true\" suppressHydrationWarning {...props}>\n {children ?? initial}\n </Element>\n );\n}\n\nexport type ScreenReaderStatusProps = PartProps<'div'>;\n\n/** The count for screen readers, once the user pauses typing. Make it visually hidden. */\nexport function ScreenReaderStatus({ asChild, children, ...props }: ScreenReaderStatusProps) {\n const initial = useContext(InitialMessage);\n const Element = asChild ? Slot : 'div';\n // The element owns `hidden`, `aria-hidden` and its text after upgrade.\n return (\n <Element data-character-count-sr-status=\"\" hidden aria-live=\"polite\" suppressHydrationWarning {...props}>\n {children ?? initial}\n </Element>\n );\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/character-count/index.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAE/E,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CA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;IACvC,oDAAoD;IACpD,OAAO,oBAAC,OAAO,wCAAkC,EAAE,EAAC,wBAAwB,WAAK,KAAK,GAAI,CAAC;AAC7F,CAAC;AAID,8FAA8F;AAC9F,MAAM,UAAU,MAAM,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,KAAK,EAAe;IACjE,MAAM,OAAO,GAAG,UAAU,CAAC,cAAc,CAAC,CAAC;IAC3C,MAAM,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;IACvC,qEAAqE;IACrE,OAAO,CACL,oBAAC,OAAO,mCAA6B,EAAE,EAAC,MAAM,uBAAa,MAAM,EAAC,wBAAwB,WAAK,KAAK,IACjG,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,OAAO,CACZ,CACX,CAAC;AACJ,CAAC;AAID,0FAA0F;AAC1F,MAAM,UAAU,kBAAkB,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,KAAK,EAA2B;IACzF,MAAM,OAAO,GAAG,UAAU,CAAC,cAAc,CAAC,CAAC;IAC3C,MAAM,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;IACvC,uEAAuE;IACvE,OAAO,CACL,oBAAC,OAAO,sCAAgC,EAAE,EAAC,MAAM,qBAAW,QAAQ,EAAC,wBAAwB,WAAK,KAAK,IACpG,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,OAAO,CACZ,CACX,CAAC;AACJ,CAAC","sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { createContext, useContext, useEffect, useRef, useState } from 'react';\nimport type { ComponentProps } from 'react';\nimport { Slot } from '../utils/slot.js';\nimport { tagName, type CharacterCountElement } from './element.js';\nimport { countMessage, messageAttributes, type CountType, type Translation } from './messages.js';\n\nexport type { CountType, Translation } from './messages.js';\n\n/*\n * Headless character count: unstyled parts over the <govuk-react-character-count> element\n * (./element.ts), which shows the count on upgrade and keeps it up to date. Every part except Root\n * takes `asChild`.\n *\n * The field's value is the state, so there is nothing else to control. Without JavaScript the\n * description is shown and the counts are hidden.\n */\n\nexport type RootProps = React.HTMLAttributes<HTMLElement> & {\n /** The most characters, or words, the user should enter. Without it there's no count. */\n maxLength?: number;\n /**\n * What to count: \"length\" (the default) counts UTF-16 code units, as `maxlength` does;\n * \"characters\" counts characters as users see them, such as an emoji as one; \"words\" counts words.\n */\n countType?: CountType;\n /** The percentage of the limit from which the count is shown. */\n threshold?: number;\n /** Shown while under the limit. `%{count}` is the number left. */\n charactersUnderLimitText?: Translation;\n /** Shown at the limit. */\n charactersAtLimitText?: Translation;\n /** Shown while over the limit. `%{count}` is the number too many. */\n charactersOverLimitText?: Translation;\n /** Shown while under the limit when counting words. `%{count}` is the number left. */\n wordsUnderLimitText?: Translation;\n /** Shown at the limit when counting words. */\n wordsAtLimitText?: Translation;\n /** Shown while over the limit when counting words. `%{count}` is the number too many. */\n wordsOverLimitText?: Translation;\n /** Classes that visually hide the description once upgraded. Without them, inline styles do. */\n hiddenDescriptionClass?: string;\n /** Classes for the visible count while within the limit. */\n withinLimitClass?: string;\n /** Classes for the visible count while over the limit. */\n overLimitClass?: string;\n /** Classes for the visible count while below `threshold`. */\n belowThresholdClass?: string;\n /** Classes for the field while over the limit. */\n inputOverLimitClass?: string;\n};\n\n// What the counts start with, so the element only changes text React rendered.\nconst InitialMessage = createContext<string | undefined>(undefined);\n\nconst isCharacterCount = (element: HTMLElement): element is CharacterCountElement => 'update' in element;\n\nexport function Root({\n maxLength,\n countType = 'length',\n threshold,\n charactersUnderLimitText,\n charactersAtLimitText,\n charactersOverLimitText,\n wordsUnderLimitText,\n wordsAtLimitText,\n wordsOverLimitText,\n hiddenDescriptionClass,\n withinLimitClass,\n overLimitClass,\n belowThresholdClass,\n inputOverLimitClass,\n ...props\n}: RootProps) {\n const element = useRef<HTMLElement>(null);\n const messages = {\n 'characters-under-limit-text': charactersUnderLimitText,\n 'characters-at-limit-text': charactersAtLimitText,\n 'characters-over-limit-text': charactersOverLimitText,\n 'words-under-limit-text': wordsUnderLimitText,\n 'words-at-limit-text': wordsAtLimitText,\n 'words-over-limit-text': wordsOverLimitText,\n };\n\n // A value set by React, rather than typed, fires no `input` event.\n useEffect(() => {\n const node = element.current;\n if (node && isCharacterCount(node)) node.update();\n });\n\n // Rendered once and then left to the element, so a re-render never puts back a stale count.\n const [initial] = useState(() =>\n maxLength === undefined ? undefined : countMessage(maxLength, countType, messages, props.lang || 'en').text\n );\n\n return (\n <InitialMessage.Provider value={initial}>\n {React.createElement(tagName, {\n ...props,\n ref: element,\n // The element owns `data-upgraded`.\n suppressHydrationWarning: true,\n maxlength: maxLength,\n 'count-type': countType,\n threshold,\n ...messageAttributes('characters-under-limit-text', charactersUnderLimitText),\n ...messageAttributes('characters-at-limit-text', charactersAtLimitText),\n ...messageAttributes('characters-over-limit-text', charactersOverLimitText),\n ...messageAttributes('words-under-limit-text', wordsUnderLimitText),\n ...messageAttributes('words-at-limit-text', wordsAtLimitText),\n ...messageAttributes('words-over-limit-text', wordsOverLimitText),\n 'hidden-description-class': hiddenDescriptionClass,\n 'within-limit-class': withinLimitClass,\n 'over-limit-class': overLimitClass,\n 'below-threshold-class': belowThresholdClass,\n 'input-over-limit-class': inputOverLimitClass,\n })}\n </InitialMessage.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 InputProps = PartProps<'textarea'>;\n\n/** The field: a `textarea`, or an `input` with `asChild`. Its `maxlength` is removed on upgrade. */\nexport function Input({ asChild, ...props }: InputProps) {\n const Element = asChild ? Slot : 'textarea';\n // The element owns `maxlength` and its over-limit classes after upgrade.\n return <Element data-character-count-input=\"\" suppressHydrationWarning {...props} />;\n}\n\nexport type DescriptionProps = PartProps<'div'>;\n\n/**\n * The description, such as \"You can enter up to 200 characters\", shown without JavaScript and\n * visually hidden on upgrade. Give it an id and add that to the field's `aria-describedby`.\n */\nexport function Description({ asChild, ...props }: DescriptionProps) {\n const Element = asChild ? Slot : 'div';\n // The element owns its hiding styles after upgrade.\n return <Element data-character-count-description=\"\" suppressHydrationWarning {...props} />;\n}\n\nexport type StatusProps = PartProps<'div'>;\n\n/** The visible count, hidden from assistive technology. Hidden until the element upgrades. */\nexport function Status({ asChild, children, ...props }: StatusProps) {\n const initial = useContext(InitialMessage);\n const Element = asChild ? Slot : 'div';\n // The element owns `hidden`, its text and its classes after upgrade.\n return (\n <Element data-character-count-status=\"\" hidden aria-hidden=\"true\" suppressHydrationWarning {...props}>\n {children ?? initial}\n </Element>\n );\n}\n\nexport type ScreenReaderStatusProps = PartProps<'div'>;\n\n/** The count for screen readers, once the user pauses typing. Make it visually hidden. */\nexport function ScreenReaderStatus({ asChild, children, ...props }: ScreenReaderStatusProps) {\n const initial = useContext(InitialMessage);\n const Element = asChild ? Slot : 'div';\n // The element owns `hidden`, `aria-hidden` and its text after upgrade.\n return (\n <Element data-character-count-sr-status=\"\" hidden aria-live=\"polite\" suppressHydrationWarning {...props}>\n {children ?? initial}\n </Element>\n );\n}\n"]}
@@ -0,0 +1 @@
1
+ export { countOf, countMessage, type CountType, type Translation } from '../character-count/messages.js';
@@ -0,0 +1,6 @@
1
+ /*
2
+ * Counting and count messages, as <govuk-react-character-count> works them out. No 'use client', so
3
+ * a server component can call them.
4
+ */
5
+ export { countOf, countMessage } from '../character-count/messages.js';
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/character-count-text/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,OAAO,EAAE,YAAY,EAAoC,MAAM,gCAAgC,CAAC","sourcesContent":["/*\n * Counting and count messages, as <govuk-react-character-count> works them out. No 'use client', so\n * a server component can call them.\n */\n\nexport { countOf, countMessage, type CountType, type Translation } from '../character-count/messages.js';\n"]}
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export * as Modal from './modal/index.js';
4
4
  export * as PasswordInput from './password-input/index.js';
5
5
  export * as Disclosure from './disclosure/index.js';
6
6
  export * as CharacterCount from './character-count/index.js';
7
+ export * as CharacterCountText from './character-count-text/index.js';
7
8
  export * as Choices from './choices/index.js';
8
9
  export * as AutoFocus from './auto-focus/index.js';
9
10
  export * as FocusLinks from './focus-links/index.js';
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ export * as Modal from './modal/index.js';
4
4
  export * as PasswordInput from './password-input/index.js';
5
5
  export * as Disclosure from './disclosure/index.js';
6
6
  export * as CharacterCount from './character-count/index.js';
7
+ export * as CharacterCountText from './character-count-text/index.js';
7
8
  export * as Choices from './choices/index.js';
8
9
  export * as AutoFocus from './auto-focus/index.js';
9
10
  export * as FocusLinks from './focus-links/index.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAC;AAClD,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAC;AACxC,OAAO,KAAK,KAAK,MAAM,kBAAkB,CAAC;AAC1C,OAAO,KAAK,aAAa,MAAM,2BAA2B,CAAC;AAC3D,OAAO,KAAK,UAAU,MAAM,uBAAuB,CAAC;AACpD,OAAO,KAAK,cAAc,MAAM,4BAA4B,CAAC;AAC7D,OAAO,KAAK,OAAO,MAAM,oBAAoB,CAAC;AAC9C,OAAO,KAAK,SAAS,MAAM,uBAAuB,CAAC;AACnD,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;AACrD,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAC;AAC5C,OAAO,KAAK,YAAY,MAAM,2BAA2B,CAAC;AAC1D,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;AACrD,OAAO,KAAK,YAAY,MAAM,yBAAyB,CAAC","sourcesContent":["export * as Accordion from './accordion/index.js';\nexport * as Tabs from './tabs/index.js';\nexport * as Modal from './modal/index.js';\nexport * as PasswordInput from './password-input/index.js';\nexport * as Disclosure from './disclosure/index.js';\nexport * as CharacterCount from './character-count/index.js';\nexport * as Choices from './choices/index.js';\nexport * as AutoFocus from './auto-focus/index.js';\nexport * as FocusLinks from './focus-links/index.js';\nexport * as Button from './button/index.js';\nexport * as ExitThisPage from './exit-this-page/index.js';\nexport * as FileUpload from './file-upload/index.js';\nexport * as Autocomplete from './autocomplete/index.js';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAC;AAClD,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAC;AACxC,OAAO,KAAK,KAAK,MAAM,kBAAkB,CAAC;AAC1C,OAAO,KAAK,aAAa,MAAM,2BAA2B,CAAC;AAC3D,OAAO,KAAK,UAAU,MAAM,uBAAuB,CAAC;AACpD,OAAO,KAAK,cAAc,MAAM,4BAA4B,CAAC;AAC7D,OAAO,KAAK,kBAAkB,MAAM,iCAAiC,CAAC;AACtE,OAAO,KAAK,OAAO,MAAM,oBAAoB,CAAC;AAC9C,OAAO,KAAK,SAAS,MAAM,uBAAuB,CAAC;AACnD,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;AACrD,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAC;AAC5C,OAAO,KAAK,YAAY,MAAM,2BAA2B,CAAC;AAC1D,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;AACrD,OAAO,KAAK,YAAY,MAAM,yBAAyB,CAAC","sourcesContent":["export * as Accordion from './accordion/index.js';\nexport * as Tabs from './tabs/index.js';\nexport * as Modal from './modal/index.js';\nexport * as PasswordInput from './password-input/index.js';\nexport * as Disclosure from './disclosure/index.js';\nexport * as CharacterCount from './character-count/index.js';\nexport * as CharacterCountText from './character-count-text/index.js';\nexport * as Choices from './choices/index.js';\nexport * as AutoFocus from './auto-focus/index.js';\nexport * as FocusLinks from './focus-links/index.js';\nexport * as Button from './button/index.js';\nexport * as ExitThisPage from './exit-this-page/index.js';\nexport * as FileUpload from './file-upload/index.js';\nexport * as Autocomplete from './autocomplete/index.js';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@govuk-react/primitives",
3
- "version": "0.3.0",
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",