@c9up/nebula 0.2.1 → 0.2.2

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.
Files changed (40) hide show
  1. package/README.md +11 -9
  2. package/dist/atoms/AccessibleIcon.d.ts +24 -0
  3. package/dist/atoms/AccessibleIcon.js +27 -0
  4. package/dist/atoms/VisuallyHidden.d.ts +30 -0
  5. package/dist/atoms/VisuallyHidden.js +35 -0
  6. package/dist/atoms/index.d.ts +2 -0
  7. package/dist/atoms/index.js +2 -0
  8. package/dist/index.d.ts +7 -4
  9. package/dist/index.js +7 -4
  10. package/dist/molecules/Toolbar.d.ts +50 -0
  11. package/dist/molecules/Toolbar.js +70 -0
  12. package/dist/molecules/index.d.ts +7 -5
  13. package/dist/molecules/index.js +7 -5
  14. package/dist/organisms/Toaster.d.ts +78 -10
  15. package/dist/organisms/Toaster.js +146 -20
  16. package/dist/organisms/index.d.ts +1 -1
  17. package/dist/primitives/direction.d.ts +38 -0
  18. package/dist/primitives/direction.js +57 -0
  19. package/dist/primitives/floating.js +1 -12
  20. package/dist/primitives/focusable.d.ts +10 -0
  21. package/dist/primitives/focusable.js +10 -1
  22. package/dist/primitives/index.d.ts +2 -1
  23. package/dist/primitives/index.js +2 -1
  24. package/dist/primitives/rovingFocus.js +14 -7
  25. package/nebula.css +1 -1
  26. package/package.json +1 -1
  27. package/registry.json +63 -0
  28. package/src/atoms/AccessibleIcon.ts +39 -0
  29. package/src/atoms/VisuallyHidden.ts +45 -0
  30. package/src/atoms/index.ts +8 -0
  31. package/src/index.ts +7 -4
  32. package/src/molecules/Toolbar.ts +100 -0
  33. package/src/molecules/index.ts +14 -5
  34. package/src/organisms/Toaster.ts +207 -33
  35. package/src/organisms/index.ts +2 -0
  36. package/src/primitives/direction.ts +61 -0
  37. package/src/primitives/floating.ts +2 -12
  38. package/src/primitives/focusable.ts +10 -1
  39. package/src/primitives/index.ts +7 -0
  40. package/src/primitives/rovingFocus.ts +18 -5
@@ -1,9 +1,23 @@
1
1
  /**
2
2
  * Toaster — transient notifications.
3
3
  *
4
- * shadcn delegates this to `sonner`. nebula keeps it in-house, and the shape
5
- * is the same: one region mounted once near the app root, and a `toast()`
6
- * function callable from anywhere with no reference to it.
4
+ * shadcn ships two: `sonner`, which wraps a third-party library, and `toast`,
5
+ * its own, built on Base UI. nebula keeps it in-house with zero dependencies
6
+ * and follows the SECOND — the shape is the same: one region mounted once near
7
+ * the app root, and a `toast` object callable from anywhere with no reference
8
+ * to it.
9
+ *
10
+ * NAMED DEVIATIONS from Base UI's manager, all deliberate:
11
+ *
12
+ * - `show` / `dismiss` / `variant` / `duration` rather than `add` / `close` /
13
+ * `type` / `timeout`. `variant` is what every other nebula component calls
14
+ * this (Button, Alert, Badge), and renaming it here alone would make the
15
+ * package less consistent with itself than with Base UI.
16
+ * - `variant` keeps `warning`, which Base UI's `type` does not have.
17
+ * - No `priority` (display ordering), `data` (typed payload for a React render
18
+ * prop — a `Child` already covers it) or `positionerProps` (anchored toasts).
19
+ * - No `updateKey`. It exists to make React replay an animation without
20
+ * remounting; `update` here replaces the entry, and aurora re-renders it.
7
21
  *
8
22
  * That decoupling is the whole design. A queue in module state, a signal the
9
23
  * region subscribes to, and `toast.error(…)` works from a route handler's
@@ -23,10 +37,16 @@
23
37
  */
