@microbit/ui 0.1.0-alpha.21 → 0.1.0-alpha.22

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@microbit/ui",
3
- "version": "0.1.0-alpha.21",
3
+ "version": "0.1.0-alpha.22",
4
4
  "description": "micro:bit design-system primitives: react-aria-components + Panda CSS with a design language ported from Chakra UI v2. Ships as source; see README for the consumption setup.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -54,10 +54,18 @@ export const numberField = defineSlotRecipe({
54
54
  borderColor: "gray.200",
55
55
  transitionProperty: "background",
56
56
  transitionDuration: "ultra-fast",
57
+ // Follow the input's corners, less the 1px border the stepper is inset
58
+ // by, so the hover and pressed fills curve away with the border instead
59
+ // of squaring off over its arc. Radii track the input recipe's size
60
+ // scale, so the `sm` variant restates them.
61
+ "&:first-child": {
62
+ borderStartEndRadius: "calc(token(radii.md) - 1px)",
63
+ },
57
64
  "&:last-child": {
58
65
  borderTop: "1px solid",
59
66
  borderTopColor: "gray.200",
60
67
  marginTop: "-1px",
68
+ borderEndEndRadius: "calc(token(radii.md) - 1px)",
61
69
  },
62
70
  "&[data-hovered]": { bg: "gray.100" },
63
71
  "&[data-pressed]": { bg: "gray.200" },
@@ -75,7 +83,15 @@ export const numberField = defineSlotRecipe({
75
83
  },
76
84
  md: {},
77
85
  sm: {
78
- stepperButton: { fontSize: "calc(token(fontSizes.sm) * 0.75)" },
86
+ stepperButton: {
87
+ fontSize: "calc(token(fontSizes.sm) * 0.75)",
88
+ "&:first-child": {
89
+ borderStartEndRadius: "calc(token(radii.sm) - 1px)",
90
+ },
91
+ "&:last-child": {
92
+ borderEndEndRadius: "calc(token(radii.sm) - 1px)",
93
+ },
94
+ },
79
95
  },
80
96
  },
81
97
  },
@@ -50,7 +50,6 @@ export const ProgressBar = ({
50
50
  {
51
51
  height: "100%",
52
52
  bg: "brand.500",
53
- transition: "width 0.2s",
54
53
  },
55
54
  barCss,
56
55
  )}
package/src/Toast.tsx CHANGED
@@ -28,7 +28,7 @@ import { VisuallyHidden } from "./VisuallyHidden";
28
28
  export type ToastStatus = "info" | "success" | "warning" | "error";
29
29
 
