{"version":3,"file":"SwitchField.mjs","names":[],"sources":["../../../../src/components/Form/SwitchField.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport type {\n  ChangeEventHandler,\n  ComponentProps,\n  KeyboardEventHandler,\n  MouseEventHandler,\n  PropsWithChildren,\n  ReactNode,\n} from 'react';\nimport React, { isValidElement, useContext, useRef, useState } from 'react';\nimport { useStableId } from '../UtilityComponents/useStableId';\nimport { AriaLiveAnnouncerContext } from '../Accessibility';\nimport { useTranslationContext } from '../../context';\n\nexport type SwitchFieldProps = Omit<\n  PropsWithChildren<ComponentProps<'input'>>,\n  'children'\n> & {\n  /** Label content, ignored when `title` is set. */\n  children?: ReactNode;\n  /** Description line below the title. Rendered only with `title`. */\n  description?: string;\n  /** Class applied to the root element. */\n  fieldClassName?: string;\n  /** Title line. Replaces `children` as the label. */\n  title?: string;\n};\n\nexport const SwitchField = ({\n  children,\n  description,\n  fieldClassName,\n  title,\n  ...props\n}: SwitchFieldProps) => {\n  const {\n    'aria-label': ariaLabel,\n    'aria-labelledby': ariaLabelledBy,\n    checked,\n    defaultChecked,\n    disabled,\n    id,\n    onChange,\n    onKeyDown,\n    ...rest\n  } = props;\n  const generatedSwitchId = useStableId();\n  const switchId = id ?? `str-chat__switch-field-${generatedSwitchId}`;\n  const switchLabelId = `${switchId}-label`;\n  const inputRef = useRef<HTMLInputElement | null>(null);\n  // Optional: a SwitchField can render outside an announcer provider, where it stays silent.\n  const announce = useContext(AriaLiveAnnouncerContext)?.announce;\n  const { t } = useTranslationContext();\n\n  const [uncontrolledChecked, setUncontrolledChecked] = useState(Boolean(defaultChecked));\n  const isControlled = checked !== undefined;\n  const isOn = isControlled ? checked : uncontrolledChecked;\n  const isReadOnly = isControlled && onChange === undefined;\n\n  // Without a title or `aria-label`, the ID of a caller-supplied child element can name the switch.\n  const childElement = isValidElement<{ id?: string }>(children) ? children : undefined;\n  const childLabelId = childElement?.props.id;\n  // Accessible name precedence: `aria-labelledby`, `aria-label`, the generated title label ID,\n  // then the child element ID.\n  const resolvedAriaLabelledBy =\n    ariaLabelledBy ?? (!ariaLabel ? (title ? switchLabelId : childLabelId) : undefined);\n\n  // The announcement names the setting by `title`, then `aria-label`, then the text of the\n  // `aria-labelledby` target (its title row when present, so the description is not read as name).\n  const resolveAnnouncementName = () => {\n    if (title) return title;\n    if (ariaLabel) return ariaLabel;\n    if (resolvedAriaLabelledBy) {\n      const labelEl = document.getElementById(resolvedAriaLabelledBy);\n      const titleEl = labelEl?.querySelector(\n        '.str-chat__form__switch-field__label__text',\n      );\n      return (titleEl?.textContent || labelEl?.textContent || '').trim() || undefined;\n    }\n    return undefined;\n  };\n\n  const handleChange: ChangeEventHandler<HTMLInputElement> = (event) => {\n    if (!isControlled) {\n      setUncontrolledChecked(event.target.checked);\n    }\n\n    onChange?.(event);\n\n    const setting = resolveAnnouncementName();\n    if (!announce || !setting) return;\n    announce(\n      event.target.checked\n        ? t('form.switchField.enabled.ariaLabel', '{{ setting }} enabled', { setting })\n        : t('form.switchField.disabled.ariaLabel', '{{ setting }} disabled', { setting }),\n      { priority: 'polite' },\n    );\n  };\n\n  const handleKeyDown: KeyboardEventHandler<HTMLInputElement> = (event) => {\n    onKeyDown?.(event);\n    if (event.defaultPrevented || event.key !== ' ') return;\n\n    event.preventDefault();\n    event.currentTarget.click();\n  };\n\n  const handleSwitchClick: MouseEventHandler<HTMLDivElement> = (event) => {\n    if (disabled || event.target === inputRef.current) return;\n    inputRef.current?.click();\n  };\n\n  return (\n    <div\n      className={clsx(\n        'str-chat__form__switch-field',\n        fieldClassName,\n        disabled && 'str-chat__form__switch-field--disabled',\n      )}\n    >\n      {title ? (\n        <SwitchFieldLabel\n          description={description}\n          htmlFor={switchId}\n          id={switchLabelId}\n          title={title}\n        />\n      ) : (\n        children\n      )}\n      <Switch\n        {...rest}\n        aria-label={ariaLabel}\n        aria-labelledby={resolvedAriaLabelledBy}\n        checked={isOn}\n        disabled={disabled}\n        id={switchId}\n        on={isOn}\n        onChange={handleChange}\n        onKeyDown={handleKeyDown}\n        onSwitchClick={handleSwitchClick}\n        readOnly={isReadOnly}\n        switchRef={inputRef}\n      />\n    </div>\n  );\n};\nexport type SwitchProps = Omit<ComponentProps<'input'>, 'type'> & {\n  on?: boolean;\n  onSwitchClick?: MouseEventHandler<HTMLDivElement>;\n  /**\n   * Renders only the visual switch, without an input, for when another element owns interaction\n   * (e.g. a button row, which cannot contain an input).\n   */\n  presentation?: boolean;\n  switchRef?: React.RefObject<HTMLInputElement | null>;\n};\n\nexport const Switch = ({\n  className,\n  on,\n  onSwitchClick,\n  presentation,\n  switchRef,\n  ...props\n}: SwitchProps) => (\n  <div\n    aria-hidden={presentation ? true : undefined}\n    className={clsx('str-chat__form__switch-field__switch', {\n      'str-chat__form__switch-field__switch--on': on,\n    })}\n    onClick={presentation ? undefined : onSwitchClick}\n  >\n    {!presentation && (\n      <input\n        {...props}\n        className={clsx('str-chat__form__switch-field__input', className)}\n        ref={switchRef}\n        role='switch'\n        type='checkbox'\n      />\n    )}\n    <span className='str-chat__form__switch-field__switch-handle' />\n  </div>\n);\n\nexport type SwitchFieldLabelProps = ComponentProps<'label'> & {\n  /** Adds the `str-chat__form__switch-field__label--as-error` class to the root. */\n  asError?: boolean;\n  title?: string;\n  description?: string;\n};\n\nexport const SwitchFieldLabel = ({\n  asError,\n  children,\n  className,\n  description,\n  title,\n  ...props\n}: SwitchFieldLabelProps) => (\n  <label\n    className={clsx(\n      'str-chat__form__switch-field__label',\n      { 'str-chat__form__switch-field__label--as-error': asError },\n      className,\n    )}\n    {...props}\n  >\n    <div className='str-chat__form__switch-field__label__content'>\n      {title ? (\n        <>\n          <SwitchFieldTitle>{title}</SwitchFieldTitle>\n          {description != null && description !== '' && (\n            <SwitchFieldDescription>{description}</SwitchFieldDescription>\n          )}\n        </>\n      ) : (\n        children\n      )}\n    </div>\n  </label>\n);\n\nexport type SwitchFieldTitleProps = ComponentProps<'div'> & {\n  title?: string;\n};\n\nexport const SwitchFieldTitle = ({\n  children,\n  className,\n  title,\n  ...props\n}: SwitchFieldTitleProps) => (\n  <div\n    className={clsx('str-chat__form__switch-field__label__text', className)}\n    {...props}\n  >\n    {children ?? title}\n  </div>\n);\n\nexport type SwitchFieldDescriptionProps = ComponentProps<'div'> & {\n  description?: string;\n};\n\nexport const SwitchFieldDescription = ({\n  children,\n  className,\n  description,\n  ...props\n}: SwitchFieldDescriptionProps) => (\n  <div\n    className={clsx('str-chat__form__switch-field__label__description', className)}\n    {...props}\n  >\n    {children ?? description}\n  </div>\n);\n"],"mappings":";;;;;;;AA4BA,IAAa,eAAe,EAC1B,UACA,aACA,gBACA,OACA,GAAG,YACmB;CACtB,MAAM,EACJ,cAAc,WACd,mBAAmB,gBACnB,SACA,gBACA,UACA,IACA,UACA,WACA,GAAG,SACD;CACJ,MAAM,oBAAoB,YAAY;CACtC,MAAM,WAAW,MAAM,0BAA0B;CACjD,MAAM,gBAAgB,GAAG,SAAS;CAClC,MAAM,WAAW,OAAgC,IAAI;CAErD,MAAM,WAAW,WAAW,wBAAwB,CAAC,EAAE;CACvD,MAAM,EAAE,MAAM,sBAAsB;CAEpC,MAAM,CAAC,qBAAqB,0BAA0B,SAAS,QAAQ,cAAc,CAAC;CACtF,MAAM,eAAe,YAAY,KAAA;CACjC,MAAM,OAAO,eAAe,UAAU;CACtC,MAAM,aAAa,gBAAgB,aAAa,KAAA;CAIhD,MAAM,gBADe,eAAgC,QAAQ,IAAI,WAAW,KAAA,EAAA,EACzC,MAAM;CAGzC,MAAM,yBACJ,mBAAmB,CAAC,YAAa,QAAQ,gBAAgB,eAAgB,KAAA;CAI3E,MAAM,gCAAgC;EACpC,IAAI,OAAO,OAAO;EAClB,IAAI,WAAW,OAAO;EACtB,IAAI,wBAAwB;GAC1B,MAAM,UAAU,SAAS,eAAe,sBAAsB;GAI9D,SAHgB,SAAS,cACvB,4CACF,EAAA,EACiB,eAAe,SAAS,eAAe,GAAA,CAAI,KAAK,KAAK,KAAA;EACxE;CAEF;CAEA,MAAM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