24
38
  import { component, html, signal } from "@c9up/aurora";
25
39
  import { cn } from "../lib/cn.js";
26
- import { XIcon } from "../lib/icons.js";
40
+ import { LoaderIcon, XIcon } from "../lib/icons.js";
27
41
  import { fadeInOut } from "../lib/motion.js";
28
42
  import { read } from "../lib/props.js";
29
43
  const DEFAULT_DURATION_MS = 5000;
44
+ /** How long a variant stays when the caller did not say. */
45
+ function defaultDuration(variant) {
46
+ if (variant === "loading")
47
+ return 0;
48
+ return variant === "error" ? DEFAULT_DURATION_MS * 2 : DEFAULT_DURATION_MS;
49
+ }
30
50
  const queue = signal([]);
31
51
  const timers = new Map();
32
52
  /** When each toast is due to disappear, as an epoch timestamp. */
@@ -35,10 +55,29 @@ const deadlines = new Map();
35
55
  const remainders = new Map();
36
56
  let nextId = 0;
37
57
  function dismiss(id) {
58
+ const leaving = queue().find((entry) => entry.id === id);
38
59
  clearTimer(id);
39
60
  deadlines.delete(id);
40
61
  remainders.delete(id);
41
62
  queue(queue().filter((entry) => entry.id !== id));
63
+ notifyClosed(leaving);
64
+ }
65
+ /**
66
+ * Run a toast's `onClose`, without letting it take the caller down.
67
+ *
68
+ * The callback belongs to application code and runs from a timer, a click and
69
+ * `clear()` alike. One that throws would otherwise break whichever of those
70
+ * happened to be running — a dismissal that leaves the queue half-updated.
71
+ */
72
+ function notifyClosed(entry) {
73
+ if (entry?.onClose === undefined)
74
+ return;
75
+ try {
76
+ entry.onClose();
77
+ }
78
+ catch (error) {
79
+ console.error("[nebula] a toast onClose callback threw", error);
80
+ }
42
81
  }
