{"version":3,"file":"Checkbox.mjs","sources":["../../../src/components/Checkbox/Checkbox.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport React, {\n  forwardRef,\n  useEffect,\n  useRef,\n  type RefObject,\n  type ForwardedRef,\n} from 'react';\nimport type { ReactNode } from 'react';\n\nimport type { EitherInclusive } from '../../util/utility-types';\n\nimport Label from '../Label';\nimport Text from '../Text';\n\nimport styles from './Checkbox.module.css';\n\ntype CheckboxHTMLElementProps = Omit<\n  React.InputHTMLAttributes<HTMLInputElement>,\n  'checked' | 'id' | 'size'\n>;\n\ntype CheckboxInputProps = CheckboxHTMLElementProps & {\n  // Component API\n  /**\n   * Whether checkbox is checked.\n   */\n  checked?: boolean;\n  /**\n   * Additional classnames passed in for styling.\n   */\n  className?: string;\n  /**\n   * Checkbox ID. Used to connect the input with a label for accessibility purposes.\n   */\n  id: string;\n  /**\n   * Whether the checkbox is \"indeterminate\". Neither checked nor unchecked. The most common use\n   * case for this is when a checkbox has sub-checkboxes, to represent a \"partially checked\" state.\n   */\n  indeterminate?: boolean;\n  // Design API\n  /**\n   * Whether the radio button is in an error state\n   */\n  isError?: boolean;\n  /**\n   * Additional descriptive text below the primary label, adding additional detail\n   */\n  subLabel?: ReactNode;\n};\n\n// id is required in CheckboxInputProps but optional in CheckboxProps, so we\n// first remove `id` from CheckboxInputProps before intersecting.\ntype CheckboxProps = Omit<CheckboxInputProps, 'id'> & {\n  /**\n   * HTML id attribute. If not passed, this component\n   * will generate an id to use for accessibility.\n   */\n  id?: string;\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\nfunction useForwardedRef<T>(ref: ForwardedRef<T>): RefObject<T | null> {\n  const innerRef = useRef<T>(null);\n\n  // Keep the internal and forwarded refs in sync.\n  useEffect(() => {\n    if (!ref) {\n      return;\n    } else if (typeof ref === 'function') {\n      ref(innerRef.current);\n    } else {\n      ref.current = innerRef.current;\n    }\n  }, [ref]);\n\n  return innerRef;\n}\n\n/**\n * Checkbox input element, exported for greater flexibility.\n * You must provide an `id` prop and connect it to a visible label.\n */\nconst CheckboxInput = React.forwardRef<HTMLInputElement, CheckboxInputProps>(\n  ({ checked, className, disabled, indeterminate, ...other }, ref) => {\n    const forwardedRef = useForwardedRef(ref);\n\n    // Make this checkbox indeterminate. Can only be done with JS for some reason.\n    // See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#indeterminate_state_checkboxes.\n    useEffect(() => {\n      if (forwardedRef.current) {\n        forwardedRef.current.indeterminate = !!indeterminate;\n      }\n    }, [forwardedRef, indeterminate]);\n\n    return (\n      <input\n        checked={checked}\n        className={clsx(className, styles['checkbox__input'])}\n        disabled={disabled}\n        ref={forwardedRef}\n        type=\"checkbox\"\n        {...other}\n      />\n    );\n  },\n);\n\n/**\n * ## Usage\n *\n * | Type/Use | Description | Example |\n * |----------|-------------|---------|\n * | Standard | Basic binary control (checked/unchecked). | Accept terms and conditions. Enable/disable a setting. |\n * | Multi-select | Used in a group to select multiple items independently. | Filter lists. Select options in forms. |\n * | Tri-state | Supports three states: checked, unchecked, and indeterminate. | Parent checkbox for nested options. |\n * | Nested | A parent checkbox controls its children; the parent reflects checked, unchecked, or indeterminate. | Selections within categories. |\n * | Inline | Placed within text or small UI elements. | Inline acknowledgement text. Compact forms. |\n * | Disabled | Non-interactive checkbox indicating a fixed or unavailable state. | Unavailable options. Permissions indicator. |\n * | Table | Selects lines or entries in a table. | Rows to perform an action on. |\n *\n * ### Best Practices\n *\n * * By default, checkboxes should have labels.\n * * Checkboxes may have no visible label if they are part of another component, such as a table row.\n * * Checkbox group labels can include an \"optional\" or \"required\" hint.\n *\n * ## Interaction\n *\n * Both the checkbox and its label(s) can trigger the interaction; do not limit the touch target to\n * the checkbox alone. Checks work independently of one another—selecting one does not deselect the\n * others unless they have a parent-child relationship. Consider using a radio button if checking one\n * option should deselect all others.\n *\n * ## Content & Accessibility\n *\n * ### Do's\n *\n * * Use group labels to describe the action taken within a group of checkboxes.\n * * Keep labels to a few words (ideally 3 or fewer) and use sentence case.\n * * Use the first person when a checkbox indicates the user accepting something, e.g. \"I agree to...\".\n * * Only use helper text when necessary to explain the action or its results.\n * * In error messages, describe the issue and what to do about it.\n *\n * ### Don'ts\n *\n * * Use helper text by default; consider better group labels first.\n * * Use end punctuation on group labels.\n * * Truncate labels.\n */\nexport const Checkbox = Object.assign(\n  forwardRef<HTMLInputElement, CheckboxProps>((props, ref) => {\n    // All remaining props are passed to the `input` element\n    const { className, id, isError, label, disabled, subLabel, ...other } =\n      props;\n\n    const generatedId = React.useId();\n    const checkboxId = id || generatedId;\n\n    return (\n      <div\n        className={clsx(\n          className,\n          styles.checkbox,\n          isError && styles['checkbox--error'],\n        )}\n      >\n        <CheckboxInput\n          disabled={disabled}\n          id={checkboxId}\n          ref={ref}\n          {...other}\n        />\n        {(label || subLabel) && (\n          <div className={styles['checkbox__labels']}>\n            {label && (\n              <Label\n                className={styles['checkbox__label']}\n                disabled={disabled}\n                htmlFor={checkboxId}\n                text={label}\n              />\n            )}\n            {subLabel && (\n              <Text\n                as=\"span\"\n                className={clsx(\n                  styles['checkbox__sub-label'],\n                  disabled && styles['checkbox--is-disabled'],\n                )}\n                preset=\"body-sm\"\n              >\n                {subLabel}\n              </Text>\n            )}\n          </div>\n        )}\n      </div>\n    );\n  }),\n  {\n    Input: CheckboxInput,\n    Label: Label,\n  },\n);\n\nCheckbox.displayName = 'Checkbox';\nCheckboxInput.displayName = 'Checkbox.Input';\n"],"names":["React"],"mappings":";;;;;;AA2EA,SAAS,eAAe,CAAI,GAAoB,EAAA;AAC9C,IAAA,MAAM,QAAQ,GAAG,MAAM,CAAI,IAAI,CAAC;;IAGhC,SAAS,CAAC,MAAK;QACb,IAAI,CAAC,GAAG,EAAE;YACR;QACF;AAAO,aAAA,IAAI,OAAO,GAAG,KAAK,UAAU,EAAE;AACp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