@workbench-kit/react 0.0.2-prototype.0.2.10 → 0.0.2-prototype.0.2.11

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.
@@ -23,6 +23,10 @@ import { TextInput } from '../../primitives/text-input';
23
23
  import { cxCodicon } from '../../utils/codicon';
24
24
  import { explorerTreeDepthStyle } from './explorer-tree-style';
25
25
  import { flattenWorkspaceTree } from './tree';
26
+ import {
27
+ resolveWorkspaceExplorerHorizontalNavigationAction,
28
+ resolveWorkspaceExplorerNavigationPath,
29
+ } from './workspaceExplorerKeyboard.js';
26
30
  import { WorkspaceFileIcon } from './WorkspaceFileIcon';
27
31
  import type { WorkspaceTreeNode } from './types';
28
32
 
@@ -30,7 +34,7 @@ export const WORKSPACE_EXPLORER_DRAG_DATA_TYPE = 'application/x-newchobo-ui-work
30
34
  export const WORKSPACE_EXPLORER_DRAG_METADATA_DATA_TYPE = `${WORKSPACE_EXPLORER_DRAG_DATA_TYPE}.metadata`;
31
35
 
32
36
  export type WorkspaceExplorerSelectionChangeReason =
33
- 'clear' | 'click' | 'context-menu' | 'drag-start' | 'folder-click';
37
+ 'clear' | 'click' | 'context-menu' | 'drag-start' | 'folder-click' | 'keyboard';
34
38
 
