{"version":3,"file":"NotificationBadge.cjs","sources":["../../../../src/components/notification-badge/NotificationBadge.tsx"],"sourcesContent":["import clsx from \"clsx\";\nimport React, { type CSSProperties, type ReactNode, useId } from \"react\";\nimport type { NotificationBadgeProps } from \"./types.js\";\n\nconst DEFAULT_LABEL = \"varsler\";\n\nfunction badgeValue(count: number, max: number) {\n    return count > max ? `${max}+` : count;\n}\n\nexport const NotificationBadge = ({\n    className,\n    children,\n    count,\n    max = 99,\n    hideCounter,\n    urgent = false,\n    label = DEFAULT_LABEL,\n    ...rest\n}: NotificationBadgeProps): ReactNode => {\n    const reactId = useId();\n    const badgeId = `jkl-notification-badge-${reactId}`;\n    const anchorName = `--jkl-notification-badge-anchor-${reactId.replace(/[^a-z0-9]/gi, \"\")}`;\n    const hasCount = count > 0;\n    const description = hasCount ? label : undefined;\n\n    return (\n        <>\n            {React.isValidElement(children)\n                ? React.cloneElement(children, {\n                      // Uten et varsel å peke på beholder ankeret bare sin egen\n                      // verdi, slik at attributtet utelates helt når den mangler.\n                      \"aria-describedby\":\n                          clsx(\n                              children.props[\"aria-describedby\"],\n                              hasCount && badgeId,\n                          ) || undefined,\n                      className: clsx(\n                          children.props.className,\n                          \"jkl-notification-badge-anchor\",\n                      ),\n                      style: {\n                          ...children.props.style,\n                          \"--jkl-notification-badge-anchor-name\": anchorName,\n                      } as CSSProperties,\n                      title: children.props.title ?? description,\n                  })\n                : children}\n            {hasCount ? (\n                <div\n                    className={clsx(\n                        \"jkl-notification-badge\",\n                        hideCounter && \"jkl-notification-badge--dot\",\n                        className,\n                    )}\n                    data-urgent={urgent}\n                    data-color={urgent ? \"error\" : undefined}\n                    data-count={count}\n                    data-hide-counter={hideCounter}\n                    {...rest}\n                    style={\n                        {\n                            ...rest.style,\n                            \"--jkl-notification-badge-anchor-name\": anchorName,\n                        } as CSSProperties\n                    }\n                    id={badgeId}\n                >\n                    {hideCounter ? null : (\n                        <span aria-hidden=\"true\">{badgeValue(count, max)}</span>\n                    )}\n                    <div className=\"jkl-sr-only\">{description}</div>\n                </div>\n            ) : null}\n        </>\n    );\n};\n"],"names":["badgeValue","count","max","className","children","hideCounter","urgent","label","rest","reactId","useId","badgeId","anchorName","replace","hasCount","description","jsxs","Fragment","React","isValidElement","cloneElement","clsx","props","style","title","id","jsx"],"mappings":"gLAMA,SAASA,EAAWC,EAAeC,GAC/B,OAAOD,EAAQC,EAAM,GAAGA,KAASD,CACrC,2BAEiC,EAC7BE,UAAAA,EACAC,SAAAA,EACAH,MAAAA,EACAC,IAAAA,EAAM,GACNG,YAAAA,EACAC,OAAAA,GAAS,EACTC,MAAAA,EAbkB,aAcfC,MAEH,MAAMC,EAAUC,EAAAA,QACVC,EAAU,0BAA0BF,IACpCG,EAAa,mCAAmCH,EAAQI,QAAQ,cAAe,MAC/EC,EAAWb,EAAQ,EACnBc,EAAcD,EAAWP,OAAQ,EAEvC,OACIS,EAAAA,KAAAC,WAAA,CACKb,SAAA,CAAAc,EAAMC,eAAef,GAChBc,EAAME,aAAahB,EAAU,CAGzB,mBACIiB,EAAAA,KACIjB,EAASkB,MAAM,oBACfR,GAAYH,SACX,EACTR,UAAWkB,EAAAA,KACPjB,EAASkB,MAAMnB,UACf,iCAEJoB,MAAO,IACAnB,EAASkB,MAAMC,MAClB,uCAAwCX,GAE5CY,MAAOpB,EAASkB,MAAME,OAAST,IAEnCX,EACLU,EACGE,EAAAA,KAAC,MAAA,CACGb,UAAWkB,EAAAA,KACP,yBACAhB,GAAe,8BACfF,GAEJ,cAAaG,EACb,aAAYA,EAAS,aAAU,EAC/B,aAAYL,EACZ,oBAAmBI,KACfG,EACJe,MACI,IACOf,EAAKe,MACR,uCAAwCX,GAGhDa,GAAId,EAEHP,SAAA,CAAAC,EAAc,WACV,OAAA,CAAK,cAAY,OAAQD,SAAAJ,EAAWC,EAAOC,KAEhDwB,EAAAA,IAAC,MAAA,CAAIvB,UAAU,cAAeC,SAAAW,OAElC"}