{"version":3,"file":"Counter.mjs","sources":["../../../src/components/Counter/Counter.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport React from 'react';\n\nimport { assertEdsUsage } from '../../util/logging';\nimport Text from '../Text';\n\nimport styles from './Counter.module.css';\n\nexport type CounterProps = {\n  // Component API\n  /**\n   * CSS class names that can be appended to the component. Use this to position the counter\n   * within its parent; the counter owns its own color treatment.\n   */\n  className?: string;\n  // Design API\n  /**\n   * The current count (the numerator), e.g., the characters entered so far or the progress made.\n   */\n  count: number;\n  /**\n   * What the count is measured against (the denominator), e.g., the characters allowed. When\n   * `count` exceeds this, the count takes on the critical treatment.\n   */\n  total: number;\n  /**\n   * How the count is written out. `\"fraction\"` reads as `\"3 / 10\"`, for counts of discrete\n   * things. `\"percentage\"` reads as `\"30%\"`, rounded to a whole number, for a share of a whole;\n   * it requires `total` to be greater than zero.\n   *\n   * **Default is `\"fraction\"`**.\n   */\n  variant?: 'fraction' | 'percentage';\n} & Omit<React.HTMLAttributes<HTMLDivElement>, 'children'>;\n\n/**\n * An internal count of a current value against its total, written either as a fraction\n * (`\"3 / 10\"`) or as a percentage (`\"30%\"`).\n *\n * This is shared by the components that show a counting construction, so the construction and\n * its error treatment stay consistent: the fields that limit text length (`InputField`,\n * `TextareaField`) use the fraction variant, and `ProgressBar` uses the percentage variant. It\n * is deliberately not exported from the package: it carries no semantics of its own and is only\n * meaningful next to the control it counts.\n *\n * Example usage:\n *\n * ```\n * <Counter\n *   className={styles['input-field__character-counter']}\n *   count={fieldLength}\n *   total={maxLengthShown}\n * />\n * ```\n */\nexport const Counter = ({\n  className,\n  count,\n  total,\n  variant = 'fraction',\n  ...other\n}: CounterProps) => {\n  const componentClassName = clsx(styles['counter'], className);\n\n  // The count is over its limit. In the fraction variant the total stays in the default\n  // treatment, since it is the limit being violated rather than the thing in error.\n  const isOverTotal = count > total;\n\n  const countClassName = clsx(\n    styles['counter__count'],\n    isOverTotal && styles['counter__count--invalid'],\n  );\n\n  // Whole percentages only. A total that isn't positive has no percentage to report, so it reads\n  // as zero rather than as a non-finite value.\n  const percentage = total > 0 ? Math.round((count / total) * 100) : 0;\n\n  assertEdsUsage(\n    [count < 0, total < 0],\n    `Counter values must not be negative (received ${count} / ${total})`,\n  );\n\n  // Dividing by a total of zero (or less) yields no usable percentage, so the fallback above is\n  // covering for a caller that cannot get the answer it asked for\n  assertEdsUsage(\n    [variant === 'percentage' && total <= 0],\n    `Counter cannot report ${count} as a percentage of ${total}; the percentage variant requires a total greater than zero`,\n    'error',\n  );\n\n  return (\n    <Text as=\"div\" className={componentClassName} preset=\"body-sm\" {...other}>\n      {variant === 'fraction' && (\n        <>\n          <span className={countClassName}>{count}</span>\n          {' / '}\n          {total}\n        </>\n      )}\n      {variant === 'percentage' && (\n        <span className={countClassName}>{percentage}%</span>\n      )}\n    </Text>\n  );\n};\n\nCounter.displayName = 'Counter';\n"],"names":["React"],"mappings":";;;;;;AAmCA;;;;;;;;;;;;;;;;;;;AAmBG;MACU,OAAO,GAAG,CAAC,EACtB,SAAS,EACT,KAAK,EACL,KAAK,EACL,OAAO,GAAG,UAAU,EACpB,GAAG,KAAK,EACK,KAAI;IACjB,MAAM,kBAAkB,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC;;;AAI7D,IAAA,MAAM,WAAW,GAAG,KAAK,GAAG,KAAK;AAEjC,IAAA,MAAM,cAAc,GAAG,IAAI,CACzB,MAAM,CAAC,gBAAgB,CAAC,EACxB,WAAW,IAAI,MAAM,CAAC,yBAAyB,CAAC,CACjD;;;IAID,MAAM,UAAU,GAAG,KAAK,GAAG,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,GAAG,KAAK,IAAI,GAAG,CAAC,GAAG,CAAC;AAEpE,IAAA,cAAc,CACZ,CAAC,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,CAAC,CAAC,EACtB,CAAA,8CAAA,EAAiD,KAAK,MAAM,KAAK,CAAA,CAAA,CAAG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