{"version":3,"file":"index.cjs","sources":["../lib/values.js","../lib/validate.js","../lib/content-switcher.js","../lib/switch.js"],"sourcesContent":["/**\n * Size of SVG for both qualifier and icon\n */\nexport const ICON_SIZES = {\n  s: { width: \"16\", height: \"16\" },\n  m: { width: \"18\", height: \"18\" },\n}\n\n/**\n * Size values\n */\nexport const SIZES = {\n  s: \"s\",\n  m: \"m\",\n}\n\n/**\n * Default size value\n */\nexport const DEFAULT_SIZE = \"m\"\n","import { SIZES, DEFAULT_SIZE } from \"./values.js\"\n\n/**\n * checks if the current switch is active\n *\n * @param {Object} param0\n * @param {number} param0.active index of the active `Switch`\n * @param {number} param0.value index of the current `Switch`\n * @returns {boolean}\n */\nexport const isSwitchActive = ({ active, value }) => active === value\n\n/**\n * checks if all the nodes in the children array are of type `Switch`\n *\n * @param {Array} children child nodes\n * @returns {boolean}\n */\nexport const areAllSwitch = (children) =>\n  children.every(({ type: { displayName } }) => displayName === \"Switch\")\n\n/**\n * checks if all the `Switch` nodes passed have unique `value` prop\n *\n * @param {Array} children child nodes\n * @param {number} count number of child nodes\n * @returns {boolean}\n */\nexport const uniqueValues = (children, count) => {\n  const { size } = new Set(children.map(({ props: { value } }) => value))\n  return size === count\n}\n\n/**\n * Resolves size value\n *\n * Size is mapped to its respective class. Returns the size based on the prop value.\n * @param {string} size value of size prop\n * @param {string} fallback fallback value of size\n * @returns {string}\n */\nexport const resolvedSize = (size, { fallback = DEFAULT_SIZE } = {}) => {\n  if (!size) {\n    return { invalid: false, size: fallback }\n  }\n\n  if (!SIZES[size]) {\n    return { invalid: true, size: fallback }\n  }\n\n  return { invalid: false, size }\n}\n","import React, { Children } from \"react\"\nimport PropTypes from \"prop-types\"\n\nimport { areAllSwitch, uniqueValues } from \"./validate.js\"\nimport styles from \"./content-switcher.module.css\"\n\nconst validateSwitch = (children) => {\n  const count = Children.count(children)\n  const childrenArr = Children.toArray(children)\n\n  if (!areAllSwitch(childrenArr)) {\n    console.warn(\n      \"ContentSwitcher: All children in <ContentSwitcher> should be a <Switch>\"\n    )\n    return false\n  }\n\n  if (!uniqueValues(childrenArr, count)) {\n    console.warn(\n      \"ContentSwitcher: `value` should be unique among all <Switch> elements\"\n    )\n    return false\n  }\n\n  return true\n}\n\nexport const ContentSwitcher = ({ children, ...props }) => {\n  // filtering out style and className\n  /* eslint-disable-next-line react/prop-types */\n  const { style, className, ...rest } = props\n\n  if (!validateSwitch(children)) {\n    return null\n  }\n  return (\n    <div {...rest} className={styles.Contentswitcher} role=\"tablist\">\n      {children}\n    </div>\n  )\n}\n\nContentSwitcher.displayName = \"ContentSwitcher\"\n\nContentSwitcher.propTypes = {\n  /**\n   * Switch components.\n   */\n  children: PropTypes.node.isRequired,\n}\n","import React from \"react\"\nimport PropTypes from \"prop-types\"\nimport cn from \"classnames\"\n\nimport { SvgNormalizer } from \"@asphalt-react/svg-normalizer\"\nimport { svgUtil, propsUtil } from \"@asphalt-react/helper\"\n\nimport { ICON_SIZES } from \"./values.js\"\nimport { resolvedSize } from \"./validate.js\"\n\nimport styles from \"./content-switcher.module.css\"\n\nconst Base = ({ children, ...props }) => {\n  return (\n    <button {...props} role=\"tab\" type=\"button\">\n      {children}\n    </button>\n  )\n}\n\nconst Icon = ({ children, size, end, ...props }) => {\n  if (!children) {\n    return null\n  }\n\n  if (!svgUtil.isSVG(children)) {\n    console.warn(\n      \"ContentSwitcher: Switch only supports SVG for icons in caption\"\n    )\n    return null\n  }\n\n  return (\n    <span\n      data-testid=\"icon-wrapper\"\n      className={cn(styles.qualifier, { [styles.qualifierEnd]: end })}\n      {...props}\n    >\n      <SvgNormalizer dimension={size}>{children}</SvgNormalizer>\n    </span>\n  )\n}\n\nexport const Switch = ({\n  children,\n  value,\n  active,\n  onAction,\n  size: rawSize = \"m\",\n  icon = false,\n  qualifier,\n  qualifierEnd = false,\n  accent = false,\n  ...props\n}) => {\n  // filtering out style and className\n  /* eslint-disable-next-line react/prop-types */\n  const { style, className, onClick: userOnClick, ...delegated } = props\n\n  const { invalid, size } = resolvedSize(rawSize)\n\n  if (invalid) {\n    console.warn(`Switch: Invalid size prop value. Defaulting to ${size}`)\n  }\n\n  const switchClass = cn(styles.switch, {\n    [styles.selected]: active,\n    [styles.selectedAccent]: active && accent,\n  })\n\n  const clickHandler = (event) => {\n    userOnClick?.(event)\n    onAction?.(value, { event })\n  }\n\n  const baseProps = {\n    ...delegated,\n    tabIndex: active ? \"-1\" : \"0\",\n    \"aria-selected\": active,\n    onClick: clickHandler,\n  }\n\n  if (icon) {\n    const iconSizeClass = propsUtil.concatClasses(`icon`, size)\n\n    return (\n      <Base {...baseProps} className={cn(switchClass, styles[iconSizeClass])}>\n        <Icon size={ICON_SIZES[size]}>{children}</Icon>\n      </Base>\n    )\n  }\n\n  return (\n    <Base {...baseProps} className={cn(switchClass, styles[size])}>\n      <Icon role=\"none\" end={qualifierEnd} size={ICON_SIZES[size]}>\n        {qualifier}\n      </Icon>\n      <span>{children}</span>\n    </Base>\n  )\n}\n\nSwitch.displayName = \"Switch\"\n\nSwitch.propTypes = {\n  /**\n   * Switch caption.\n   */\n  children: PropTypes.node.isRequired,\n  /**\n   * Index of the Switch.\n   */\n  value: PropTypes.number.isRequired,\n  /**\n   * Adds styles to show the Switch is active.\n   */\n  active: PropTypes.bool,\n  /**\n   * Callback to handle Switch selection.\n   *\n   * The function accepts 2 arguments:\n   *\n   * - value: `value` prop of the Switch.\n   * - options: object containing the React synthetic event.\n   *\n   * ```js\n   * onAction(value, { event }) {\n   *  console.log(value)\n   * }\n   * ```\n   */\n  onAction: PropTypes.func,\n  /**\n   * Size of the Switches. Accepts one of \"s\" or \"m\" for small & medium.\n   */\n  size: PropTypes.oneOf([\"s\", \"m\"]),\n  /**\n   * Renders icon as caption.\n   */\n  icon: PropTypes.bool,\n  /**\n   * Icons to add more context to the textual caption.\n   *\n   * Accepts SVG or SVG wrapped React component. Checkout `@asphalt-react/iconpack` for SVG wrapped React components.\n   *\n   * > ⚠️ Do not use `qualifier` to render a Switch with icon as caption; use `icon` prop instead.\n   */\n  qualifier: PropTypes.element,\n  /**\n   * Appends qualifier to the text in caption. Switch prepends the qualifier by default.\n   */\n  qualifierEnd: PropTypes.bool,\n  /**\n   * Applies accent intent when Switch is active.\n   */\n  accent: PropTypes.bool,\n}\n\nSwitch.defaultProps = {\n  size: \"m\",\n  icon: false,\n  qualifierEnd: false,\n  accent: false,\n}\n"],"names":["ICON_SIZES","s","width","height","m","SIZES","DEFAULT_SIZE","areAllSwitch","children","every","type","displayName","uniqueValues","count","size","Set","map","props","value","resolvedSize","fallback","invalid","validateSwitch","Children","childrenArr","toArray","console","warn","ContentSwitcher","style","className","rest","React","createElement","_extends","styles","Contentswitcher","role","propTypes","PropTypes","node","isRequired","Base","Icon","end","svgUtil","isSVG","cn","qualifier","qualifierEnd","SvgNormalizer","dimension","Switch","active","onAction","rawSize","icon","accent","onClick","userOnClick","delegated","switchClass","switch","selected","selectedAccent","clickHandler","event","baseProps","tabIndex","iconSizeClass","propsUtil","concatClasses","number","bool","func","oneOf","element","defaultProps"],"mappings":";;;;;;;;;;;;;;;;;;;AAGO,MAAMA,UAAU,CAAG,CACxBC,CAAC,CAAE,CAAEC,KAAK,CAAE,IAAI,CAAEC,MAAM,CAAE,IAAK,CAAC,CAChCC,CAAC,CAAE,CAAEF,KAAK,CAAE,IAAI,CAAEC,MAAM,CAAE,IAAK,CACjC,CAAC,OAKYE,KAAK,CAAG,CACnBJ,CAAC,CAAE,GAAG,CACNG,CAAC,CAAE,GACL,CAAC,CAKM,MAAME,YAAY,CAAG,GAAG;;ACDxB,MAAMC,YAAY,CAAIC,QAAQ,EACnCA,QAAQ,CAACC,KAAK,CAAC,CAAC,CAAEC,IAAI,CAAE,CAAEC,WAAY,CAAE,CAAC,GAAKA,WAAW,GAAK,QAAQ,CAAC,OAS5DC,YAAY,CAAGA,CAACJ,QAAQ,CAAEK,KAAK,GAAK,CAC/C,KAAM,CAAEC,IAAK,CAAC,CAAG,IAAIC,GAAG,CAACP,QAAQ,CAACQ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