@tangible/ui 0.2.42 → 0.2.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { forwardRef } from 'react';
3
3
  import { Tooltip } from '../Tooltip/index.js';
4
4
  import { IconButtonCore } from './IconButtonCore.js';
5
+ import { resolveBadge } from './badge.js';
5
6
  // =============================================================================
6
7
  // IconButton Component
7
8
  // =============================================================================
@@ -24,7 +25,9 @@ export const IconButton = forwardRef((props, ref) => {
24
25
  if (!showTooltip)
25
26
  return element;
26
27
  return (
27
- // The tooltip shows the label, which is already the button's name.
28
- _jsxs(Tooltip, { type: "label", children: [_jsx(Tooltip.Trigger, { asChild: true, children: element }), _jsx(Tooltip.Content, { side: tooltipSide, children: props.label })] }));
28
+ // The tooltip shows the button's name, badge count included ("Notifications,
29
+ // 5 new"), so its words match what AT hears. Not the busy name: loading
30
+ // is shown by the spinner.
31
+ _jsxs(Tooltip, { type: "label", children: [_jsx(Tooltip.Trigger, { asChild: true, children: element }), _jsx(Tooltip.Content, { side: tooltipSide, children: resolveBadge(props).name })] }));
29
32
  });
30
33
  IconButton.displayName = 'IconButton';
@@ -7,6 +7,7 @@ import { isDev } from '../../utils/is-dev.js';
7
7
  import { composeRefs } from '../../utils/compose-refs.js';
8
8
  import { useLoadingAnnouncer } from '../../utils/use-loading-announcer.js';
9
9
  import { defaultIconButtonLabels } from './types.js';
10
+ import { resolveBadge } from './badge.js';
10
11
  // =============================================================================
11
12
  // IconButtonCore (@internal)
12
13
  // =============================================================================
@@ -30,10 +31,19 @@ import { defaultIconButtonLabels } from './types.js';
30
31
  //
31
32
  // =============================================================================