35
39
  export interface WorkspaceExplorerSelectionChangeMeta {
36
40
  event?: DragEvent<HTMLButtonElement> | MouseEvent<HTMLButtonElement>;
@@ -91,6 +95,8 @@ export interface WorkspaceExplorerInlineEditCommitMeta {
91
95
 
92
96
  export interface WorkspaceExplorerProps {
93
97
  activePath?: string;
98
+ /** Accessible name for the `role="tree"` list. Defaults to "Workspace files". */
99
+ ariaLabel?: string;
94
100
  dragDataType?: string;
95
101
  dragMetadataDataType?: string;
96
102
  dragMetadataFactory?: WorkspaceExplorerDragMetadataFactory;
@@ -120,10 +126,16 @@ export interface WorkspaceExplorerProps {
120
126
  renderItemActions?: (node: WorkspaceTreeNode, meta: WorkspaceExplorerItemActionMeta) => ReactNode;
121
127
  selectedPaths?: Iterable<string>;
122
128
  selectionAnchorPath?: string;
129
+ /**
130
+ * When true (default), Arrow/Home/End focus updates also emit selection
131
+ * (file → single `paths`; folder → `{ focusedPath, paths: [] }`).
132
+ */
133
+ selectionFollowsFocus?: boolean;
123
134
  }
124
135
 
125
136
  export function WorkspaceExplorer({
126
137
  activePath,
138
+ ariaLabel = 'Workspace files',
127
139
  dragDataType = WORKSPACE_EXPLORER_DRAG_DATA_TYPE,
128
140
  dragMetadataDataType = WORKSPACE_EXPLORER_DRAG_METADATA_DATA_TYPE,
129
141
  dragMetadataFactory,
@@ -146,6 +158,7 @@ export function WorkspaceExplorer({
146
158
  renderItemActions,
147
159
  selectedPaths = [],
148
160
  selectionAnchorPath,
161
+ selectionFollowsFocus = true,
149
162
  }: WorkspaceExplorerProps) {
150
163
  const sectionBaseDepth = useSidebarSectionBaseDepth();
151
164
  const draggedPathsRef = useRef<string[]>([]);
@@ -311,7 +324,85 @@ export function WorkspaceExplorer({
311
324
  onItemContextMenu?.(event, node, meta);
312
325
  };
313
326
 
327
+ const focusPath = (path: string, node: WorkspaceTreeNode | undefined) => {
328
+ if (!onSelectionChange || !selectionFollowsFocus) {
329
+ if (onSelectionChange) {
330
+ onSelectionChange(
331
+ {
332
+ ...currentSelection,
333
+ focusedPath: path,
334
+ paths: node?.type === 'folder' ? [] : currentSelection.paths,
335
+ },
336
+ { mode: 'single', node, reason: 'keyboard' },
337
+ );
338
+ }
339
+ return;
340
+ }
341
+
342
+ if (node?.type === 'folder') {
343
+ onSelectionChange(
344
+ { ...currentSelection, focusedPath: path, paths: [] },
345
+ { mode: 'single', node, reason: 'keyboard' },
346
+ );
347
+ return;
348
+ }
349
+
350
+ onSelectionChange(
351
+ {
352
+ ...updateWorkspaceSelection({
353
+ mode: 'single',
354
+ orderedPaths: visibleFilePaths,
355
+ selection: currentSelection,
356
+ targetPath: path,
357
+ }),
358
+ focusedPath: path,
359
+ },
360
+ { mode: 'single', node, reason: 'keyboard' },
361
+ );
362
+ };
363
+
314
364
  const handleItemKeyDown = (event: KeyboardEvent<HTMLButtonElement>, node: WorkspaceTreeNode) => {
365
+ if (
366
+ event.key === 'ArrowDown' ||
367
+ event.key === 'ArrowUp' ||
368
+ event.key === 'Home' ||
369
+ event.key === 'End'
370
+ ) {
371
+ const nextPath = resolveWorkspaceExplorerNavigationPath(
372
+ visibleNodes,
373
+ focusedPath ?? node.path,
374
+ event.key,
375
+ );
376
+ if (!nextPath || nextPath === (focusedPath ?? node.path)) {
377
+ return;
378
+ }
379
+ event.preventDefault();
380
+ const nextNode = visibleNodes.find((row) => row.node.path === nextPath)?.node;
381
+ focusPath(nextPath, nextNode);
382
+ return;
383
+ }
384
+
385
+ if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
386
+ const action = resolveWorkspaceExplorerHorizontalNavigationAction(
387
+ visibleNodes,
388
+ focusedPath ?? node.path,
389
+ expandedPaths,
390
+ event.key,
391
+ { filterActive: Boolean(filterQuery.trim()) },
392
+ );
393
+ if (!action) {
394
+ return;
395
+ }
396
+ event.preventDefault();
397
+ if (action.type === 'toggle') {
398
+ onToggleFolder(action.path);
399
+ return;
400
+ }
401
+ const nextNode = visibleNodes.find((row) => row.node.path === action.path)?.node;
402
+ focusPath(action.path, nextNode);
403
+ return;
404
+ }
405
+
315
406
  if (event.key !== 'Delete' && event.key !== 'F2') return;
316
407
 
317
408
  const meta = getItemActionMeta(node);
@@ -506,8 +597,10 @@ export function WorkspaceExplorer({
506
597
  return (
507
598
  <SideBarList
508
599
  fill
509
- aria-label="Workspace files"
600
+ aria-keyshortcuts="ArrowLeft ArrowRight ArrowUp ArrowDown Home End Delete F2"
601
+ aria-label={ariaLabel}
510
602
  dropTarget={dropTargetPath === ''}
603
+ role="tree"
511
604
  onContextMenu={(event) => {
512
605
  onBackgroundContextMenu?.(event);
513
606
  }}
@@ -520,7 +613,7 @@ export function WorkspaceExplorer({
520
613
  }
521
614
  }}
522
615
  >
523
- {visibleNodes.map(({ depth, node }) => {
616
+ {visibleNodes.map(({ depth, node }, index) => {
524
617
  const isFolder = node.type === 'folder';
525
618
  const expanded = expandedPaths.has(node.path) || Boolean(filterQuery.trim());
526
619
  const isFocused = focusedPath === node.path;
@@ -528,6 +621,7 @@ export function WorkspaceExplorer({
528
621
  const dropTarget = isFolder && dropTargetPath === node.path;
529
622
  const editingRename =
530
623
  inlineEdit?.kind.startsWith('rename') && inlineEdit.path === node.path;
624
+ const tabIndex = isFocused || (!focusedPath && index === 0) ? 0 : -1;
531
625
 
532
626
  return editingRename ? (
533
627
  renderInlineEdit({
@@ -553,6 +647,13 @@ export function WorkspaceExplorer({
553
647
  draggable={Boolean(onRequestMove)}
554
648
  dropTarget={dropTarget}
555
649
  selected={selected}
650
+ tabIndex={tabIndex}
651
+ wrapperProps={{
652
+ role: 'treeitem',
653
+ 'aria-level': depth + 1,
654
+ 'aria-selected': selected,
655
+ ...(isFolder ? { 'aria-expanded': expanded } : {}),
656
+ }}
556
657
  onClick={(event) => {
557
658
  if (isFolder) {
558
659
  selectFolder(event, node);
@@ -0,0 +1,109 @@
1
+ import { parentPathOf } from '@workbench-kit/workspace';
2
+
3
+ import type { WorkspaceTreeNode } from './types.js';
4
+
5
+ export type WorkspaceExplorerVisibleRow = {
6
+ readonly depth: number;
7
+ readonly node: WorkspaceTreeNode;
8
+ };
9
+
10
+ export type WorkspaceExplorerVerticalNavigationKey = 'ArrowDown' | 'ArrowUp' | 'Home' | 'End';
11
+
12
+ export type WorkspaceExplorerHorizontalNavigationKey = 'ArrowLeft' | 'ArrowRight';
13
+
14
+ export type WorkspaceExplorerHorizontalNavigationAction =
15
+ | { readonly type: 'toggle'; readonly path: string }
16
+ | { readonly type: 'focus'; readonly path: string };
17
+
18
+ function rowIndexByPath(
19
+ rows: readonly WorkspaceExplorerVisibleRow[],
20
+ path: string | undefined,
21
+ ): number {
22
+ if (!path) {
23
+ return -1;
24
+ }
25
+ return rows.findIndex((row) => row.node.path === path);
26
+ }
27
+
28
+ /**
29
+ * Resolve the next focused path for vertical tree navigation over visible
30
+ * (already collapse-filtered) explorer rows.
31
+ */
32
+ export function resolveWorkspaceExplorerNavigationPath(
33
+ rows: readonly WorkspaceExplorerVisibleRow[],
34
+ focusedPath: string | undefined,
35
+ key: WorkspaceExplorerVerticalNavigationKey,
36
+ ): string | null {
37
+ if (rows.length === 0) {
38
+ return null;
39
+ }
40
+
41
+ const currentIndex = rowIndexByPath(rows, focusedPath);
42
+ const fallbackIndex = currentIndex >= 0 ? currentIndex : 0;
43
+
44
+ if (key === 'Home') {
45
+ return rows[0]?.node.path ?? null;
46
+ }
47
+ if (key === 'End') {
48
+ return rows[rows.length - 1]?.node.path ?? null;
49
+ }
50
+ if (key === 'ArrowDown') {
51
+ return rows[Math.min(rows.length - 1, fallbackIndex + 1)]?.node.path ?? null;
52
+ }
53
+ return rows[Math.max(0, fallbackIndex - 1)]?.node.path ?? null;
54
+ }
55
+
56
+ function firstChildPath(
57
+ rows: readonly WorkspaceExplorerVisibleRow[],
58
+ parentPath: string,
59
+ ): string | null {
60
+ return rows.find((row) => parentPathOf(row.node.path) === parentPath)?.node.path ?? null;
61
+ }
62
+
63
+ /**
64
+ * Resolve expand/collapse/parent focus for ArrowLeft/ArrowRight.
65
+ * Call `onToggleFolder` only for `toggle` actions (toggle-only host API).
66
+ */
67
+ export function resolveWorkspaceExplorerHorizontalNavigationAction(
68
+ rows: readonly WorkspaceExplorerVisibleRow[],
69
+ focusedPath: string | undefined,
70
+ expandedPaths: ReadonlySet<string>,
71
+ key: WorkspaceExplorerHorizontalNavigationKey,
72
+ options: { readonly filterActive?: boolean } = {},
73
+ ): WorkspaceExplorerHorizontalNavigationAction | null {
74
+ const currentIndex = rowIndexByPath(rows, focusedPath);
75
+ const current = rows[currentIndex >= 0 ? currentIndex : 0];
76
+ if (!current) {
77
+ return null;
78
+ }
79
+
80
+ const { node } = current;
81
+ const filterActive = options.filterActive ?? false;
82
+ const isFolder = node.type === 'folder';
83
+ const expanded = isFolder && (expandedPaths.has(node.path) || filterActive);
84
+
85
+ if (key === 'ArrowRight') {
86
+ if (!isFolder) {
87
+ return null;
88
+ }
89
+ if (!expanded && !filterActive) {
90
+ return { type: 'toggle', path: node.path };
91
+ }
92
+ const childPath = firstChildPath(rows, node.path);
93
+ return childPath ? { type: 'focus', path: childPath } : null;
94
+ }
95
+
96
+ // ArrowLeft
97
+ if (isFolder && expanded && !filterActive) {
98
+ return { type: 'toggle', path: node.path };
99
+ }
100
+
101
+ const parentPath = parentPathOf(node.path);
102
+ if (!parentPath) {
103
+ return null;
104
+ }
105
+ if (rowIndexByPath(rows, parentPath) < 0) {
106
+ return null;
107
+ }
108
+ return { type: 'focus', path: parentPath };
109
+ }