@dxos/react-ui-attention 0.8.4-main.d05539e30a → 0.8.4-main.d9fc60f731

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": "@dxos/react-ui-attention",
3
- "version": "0.8.4-main.d05539e30a",
3
+ "version": "0.8.4-main.d9fc60f731",
4
4
  "description": "Signifier components and providers for attention system.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -46,30 +46,31 @@
46
46
  "@radix-ui/react-primitive": "2.0.2",
47
47
  "@radix-ui/react-slot": "1.1.2",
48
48
  "@radix-ui/react-use-controllable-state": "1.1.0",
49
- "effect": "3.20.0",
50
- "@dxos/echo": "0.8.4-main.d05539e30a",
51
- "@dxos/invariant": "0.8.4-main.d05539e30a",
52
- "@dxos/log": "0.8.4-main.d05539e30a",
53
- "@dxos/react-hooks": "0.8.4-main.d05539e30a",
54
- "@dxos/util": "0.8.4-main.d05539e30a"
49
+ "effect": "3.21.2",
50
+ "@dxos/echo": "0.8.4-main.d9fc60f731",
51
+ "@dxos/keyboard": "0.8.4-main.d9fc60f731",
52
+ "@dxos/log": "0.8.4-main.d9fc60f731",
53
+ "@dxos/react-hooks": "0.8.4-main.d9fc60f731",
54
+ "@dxos/util": "0.8.4-main.d9fc60f731",
55
+ "@dxos/invariant": "0.8.4-main.d9fc60f731"
55
56
  },
56
57
  "devDependencies": {
57
58
  "@types/react": "~19.2.7",
58
59
  "@types/react-dom": "~19.2.3",
59
60
  "react": "~19.2.3",
60
61
  "react-dom": "~19.2.3",
61
- "vite": "^8.0.13",
62
- "@dxos/random": "0.8.4-main.d05539e30a",
63
- "@dxos/storybook-utils": "0.8.4-main.d05539e30a",
64
- "@dxos/react-ui": "0.8.4-main.d05539e30a",
65
- "@dxos/ui-theme": "0.8.4-main.d05539e30a"
62
+ "vite": "^8.0.14",
63
+ "@dxos/random": "0.8.4-main.d9fc60f731",
64
+ "@dxos/storybook-utils": "0.8.4-main.d9fc60f731",
65
+ "@dxos/react-ui": "0.8.4-main.d9fc60f731",
66
+ "@dxos/ui-theme": "0.8.4-main.d9fc60f731"
66
67
  },
67
68
  "peerDependencies": {
68
- "effect": "3.20.0",
69
+ "effect": "3.21.2",
69
70
  "react": "~19.2.3",
70
71
  "react-dom": "~19.2.3",
71
- "@dxos/react-ui": "0.8.4-main.d05539e30a",
72
- "@dxos/ui-theme": "0.8.4-main.d05539e30a"
72
+ "@dxos/react-ui": "0.8.4-main.d9fc60f731",
73
+ "@dxos/ui-theme": "0.8.4-main.d9fc60f731"
73
74
  },
