|
| 1 | +// Copyright 2025 Signal Messenger, LLC |
| 2 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 3 | +import type { FC } from 'react'; |
| 4 | +import React, { memo, useMemo } from 'react'; |
| 5 | +import { AxoSymbol } from './AxoSymbol.js'; |
| 6 | +import type { TailwindStyles } from './tw.js'; |
| 7 | +import { tw } from './tw.js'; |
| 8 | +import { unreachable } from './_internal/assert.js'; |
| 9 | + |
| 10 | +const Namespace = 'AxoBadge'; |
| 11 | + |
| 12 | +/** |
| 13 | + * @example Anatomy |
| 14 | + * ```tsx |
| 15 | + * <AxoBadge.Root aria-label="42 unread messages"> |
| 16 | + * <AxoBadge.Count value={42} max={999}/> |
| 17 | + * </AxoBadge.Root> |
| 18 | + * |
| 19 | + * <AxoBadge.Root aria-label="Marked unread"/> |
| 20 | + * |
| 21 | + * <AxoBadge.Root aria-label="You were mentioned"> |
| 22 | + * <AxoBadge.Icon symbol="at" /> |
| 23 | + * </AxoBadge.Root> |
| 24 | + * ```` |
| 25 | + */ |
| 26 | +export namespace ExperimentalAxoBadge { |
| 27 | + export type BadgeSize = 'sm' | 'md' | 'lg'; |
| 28 | + export type BadgeValue = number | 'mention' | 'unread'; |
| 29 | + |
| 30 | + const baseStyles = tw( |
| 31 | + 'flex size-fit items-center justify-center-safe overflow-clip', |
| 32 | + 'rounded-full font-semibold', |
| 33 | + 'bg-color-fill-primary text-label-primary-on-color', |
| 34 | + 'select-none' |
| 35 | + ); |
| 36 | + |
| 37 | + type BadgeConfig = Readonly<{ |
| 38 | + rootStyles: TailwindStyles; |
| 39 | + countStyles: TailwindStyles; |
| 40 | + }>; |
| 41 | + |
| 42 | + const BadgeSizes: Record<BadgeSize, BadgeConfig> = { |
| 43 | + sm: { |
| 44 | + rootStyles: tw(baseStyles, 'min-h-3.5 min-w-3.5 text-[8px] leading-3.5'), |
| 45 | + countStyles: tw('px-[3px]'), |
| 46 | + }, |
| 47 | + md: { |
| 48 | + rootStyles: tw(baseStyles, 'min-h-4 min-w-4 text-[11px] leading-4'), |
| 49 | + countStyles: tw('px-[4px]'), |
| 50 | + }, |
| 51 | + lg: { |
| 52 | + rootStyles: tw(baseStyles, 'min-h-4.5 min-w-4.5 text-[11px] leading-4.5'), |
| 53 | + countStyles: tw('px-[5px]'), |
| 54 | + }, |
| 55 | + }; |
| 56 | + |
| 57 | + export function _getAllBadgeSizes(): ReadonlyArray<BadgeSize> { |
| 58 | + return Object.keys(BadgeSizes) as Array<BadgeSize>; |
| 59 | + } |
| 60 | + |
| 61 | + let cachedNumberFormat: Intl.NumberFormat; |
| 62 | + |
| 63 | + // eslint-disable-next-line no-inner-declarations |
| 64 | + function formatBadgeCount( |
| 65 | + value: number, |
| 66 | + max: number, |
| 67 | + maxDisplay: string |
| 68 | + ): string { |
| 69 | + if (value > max) { |
| 70 | + return maxDisplay; |
| 71 | + } |
| 72 | + cachedNumberFormat ??= new Intl.NumberFormat(); |
| 73 | + return cachedNumberFormat.format(value); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Component: <AxoBadge.Root> |
| 78 | + * -------------------------- |
| 79 | + */ |
| 80 | + |
| 81 | + export type RootProps = Readonly<{ |
| 82 | + size: BadgeSize; |
| 83 | + value: BadgeValue; |
| 84 | + max: number; |
| 85 | + maxDisplay: string; |
| 86 | + 'aria-label': string | null; |
| 87 | + }>; |
| 88 | + |
| 89 | + export const Root: FC<RootProps> = memo(props => { |
| 90 | + const { value, max, maxDisplay } = props; |
| 91 | + const config = BadgeSizes[props.size]; |
| 92 | + |
| 93 | + const children = useMemo(() => { |
| 94 | + if (value === 'unread') { |
| 95 | + return null; |
| 96 | + } |
| 97 | + if (value === 'mention') { |
| 98 | + return ( |
| 99 | + <span aria-hidden className={tw('leading-none')}> |
| 100 | + <AxoSymbol.InlineGlyph symbol="at" label={null} /> |
| 101 | + </span> |
| 102 | + ); |
| 103 | + } |
| 104 | + if (typeof value === 'number') { |
| 105 | + return ( |
| 106 | + <span aria-hidden className={config.countStyles}> |
| 107 | + {formatBadgeCount(value, max, maxDisplay)} |
| 108 | + </span> |
| 109 | + ); |
| 110 | + } |
| 111 | + unreachable(value); |
| 112 | + }, [value, max, maxDisplay, config]); |
| 113 | + |
| 114 | + return ( |
| 115 | + <span |
| 116 | + aria-label={props['aria-label'] ?? undefined} |
| 117 | + className={config.rootStyles} |
| 118 | + > |
| 119 | + {children} |
| 120 | + </span> |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + Root.displayName = `${Namespace}.Root`; |
| 125 | +} |
0 commit comments