{"version":3,"file":"Toggle.mjs","sources":["../../../src/components/Toggle/Toggle.tsx"],"sourcesContent":["import { Switch, Label, Field, Description } from '@headlessui/react';\nimport clsx from 'clsx';\nimport React from 'react';\nimport type { ReactNode } from 'react';\nimport type { EitherInclusive, ExtractProps } from '../../util/utility-types';\nimport { default as EDSLabel } from '../Label';\nimport styles from './Toggle.module.css';\n\ntype ToggleLabelProps = {\n  /**\n   * Visible text label for the toggle.\n   */\n  children: ReactNode;\n  /**\n   * Additional classnames passed in for styling the toggle label.\n   */\n  className?: string;\n};\n\ntype ToggleButtonProps = {\n  /**\n   * Additional classnames passed in for styling the toggle button.\n   */\n  className?: string;\n  /**\n   * Whether toggle is checked or unchecked.\n   */\n  checked: boolean;\n  /**\n   * Callback called when toggle state is changed.\n   */\n  onChange: (checked: boolean) => void;\n  /**\n   * Whether toggle is disabled.\n   */\n  disabled?: boolean;\n};\n\ntype ToggleProps = ToggleButtonProps & {\n  children?: ReactNode;\n  subLabel?: ReactNode;\n} & EitherInclusive<\n    {\n      /**\n       * Visible text label for the component.\n       */\n      label: ReactNode;\n    },\n    {\n      /**\n       * Aria-label to provide an accesible name for the text input if no visible label is provided.\n       */\n      'aria-label': string;\n    }\n  >;\n\nconst ToggleLabel = ({ children, className }: ToggleLabelProps) => {\n  const componentClassName = clsx(styles['toggle__label'], className);\n  return <Label as={EDSLabel} className={componentClassName} text={children} />;\n};\n\nconst ToggleSubLabel = ({ children, className }: ToggleLabelProps) => {\n  const componentClassName = clsx(styles['toggle__subLabel'], className);\n  return (\n    <Description as=\"span\" className={componentClassName}>\n      {children}\n    </Description>\n  );\n};\n\nconst ToggleButton = ({ className, checked, ...other }: ToggleButtonProps) => (\n  <Switch\n    checked={checked}\n    className={clsx(\n      styles['toggle__button'],\n      checked\n        ? styles['toggle__button--checked']\n        : styles['toggle__button--unchecked'],\n      className,\n    )}\n    {...other}\n  >\n    {/* This span is the circular part of the toggle */}\n    <span className={styles['toggle__button-thumb']} />\n  </Switch>\n);\n\n/**\n * In order to use the individual <Toggle.Label> and <Toggle.Button> sub-components for customization,\n * they need to be wrapped in this <ToggleWrapper> component for some functionality HeadlessUI provides.\n *\n * Instead of direct assignment to ToggleWrapper, wrapping into a custom component prevents TypeScript erroring of using private Headless Switch Group.\n */\nconst ToggleWrapper = (props: ExtractProps<typeof Field>) => (\n  <Field {...props} />\n);\n\n/**\n * ## Usage\n *\n * | Type/Use | Description | Example |\n * |----------|-------------|---------|\n * | Enable/Disable a feature | Turns a feature on or off. | Dark mode toggle in a settings menu. |\n * | Show/Hide content | Reveals or hides additional UI elements. | Show/hide advanced search filters. |\n * | Activate notifications | Turns notifications on or off. | Push notifications toggle in a mobile app. |\n * | Privacy & security settings | Adjusts user privacy or security preferences. | Allow/block location sharing. |\n * | Subscription/opt-in settings | Lets users opt in or out of subscriptions. | Newsletter sign-up toggle. |\n * | Account preferences | Changes user-specific account features. | Two-factor authentication toggle. |\n * | Mode switching | Switches between different UI modes. | Toggle between grid view and list view. |\n * | Accessibility options | Improves usability for accessibility. | High contrast mode toggle. |\n *\n * **NOTE**: This component requires a `label` or `aria-label` prop.\n *\n * ### Best Practices\n *\n * * Use only for binary yes/no or on/off options.\n * * Don't use a toggle to perform a single action.\n *\n * ## Content & Accessibility\n *\n * ### Do's\n *\n * * Write labels in sentence case.\n * * Use punctuation in full-sentence secondary labels, and punctuate all secondary labels in a list the same way.\n * * Ensure labels are clear without surrounding information for context.\n * * Include alt text for toggles that don't have a label.\n *\n * ### Don'ts\n *\n * * Don't add end punctuation to the primary label.\n * * Don't mix full-sentence and partial-sentence secondary labels.\n *\n * ## Resources\n *\n * * https://headlessui.dev/react/switch\n */\nexport const Toggle = ({ label, subLabel, ...other }: ToggleProps) => {\n  const wrapperClassName = clsx(\n    styles['toggle__wrapper'],\n    subLabel && styles['toggle--has-subLabel'],\n  );\n  return label ? (\n    <ToggleWrapper as=\"div\" className={wrapperClassName}>\n      <ToggleButton {...other} />\n      <ToggleLabel>\n        <>\n          {label}\n          {subLabel && <ToggleSubLabel>{subLabel}</ToggleSubLabel>}\n        </>\n      </ToggleLabel>\n    </ToggleWrapper>\n  ) : (\n    <ToggleButton {...other} />\n  );\n};\n\nToggle.displayName = 'Toggle';\nToggleLabel.displayName = 'Toggle.Label';\nToggleButton.displayName = 'Toggle.Button';\nToggleWrapper.displayName = 'Toggle.Wrapper';\nToggleSubLabel.displayName = 'Toggle.SubLabel';\n\nToggle.Label = ToggleLabel;\nToggle.Button = ToggleButton;\nToggle.Wrapper = ToggleWrapper;\nToggle.SubLabel = ToggleSubLabel;\n"],"names":["React","EDSLabel"],"mappings":";;;;;;AAwDA,MAAM,WAAW,GAAG,CAAC,EAAE,QAAQ,EAAE,SAAS,EAAoB,KAAI;IAChE,MAAM,kBAAkB,GAAG,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,EAAE,SAAS,CAAC;AACnE,IAAA,OAAOA,cAAA,CAAA,aAAA,CAAC,KAAK,EAAA,EAAC,EAAE,EAAEC,OAAQ,EAAE,SAAS,EAAE,kBAAkB,EAAE,IAAI,EAAE,QAAQ,GAAI;AAC/E,CAAC;AAED,MAAM,cAAc,GAAG,CAAC,EAAE,QAAQ,EAAE,SAAS,EAAoB,KAAI;IACnE,MAAM,kBAAkB,GAAG,IAAI,CAAC,MAAM,CAAC,kBAAkB,CAAC,EAAE,SAAS,CAAC;AACtE,IAAA,QACED,cAAA,CAAA,aAAA,CAAC,WAAW,EAAA,EAAC,EAAE,EAAC,MAAM,EAAC,SAAS,EAAE,kBAAkB,EAAA,EACjD,QAAQ,CACG;AAElB,CAAC;AAED,MAAM,YAAY,GAAG,CAAC,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,KAAK,EAAqB,MACvEA,cAAA,CAAA,aAAA,CAAC,MAAM,EAAA,EACL,OAAO,EAAE,OAAO,EAChB,SAAS,EAAE,IAAI,CACb,MAAM,CAAC,gBAAgB,CAAC,EACxB;AACE,UAAE,MAAM,CAAC,yBAAyB;UAChC,MAAM,CAAC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