74
75
  "publishConfig": {
75
76
  "access": "public"
@@ -11,12 +11,12 @@ import { mx } from '@dxos/ui-theme';
11
11
  const attentionGlyphStyles = mx(
12
12
  'inline-block rounded-xs w-3 h-3 bg-transparent text-accent-text transition-colors',
13
13
  '[[data-contains-attended=true]_&]:bg-attention-contains',
14
- '[[data-attention=true]_&]:bg-accent-surface',
15
- '[[data-attention=true]_&]:text-accent-foreground',
16
- '[[aria-current][data-attention=true]_&]:bg-accent-surface',
17
- '[[aria-current][data-attention=true]_&]:text-accent-foreground',
18
- '[[aria-selected="true"][data-attention=true]_&]:bg-accent-surface',
19
- '[[aria-selected="true"][data-attention=true]_&]:text-accent-foreground',
14
+ '[[data-attention=true]_&]:bg-accent-bg',
15
+ '[[data-attention=true]_&]:text-accent-fg',
16
+ '[[aria-current][data-attention=true]_&]:bg-accent-bg',
17
+ '[[aria-current][data-attention=true]_&]:text-accent-fg',
18
+ '[[aria-selected="true"][data-attention=true]_&]:bg-accent-bg',
19
+ '[[aria-selected="true"][data-attention=true]_&]:text-accent-fg',
20
20
  );
21
21
 
22
22
  const presenceIconStyles = mx('w-3 h-3');
package/src/index.ts CHANGED
@@ -5,3 +5,4 @@
5
5
  export * from './attention';
6
6
  export * from './components';
7
7
  export * from './selection';
8
+ export * from './useArticleKeyboardNavigation';
@@ -0,0 +1,50 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { describe, test } from 'vitest';
6
+
7
+ import { advance } from './useArticleKeyboardNavigation';
8
+
9
+ describe('advance', () => {
10
+ const ids = ['a', 'b', 'c', 'd'];
11
+
12
+ test('returns undefined for an empty list', ({ expect }) => {
13
+ expect(advance({ ids: [], currentId: undefined, delta: 1 })).to.equal(undefined);
14
+ expect(advance({ ids: [], currentId: 'x', delta: -1 })).to.equal(undefined);
15
+ });
16
+
17
+ test('with no current selection, delta=+1 enters at the first item', ({ expect }) => {
18
+ expect(advance({ ids, currentId: undefined, delta: 1 })).to.equal('a');
19
+ });
20
+
21
+ test('with no current selection, delta=-1 enters at the last item', ({ expect }) => {
22
+ expect(advance({ ids, currentId: undefined, delta: -1 })).to.equal('d');
23
+ });
24
+
25
+ test('advances forward from the middle of the list', ({ expect }) => {
26
+ expect(advance({ ids, currentId: 'b', delta: 1 })).to.equal('c');
27
+ });
28
+
29
+ test('advances backward from the middle of the list', ({ expect }) => {
30
+ expect(advance({ ids, currentId: 'c', delta: -1 })).to.equal('b');
31
+ });
32
+
33
+ test('clamps at the last item when pressing next', ({ expect }) => {
34
+ expect(advance({ ids, currentId: 'd', delta: 1 })).to.equal('d');
35
+ });
36
+
37
+ test('clamps at the first item when pressing previous', ({ expect }) => {
38
+ expect(advance({ ids, currentId: 'a', delta: -1 })).to.equal('a');
39
+ });
40
+
41
+ test('treats an unknown currentId like no selection', ({ expect }) => {
42
+ expect(advance({ ids, currentId: 'unknown', delta: 1 })).to.equal('a');
43
+ expect(advance({ ids, currentId: 'unknown', delta: -1 })).to.equal('d');
44
+ });
45
+
46
+ test('single-item list clamps both directions to that item', ({ expect }) => {
47
+ expect(advance({ ids: ['only'], currentId: 'only', delta: 1 })).to.equal('only');
48
+ expect(advance({ ids: ['only'], currentId: 'only', delta: -1 })).to.equal('only');
49
+ });
50
+ });
@@ -0,0 +1,116 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { useEffect, useMemo } from 'react';
6
+
7
+ import { Keyboard } from '@dxos/keyboard';
8
+
9
+ import { useAttention } from './components';
10
+
11
+ /**
12
+ * Compute the id to select after pressing 'j' (delta = 1) or 'k' (delta = -1).
13
+ * Clamps at the list ends — pressing 'j' on the last item keeps the last item
14
+ * selected (vim-style), and 'k' on the first item keeps the first.
15
+ *
16
+ * When no current selection exists, returns the first id (for delta = 1) or the
17
+ * last id (for delta = -1) so the user can enter the list with either key.
18
+ */
19
+ export const advance = ({
20
+ ids,
21
+ currentId,
22
+ delta,
23
+ }: {
24
+ ids: readonly string[];
25
+ currentId: string | undefined;
26
+ delta: 1 | -1;
27
+ }): string | undefined => {
28
+ if (ids.length === 0) {
29
+ return undefined;
30
+ }
31
+
32
+ const idx = currentId ? ids.indexOf(currentId) : -1;
33
+ if (idx === -1) {
34
+ return delta === 1 ? ids[0] : ids[ids.length - 1];
35
+ }
36
+
37
+ const next = Math.max(0, Math.min(ids.length - 1, idx + delta));
38
+ return ids[next];
39
+ };
40
+
41
+ export type UseArticleKeyboardNavigationOptions<T> = {
42
+ /** Stable id for the article (used as the keyboard context path). */
43
+ articleId: string;
44
+ /** Ordered list of selectable items as they appear in the article. */
45
+ items: readonly T[];
46
+ /** Currently-selected id, if any. */
47
+ currentId: string | undefined;
48
+ /** Extracts the id from an item. Defaults to `item.id`. */
49
+ getId?: (item: T) => string;
50
+ /** Called with the id to select when the user presses 'j' or 'k'. */
51
+ onSelect: (id: string) => void;
52
+ };
53
+
54
+ /**
55
+ * Wire 'j' (next) and 'k' (previous) keyboard shortcuts for an article that
56
+ * navigates a list of items (e.g., messages, events). Bindings are scoped to
57
+ * the article's keyboard context so they only fire while the article has attention.
58
+ *
59
+ * Pass the items directly; ids are derived via `getId` (defaults to `item.id`).
60
+ * Clamps at list boundaries. Active only when the article is attended.
61
+ */
62
+ export const useArticleKeyboardNavigation: {
63
+ <T extends { id: string }>(options: UseArticleKeyboardNavigationOptions<T>): void;
64
+ <T>(options: UseArticleKeyboardNavigationOptions<T> & { getId: (item: T) => string }): void;
65
+ } = <T>({ articleId, items, currentId, getId, onSelect }: UseArticleKeyboardNavigationOptions<T>): void => {
66
+ const { hasAttention } = useAttention(articleId);
67
+
68
+ // `getId` is optional only when `T extends { id: string }` (enforced by the overloads above),
69
+ // so the fallback is sound; the cast bridges the generic erased by the implementation signature.
70
+ const ids = useMemo(() => items.map((item) => (getId ? getId(item) : (item as { id: string }).id)), [items, getId]);
71
+
72
+ useEffect(() => {
73
+ if (!hasAttention) {
74
+ return;
75
+ }
76
+
77
+ const context = Keyboard.singleton.getContext(articleId);
78
+ const prevContext = Keyboard.singleton.getCurrentContext();
79
+ Keyboard.singleton.setCurrentContext(articleId);
80
+
81
+ const nextBinding = {
82
+ shortcut: 'j',
83
+ handler: () => {
84
+ const target = advance({ ids, currentId, delta: 1 });
85
+ if (target !== undefined) {
86
+ onSelect(target);
87
+ }
88
+ },
89
+ data: 'Next item',
90
+ disableInput: true,
91
+ };
92
+ const prevBinding = {
93
+ shortcut: 'k',
94
+ handler: () => {
95
+ const target = advance({ ids, currentId, delta: -1 });
96
+ if (target !== undefined) {
97
+ onSelect(target);
98
+ }
99
+ },
100
+ data: 'Previous item',
101
+ disableInput: true,
102
+ };
103
+
104
+ context.bind(nextBinding);
105
+ context.bind(prevBinding);
106
+
107
+ return () => {
108
+ context.unbind('j');
109
+ context.unbind('k');
110
+ // Restore the prior context if we were the ones who set it.
111
+ if (Keyboard.singleton.getCurrentContext() === articleId) {
112
+ Keyboard.singleton.setCurrentContext(prevContext);
113
+ }
114
+ };
115
+ }, [articleId, ids, currentId, onSelect, hasAttention]);
116
+ };