{"version":3,"file":"IconSlot.mjs","sources":["../../../src/components/Icon/IconSlot.tsx"],"sourcesContent":["import React, { Fragment } from 'react';\nimport type { ReactNode } from 'react';\nimport { Icon } from './Icon';\nimport type { IconName } from './Icon';\nimport icons from '../../icons/spritemap';\nimport type { IconOrContent } from '../../util/utility-types';\n\n/**\n * Whether a slot value will render anything.\n *\n * These slots take any `ReactNode`, so a plain truthiness check is wrong twice over: it\n * treats `0` as absent, and `{0 && <El />}` leaks a stray \"0\" into the markup. Only the\n * values React itself renders as nothing count as empty here.\n *\n * An array is judged by what is in it, because that is what React does with one. `[]` and\n * `[null, false]` render nothing, so they are empty; `[0]` renders \"0\", so it is not. This\n * matters for the arrays a caller does not write on purpose — an `items.map(...)` over an\n * empty list, or a fragment's children arriving together — where counting the array itself\n * as content reserves a wrapper and lays out space around nothing. Recursive, because React\n * flattens nested arrays before rendering them.\n *\n * Arrays only, though React renders any iterable. Inspecting one means iterating it, which\n * exhausts a generator and leaves nothing for the caller to render afterwards — losing valid\n * content, which is worse than the alternative this gives up: a non-array iterable counts as\n * content even when it would yield none, so an empty `Set` reserves the space an icon would\n * have taken. Pass an array if the collection might be empty.\n *\n * A fragment is judged by its children too, since it contributes no element of its own:\n * `<></>` renders nothing at all, so counting it as content would leave an icon-only control\n * blank but still clickable. Every other element counts, including one that happens to\n * render nothing — `<span>{null}</span>` still produces a node, and what a component returns\n * cannot be known without rendering it.\n */\nexport function hasSlotContent(content: IconOrContent): boolean {\n  if (Array.isArray(content)) {\n    return content.some((item) => hasSlotContent(item));\n  }\n\n  if (React.isValidElement(content) && content.type === Fragment) {\n    return hasSlotContent((content.props as { children?: ReactNode }).children);\n  }\n\n  return (\n    content !== null &&\n    content !== undefined &&\n    typeof content !== 'boolean' &&\n    content !== ''\n  );\n}\n\n/**\n * Whether a slot will actually put something on screen.\n *\n * Stricter than `hasSlotContent`, which only asks whether a value is renderable in\n * principle. A string that is not an EDS icon name passes that and then draws nothing,\n * because `Icon` reports it and renders `null`. Use this wherever layout depends on the icon\n * being visible — reserving space for an icon that never arrives leaves a gap.\n *\n * `hasSlotContent` stays the right question for \"did the consumer give me something\", which\n * is what the content slots ask when deciding precedence.\n */\nexport function willRenderSlotContent(content: IconOrContent) {\n  if (!hasSlotContent(content)) {\n    return false;\n  }\n\n  return typeof content === 'string'\n    ? Object.prototype.hasOwnProperty.call(icons, content)\n    : true;\n}\n\ntype IconSlotPropsBase = {\n  /**\n   * Element used to wrap custom content so it can carry `className`.\n   *\n   * Defaults to `span`, which is valid wherever the slot itself is, including inside a\n   * `button`. Use `div` when the slot sits in a flow-content container, so consumers\n   * passing block-level content do not produce a `span` wrapping a `div`.\n   */\n  as?: 'div' | 'span';\n  /**\n   * CSS class names applied to the rendered icon, or to the wrapper around custom\n   * content so both branches sit the same way in the layout.\n   */\n  className?: string;\n  /**\n   * The slot's value. A string is treated as an EDS icon name; anything else renders\n   * as-is.\n   */\n  content: IconOrContent;\n  /**\n   * Width/height passed through to `Icon` when rendering an icon name.\n   */\n  size?: string;\n};\n\nexport type IconSlotProps = IconSlotPropsBase &\n  (\n    | {\n        /**\n         * Mirrors `Icon`'s `purpose`, and applies only when rendering an icon name.\n         * Custom content carries its own accessible treatment.\n         */\n        purpose?: 'decorative';\n        title?: never;\n      }\n    | {\n        purpose: 'informative';\n        title: string;\n      }\n  );\n\n/**\n * Renders a leading/trailing slot that accepts either an EDS icon name or arbitrary\n * content.\n *\n * Components that used to take an `IconName` now take `IconOrContent`, so they need to\n * decide at runtime which of the two they were handed. A string is the only thing a\n * consumer can pass that is ambiguous, and by convention it means an icon name, so it\n * renders through `Icon` with that component's own sizing. Everything else is content\n * and renders untouched.\n *\n * `purpose` and `title` describe the icon branch only. When a consumer supplies their\n * own content, they own its accessible treatment.\n *\n * Only render one that has something to draw. Returning `null` for a value that renders\n * nothing reads as harmless and is not: everything around the slot stays as it was, so a\n * row lays out space for an icon that never arrives, and an icon-only control comes out\n * blank while staying clickable. Throwing puts that in front of whoever wrote the call,\n * from render, where the nearest error boundary catches it like any other render error.\n *\n * The check belongs to the caller because only the caller knows what to do instead — drop\n * the wrapper around it, fall back to an avatar, leave the row as it is. So ask\n * `hasSlotContent(content)` and leave the slot out when it says no.\n *\n * Not exported from the package. Consumers use the slot props on each component.\n */\nexport const IconSlot = (props: IconSlotProps) => {\n  const { as: Wrapper = 'span', className, content, size } = props;\n\n  if (!hasSlotContent(content)) {\n    throw new Error(\n      \"IconSlot: rendered with content that draws nothing, which leaves the layout around it holding space for an icon that never arrives. Whether there is something to draw is the caller's to decide: guard the slot with `hasSlotContent(content)` and leave it out when there is none.\",\n    );\n  }\n\n  if (typeof content === 'string') {\n    // Safe because a string in this slot is an icon name by convention. The union\n    // collapses to `ReactNode`, so TypeScript cannot narrow to `IconName` on its own.\n    const name = content as IconName;\n\n    return props.purpose === 'informative' ? (\n      <Icon\n        className={className}\n        name={name}\n        purpose=\"informative\"\n        size={size}\n        title={props.title}\n      />\n    ) : (\n      <Icon\n        className={className}\n        name={name}\n        purpose=\"decorative\"\n        size={size}\n      />\n    );\n  }\n\n  return className ? (\n    <Wrapper className={className}>{content}</Wrapper>\n  ) : (\n    <>{content}</>\n  );\n};\n\nIconSlot.displayName = 'IconSlot';\n"],"names":["React"],"mappings":";;;;AAOA;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;AACG,SAAU,cAAc,CAAC,OAAsB,EAAA;AACnD,IAAA,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;AAC1B,QAAA,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,KAAK,cAAc,CAAC,IAAI,CAAC,CAAC;IACrD;AAEA,IAAA,IAAIA,cAAK,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,EAAE;QAC9D,OAAO,cAAc,CAAE,OAAO,CAAC,KAAkC,CAAC,QAAQ,CAAC;IAC7E;IAEA,QACE,OAAO,KAAK,IAAI;AAChB,QAAA,OAAO,KAA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