{"version":3,"file":"Icon.mjs","sources":["../../../src/components/Icon/Icon.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport type { ReactNode, CSSProperties } from 'react';\nimport React from 'react';\nimport icons, { type IconName } from '../../icons/spritemap';\nimport { assertEdsUsage } from '../../util/logging';\nimport styles from './Icon.module.css';\n\nexport type { IconName } from '../../icons/spritemap';\n\ntype IconPropsBase = {\n  /**\n   * CSS class names that can be appended to the component.\n   */\n  className?: string;\n  /**\n   * Either a fragment of svg elements (g, path, circle, etc.), or a single\n   * svg element. Useful for creating specific icon components.\n   *\n   * @example\n   * function CircleIcon(props: IconProps) {\n   *   return (\n   *     <Icon {...props}>\n   *       <circle cx=\"50\" cy=\"50\" r=\"50\" />\n   *     </Icon>\n   *   )\n   * }\n   */\n  children?: ReactNode;\n  /**\n   * The SVG Color, expects a valid css color (hex, rgb, css variable, etc.).\n   *\n   */\n  color?: string;\n  /**\n   * ID used so the svg can read the title of the SVG icon to the user when accessibility is needed\n   */\n  id?: string;\n  /**\n   * Name of icon to reference in icon sprite\n   */\n  name?: IconName;\n  /**\n   * Width/Height string (px, rem, em, vh, etc.)\n   * Generally prefer using \"em\" as it sizes to its parent container.\n   */\n  size?: string;\n  /**\n   * viewBox for the svg, used when the svg information is passed via children.\n   * To match included icons, recommend view box of `\"0 0 24 24\"`\n   */\n  viewBox?: string;\n};\n\ntype InformativeIconProps = IconPropsBase & {\n  /**\n   * The role of the icon.\n   *\n   * Use \"informative\" when the icon **_does_** provide additional meaning to other text on the\n   * page. You'll be required to pass in a title to label the icon.\n   */\n  purpose: 'informative';\n  title: string;\n};\n\ntype DecorativeIconProps = IconPropsBase & {\n  /**\n   * The role of the icon.\n   *\n   * Use \"decorative\" when the icon **_does not_** provide any additional context or meaning to\n   * associated text. Basically the icon is for show and people don't need it to understand what's\n   * on the page.\n   */\n  purpose: 'decorative';\n};\n\nexport type IconProps = DecorativeIconProps | InformativeIconProps;\n\ntype SvgStyle = CSSProperties & {\n  '--icon-size'?: string;\n};\n\n/**\n * ## Usage\n *\n * Small illustrations to highlight and emphasize text and screen elements with visual detail.\n * Icons align with adjacent text, and will inherit the text's color. Icons can also be embedded\n * in other components.\n *\n * Icons come from a spritemap in `src/icons`; pick one with `name`. To render your own SVG\n * instead, pass the path data as `children` along with a `viewBox`.\n *\n * | Type/Use | Description | Example |\n * |----------|-------------|---------|\n * | Informative | `purpose=\"informative\"` renders `role=\"img\"` and requires a `title`, so the icon is announced. | A status icon that is the only thing conveying the status. |\n * | Decorative | `purpose=\"decorative\"` renders `aria-hidden`, so assistive tech skips the icon entirely. | A plus icon next to the word \"Add\". |\n *\n * `color` defaults to `currentColor` so the icon picks up the surrounding text color. Prefer `em`\n * for `size` so the icon scales with the text it sits beside.\n *\n * ## Content & Accessibility\n *\n * ### Do's\n *\n * * Use `Icon` to add clarity and character to any buttons, especially those which are closely associated with user actions (e.g., an \"add\" button may use a plus icon).\n * * Use decorative icons when paired with adjacent, descriptive text.\n *\n * ### Don'ts\n *\n * * Never use decorative `Icon` elements in isolation. Icons must have an associated accessible purpose, so when the icon is the only thing carrying the meaning, use `purpose=\"informative\"` and give it a `title`.\n *\n * ## Resources\n *\n * * https://fonts.google.com/icons?icon.set=Material+Icons&icon.style=Rounded\n */\nexport const Icon = (props: IconProps) => {\n  const {\n    children,\n    className,\n    color = 'currentColor',\n    name,\n    id,\n    purpose,\n    size,\n    viewBox,\n  } = props;\n\n  const componentClassName = clsx(styles['icon'], className);\n  const style: SvgStyle = {\n    '--icon-size': size,\n  };\n\n  // A name off the spritemap used to throw on the lookup below, taking the page with it.\n  // The type system cannot prevent one: every icon slot takes `IconOrContent`, which\n  // collapses to `ReactNode` and so admits any string, and `IconProvider` raises the stakes\n  // by letting one bad entry reach every component drawing that role at once. Warn and draw\n  // nothing instead, so a typo costs an icon rather than the render.\n  // An own-property check, not `in`: the spritemap inherits from `Object.prototype`, so\n  // `'toString' in icons` is true and the lookups below would read a function's missing\n  // `viewBox` and `content`, rendering an empty `<svg>` instead of warning.\n  //\n  // Spelled the long way rather than with `Object.hasOwn`, which is ES2022. This package\n  // compiles to ES2018 and ships no polyfill, and `hasOwn` only type-checks here because\n  // `@types/node` declares it, so it would pass the build and then throw in any environment\n  // the target says is supported.\n  //\n  // `name === undefined` rather than `!name`: an absent name is the custom-SVG case, which\n  // renders `children` against the given `viewBox`, but `''` is just an invalid name and\n  // should be reported like any other.\n  const isKnownName =\n    name === undefined || Object.prototype.hasOwnProperty.call(icons, name);\n\n  assertEdsUsage(\n    [!isKnownName],\n    `Icon: \"${name}\" is not an EDS icon. Nothing is rendered in its place.`,\n  );\n\n  if (!isKnownName) {\n    return null;\n  }\n\n  const svgCommonProps = {\n    className: componentClassName,\n    fill: color,\n    height: size,\n    /**\n     * height/width html properties are overriden by the defaults applied in CSS module\n     */\n    style,\n    width: size,\n    xmlns: 'http://www.w3.org/2000/svg',\n    viewBox: name ? icons[name].viewBox : viewBox,\n  };\n\n  // allow passing custom SVGs to render, otherwise\n  // load from the spritemap of EDS icons\n  const computedSvg = name ? icons[name].content : children;\n\n  if (purpose === 'informative') {\n    return (\n      <svg {...svgCommonProps} role=\"img\">\n        <title id={id}>{props.title}</title>\n        {computedSvg}\n      </svg>\n    );\n  } else {\n    return (\n      <svg {...svgCommonProps} aria-hidden>\n        {computedSvg}\n      </svg>\n    );\n  }\n};\n\nIcon.displayName = 'Icon';\n"],"names":["React"],"mappings":";;;;;;AAiFA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCG;AACI,MAAM,IAAI,GAAG,CAAC,KAAgB,KAAI;IACvC,MAAM,EACJ,QAAQ,EACR,SAAS,EACT,KAAK,GAAG,cAAc,EACtB,IAAI,EACJ,EAAE,EACF,OAAO,EACP,IAAI,EACJ,OAAO,GACR,GAAG,KAAK;IAET,MAAM,kBAAkB,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC;AAC1D,IAAA,MAAM,KAAK,GAAa;AACtB,QAAA,aAAa,EA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