32
33
  export const IconButtonCore = forwardRef((props, ref) => {
33
- const { icon, label, size = 'sm', theme = 'secondary', variant = 'ghost', disabled = false, loading = false, loadingLabel: loadingLabelProp, labels: labelsProp, pressed, shape = 'square', className, ...restWithStray } = props;
34
+ const { icon, label, size = 'sm', theme = 'secondary', variant = 'ghost', disabled = false, loading = false, loadingLabel: loadingLabelProp, labels: labelsProp, pressed, shape = 'square', badge, badgeDot, badgeMax, className, ...restWithStray } = props;
34
35
  const stray = restWithStray;
35
36
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
36
37
  const { showTooltip: strayShowTooltip, tooltipSide: _tooltipSide, ...rest } = stray;
38
+ // `aria-label` is omitted from the props type, but JSX lets hyphenated
39
+ // attributes through anyway (see CLAUDE.md), so read it untyped.
40
+ const restAria = rest;
41
+ const overridesName = restAria['aria-label'] !== undefined || restAria['aria-labelledby'] !== undefined;
42
+ useEffect(() => {
43
+ if (isDev() && overridesName && (badge || badgeDot)) {
44
+ console.warn(`IconButton ("${label}"): \`aria-label\` or \`aria-labelledby\` replaces the name the badge is spoken in, so screen readers won't hear the count. Use \`label\` and \`labels.badgeCount\` instead.`);
45
+ }
46
+ }, [overridesName, badge, badgeDot, label]);
37
47
  useEffect(() => {
38
48
  if (isDev() && strayShowTooltip) {
39
49
  console.warn(`IconButtonCore ("${label}"): \`showTooltip\` does nothing here. A component that offers the tooltip must render IconButton.`);
@@ -44,14 +54,16 @@ export const IconButtonCore = forwardRef((props, ref) => {
44
54
  // Loading keeps focus (see Button): aria-disabled plus a click guard, never
45
55
  // native `disabled` or a dropped href. Only explicit `disabled` goes native.
46
56
  const loadingOnly = loading && !disabled;
47
- const resolvedLabel = loading ? (loadingLabelProp ?? `${label}, loading`) : label;
48
57
  const labels = { ...defaultIconButtonLabels, ...labelsProp };
58
+ const badgeState = resolveBadge({ label, badge, badgeDot, badgeMax, labels: labelsProp });
59
+ const namedLabel = badgeState.name;
60
+ const resolvedLabel = loading ? (loadingLabelProp ?? `${namedLabel}, loading`) : namedLabel;
49
61
  // Opt-in loading announcements (#131): portaled, so no DOM sibling.
50
62
  const elementRef = useRef(null);
51
63
  const announcer = useLoadingAnnouncer(elementRef, loading, labels.loadingStart, labels.loadingEnd);
52
64
  const mergedRef = useMemo(() => composeRefs(ref, elementRef), [ref]);
53
- const classes = cx('tui-icon-button', `is-size-${size}`, `is-theme-${theme}`, `is-style-${variant}`, isDisabled && 'is-disabled', shape === 'circle' && 'is-shape-circle', className);
54
- const iconContent = (_jsxs(_Fragment, { children: [_jsx(Icon, { name: icon }), loading && _jsx("span", { className: "tui-icon-button__spinner", "aria-hidden": "true" })] }));
65
+ const classes = cx('tui-icon-button', `is-size-${size}`, `is-theme-${theme}`, `is-style-${variant}`, isDisabled && 'is-disabled', shape === 'circle' && 'is-shape-circle', badgeState.shown && 'has-badge', className);
66
+ const iconContent = (_jsxs(_Fragment, { children: [_jsx(Icon, { name: icon }), loading && _jsx("span", { className: "tui-icon-button__spinner", "aria-hidden": "true" }), badgeState.shown && (_jsx("span", { className: cx('tui-icon-button__badge', badgeState.dot && 'is-dot'), "aria-hidden": "true", "data-count": badgeState.count || undefined, children: badgeState.text }))] }));
55
67
  // Render the base element (button or anchor)
56
68
  let element;
57
69
  if (isLink) {
@@ -0,0 +1,20 @@
1
+ import type { IconButtonLabels } from './types';
2
+ /**
3
+ * The badge's count, visible text and the button name it produces. One
4
+ * function, so the button's name and its label tooltip can't drift apart.
5
+ * Not a component file, so IconButton.tsx stays component-only.
6
+ * @internal
7
+ */
8
+ export declare function resolveBadge(options: {
9
+ label: string;
10
+ badge?: number;
11
+ badgeDot?: boolean;
12
+ badgeMax?: number;
13
+ labels?: IconButtonLabels;
14
+ }): {
15
+ shown: boolean;
16
+ dot: boolean;
17
+ count: number;
18
+ text: string;
19
+ name: string;
20
+ };
@@ -0,0 +1,22 @@
1
+ import { defaultIconButtonLabels } from './types.js';
2
+ /**
3
+ * The badge's count, visible text and the button name it produces. One
4
+ * function, so the button's name and its label tooltip can't drift apart.
5
+ * Not a component file, so IconButton.tsx stays component-only.
6
+ * @internal
7
+ */
8
+ export function resolveBadge(options) {
9
+ const { label, badge, badgeDot = false, labels: labelsProp } = options;
10
+ const labels = { ...defaultIconButtonLabels, ...labelsProp };
11
+ // A count is a positive whole number; anything else (0, NaN, negative) is
12
+ // no count, so `badge={items.length}` needs no guard at the call site.
13
+ const count = typeof badge === 'number' && Number.isFinite(badge) && badge >= 1 ? Math.floor(badge) : 0;
14
+ const rawMax = options.badgeMax ?? 99;
15
+ const max = Number.isFinite(rawMax) ? Math.max(1, Math.floor(rawMax)) : 99;
16
+ const shown = count > 0 || badgeDot;
17
+ const text = badgeDot || count === 0 ? '' : count > max ? `${max}+` : String(count);
18
+ // The badge is aria-hidden; its meaning goes into the name instead. A
19
+ // count beats the dot for the name, so a dot that stands for 5 says 5.
20
+ const name = count > 0 ? labels.badgeCount(count, label) : badgeDot ? labels.badgeDot(label) : label;
21
+ return { shown, dot: badgeDot, count, text, name };
22
+ }
@@ -16,6 +16,20 @@ export type IconButtonLabels = {
16
16
  * @default '' (not announced)
17
17
  */
18
18
  loadingEnd?: string;
19
+ /**
20
+ * The button's whole name when it has a count: "Notifications, 5 new". It
21
+ * gets the count and `label`, so a translation can put them in any order.
22
+ * Say what the count counts when "new" doesn't fit, and mind plurals:
23
+ * `(n, label) => \`${label}, ${n === 1 ? '1 item' : \`${n} items\`}\``
24
+ * for a cart (or `Intl.PluralRules`).
25
+ * @default (count, label) => `${label}, ${count} new`
26
+ */
27
+ badgeCount?: (count: number, label: string) => string;
28
+ /**
29
+ * The button's whole name when `badgeDot` is set and there's no count.
30
+ * @default (label) => `${label}, new`
31
+ */
32
+ badgeDot?: (label: string) => string;
19
33
  };
20
34
  export declare const defaultIconButtonLabels: Required<IconButtonLabels>;
21
35
  type IconButtonBaseProps = {
@@ -89,8 +103,9 @@ type IconButtonBaseProps = {
89
103
  */
90
104
  loading?: boolean;
91
105
  /**
92
- * Accessible label override during loading state. For i18n support.
93
- * @default `${label}, loading` (English)
106
+ * Accessible label override during loading state. For i18n support. It
107
+ * is the whole busy name: include the badge count yourself if it matters.
108
+ * @default `${name}, loading` (English), where `name` includes any badge
94
109
  */
95
110
  loadingLabel?: string;
96
111
  /**
@@ -120,6 +135,43 @@ type IconButtonBaseProps = {
120
135
  * @default 'top'
121
136
  */
122
137
  tooltipSide?: 'top' | 'right' | 'bottom' | 'left';
138
+ /**
139
+ * A count on the button's corner: unread notifications, items in a cart.
140
+ * Above `badgeMax` it shows "99+". `0`, a negative or a non-number shows
141
+ * nothing, so `badge={items.length}` needs no guard.
142
+ *
143
+ * The count is part of the button's name, not only its look: "Notifications,
144
+ * 5 new" (`labels.badgeCount`). The full count is spoken even when the badge
145
+ * is capped. Anything that replaces the name replaces the count with it: an
146
+ * `aria-label` or `aria-labelledby` (a dev warning fires), and
147
+ * `loadingLabel`, which is the whole busy name.
148
+ *
149
+ * A change of count is NOT announced: the name changes silently on a button
150
+ * the user isn't on. If people need to know about an arrival right away,
151
+ * tell them with your own status message (a Toast).
152
+ *
153
+ * The badge overhangs the button's end edge (a "99+" by about 10px at `sm`)
154
+ * and paints above later siblings, so leave a gap after a badged button.
155
+ * Its colour doesn't follow `theme`: it is `--tui-icon-button-badge-bg`
156
+ * (primary by default) on every button. Set `--tui-icon-button-badge-ring`
157
+ * to the surface the button sits on (a header) so its cut-out blends in.
158
+ * @example badge={unreadCount}
159
+ */
160
+ badge?: number;
161
+ /**
162
+ * Show the badge as a dot instead of a number: "there's something new"
163
+ * without a count, or a quieter look for one. With a `badge` count as well,
164
+ * the dot still speaks the count in the name.
165
+ * @default false
166
+ */
167
+ badgeDot?: boolean;
168
+ /**
169
+ * The largest count the badge shows; above it, the badge reads "99+". At
170
+ * `xs`, consider 9: a wide count overhangs a small button a long way.
171
+ * Whole numbers of 1 or more.
172
+ * @default 99
173
+ */
174
+ badgeMax?: number;
123
175
  /**
124
176
  * Button shape. Use `'circle'` for avatar-adjacent actions, FABs,
125
177
  * or anywhere a round affordance is expected.
@@ -1,4 +1,6 @@
1
1
  export const defaultIconButtonLabels = {
2
2
  loadingStart: '',
3
3
  loadingEnd: '',
4
+ badgeCount: (count, label) => `${label}, ${count} new`,
5
+ badgeDot: (label) => `${label}, new`,
4
6
  };
package/figma-tokens.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.2.42",
2
+ "version": "0.2.43",
3
3
  "source": "publish/styles/all.expanded.css",
4
4
  "prefix": "tui-",
5
5
  "note": "Layer 1 primitives only. Component API tokens (--tui-{component}-*) are never defined in CSS — they exist only as fallback reads — so they have no Figma representation. CONTRACT: consumers read TUI defaults from this file at build time (variables[].cssVar, the \"Light\"/\"Dark\" mode names, and alias chains into TUI/Primitives). Renaming a cssVar or a mode is a breaking change to those consumers even when the CSS value is unchanged — a rename silently becomes an undefined default downstream — so treat it as at least a minor bump and call it out in the CHANGELOG.",
package/icons/icons.svg CHANGED
@@ -1,4 +1,4 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" data-tui-icon-sprite="all" data-tui-icon-sprite-version="0.2.42" aria-hidden="true" focusable="false" style="position:absolute;width:0;height:0;overflow:hidden">
1
+ <svg xmlns="http://www.w3.org/2000/svg" data-tui-icon-sprite="all" data-tui-icon-sprite-version="0.2.43" aria-hidden="true" focusable="false" style="position:absolute;width:0;height:0;overflow:hidden">
2
2
  <symbol id="tui-cred-badge-check" viewBox="0 0 24 24"><path fill-rule="evenodd" d="M10.893 4.558a1.377 1.377 0 0 1 2.214 0c.381.515 1.068.7 1.656.444a1.377 1.377 0 0 1 1.917 1.106c.072.637.575 1.14 1.212 1.213a1.377 1.377 0 0 1 1.106 1.916 1.38 1.38 0 0 0 .444 1.656c.744.55.744 1.663 0 2.214a1.38 1.38 0 0 0-.444 1.656 1.377 1.377 0 0 1-1.106 1.917 1.38 1.38 0 0 0-1.212 1.212 1.377 1.377 0 0 1-1.917 1.106 1.38 1.38 0 0 0-1.656.444 1.377 1.377 0 0 1-2.214 0 1.38 1.38 0 0 0-1.656-.444 1.377 1.377 0 0 1-1.916-1.106 1.38 1.38 0 0 0-1.213-1.212 1.377 1.377 0 0 1-1.106-1.917 1.38 1.38 0 0 0-.444-1.656 1.377 1.377 0 0 1 0-2.214c.515-.381.7-1.068.444-1.656a1.377 1.377 0 0 1 1.106-1.916 1.38 1.38 0 0 0 1.213-1.213 1.377 1.377 0 0 1 1.916-1.106 1.38 1.38 0 0 0 1.656-.444m5.193 5.91a.75.75 0 0 0-1.172-.936l-3.531 4.414-1.914-1.532a.75.75 0 1 0-.937 1.172l3.085 2.468z" clip-rule="evenodd"/></symbol>
3
3
  <symbol id="tui-cred-badge-ribbon" viewBox="0 0 24 24"><path d="M11.032 3.488a1.205 1.205 0 0 1 1.936 0c.334.451.935.612 1.45.388a1.205 1.205 0 0 1 1.677.969c.063.557.503.997 1.06 1.06.805.092 1.291.935.969 1.678a1.204 1.204 0 0 0 .388 1.449c.65.481.65 1.455 0 1.936a1.205 1.205 0 0 0-.388 1.45 1.205 1.205 0 0 1-.969 1.677 1.204 1.204 0 0 0-1.06 1.06 1.205 1.205 0 0 1-1.678.969 1.205 1.205 0 0 0-1.449.388c-.481.65-1.455.65-1.936 0a1.204 1.204 0 0 0-1.45-.388 1.205 1.205 0 0 1-1.677-.969 1.205 1.205 0 0 0-1.06-1.06 1.205 1.205 0 0 1-.969-1.678 1.205 1.205 0 0 0-.388-1.449 1.205 1.205 0 0 1 0-1.936c.451-.334.612-.935.388-1.45a1.205 1.205 0 0 1 .969-1.677 1.205 1.205 0 0 0 1.06-1.06 1.205 1.205 0 0 1 1.678-.969 1.205 1.205 0 0 0 1.449-.388M8 17.482c.442.175.953.185 1.437-.025a.88.88 0 0 1 1.054.282 1.877 1.877 0 0 0 3.018 0 .88.88 0 0 1 1.054-.282c.484.21.995.2 1.437.025V21l-4-2.286L8 21z"/></symbol>
4
4
  <symbol id="tui-cred-badge" viewBox="0 0 24 24"><path d="M13.107 4.558a1.377 1.377 0 0 0-2.214 0c-.381.515-1.068.7-1.656.444a1.377 1.377 0 0 0-1.916 1.106 1.38 1.38 0 0 1-1.213 1.213 1.377 1.377 0 0 0-1.106 1.916 1.38 1.38 0 0 1-.444 1.656 1.377 1.377 0 0 0 0 2.214c.515.381.7 1.068.444 1.656a1.377 1.377 0 0 0 1.106 1.917c.637.072 1.14.575 1.213 1.212a1.377 1.377 0 0 0 1.916 1.106 1.38 1.38 0 0 1 1.656.444c.55.744 1.663.744 2.214 0a1.38 1.38 0 0 1 1.656-.444 1.377 1.377 0 0 0 1.917-1.106 1.38 1.38 0 0 1 1.212-1.212 1.377 1.377 0 0 0 1.106-1.917 1.38 1.38 0 0 1 .444-1.656 1.377 1.377 0 0 0 0-2.214 1.38 1.38 0 0 1-.444-1.656 1.377 1.377 0 0 0-1.106-1.916 1.38 1.38 0 0 1-1.212-1.213 1.377 1.377 0 0 0-1.917-1.106 1.38 1.38 0 0 1-1.656-.444"/></symbol>
@@ -1 +1 @@
1
- export const SPRITE_VERSION = '0.2.42';
1
+ export const SPRITE_VERSION = '0.2.43';
package/icons/sprite.php CHANGED
@@ -21,7 +21,7 @@
21
21
  */
22
22
 
23
23
  // Replaced with the package version at build time.
24
- $GLOBALS['tangible_ui_icon_sprite_copies'][__DIR__] = '0.2.42';
24
+ $GLOBALS['tangible_ui_icon_sprite_copies'][__DIR__] = '0.2.43';
25
25
 
26
26
  if (!function_exists('tangible_ui_print_icon_sprite')) {
27
27
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangible/ui",
3
- "version": "0.2.42",
3
+ "version": "0.2.43",
4
4
  "description": "React design system for Tangible's products: standalone React apps and WordPress plugins. Components, CSS custom property tokens, cascade layers.",
5
5
  "type": "module",
6
6
  "main": "./components/index.js",
@@ -537,6 +537,24 @@ up descender space unless it's `display: flex`. `decorative` ignores `tooltip` a
537
537
  `className` is for layout integration (margin, grid placement), not appearance. Beyond the token
538
538
  API the sanctioned escape hatch is to **swizzle** — copy the component source and own the fork.
539
539
 
540
+ ### A badged icon button in a header
541
+
542
+ `<IconButton icon="system/bell" label="Notifications" badge={unread} />` is named "Notifications,
543
+ 5 new": the count lives in the name, so don't add your own `aria-label` (it drops the count; a dev
544
+ warning fires) and translate through `labels.badgeCount(count, label)`. The header that holds it
545
+ sets the badge's cut-out ring to its own background, once, and its colour if not primary:
546
+
547
+ ```css
548
+ .app-header {
549
+ --tui-icon-button-badge-ring: var(--tui-color-bg-elevated); /* the header's surface */
550
+ --tui-icon-button-badge-bg: var(--tui-theme-danger-base); /* optional: red badges */
551
+ --tui-icon-button-badge-fg: var(--tui-theme-danger-on-base);
552
+ }
553
+ ```
554
+
555
+ Leave a gap after a badged button: a "99+" overhangs its end edge by about 10px at `sm`. A changing
556
+ count is not announced; if an arrival matters right away, say so with a Toast.
557
+
540
558
  ### Card spacing, container and links
541
559
 
542
560
  - **Padding vs gap.** `--tui-card-padding` is the card's *outer* edge: inline on every slot, and