43
82
  function clearTimer(id) {
44
83
  const timer = timers.get(id);
@@ -77,17 +116,67 @@ function resumeAll(minimumMs) {
77
116
  remainders.clear();
78
117
  }
79
118
  function show(options) {
80
- nextId += 1;
81
- const id = nextId;
82
- const entry = {
83
- ...options,
84
- id,
85
- variant: options.variant ?? "default",
86
- };
119
+ // A supplied id that is already on screen updates rather than stacks.
120
+ if (options.id !== undefined && has(options.id)) {
121
+ update(options.id, options);
122
+ return options.id;
123
+ }
124
+ let id;
125
+ if (options.id === undefined) {
126
+ nextId += 1;
127
+ id = nextId;
128
+ }
129
+ else {
130
+ id = options.id;
131
+ }
132
+ const variant = options.variant ?? "default";
133
+ const entry = { ...options, id, variant };
87
134
  queue([...queue(), entry]);
88
- schedule(id, options.duration ?? DEFAULT_DURATION_MS);
135
+ schedule(id, options.duration ?? defaultDuration(variant));
89
136
  return id;
90
137
  }
138
+ function has(id) {
139
+ return queue().some((entry) => entry.id === id);
140
+ }
141
+ /**
142
+ * Change a toast that is already on screen. No-op if it has gone.
143
+ *
144
+ * The countdown is recomputed, because that is the part a caller would not
145
+ * think to ask for and always wants: a `loading` toast has no timer at all, so
146
+ * turning it into a success without rescheduling would leave it on screen for
147
+ * good. Which is exactly what {@link toast.promise} does to it.
148
+ */
149
+ function update(id, patch) {
150
+ const current = queue().find((entry) => entry.id === id);
151
+ if (current === undefined)
152
+ return;
153
+ const variant = patch.variant ?? current.variant;
154
+ const next = { ...current, ...patch, id, variant };
155
+ queue(queue().map((entry) => (entry.id === id ? next : entry)));
156
+ clearTimer(id);
157
+ deadlines.delete(id);
158
+ remainders.delete(id);
159
+ schedule(id, patch.duration ?? next.duration ?? defaultDuration(variant));
160
+ }
161
+ function optionsOf(message) {
162
+ // A plain object with a `title` is the options form; anything else — a
163
+ // string, a number, a template — is the title itself. Checked on `title`
164
+ // rather than on the shape in general, because a template result IS an
165
+ // object and would otherwise be mistaken for options.
166
+ if (typeof message === "object" &&
167
+ message !== null &&
168
+ "title" in message &&
169
+ !Array.isArray(message)) {
170
+ return message;
171
+ }
172
+ return { title: message };
173
+ }
174
+ function resolveMessage(message, value) {
175
+ if (typeof message === "function") {
176
+ return optionsOf(message(value));
177
+ }
178
+ return optionsOf(message);
179
+ }
91
180
  /**
92
181
  * Raise a toast. Returns its id, which `dismiss` accepts.
93
182
  *
@@ -95,17 +184,45 @@ function show(options) {
95
184
  */
96
185
  export const toast = {
97
186
  show,
187
+ update,
98
188
  dismiss,
99
189
  success: (title, description) => show({ title, description, variant: "success" }),
100
- error: (title, description) =>
101
- // Errors stay twice as long: the user has to read them, and often act.
102
- show({
103
- title,
104
- description,
105
- variant: "error",
106
- duration: DEFAULT_DURATION_MS * 2,
107
- }),
190
+ error: (title, description) => show({ title, description, variant: "error" }),
108
191
  warning: (title, description) => show({ title, description, variant: "warning" }),
192
+ loading: (title, description) => show({ title, description, variant: "loading" }),
193
+ /**
194
+ * One toast for the whole life of an async call: waiting, then the outcome.
195
+ *
196
+ * ```ts
197
+ * await toast.promise(save(form), {
198
+ * loading: 'Saving…',
199
+ * success: (saved) => `Saved ${saved.name}`,
200
+ * error: (reason) => ({ title: 'Could not save', description: String(reason) }),
201
+ * })
202
+ * ```
203
+ *
204
+ * Returns the SAME promise it was given, rejection included. It reports, it
205
+ * does not handle: swallowing the rejection here would turn a failed call
206
+ * into a red toast and nothing else, with the caller's own error path never
207
+ * running.
208
+ */
209
+ promise(work, messages) {
210
+ const id = show({ ...optionsOf(messages.loading), variant: "loading" });
211
+ // A separate chain, whose own rejection IS handled — so reporting the
212
+ // failure never adds an unhandled rejection of its own.
213
+ work.then((value) => {
214
+ update(id, {
215
+ ...resolveMessage(messages.success, value),
216
+ variant: "success",
217
+ });
218
+ }, (reason) => {
219
+ update(id, {
220
+ ...resolveMessage(messages.error, reason),
221
+ variant: "error",
222
+ });
223
+ });
224
+ return work;
225
+ },
109
226
  /** Clear everything — on navigation, say. */
110
227
  clear() {
111
228
  for (const timer of timers.values())
@@ -113,11 +230,17 @@ export const toast = {
113
230
  timers.clear();
114
231
  deadlines.clear();
115
232
  remainders.clear();
233
+ const leaving = queue();
116
234
  queue([]);
235
+ // After the queue is empty, so a callback that raises a toast of its own
236
+ // is not swept away by the clear that called it.
237
+ for (const entry of leaving)
238
+ notifyClosed(entry);
117
239
  },
118
240
  };
119
241
  const variantClasses = {
120
242
  default: "bg-popover text-popover-foreground border",
243
+ loading: "bg-popover text-muted-foreground border",
121
244
  success: "bg-popover text-popover-foreground border-l-4 border-l-primary border",
122
245
  error: "bg-popover text-destructive border-l-4 border-l-destructive border",
123
246
  warning: "bg-popover text-popover-foreground border-l-4 border-l-chart-4 border",
@@ -163,6 +286,9 @@ function renderToast(entry) {
163
286
  data-state="open"
164
287
  class="${cn("pointer-events-auto mt-2 flex w-full items-start gap-3 rounded-md p-4 shadow-lg", variantClasses[entry.variant], fadeInOut)}"
165
288
  >
289
+ ${entry.variant === "loading"
290
+ ? LoaderIcon({ class: "size-4 mt-0.5 shrink-0 animate-spin" })
291
+ : null}
166
292
  <div class="flex-1 text-sm">
167
293
  <div class="font-medium">${entry.title}</div>
168
294
  ${entry.description === undefined
@@ -36,5 +36,5 @@ export { type Answer, type Answers, type FreeformQuestion, isAnswered, type Mult
36
36
  export { Select, SelectContent, type SelectContentProps, SelectGroup, type SelectGroupProps, SelectItem, type SelectItemProps, SelectLabel, type SelectLabelProps, type SelectProps, type SelectScrollButtonProps, SelectScrollDownButton, SelectScrollUpButton, SelectSeparator, type SelectSeparatorProps, SelectTrigger, type SelectTriggerProps, SelectValue, type SelectValueProps, selectContentClasses, selectTriggerClasses, } from "./Select.js";
37
37
  export { Sheet, SheetClose, type SheetCloseProps, SheetContent, type SheetContentProps, SheetDescription, SheetFooter, SheetHeader, SheetOverlay, type SheetOverlayProps, type SheetProps, type SheetSectionProps, SheetTitle, type SheetTitleProps, SheetTrigger, type SheetTriggerProps, } from "./Sheet.js";
38
38
  export { Sidebar, type SidebarActionProps, type SidebarApi, SidebarContent, SidebarFooter, SidebarGroup, SidebarGroupAction, SidebarGroupContent, SidebarGroupLabel, SidebarHeader, SidebarInput, type SidebarInputProps, SidebarInset, type SidebarInsetProps, SidebarMenu, SidebarMenuAction, SidebarMenuBadge, SidebarMenuButton, type SidebarMenuButtonProps, type SidebarMenuButtonVariants, SidebarMenuItem, SidebarMenuSkeleton, type SidebarMenuSkeletonProps, SidebarMenuSub, SidebarMenuSubButton, type SidebarMenuSubButtonProps, SidebarMenuSubItem, type SidebarProps, SidebarProvider, type SidebarProviderProps, SidebarRail, type SidebarRailProps, SidebarSeparator, type SidebarSeparatorProps, SidebarTrigger, type SidebarTriggerProps, sidebarCollapsed, sidebarMenuButtonVariants, sidebarState, useSidebar, } from "./Sidebar.js";
39
- export { Toaster, type ToasterProps, type ToastOptions, type ToastVariant, toast, } from "./Toaster.js";
39
+ export { Toaster, type ToasterProps, type ToastId, type ToastMessage, type ToastOptions, type ToastVariant, toast, } from "./Toaster.js";
40
40
  export { Tooltip, TooltipContent, type TooltipContentProps, type TooltipProps, TooltipProvider, type TooltipProviderProps, TooltipTrigger, type TooltipTriggerProps, } from "./Tooltip.js";
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Reading direction — the one place that answers "which way is forward?".
3
+ *
4
+ * Radix ships a `DirectionProvider` and threads the answer through React
5
+ * context. Aurora has a context, but a provider would be the wrong mechanism
6
+ * here: the direction is already on the page. `<html dir="rtl">`, a `dir`
7
+ * attribute on one panel, a stylesheet setting `direction` — all of them are
8
+ * answered by the computed style, and none of them requires the application to
9
+ * remember to wrap anything. A provider would be a second source of truth that
10
+ * silently disagrees with the first.
11
+ *
12
+ * The distinction that matters to callers is PHYSICAL versus LOGICAL. An arrow
13
+ * key is physical: ArrowRight means the key to the right, always. "The next
14
+ * item" is logical, and in a right-to-left list the next item is to the LEFT.
15
+ * `forward()` converts one into the other, and is the whole of this module's
16
+ * job.
17
+ */
18
+ export type Direction = "ltr" | "rtl";
19
+ /**
20
+ * The direction this element is laid out in.
21
+ *
22
+ * The computed style rather than the `dir` attribute, so an element inheriting
23
+ * from `<html dir="rtl">` answers correctly without every anchor carrying the
24
+ * attribute itself. `ltr` wherever there is no layout to ask — a server
25
+ * render, a jsdom without styles — which is also the correct default.
26
+ */
27
+ export declare function directionOf(element: Element | null | undefined): Direction;
28
+ /** Shorthand for the check that reads better at a call site than `=== "rtl"`. */
29
+ export declare function isRightToLeft(element: Element | null | undefined): boolean;
30
+ /**
31
+ * Which way a horizontal arrow key actually moves.
32
+ *
33
+ * `+1` for the next item, `-1` for the previous. ArrowRight is `+1` in a
34
+ * left-to-right widget and `-1` in a right-to-left one — that mirroring is the
35
+ * entire reason this exists, and getting it wrong makes every menu, tab list
36
+ * and toolbar navigate backwards for half the world's readers.
37
+ */
38
+ export declare function forward(key: "ArrowLeft" | "ArrowRight", element: Element | null | undefined): -1 | 1;
@@ -0,0 +1,57 @@
1
+ /// <reference lib="dom" />
2
+ /**
3
+ * Reading direction — the one place that answers "which way is forward?".
4
+ *
5
+ * Radix ships a `DirectionProvider` and threads the answer through React
6
+ * context. Aurora has a context, but a provider would be the wrong mechanism
7
+ * here: the direction is already on the page. `<html dir="rtl">`, a `dir`
8
+ * attribute on one panel, a stylesheet setting `direction` — all of them are
9
+ * answered by the computed style, and none of them requires the application to
10
+ * remember to wrap anything. A provider would be a second source of truth that
11
+ * silently disagrees with the first.
12
+ *
13
+ * The distinction that matters to callers is PHYSICAL versus LOGICAL. An arrow
14
+ * key is physical: ArrowRight means the key to the right, always. "The next
15
+ * item" is logical, and in a right-to-left list the next item is to the LEFT.
16
+ * `forward()` converts one into the other, and is the whole of this module's
17
+ * job.
18
+ */
19
+ /**
20
+ * The direction this element is laid out in.
21
+ *
22
+ * The computed style rather than the `dir` attribute, so an element inheriting
23
+ * from `<html dir="rtl">` answers correctly without every anchor carrying the
24
+ * attribute itself. `ltr` wherever there is no layout to ask — a server
25
+ * render, a jsdom without styles — which is also the correct default.
26
+ */
27
+ export function directionOf(element) {
28
+ if (element === null || element === undefined)
29
+ return "ltr";
30
+ if (typeof getComputedStyle !== "function")
31
+ return "ltr";
32
+ try {
33
+ return getComputedStyle(element).direction === "rtl" ? "rtl" : "ltr";
34
+ }
35
+ catch {
36
+ // A detached node in some environments. Not a reason to fail a keypress.
37
+ return "ltr";
38
+ }
39
+ }
40
+ /** Shorthand for the check that reads better at a call site than `=== "rtl"`. */
41
+ export function isRightToLeft(element) {
42
+ return directionOf(element) === "rtl";
43
+ }
44
+ /**
45
+ * Which way a horizontal arrow key actually moves.
46
+ *
47
+ * `+1` for the next item, `-1` for the previous. ArrowRight is `+1` in a
48
+ * left-to-right widget and `-1` in a right-to-left one — that mirroring is the
49
+ * entire reason this exists, and getting it wrong makes every menu, tab list
50
+ * and toolbar navigate backwards for half the world's readers.
51
+ */
52
+ export function forward(key, element) {
53
+ const rightIsForward = !isRightToLeft(element);
54
+ if (key === "ArrowRight")
55
+ return rightIsForward ? 1 : -1;
56
+ return rightIsForward ? -1 : 1;
57
+ }
@@ -24,6 +24,7 @@
24
24
  * clipping ancestor between it and the root; an absolutely positioned popover
25
25
  * has to find an offset parent, and gets it wrong the moment one appears.
26
26
  */
27
+ import { isRightToLeft } from "./direction.js";
27
28
  const DEFAULT_OFFSET = 4;
28
29
  const DEFAULT_PADDING = 8;
29
30
  /**
@@ -285,18 +286,6 @@ export function autoPosition(anchor, floating, options = {}) {
285
286
  },
286
287
  };
287
288
  }
288
- /**
289
- * Is this element laid out right to left?
290
- *
291
- * The computed style rather than the `dir` attribute, so an element inheriting
292
- * the direction from `<html dir="rtl">` is answered correctly without every
293
- * anchor having to carry the attribute itself.
294
- */
295
- function isRightToLeft(element) {
296
- if (typeof getComputedStyle !== "function")
297
- return false;
298
- return getComputedStyle(element).direction === "rtl";
299
- }
300
289
  /**
301
290
  * Watch elements for size changes, where the platform supports it.
302
291
  *
@@ -11,6 +11,16 @@
11
11
  * layout engine, which is why `isVisible` reads `offsetParent` and computed
12
12
  * styles rather than trusting the markup.
13
13
  */
14
+ /**
15
+ * The standard tabbable set.
16
+ *
17
+ * Exported because a composite widget that navigates "whatever controls are in
18
+ * here" — a toolbar — needs the same definition, and a second copy next door
19
+ * is how the two drift into disagreeing about `summary` or `contenteditable`.
20
+ * It is a selector, not an answer: `isFocusable` below is still what decides,
21
+ * because a selector cannot see `display: none` or a collapsed `<details>`.
22
+ */
23
+ export declare const FOCUSABLE_SELECTOR: string;
14
24
  /**
15
25
  * Is the element rendered and interactive right now?
16
26
  *
@@ -12,7 +12,16 @@
12
12
  * layout engine, which is why `isVisible` reads `offsetParent` and computed
13
13
  * styles rather than trusting the markup.
14
14
  */
15
- const FOCUSABLE_SELECTOR = [
15
+ /**
16
+ * The standard tabbable set.
17
+ *
18
+ * Exported because a composite widget that navigates "whatever controls are in
19
+ * here" — a toolbar — needs the same definition, and a second copy next door
20
+ * is how the two drift into disagreeing about `summary` or `contenteditable`.
21
+ * It is a selector, not an answer: `isFocusable` below is still what decides,
22
+ * because a selector cannot see `display: none` or a collapsed `<details>`.
23
+ */
24
+ export const FOCUSABLE_SELECTOR = [
16
25
  "a[href]",
17
26
  "area[href]",
18
27
  "button:not([disabled])",
@@ -13,10 +13,11 @@
13
13
  * subtly different implementation next door.
14
14
  */
15
15
  export { type Controllable, type ControllableOptions, controllable, } from "./controllable.js";
16
+ export { type Direction, directionOf, forward, isRightToLeft, } from "./direction.js";
16
17
  export { type DismissableLayer, type DismissableOptions, type DismissReason, dismissable, layerCount, } from "./dismissable.js";
17
18
  export { type Align, type AutoPosition, type AutoPositionOptions, autoPosition, type Placement, type Position, type PositionOptions, type Rect, resolvePosition, type Side, type Viewport, } from "./floating.js";
18
19
  export { type FloatingSurfaceOptions, floatingSurface, } from "./floatingSurface.js";
19
- export { firstFocusable, focusableWithin, focusSilently, isFocusable, isVisible, } from "./focusable.js";
20
+ export { FOCUSABLE_SELECTOR, firstFocusable, focusableWithin, focusSilently, isFocusable, isVisible, } from "./focusable.js";
20
21
  export { type FocusTrap, type FocusTrapOptions, focusTrap, } from "./focusTrap.js";
21
22
  export { type MenuAction, type MenuCheckbox, type MenuEntry, type MenuLabel, type MenuRadioGroup, type MenuSeparator, type MenuSubmenu, menuPanel, menuPanelClasses, wireMenu, } from "./menu.js";
22
23
  export { type ModalSurfaceOptions, modalSurface } from "./modalSurface.js";
@@ -13,10 +13,11 @@
13
13
  * subtly different implementation next door.
14
14
  */
15
15
  export { controllable, } from "./controllable.js";
16
+ export { directionOf, forward, isRightToLeft, } from "./direction.js";
16
17
  export { dismissable, layerCount, } from "./dismissable.js";
17
18
  export { autoPosition, resolvePosition, } from "./floating.js";
18
19
  export { floatingSurface, } from "./floatingSurface.js";
19
- export { firstFocusable, focusableWithin, focusSilently, isFocusable, isVisible, } from "./focusable.js";
20
+ export { FOCUSABLE_SELECTOR, firstFocusable, focusableWithin, focusSilently, isFocusable, isVisible, } from "./focusable.js";
20
21
  export { focusTrap, } from "./focusTrap.js";
21
22
  export { menuPanel, menuPanelClasses, wireMenu, } from "./menu.js";
22
23
  export { modalSurface } from "./modalSurface.js";
@@ -18,6 +18,7 @@
18
18
  * the moment they change. The query costs a few microseconds per keypress,
19
19
  * which is not a budget worth optimising against correctness.
20
20
  */
21
+ import { forward } from "./direction.js";
21
22
  import { focusSilently, isVisible } from "./focusable.js";
22
23
  const DEFAULT_ITEM_SELECTOR = "[data-nebula-item]";
23
24
  export function rovingFocus(options) {
@@ -99,7 +100,7 @@ export function rovingFocus(options) {
99
100
  // A modifier means the user is talking to the browser, not the widget.
100
101
  if (event.altKey || event.ctrlKey || event.metaKey)
101
102
  return;
102
- const direction = directionFor(event.key, orientation);
103
+ const direction = directionFor(event.key, orientation, container);
103
104
  if (direction !== 0) {
104
105
  event.preventDefault();
105
106
  move(direction);
@@ -142,18 +143,24 @@ export function rovingFocus(options) {
142
143
  },
143
144
  };
144
145
  }
145
- /** `-1` for previous, `+1` for next, `0` when the key is not ours. */
146
- function directionFor(key, orientation) {
146
+ /**
147
+ * `-1` for previous, `+1` for next, `0` when the key is not ours.
148
+ *
149
+ * The horizontal pair is mirrored in a right-to-left group: ArrowRight means
150
+ * the key to the right, and in an RTL list the item to the right is the
151
+ * PREVIOUS one. Vertical never mirrors — `dir` does not turn a page upside
152
+ * down.
153
+ */
154
+ function directionFor(key, orientation, container) {
147
155
  const vertical = orientation === "vertical" || orientation === "both";
148
156
  const horizontal = orientation === "horizontal" || orientation === "both";
149
157
  if (vertical && key === "ArrowDown")
150
158
  return 1;
151
159
  if (vertical && key === "ArrowUp")
152
160
  return -1;
153
- if (horizontal && key === "ArrowRight")
154
- return 1;
155
- if (horizontal && key === "ArrowLeft")
156
- return -1;
161
+ if (horizontal && (key === "ArrowRight" || key === "ArrowLeft")) {
162
+ return forward(key, container);
163
+ }
157
164
  return 0;
158
165
  }
159
166
  /**