{"version":3,"sources":["../src/form/error-summary/error-summary.tsx"],"sourcesContent":["import { forwardRef, useEffect, useRef } from \"react\";\nimport { Message, type MessageProps, type MessageTitleProps } from \"../../message\";\nimport { UnorderedList, type ListProps } from \"../../list\";\nimport { Link } from \"../../link\";\nimport { useMergeRefs } from \"../../utils\";\nimport { focusWithLegendOrLabelInViewport } from \"./focus\";\n\ninterface ErrorSummaryHeadingPropsAutoFocus {\n  /**\n   * The heading will be focused when the component mounts\n   *\n   * On following errornous form submissions you should manually focus the heading\n   * e.g. by passing a ref and calling `ref.current.focus()`\n   *\n   * @default true\n   */\n  autoFocus?: boolean;\n}\n\ninterface ErrorSummaryHeadingPropsAs {\n  /**\n   * A heading level must be selected, or optionally opting out for a different element\n   *\n   * Use {@link ErrorSummaryHeadingPropsAsChild.asChild} if you need more control of the rendered element.\n   */\n  as: \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\" | \"span\" | \"div\" | \"label\" | \"p\";\n  asChild?: never;\n}\n\ninterface ErrorSummaryHeadingPropsAsChild {\n  /**\n   * Change the default rendered element for the one passed as a child, merging their props and behavior.\n   */\n  asChild: true;\n  as?: never;\n}\n\nexport type ErrorSummaryHeadingProps = MessageTitleProps &\n  ErrorSummaryHeadingPropsAutoFocus &\n  (ErrorSummaryHeadingPropsAs | ErrorSummaryHeadingPropsAsChild);\n\nexport const ErrorSummaryHeading = forwardRef<\n  HTMLParagraphElement,\n  ErrorSummaryHeadingProps & (ErrorSummaryHeadingPropsAs | ErrorSummaryHeadingPropsAsChild)\n>(({ children, as: Tag, autoFocus = true, ...rest }, ref) => {\n  const focusRef = useRef<HTMLElement>(null);\n  const mergedRef = useMergeRefs([focusRef, ref]);\n\n  useEffect(() => {\n    /**\n     * Hack: Safari 18 on mac at the time of writing\n     * does not correctly focus it with VoiceOver without the timeout\n     */\n    setTimeout(() => {\n      if (focusRef.current && autoFocus) {\n        focusRef.current.focus();\n      }\n    });\n    // eslint-disable-next-line react-hooks/exhaustive-deps -- Only on initial render\n  }, []);\n\n  return (\n    <Message.Title ref={mergedRef} tabIndex={-1} asChild {...rest}>\n      {Tag ? <Tag>{children}</Tag> : children}\n    </Message.Title>\n  );\n});\nErrorSummaryHeading.displayName = \"ErrorSummary.Heading\";\n\nexport interface ErrorSummaryListProps extends ListProps {\n  /**\n   * Sets the size of the items (font)\n   *\n   * @default \"small\"\n   */\n  size?: ListProps[\"size\"];\n}\nexport const ErrorSummaryList = forwardRef<HTMLUListElement, ErrorSummaryListProps>(\n  ({ children, style: _style, size = \"small\", ...rest }, ref) => {\n    const style = {\n      // Match the link `solid` style, which black underline\n      \"--_hds-list-marker-color\": \"var(--hds-ui-colors-black)\",\n      ..._style,\n    };\n    return (\n      <Message.Description asChild>\n        <UnorderedList size={size} ref={ref} style={style} {...rest}>\n          {children}\n        </UnorderedList>\n      </Message.Description>\n    );\n  },\n);\nErrorSummaryList.displayName = \"ErrorSummary.List\";\n\nexport interface ErrorSummaryItemProps extends React.HTMLAttributes<HTMLLIElement> {\n  /**\n   * A hash link to the element that the error message refers to\n   *\n   * Must start with \"#\" as it's passed to the `href` attribute of an anchor element\n   *\n   * @example \"#email\"\n   */\n  href: `#${string}`;\n\n  /**\n   * Extra props to pass to the link element\n   */\n  linkProps?: React.AnchorHTMLAttributes<HTMLAnchorElement>;\n}\nexport const ErrorSummaryItem = forwardRef<HTMLLIElement, ErrorSummaryItemProps>(\n  ({ children, href, linkProps, ...rest }, ref) => {\n    function onClick(e: React.MouseEvent<HTMLAnchorElement>) {\n      linkProps?.onClick?.(e);\n      if (focusWithLegendOrLabelInViewport(href.replace(\"#\", \"\"))) {\n        e.preventDefault();\n      }\n    }\n\n    return (\n      <li ref={ref} {...rest}>\n        <Link size=\"small\" href={href} variant=\"solid\" {...linkProps} onClick={onClick}>\n          {children}\n        </Link>\n      </li>\n    );\n  },\n);\nErrorSummaryItem.displayName = \"ErrorSummary.Item\";\n\nexport type ErrorSummaryProps = Omit<MessageProps, \"variant\" | \"icon\" | \"iconClassName\">;\n\nexport const ErrorSummary = forwardRef<HTMLDivElement, ErrorSummaryProps>(\n  ({ children, ...rest }, ref) => {\n    return (\n      <Message variant=\"warning\" ref={ref} {...rest}>\n        {children}\n      </Message>\n    );\n  },\n) as ErrorSummaryType;\nErrorSummary.displayName = \"ErrorSummary\";\n\ntype ErrorSummaryType = ReturnType<typeof forwardRef<HTMLDivElement, ErrorSummaryProps>> & {\n  Heading: typeof ErrorSummaryHeading;\n  List: typeof ErrorSummaryList;\n  Item: typeof ErrorSummaryItem;\n};\nErrorSummary.Heading = ErrorSummaryHeading;\nErrorSummary.List = ErrorSummaryList;\nErrorSummary.Item = ErrorSummaryItem;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,YAAY,WAAW,cAAc;AA+DjC;AAtBN,IAAM,sBAAsB,WAGjC,CAAC,IAAkD,QAAQ;AAA1D,eAAE,YAAU,IAAI,KAAK,YAAY,KA5CpC,IA4CG,IAA0C,iBAA1C,IAA0C,CAAxC,YAAU,MAAS;AACtB,QAAM,WAAW,OAAoB,IAAI;AACzC,QAAM,YAAY,aAAa,CAAC,UAAU,GAAG,CAAC;AAE9C,YAAU,MAAM;AAKd,eAAW,MAAM;AACf,UAAI,SAAS,WAAW,WAAW;AACjC,iBAAS,QAAQ,MAAM;AAAA,MACzB;AAAA,IACF,CAAC;AAAA,EAEH,GAAG,CAAC,CAAC;AAEL,SACE,oBAAC,QAAQ,OAAR,+BAAc,KAAK,WAAW,UAAU,IAAI,SAAO,QAAK,OAAxD,EACE,gBAAM,oBAAC,OAAK,UAAS,IAAS,WACjC;AAEJ,CAAC;AACD,oBAAoB,cAAc;AAU3B,IAAM,mBAAmB;AAAA,EAC9B,CAAC,IAAsD,QAAQ;AAA9D,iBAAE,YAAU,OAAO,QAAQ,OAAO,QA9ErC,IA8EG,IAA8C,iBAA9C,IAA8C,CAA5C,YAAU,SAAe;AAC1B,UAAM,QAAQ;AAAA;AAAA,MAEZ,4BAA4B;AAAA,OAC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