{"version":3,"file":"Heading.mjs","sources":["../../../src/components/Heading/Heading.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport React, { forwardRef } from 'react';\nimport type {\n  ReactNode,\n  HTMLAttributes,\n  ForwardRefExoticComponent,\n  RefAttributes,\n} from 'react';\nimport type { AnyPreset, Preset } from '../../util/variant-types';\n\nimport styles from '../Text/Text.module.css';\n\nexport type HeadingElement = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';\n\ntype HeadingProps = HTMLAttributes<HTMLHeadingElement> & {\n  /**\n   * Which `h1`-`h6` tag renders. Pick it from the document structure, by the level\n   * the page outline calls for, not by the size you want. Each level brings its own\n   * default preset, which `preset` can override.\n   *\n   * **Default is `\"h1\"`**.\n   */\n  as?: HeadingElement;\n  /**\n   * The contents of the header tag, usually just text, but can include other content.\n   */\n  children: ReactNode;\n  /**\n   * Additional classnames passed in for styling.\n   */\n  className?: string;\n  /**\n   * The specified tab index of the heading element (used for focusing in tabbing)\n   */\n  tabIndex?: number;\n  /**\n   * Which typography treatment renders. Pick it from the design, and only when the\n   * design asks for something other than the default for the level set by `as`. It\n   * changes the treatment alone, never which tag renders.\n   *\n   * For details, see https://chanzuckerberg.github.io/edu-design-system/?path=/story/design-tokens-tier-2-usage--typography\n   */\n  preset?: Preset;\n};\n\n/**\n * `HeadingProps`, widened to also accept the presets a component reserves for its own use.\n */\nexport type InternalHeadingProps = Omit<HeadingProps, 'preset'> & {\n  preset?: AnyPreset;\n};\n\n/**\n * Given a certain HeadingElement, what is the default preset to use?\n */\nconst headingPresetMap: Record<HeadingElement, Preset> = {\n  h1: 'headline-lg',\n  h2: 'headline-md',\n  h3: 'headline-sm',\n  h4: 'title-lg',\n  h5: 'title-md',\n  h6: 'title-sm',\n};\n\n/**\n * ## Usage\n *\n * `as` comes from document structure, `preset` comes from design. Set `as` to the level the\n * page outline calls for, then set `preset` only when the design asks for a treatment other\n * than that level's default.\n *\n * | Prop | What it sets | Where the value comes from |\n * |------|--------------|----------------------------|\n * | `as` | Which `h1`-`h6` tag renders, and the default preset that comes with that level. | The document structure: `h1` for the page title, `h2` for a section within it. |\n * | `preset` | The typography treatment, without changing which tag renders. | The design: a section heading drawn to read smaller than its level implies. |\n *\n * The two stay independent. Passing a `preset` never changes the tag, and the tag you pick\n * never overrides a `preset` you passed, so a heading can sit at the right level in the\n * outline and still carry the treatment design asked for.\n *\n * Each level brings a default preset, used when `preset` is left off:\n *\n * | `as` | Default `preset` |\n * |------|------------------|\n * | `h1` | `headline-lg` |\n * | `h2` | `headline-md` |\n * | `h3` | `headline-sm` |\n * | `h4` | `title-lg` |\n * | `h5` | `title-md` |\n * | `h6` | `title-sm` |\n *\n * ## Content & Accessibility\n *\n * ### Do's\n *\n * * Use `Heading` in place of any `h1`-`h6` HTML tag.\n * * Pass the correct heading element via `as` to avoid skipping heading levels, because heading levels increasing by only one level at a time is important for screen reader users.\n * * Reach for `preset` when the design calls for a treatment the level's default doesn't give you.\n *\n * ### Don'ts\n *\n * * Avoid all direct usage of `h1`-`h6` tags. Render them through `Heading` and its `as` prop instead.\n * * Don't reach for `preset` to change the heading level. Change `as` so the visual order and the document outline stay in step.\n * * Don't pick `as` for the size it renders at. Set the level the outline calls for, then set `preset` for the size.\n *\n * ## Presets\n *\n * `preset` only takes the reusable presets. A handful of presets belong to one component\n * (`tag`, `appHeader-label`, and friends) and are not available here, since they carry\n * that component's own treatment and can change with it.\n *\n * ## Resources\n *\n * * https://www.w3.org/WAI/tutorials/page-structure/headings/\n */\nexport const Heading = forwardRef(\n  (\n    { as = 'h1', children, className, preset, ...other }: HeadingProps,\n    ref: React.ForwardedRef<HTMLHeadingElement>,\n  ) => {\n    const TagName = as;\n    const componentClassName = clsx(\n      styles['text'],\n      preset && styles[`text--${preset}`],\n      !preset && styles[`text--${headingPresetMap[as]}`],\n      className,\n    );\n    return (\n      <TagName className={componentClassName} ref={ref} {...other}>\n        {children}\n      </TagName>\n    );\n  },\n);\n\nHeading.displayName = 'Heading';\n\n/**\n * `Heading`, typed to also accept the presets components reserve for their own use.\n *\n * For use inside EDS components only. This is deliberately absent from the package\n * exports, so consumers get `Heading` and its reusable presets instead.\n *\n * The cast widens the `preset` type; nothing changes at runtime, since the component\n * only ever interpolates `preset` into a class name.\n */\nexport const InternalHeading = Heading as ForwardRefExoticComponent<\n  InternalHeadingProps & RefAttributes<HTMLHeadingElement>\n>;\n"],"names":["styles","React"],"mappings":";;;;AAoDA;;AAEG;AACH,MAAM,gBAAgB,GAAmC;AACvD,IAAA,EAAE,EAAE,aAAa;AACjB,IAAA,EAAE,EAAE,aAAa;AACjB,IAAA,EAAE,EAAE,aAAa;AACjB,IAAA,EAAE,EAAE,UAAU;AACd,IAAA,EAAE,EAAE,UAAU;AACd,IAAA,EAAE,EAAE,UAAU;CACf;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkDG;AACI,MAAM,OAAO,GAAG,UAAU,CAC/B,CACE,EAAE,EAAE,GAAG,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,KAAK,EAAgB,EAClE,GAA2C,KACzC;IACF,MAAM,OAAO,GAAG,EAAE;AAClB,IAAA,MAAM,kBAAkB,GAAG,IAAI,CAC7BA,UAAM,CAAC,MAAM,CAAC,EACd,MAAM,IAAIA,UAAM,CAAC,SAAS,MAAM,CAAA,CAAE,CAAC,EACnC,CAAC,MAAM,IAAIA,UAAM,CAAC,CAAA,MAAA,EAAS,gBAAgB,CAAC,EAAE,CAAC,CAAA,CAAE,CAAC,EAClD,SAAS,CACV;AACD,IAAA,QACEC,cAAA,CAAA,aAAA,CAAC,OAAO,EAAA,EAAC,SAAS,EAAE,kBAAkB,EAAE,GAAG,EAAE,GAAG,EAAA,GAAM,KAAK,IACxD,QAAQ,CACD;AAEd,CAAC;AAGH,OAAO,CAAC,WAAW,GAAG,SAAS;;"}