{"version":3,"file":"use-inline-field-label.mjs","names":[],"sources":["../../../../src/_common/form-control/use-inline-field-label.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 classNames from 'classnames';\nimport { useEffect, useMemo } from 'react';\n\nimport { needleWarningMessage } from '../utils';\n\ntype UseInlineFieldLabelOptions = {\n  /** Component name used in the missing-label console warning. */\n  componentName: string;\n  /** The label rendered next to the control, inside its wrapping `<label>`. */\n  label: React.ReactNode;\n  // TODO v5: only needed while naming a selection control is split across\n  // `label` and `ariaLabel`. Whenever the label alone names the control, as it\n  // does on Combobox, the warning below has nothing left to check.\n  /** The aria label naming the control when the label is not a string. */\n  ariaLabel: string | undefined;\n  /** Dims the label and the control it belongs to. */\n  isDisabled?: boolean;\n  /** Lets the field take the full available width of its container. */\n  isFluid?: boolean;\n  /**\n   * Truncates an overflowing string label to one line with an ellipsis (the\n   * `title` set on the span keeps the full text reachable). Radio behavior\n   * today; a candidate to unify across all selection controls in v5.\n   */\n  isTruncated?: boolean;\n};\n\ntype UseInlineFieldLabelResult = {\n  /** The label, rendered next to the control. */\n  labelContent: React.ReactNode;\n  /**\n   * Classes for the `<label>` that wraps the control and the label, which the\n   * adopting component renders. Owned here so that the shared classes stay an\n   * implementation detail of this module.\n   */\n  fieldClassName: string;\n};\n\n/**\n * The inline label of a selection control (Checkbox, Radio, Switch): the\n * control and its label text sit side by side inside one wrapping `<label>`.\n * A string label is wrapped in a titled span so a truncated label stays\n * readable through its tooltip; a custom node is rendered as given, which is\n * also why it needs `ariaLabel` to name the control. Warns when neither a\n * label nor an aria label is provided.\n *\n * Returns the classes of the wrapping label along with its content, so that\n * an adopting component never names a shared class itself. Typography and\n * color sit on that wrapper rather than on the span here, so that a custom\n * node inherits them too.\n *\n * TODO v5: wrap a custom node as well, once the DOM of a custom label can\n * change. This would own all label styling, and the difference between the\n * full-strength inline label and the weaker label above a control could then\n * become a prop here instead of living in two stylesheet classes.\n */\nexport const useInlineFieldLabel = ({\n  componentName,\n  label,\n  ariaLabel,\n  isDisabled = false,\n  isFluid = false,\n  isTruncated = false,\n}: UseInlineFieldLabelOptions): UseInlineFieldLabelResult => {\n  useEffect(() => {\n    if (Boolean(label) === false && Boolean(ariaLabel) === false) {\n      needleWarningMessage(\n        `${componentName} needs a valid label or ariaLabel: https://dequeuniversity.com/rules/axe/4.2/label?application=axeAPI`,\n      );\n    }\n  }, [ariaLabel, componentName, label]);\n\n  const labelContent = useMemo(() => {\n    if (Boolean(label) === false) {\n      return null;\n    }\n\n    if (typeof label === 'string') {\n      return (\n        <span\n          title={label}\n          className={classNames('ndl-form-inline-label-text', {\n            'ndl-disabled': isDisabled,\n            'ndl-truncated': isTruncated,\n          })}\n        >\n          {label}\n        </span>\n      );\n    }\n\n    return label;\n  }, [label, isDisabled, isTruncated]);\n\n  return {\n    fieldClassName: classNames('ndl-form-inline-field', {\n      'ndl-disabled': isDisabled,\n      'ndl-fluid': isFluid,\n    }),\n    labelContent,\n  };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6EA,MAAa,uBAAuB,EAClC,eACA,OACA,WACA,aAAa,OACb,UAAU,OACV,cAAc,YAC6C;CAC3D,gBAAgB;EACd,IAAI,QAAQ,KAAK,MAAM,SAAS,QAAQ,SAAS,MAAM,OACrD,qBACE,GAAG,cAAc,sGACnB;CAEJ,GAAG;EAAC;EAAW;EAAe;CAAK,CAAC;CAEpC,MAAM,eAAe,cAAc;EACjC,IAAI,QAAQ,KAAK,MAAM,OACrB,OAAO;EAGT,IAAI,OAAO,UAAU,UACnB,OACE,oBAAC,QAAD;GACE,OAAO;GACP,WAAW,WAAW,8BAA8B;IAClD,gBAAgB;IAChB,iBAAiB;GACnB,CAAC;GAEA,UAAA;EACG,CAAA;EAIV,OAAO;CACT,GAAG;EAAC;EAAO;EAAY;CAAW,CAAC;CAEnC,OAAO;EACL,gBAAgB,WAAW,yBAAyB;GAClD,gBAAgB;GAChB,aAAa;EACf,CAAC;EACD;CACF;AACF"}