{"version":3,"file":"Switch.mjs","names":[],"sources":["../../../src/switch/Switch.tsx"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\n\nimport { useMergeRefs } from '@floating-ui/react';\nimport classNames from 'classnames';\nimport { useEffect, useRef } from 'react';\n\nimport { useInlineFieldLabel } from '../_common/form-control';\nimport { type CommonProps, type HtmlAttributes } from '../_common/types';\nimport { useSemiControlledInput } from '../_common/use-semi-controlled-input';\n\ntype SwitchProps = {\n  /** Whether the switch is checked */\n  isChecked?: boolean;\n  /** Checks the input by default in uncontrolled mode */\n  isDefaultChecked?: boolean; // isDefaultChecked sounds weird to me?\n  /** Whether the switch is indeterminate */\n  isIndeterminate?: boolean;\n  /** Whether the switch is disabled */\n  isDisabled?: boolean;\n  /** Whether the switch & label should take the full available width of its container */\n  isFluid?: boolean;\n  /** The value of the switch */\n  value?: HtmlAttributes<'input'>['value']; // should this be required?\n  /** Whether the label should be displayed before the switch */\n  hasLeadingLabel?: boolean;\n  /** The callback function triggered when the switch value changes */\n  onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;\n  /** The callback function triggered when the switch is clicked */\n  onClick?: (event: React.MouseEvent<HTMLInputElement>) => void;\n} & (\n  | {\n      /** The aria label of the switch. Required if the label is not a string. */\n      ariaLabel: string;\n      // TODO v5: inconsistent naming. Naming the switch takes a union of\n      // `label` and `ariaLabel`, so the accessible name comes from a different\n      // prop depending on what the label renders. Combobox instead requires the\n      // label and clips it with `hasVisuallyHiddenLabel`, and TextInput reaches\n      // for htmlAttributes['aria-label'].\n      /** The label of the switch. If not a string, supply an aria label for screen reader support. */\n      label?: undefined;\n    }\n  | {\n      ariaLabel?: string;\n      label: React.ReactNode;\n    }\n);\n\nexport const Switch = ({\n  isChecked,\n  isDefaultChecked,\n  isIndeterminate = false,\n  isFluid = false,\n  isDisabled = false,\n  hasLeadingLabel,\n  ariaLabel,\n  onChange,\n  onClick,\n  value,\n  label,\n  className,\n  style,\n  htmlAttributes,\n  ref,\n  ...restProps\n}: CommonProps<'input', SwitchProps>) => {\n  const internalRef = useRef<HTMLInputElement>(null);\n  const mergedRefs = useMergeRefs([internalRef, ref]);\n\n  const [isCheckedState, internalOnChange] = useSemiControlledInput({\n    inputType: 'switch',\n    isControlled: isChecked !== undefined,\n    onChange,\n    state: isChecked ?? isDefaultChecked ?? false,\n  });\n\n  const switchClasses = classNames('ndl-switch', className, {\n    'ndl-disabled': isDisabled,\n    'ndl-indeterminate': isIndeterminate,\n  });\n\n  useEffect(() => {\n    if (internalRef.current) {\n      internalRef.current.indeterminate = isIndeterminate;\n    }\n  }, [isIndeterminate]);\n\n  const { labelContent, fieldClassName } = useInlineFieldLabel({\n    ariaLabel,\n    componentName: 'Switch',\n    isDisabled,\n    isFluid,\n    label,\n  });\n\n  return (\n    <label className={fieldClassName}>\n      {hasLeadingLabel === true && labelContent}\n      <input\n        ref={mergedRefs}\n        type=\"checkbox\"\n        role=\"switch\"\n        aria-label={ariaLabel}\n        disabled={isDisabled}\n        checked={isCheckedState}\n        onClick={onClick}\n        onChange={internalOnChange}\n        value={value}\n        style={style}\n        className={switchClasses}\n        {...restProps}\n        {...htmlAttributes}\n      />\n      {Boolean(hasLeadingLabel) === false && labelContent}\n    </label>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmEE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;AAfF,MAAa,UAAU,SAiBkB;;CAjBlB,IAAA,EACrB,WACA,kBACA,kBAAkB,OAClB,UAAU,OACV,aAAa,OACb,iBACA,WACA,UACA,SACA,OACA,OACA,WACA,OACA,gBACA,QAAA,MACG,YAAA,yBAAA,MAAA,SAAA;CAEH,MAAM,cAAc,OAAyB,IAAI;CACjD,MAAM,aAAa,aAAa,CAAC,aAAa,GAAG,CAAC;CAElD,MAAM,CAAC,gBAAgB,oBAAoB,uBAAuB;EAChE,WAAW;EACX,cAAc,cAAc,KAAA;EAC5B;EACA,QAAA,QAAO,cAAA,QAAA,cAAA,KAAA,IAAA,YAAa,sBAAA,QAAA,UAAA,KAAA,IAAA,QAAoB;CAC1C,CAAC;CAED,MAAM,gBAAgB,WAAW,cAAc,WAAW;EACxD,gBAAgB;EAChB,qBAAqB;CACvB,CAAC;CAED,gBAAgB;EACd,IAAI,YAAY,SACd,YAAY,QAAQ,gBAAgB;CAExC,GAAG,CAAC,eAAe,CAAC;CAEpB,MAAM,EAAE,cAAc,mBAAmB,oBAAoB;EAC3D;EACA,eAAe;EACf;EACA;EACA;CACF,CAAC;CAED,OACE,qBAAC,SAAD;EAAO,WAAW;EAAlB,UAAA;GACG,oBAAoB,QAAQ;GAC7B,oBAAC,SAAA,eAAA,eAAA;IACC,KAAK;IACL,MAAK;IACL,MAAK;IACL,cAAY;IACZ,UAAU;IACV,SAAS;IACA;IACT,UAAU;IACH;IACA;IACP,WAAW;GACP,GAAA,SAAA,GACA,cACL,CAAA;GACA,QAAQ,eAAe,MAAM,SAAS;EAClC;;AAEX"}