30
30
  export interface ToastContent {
31
- /** Dedup key: adding a toast whose id is already visible is a no-op. */
31
+ /** Dedup key: adding a toast whose id is already queued is a no-op. */
32
32
  id?: string;
33
33
  title?: ReactNode;
34
34
  description?: ReactNode;
@@ -75,6 +75,15 @@ export const toastQueue = new RACToastQueue<ToastContent>({
75
75
  wrapUpdate,
76
76
  });
77
77
 
78
+ // Index of our ids to the queue's own keys. The queue only exposes its
79
+ // visible slice — the newest `maxVisibleToasts` — so ids can't be resolved by
80
+ // scanning it: once newer toasts arrive an older one is still queued but out
81
+ // of sight, and dedup would let a second copy through while update() added
82
+ // rather than replaced. react-aria's per-toast `onClose` keeps this honest
83
+ // however a toast goes (timeout, close button, or update). clear() doesn't
84
+ // call onClose, so closeAll empties both.
85
+ const keysById = new Map<string, string>();
86
+
78
87
  // Status icon matching Chakra's AlertIcon (filled glyphs, coloured by the
79
88
  // toast foreground = white here). Warning is a triangle, error a circle, as
80
89
  // in Chakra — the glyph must distinguish them because the colours alone
@@ -166,14 +175,18 @@ export interface ToastOptions extends ToastContent {
166
175
 
167
176
  export interface ToastFn {
168
177
  (options: ToastOptions): void;
169
- /** Whether a toast with this id is currently visible. */
178
+ /**
179
+ * Whether a toast with this id is still queued — displayed, or waiting
180
+ * behind newer toasts for its turn.
181
+ */
170
182
  isActive(id: string): boolean;
171
183
  /**
172
- * Replace a visible toast's content (Chakra's toast.update). The toast is
173
- * re-added, so unlike Chakra it re-animates and restarts any timeout.
184
+ * Replace a queued toast's content (Chakra's toast.update). The toast is
185
+ * re-added, so unlike Chakra it re-animates, restarts any timeout, and
186
+ * takes its place at the front of the queue.
174
187
  */
175
188
  update(id: string, options: ToastOptions): void;
176
- /** Dismiss all visible toasts (Chakra's toast.closeAll). */
189
+ /** Dismiss every toast, queued as well as displayed (Chakra's toast.closeAll). */
177
190
  closeAll(): void;
178
191
  }
179
192
 
@@ -184,8 +197,7 @@ export interface ToastFn {
184
197
  */
185
198
  export const useToast = (): ToastFn =>
186
199
  useMemo(() => {
187
- const isActive = (id: string) =>
188
- toastQueue.visibleToasts.some((t) => t.content.id === id);
200
+ const isActive = (id: string) => keysById.has(id);
189
201
  const add = ({
190
202
  id,
191
203
  title,
@@ -198,7 +210,7 @@ export const useToast = (): ToastFn =>
198
210
  if (id && isActive(id)) {
199
211
  return;
200
212
  }
201
- toastQueue.add(
213
+ const key = toastQueue.add(
202
214
  {
203
215
  id,
204
216
  title,
@@ -206,21 +218,37 @@ export const useToast = (): ToastFn =>
206
218
  status,
207
219
  isClosable: isClosable || persistent,
208
220
  },
209
- { timeout: persistent ? undefined : duration ?? 5000 },
221
+ {
222
+ timeout: persistent ? undefined : duration ?? 5000,
223
+ onClose: id
224
+ ? // Only our own entry is ours to drop: an id reused after this
225
+ // toast closed belongs to the later add.
226
+ () => {
227
+ if (keysById.get(id) === key) {
228
+ keysById.delete(id);
229
+ }
230
+ }
231
+ : undefined,
232
+ },
210
233
  );
234
+ if (id) {
235
+ keysById.set(id, key);
236
+ }
211
237
  };
212
238
  const update = (id: string, options: ToastOptions) => {
213
- const existing = toastQueue.visibleToasts.find(
214
- (t) => t.content.id === id,
215
- );
216
- if (existing) {
217
- toastQueue.close(existing.key);
239
+ const key = keysById.get(id);
240
+ if (key !== undefined) {
241
+ toastQueue.close(key);
218
242
  }
219
243
  add({ ...options, id });
220
244
  };
245
+ // clear() empties the whole queue, including the toasts held back by
246
+ // maxVisibleToasts. Closing the visible ones one by one would only
247
+ // promote the queued ones into view. It's also a single update, so the
248
+ // whole set exits in one view transition.
221
249
  const closeAll = () => {
222
- // Copy first: closing mutates visibleToasts as we iterate.
223
- [...toastQueue.visibleToasts].forEach((t) => toastQueue.close(t.key));
250
+ keysById.clear();
251
+ toastQueue.clear();
224
252
  };
225
253
  return Object.assign(add, { isActive, update, closeAll });
226
254
  }, []);
package/src/Tooltip.tsx CHANGED
@@ -40,8 +40,41 @@ export interface TooltipProps {
40
40
  * the trigger (i.e. it is not a RAC component or `Focusable`).
41
41
  */
42
42
  triggerRef?: RefObject<HTMLElement | null>;
43
- /** Hover open delay in ms (RAC default ~1500; pass 0 for instant). */
43
+ /**
44
+ * Hover open delay in ms, defaulting to react-aria's 1500.
45
+ *
46
+ * The delay is per bout of interest, not per control: react-aria keeps a
47
+ * global "warm" flag, so the first tooltip waits and every one after it opens
48
+ * instantly until half a second or so after the last one closes. That is what
49
+ * keeps a row of buttons from firing tooltips at a pointer merely crossing
50
+ * them.
51
+ *
52
+ * **Pass 0 where the tooltip is the label** — an icon-only button, where the
53
+ * text is the only explanation of the glyph and waiting for it reads as
54
+ * broken. Leave it alone where the control already says what it is and the
55
+ * tooltip adds detail.
56
+ */
44
57
  delay?: number;
58
+ /**
59
+ * Close delay in ms, defaulting to react-aria's 500.
60
+ *
61
+ * This is what makes a tooltip hoverable, as WCAG 1.4.13 asks: react-aria
62
+ * puts hover handlers on the tooltip that re-open it, but with an immediate
63
+ * close it has unmounted before the pointer can cross the gap. Chakra closed
64
+ * on mouse-out and we matched that at first; a tooltip that vanishes as you
65
+ * reach for it is not worth the parity. Pass 0 where the delay is wrong for a
66
+ * particular control.
67
+ */
68
+ closeDelay?: number;
69
+ /**
70
+ * Whether pressing the trigger closes the tooltip (RAC default true).
71
+ *
72
+ * react-aria binds this to keydown as well as pointerdown, so with the
73
+ * default *any* key press dismisses the tooltip and only hover or focus
74
+ * brings it back. Pass false where the tooltip's text is the point of the
75
+ * control rather than a hint about an action.
76
+ */
77
+ shouldCloseOnPress?: boolean;
45
78
  css?: SystemStyleObject;
46
79
  }
47
80
 
@@ -58,10 +91,17 @@ export const Tooltip = ({
58
91
  hasArrow,
59
92
  isOpen,
60
93
  triggerRef,
61
- delay = 0,
94
+ delay,
95
+ closeDelay,
96
+ shouldCloseOnPress,
62
97
  css: cssProp,
63
98
  }: TooltipProps) => (
64
- <TooltipTrigger isOpen={isOpen} delay={delay} closeDelay={0}>
99
+ <TooltipTrigger
100
+ isOpen={isOpen}
101
+ delay={delay}
102
+ closeDelay={closeDelay}
103
+ shouldCloseOnPress={shouldCloseOnPress}
104
+ >
65
105
  {children}
66
106
  <RACTooltip
67
107
  triggerRef={triggerRef}
@@ -0,0 +1,230 @@
1
+ /**
2
+ * (c) 2026, Micro:bit Educational Foundation and contributors
3
+ *
4
+ * SPDX-License-Identifier: MIT
5
+ */
6
+ import {
7
+ ReactNode,
8
+ useCallback,
9
+ useContext,
10
+ useEffect,
11
+ useId,
12
+ useRef,
13
+ } from "react";
14
+ import { TooltipTriggerStateContext } from "react-aria-components";
15
+ import { css } from "styled-system/css";
16
+ import { SystemStyleObject } from "styled-system/types";
17
+ import { Button } from "./Button";
18
+ import { Tooltip, TooltipProps } from "./Tooltip";
19
+ import { VisuallyHidden } from "./VisuallyHidden";
20
+
21
+ // A tooltip whose text *is* the point of the control — an information affordance
22
+ // beside a heading, say — rather than a hint about what a button does. That
23
+ // difference drives everything here, because react-aria's tooltips are built for
24
+ // the second case:
25
+ //
26
+ // - They never open on press, since a tooltip isn't a touch pattern. Sighted
27
+ // touch users would have no way in, so this toggles on press.
28
+ // - Their text is associated with the trigger only while open, so touch screen
29
+ // readers (iPadOS VoiceOver, TalkBack) never reach it. The same text is
30
+ // therefore always present on a visually hidden node, named or described from
31
+ // the button. The visible tooltip is aria-hidden to avoid double announcement.
32
+ // - Any key press dismisses them (see Tooltip's shouldCloseOnPress), which for
33
+ // this pattern means a keyboard user can dismiss but never re-open.
34
+ //
35
+ // Everything else is left to react-aria: it opens on hover and on keyboard
36
+ // focus, closes on Escape without disturbing a surrounding dialog, and keeps
37
+ // only one tooltip open at a time across the whole document. Hovering the
38
+ // tooltip to keep it open relies on Tooltip's non-zero close delay, so don't
39
+ // pass `closeDelay={0}` through to it.
40
+
41
+ // How far outside the tooltip the pointer still counts as on it, covering the
42
+ // trigger/tooltip gap and the arrow.
43
+ const pointerMarginPx = 12;
44
+
45
+ const triggerStyle: SystemStyleObject = {
46
+ // The button recipe's size variants set a height and horizontal padding for
47
+ // text buttons; shrink to the glyph so the focus ring is an even circle
48
+ // around it and the control doesn't stretch its row.
49
+ display: "inline-flex",
50
+ alignItems: "center",
51
+ justifyContent: "center",
52
+ height: "auto",
53
+ minHeight: "0",
54
+ minWidth: "0",
55
+ padding: "0",
56
+ lineHeight: "1",
57
+ cursor: "pointer",
58
+ borderRadius: "50%",
59
+ _focusVisible: { focusShadow: "outline" },
60
+ };
61
+
62
+ export interface TooltipButtonProps {
63
+ /**
64
+ * Tooltip body. Also the button's accessible name, or its description when
65
+ * `aria-label` is given.
66
+ */
67
+ label: ReactNode;
68
+ /** Button content, typically an `Icon`. */
69
+ children: ReactNode;
70
+ /**
71
+ * Short accessible name for the button, e.g. "Live graph". Recommended when
72
+ * `label` runs to more than a few words: without it the whole body becomes
73
+ * the button's name, which a screen reader reads out in full.
74
+ */
75
+ "aria-label"?: string;
76
+ placement?: TooltipProps["placement"];
77
+ hasArrow?: boolean;
78
+ /** Style overrides for the tooltip, e.g. padding for a multi-line body. */
79
+ css?: SystemStyleObject;
80
+ /** Style overrides for the button. */
81
+ triggerCss?: SystemStyleObject;
82
+ }
83
+
84
+ /**
85
+ * TooltipButton — a small button, usually an icon, whose tooltip carries
86
+ * information the user needs rather than a hint about an action.
87
+ *
88
+ * Unlike a bare `Tooltip` it works by pointer, keyboard and touch, and its text
89
+ * reaches screen readers on every platform. Use it for an information icon
90
+ * beside a heading or a "partially supported" marker; use `Tooltip` for a hint
91
+ * on a button that does something else.
92
+ *
93
+ * Open question: react-spectrum makes this pattern a popover
94
+ * (`ContextualHelp`), not a tooltip, which would remove the hidden copy of the
95
+ * body and the pointer-geometry keep-alive below rather than work around them.
96
+ * Tracked as microbit-foundation/ui#63, which would deprecate this component;
97
+ * see "Open across the completed migrations" in docs/migration-playbook.md
98
+ * before extending it.
99
+ */
100
+ export const TooltipButton = ({
101
+ label,
102
+ children,
103
+ "aria-label": ariaLabel,
104
+ placement,
105
+ hasArrow,
106
+ css: cssProp,
107
+ triggerCss,
108
+ }: TooltipButtonProps) => {
109
+ const textId = useId();
110
+ const tooltipBodyId = useId();
111
+ return (
112
+ <Tooltip
113
+ label={
114
+ <div id={tooltipBodyId} aria-hidden={true}>
115
+ {label}
116
+ </div>
117
+ }
118
+ placement={placement}
119
+ hasArrow={hasArrow}
120
+ css={cssProp}
121
+ // The tooltip is this button's whole explanation — an icon with a 1.5s
122
+ // wait before anything appears reads as broken — so opt out of the warmup
123
+ // the labelled controls want.
124
+ delay={0}
125
+ shouldCloseOnPress={false}
126
+ >
127
+ <span className={css({ display: "flex" })}>
128
+ <TooltipButtonTrigger
129
+ aria-label={ariaLabel}
130
+ textId={textId}
131
+ tooltipBodyId={tooltipBodyId}
132
+ css={triggerCss}
133
+ >
134
+ {children}
135
+ </TooltipButtonTrigger>
136
+ <VisuallyHidden as="div" id={textId} aria-hidden={true}>
137
+ {label}
138
+ </VisuallyHidden>
139
+ </span>
140
+ </Tooltip>
141
+ );
142
+ };
143
+
144
+ interface TooltipButtonTriggerProps {
145
+ children: ReactNode;
146
+ "aria-label"?: string;
147
+ /** Visually hidden copy of the body, naming or describing the button. */
148
+ textId: string;
149
+ /** The body inside the visible tooltip, used to find it in the document. */
150
+ tooltipBodyId: string;
151
+ css?: SystemStyleObject;
152
+ }
153
+
154
+ /**
155
+ * The button itself, split out so it can read the tooltip's state from context.
156
+ * Being a RAC component it registers itself as the tooltip's trigger — hover,
157
+ * focus and positioning all follow from that, even nested inside the span.
158
+ */
159
+ const TooltipButtonTrigger = ({
160
+ children,
161
+ "aria-label": ariaLabel,
162
+ textId,
163
+ tooltipBodyId,
164
+ css: cssProp,
165
+ }: TooltipButtonTriggerProps) => {
166
+ const state = useContext(TooltipTriggerStateContext);
167
+ const ref = useRef<HTMLButtonElement>(null);
168
+ const handlePress = useCallback(() => {
169
+ if (state?.isOpen) {
170
+ state.close(true);
171
+ } else {
172
+ state?.open(true);
173
+ }
174
+ }, [state]);
175
+ // Hovering the tooltip keeps it open, so it can be read at magnification
176
+ // (WCAG 1.4.13). react-aria does that by re-opening on hover, which fails
177
+ // when the tooltip is portalled into a container a modal has marked inert:
178
+ // it is painted but can never be the target of a mouse event. Pointer
179
+ // geometry works either way — open() clears the pending close.
180
+ //
181
+ // Leaving the tooltip has to close it here too. The trigger's own hover-end
182
+ // fired long ago, when the pointer set off across the gap, so nothing else
183
+ // will. Not while the trigger is hovered or focused, though: those are
184
+ // react-aria's own reasons to be open, and it will close on its own terms.
185
+ const isOpen = state?.isOpen;
186
+ useEffect(() => {
187
+ if (!isOpen) {
188
+ return;
189
+ }
190
+ const listener = (e: MouseEvent) => {
191
+ const rect = document
192
+ .getElementById(tooltipBodyId)
193
+ ?.closest('[role="tooltip"]')
194
+ ?.getBoundingClientRect();
195
+ const onTooltip =
196
+ !!rect &&
197
+ e.clientX >= rect.left - pointerMarginPx &&
198
+ e.clientX <= rect.right + pointerMarginPx &&
199
+ e.clientY >= rect.top - pointerMarginPx &&
200
+ e.clientY <= rect.bottom + pointerMarginPx;
201
+ if (onTooltip) {
202
+ state?.open(true);
203
+ } else if (
204
+ ref.current !== document.activeElement &&
205
+ !ref.current?.matches(":hover")
206
+ ) {
207
+ state?.close();
208
+ }
209
+ };
210
+ document.addEventListener("mousemove", listener);
211
+ return () => document.removeEventListener("mousemove", listener);
212
+ }, [isOpen, state, tooltipBodyId]);
213
+ return (
214
+ <Button
215
+ ref={ref}
216
+ variant="unstyled"
217
+ aria-label={ariaLabel}
218
+ // Without a short name the body is the name; with one it is the
219
+ // description. react-aria overwrites aria-describedby with the visible
220
+ // tooltip's id while open, and that copy is aria-hidden so announces
221
+ // nothing; closed — the state a touch screen reader is in — this applies.
222
+ aria-labelledby={ariaLabel ? undefined : textId}
223
+ aria-describedby={ariaLabel ? textId : undefined}
224
+ onPress={handlePress}
225
+ css={{ ...triggerStyle, ...cssProp }}
226
+ >
227
+ {children}
228
+ </Button>
229
+ );
230
+ };
package/src/index.ts CHANGED
@@ -58,6 +58,7 @@ export * from "./Modal";
58
58
  export * from "./PopoverArrow";
59
59
  export * from "./SharedUIProvider";
60
60
  export * from "./Tooltip";
61
+ export * from "./TooltipButton";
61
62
  export * from "./Toast";
62
63
  export * from "./VisuallyHidden";
63
64
  export { useBreakpointValue } from "./hooks/useBreakpointValue";