@dxos/plugin-navtree 0.7.5-main.499c70c → 0.7.5-main.937ce75

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,24 +2,12 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { useControllableState } from '@radix-ui/react-use-controllable-state';
6
- import React, { type MutableRefObject, type PropsWithChildren, useRef, useState } from 'react';
5
+ import React, { type MutableRefObject, useRef } from 'react';
7
6
 
8
- import { type Action, type ActionLike, type Node } from '@dxos/app-graph';
9
- import { keySymbols } from '@dxos/keyboard';
10
- import {
11
- Button,
12
- Dialog,
13
- DropdownMenu,
14
- ContextMenu,
15
- Tooltip,
16
- useTranslation,
17
- toLocalizedString,
18
- Icon,
19
- } from '@dxos/react-ui';
20
- import { SearchList } from '@dxos/react-ui-searchlist';
21
- import { descriptionText, hoverableControlItem, hoverableOpenControlItem, mx } from '@dxos/react-ui-theme';
22
- import { getHostPlatform } from '@dxos/util';
7
+ import { type Action, type Node } from '@dxos/app-graph';
8
+ import { useTranslation, toLocalizedString, IconButton } from '@dxos/react-ui';
9
+ import { DropdownMenu, type MenuAction } from '@dxos/react-ui-menu';
10
+ import { hoverableControlItem, hoverableOpenControlItem, mx } from '@dxos/react-ui-theme';
23
11
 
24
12
  import { NAVTREE_PLUGIN } from '../meta';
25
13
  import { type ActionProperties } from '../types';
@@ -30,338 +18,87 @@ export type NavTreeItemActionMenuProps = ActionProperties & {
30
18
  monolithic?: boolean;
31
19
  menuActions?: Action[];
32
20
  suppressNextTooltip?: MutableRefObject<boolean>;
33
- menuOpen?: boolean;
34
- defaultMenuOpen?: boolean;
35
- onChangeMenuOpen?: (nextOpen: boolean) => void;
36
21
  onAction?: (action: Action) => void;
37
22
  };
38
23
 
39
- const getShortcut = (action: ActionLike) => {
40
- return typeof action.properties?.keyBinding === 'string'
41
- ? action.properties.keyBinding
42
- : action.properties?.keyBinding?.[getHostPlatform()];
43
- };
44
-
45
24
  const fallbackIcon = 'ph--placeholder--regular';
46
25
 
26
+ const actionButtonProps = {
27
+ iconOnly: true,
28
+ size: 4 as const,
29
+ variant: 'ghost' as const,
30
+ density: 'fine' as const,
31
+ classNames: mx('shrink-0 !pli-2 pointer-fine:!pli-1', hoverableControlItem, hoverableOpenControlItem),
32
+ };
33
+
47
34
  export const NavTreeItemActionDropdownMenu = ({
48
35
  label,
49
36
  icon,
50
37
  testId,
51
- defaultMenuOpen,
52
38
  menuActions,
53
- menuOpen,
54
- suppressNextTooltip,
55
- onChangeMenuOpen,
56
39
  onAction,
57
40
  }: NavTreeItemActionMenuProps) => {
58
41
  const { t } = useTranslation(NAVTREE_PLUGIN);
59
-
60
- const [optionsMenuOpen, setOptionsMenuOpen] = useControllableState({
61
- prop: menuOpen,
62
- defaultProp: defaultMenuOpen,
63
- onChange: onChangeMenuOpen,
64
- });
42
+ const suppressNextTooltip = useRef<boolean>(false);
65
43
 
66
44
  return (
67
45
  <DropdownMenu.Root
68
- {...{
69
- open: optionsMenuOpen,
70
- onOpenChange: (nextOpen: boolean) => {
71
- if (!nextOpen && suppressNextTooltip) {
72
- suppressNextTooltip.current = true;
73
- }
74
- return setOptionsMenuOpen(nextOpen);
75
- },
76
- }}
46
+ actions={menuActions as MenuAction[]}
47
+ onAction={onAction}
48
+ suppressNextTooltip={suppressNextTooltip}
77
49
  >
78
- <Tooltip.Trigger asChild>
79
- <DropdownMenu.Trigger asChild>
80
- <Button
81
- variant='ghost'
82
- density='fine'
83
- classNames={mx('shrink-0 !pli-2 pointer-fine:!pli-1', hoverableControlItem, hoverableOpenControlItem)}
84
- data-testid={testId}
85
- aria-label={t('tree item actions label')}
86
- >
87
- <span className='sr-only'>{toLocalizedString(label, t)}</span>
88
- <Icon icon={icon ?? fallbackIcon} size={4} />
89
- </Button>
90
- </DropdownMenu.Trigger>
91
- </Tooltip.Trigger>
92
- <DropdownMenu.Portal>
93
- <DropdownMenu.Content>
94
- <DropdownMenu.Viewport>
95
- {menuActions?.map((action) => {
96
- const shortcut = getShortcut(action);
97
- return (
98
- <DropdownMenu.Item
99
- key={action.id}
100
- onClick={(event) => {
101
- if (action.properties?.disabled) {
102
- return;
103
- }
104
- event.stopPropagation();
105
- // TODO(thure): Why does Dialog’s modal-ness cause issues if we don’t explicitly close the menu here?
106
- if (suppressNextTooltip) {
107
- suppressNextTooltip.current = true;
108
- }
109
- setOptionsMenuOpen(false);
110
- onAction?.(action);
111
- }}
112
- classNames='gap-2'
113
- disabled={action.properties?.disabled}
114
- {...(action.properties?.testId && { 'data-testid': action.properties.testId })}
115
- >
116
- {action.properties?.icon && <Icon icon={action.properties!.icon} size={4} />}
117
- <span className='grow truncate'>{toLocalizedString(action.properties!.label, t)}</span>
118
- {shortcut && <span className={mx('shrink-0', descriptionText)}>{keySymbols(shortcut).join('')}</span>}
119
- </DropdownMenu.Item>
120
- );
121
- })}
122
- </DropdownMenu.Viewport>
123
- <DropdownMenu.Arrow />
124
- </DropdownMenu.Content>
125
- </DropdownMenu.Portal>
50
+ <DropdownMenu.Trigger asChild>
51
+ <IconButton
52
+ {...actionButtonProps}
53
+ icon={icon ?? fallbackIcon}
54
+ label={toLocalizedString(label, t)}
55
+ data-testid={testId}
56
+ suppressNextTooltip={suppressNextTooltip}
57
+ />
58
+ </DropdownMenu.Trigger>
126
59
  </DropdownMenu.Root>
127
60
  );
128
61
  };
129
62
 
130
- export const NavTreeItemActionContextMenu = (
131
- props: PropsWithChildren<Pick<NavTreeItemActionMenuProps, 'menuActions' | 'onAction'>>,
132
- ) => {
133
- return (props.menuActions?.length ?? 0) > 0 ? <NavTreeItemActionContextMenuImpl {...props} /> : <>{props.children}</>;
134
- };
135
-
136
- const NavTreeItemActionContextMenuImpl = ({
137
- menuActions,
138
- onAction,
139
- children,
140
- }: PropsWithChildren<Pick<NavTreeItemActionMenuProps, 'menuActions' | 'onAction'>>) => {
141
- const { t } = useTranslation(NAVTREE_PLUGIN);
142
-
143
- return (
144
- <ContextMenu.Root>
145
- <ContextMenu.Trigger asChild>{children}</ContextMenu.Trigger>
146
- <ContextMenu.Portal>
147
- <ContextMenu.Content>
148
- <ContextMenu.Viewport>
149
- {menuActions?.map((action) => {
150
- const shortcut = getShortcut(action);
151
- return (
152
- <ContextMenu.Item
153
- key={action.id}
154
- onClick={(event) => {
155
- if (action.properties?.disabled) {
156
- return;
157
- }
158
- event.stopPropagation();
159
- onAction?.(action);
160
- }}
161
- classNames='gap-2'
162
- disabled={action.properties?.disabled}
163
- {...(action.properties?.testId && { 'data-testid': action.properties.testId })}
164
- >
165
- {action.properties?.icon && <Icon icon={action.properties?.icon} size={4} />}
166
- <span className='grow truncate'>{toLocalizedString(action.properties!.label, t)}</span>
167
- {shortcut && <span className={mx('shrink-0', descriptionText)}>{keySymbols(shortcut).join('')}</span>}
168
- </ContextMenu.Item>
169
- );
170
- })}
171
- </ContextMenu.Viewport>
172
- <ContextMenu.Arrow />
173
- </ContextMenu.Content>
174
- </ContextMenu.Portal>
175
- </ContextMenu.Root>
176
- );
177
- };
178
-
179
- export const NavTreeItemActionSearchList = ({
180
- menuActions,
181
- icon,
182
- label,
183
- testId,
184
- suppressNextTooltip,
185
- onAction,
186
- }: Pick<
187
- NavTreeItemActionMenuProps,
188
- 'icon' | 'menuActions' | 'testId' | 'label' | 'onAction' | 'suppressNextTooltip'
189
- >) => {
190
- const { t } = useTranslation(NAVTREE_PLUGIN);
191
-
192
- const [optionsMenuOpen, setOptionsMenuOpen] = useState(false);
193
- const button = useRef<HTMLButtonElement | null>(null);
194
-
195
- // TODO(burdon): Optionally sort.
196
- const sortedActions = menuActions?.sort(({ properties: p1 }, { properties: p2 }) => {
197
- const t1 = toLocalizedString(p1!.label, t).toLowerCase();
198
- const t2 = toLocalizedString(p2!.label, t).toLowerCase();
199
- return t1.localeCompare(t2);
200
- });
201
-
202
- // TODO(thure): Use LayoutPlugin’s global Dialog.
203
- return (
204
- <Dialog.Root
205
- {...{
206
- open: optionsMenuOpen,
207
- onOpenChange: (nextOpen: boolean) => {
208
- if (!nextOpen && suppressNextTooltip) {
209
- suppressNextTooltip.current = true;
210
- }
211
- return setOptionsMenuOpen(nextOpen);
212
- },
213
- }}
214
- >
215
- <Tooltip.Trigger asChild>
216
- <Dialog.Trigger asChild>
217
- <Button
218
- variant='ghost'
219
- density='fine'
220
- classNames={mx('shrink-0 !pli-2 pointer-fine:!pli-1', hoverableControlItem, hoverableOpenControlItem)}
221
- data-testid={testId}
222
- onKeyDownCapture={(event) => {
223
- // TODO(thure): Why is only this `Button` affected and not `DropdownMenu.Trigger`’s?
224
- if (button.current && button.current === event.target) {
225
- switch (event.key) {
226
- case 'Enter':
227
- case ' ':
228
- // Capture this event and stop propagation to the drag start handler.
229
- event.stopPropagation();
230
- setOptionsMenuOpen(true);
231
- break;
232
- default:
233
- }
234
- }
235
- }}
236
- ref={button}
237
- >
238
- <Icon icon={icon ?? fallbackIcon} size={4} />
239
- </Button>
240
- </Dialog.Trigger>
241
- </Tooltip.Trigger>
242
- <Dialog.Portal>
243
- <Dialog.Overlay>
244
- <Dialog.Content classNames='is-full max-is-[24rem] pli-2 plb-1'>
245
- <SearchList.Root label={t('tree item searchlist input placeholder')}>
246
- <SearchList.Input placeholder={t('tree item searchlist input placeholder')} classNames='pli-3' />
247
- <SearchList.Content classNames='min-bs-[12rem] bs-[50dvh] max-bs-[30rem] overflow-auto'>
248
- {sortedActions?.map((action) => {
249
- const label = toLocalizedString(action.properties!.label, t);
250
- const shortcut = getShortcut(action);
251
- return (
252
- <SearchList.Item
253
- // TODO(burdon): Value should be id since label is not unique.
254
- value={label}
255
- key={action.id}
256
- onSelect={() => {
257
- if (action.properties?.disabled) {
258
- return;
259
- }
260
- if (suppressNextTooltip) {
261
- suppressNextTooltip.current = true;
262
- }
263
- setOptionsMenuOpen(false);
264
- onAction?.(action);
265
- }}
266
- classNames='flex items-center gap-2 pli-2'
267
- disabled={action.properties?.disabled}
268
- {...(action.properties?.testId && { 'data-testid': action.properties.testId })}
269
- >
270
- {action.properties?.icon && <Icon icon={action.properties?.icon} size={4} />}
271
- <span className='grow truncate'>{label}</span>
272
- {shortcut && (
273
- <span className={mx('shrink-0', descriptionText)}>{keySymbols(shortcut).join('')}</span>
274
- )}
275
- </SearchList.Item>
276
- );
277
- })}
278
- </SearchList.Content>
279
- <div role='none' className='flex items-center plb-2 pli-3'>
280
- <span className={descriptionText}>{toLocalizedString(label, t)}</span>
281
- </div>
282
- </SearchList.Root>
283
- </Dialog.Content>
284
- </Dialog.Overlay>
285
- </Dialog.Portal>
286
- </Dialog.Root>
287
- );
288
- };
289
-
290
63
  export const NavTreeItemMonolithicAction = ({
291
64
  parent,
292
- properties: { disabled, caller, testId, label, icon } = { label: 'never' },
65
+ properties: { disabled, caller, testId, icon } = { label: 'never' },
293
66
  data: invoke,
294
- }: Action & { parent: Node; onAction?: (action: Action) => void }) => {
295
- const { t } = useTranslation(NAVTREE_PLUGIN);
67
+ baseLabel,
68
+ }: Action & { parent: Node; onAction?: (action: Action) => void; baseLabel: string }) => {
296
69
  return (
297
- <Tooltip.Trigger asChild>
298
- <Button
299
- variant='ghost'
300
- density='fine'
301
- classNames={mx('shrink-0 !pli-2 pointer-fine:!pli-1', hoverableControlItem, hoverableOpenControlItem)}
302
- disabled={disabled}
303
- onClick={(event) => {
304
- if (disabled) {
305
- return;
306
- }
307
- event.stopPropagation();
308
- void invoke?.(caller ? { node: parent, caller } : { node: parent });
309
- }}
310
- data-testid={testId}
311
- >
312
- <span className='sr-only'>{toLocalizedString(label, t)}</span>
313
- <Icon icon={icon ?? fallbackIcon} size={4} />
314
- </Button>
315
- </Tooltip.Trigger>
70
+ <IconButton
71
+ {...actionButtonProps}
72
+ icon={icon ?? fallbackIcon}
73
+ label={baseLabel}
74
+ disabled={disabled}
75
+ onClick={(event) => {
76
+ if (disabled) {
77
+ return;
78
+ }
79
+ event.stopPropagation();
80
+ void invoke?.(caller ? { node: parent, caller } : { node: parent });
81
+ }}
82
+ data-testid={testId}
83
+ />
316
84
  );
317
85
  };
318
86
 
319
87
  export const NavTreeItemAction = ({ monolithic, menuActions, parent: node, ...props }: NavTreeItemActionMenuProps) => {
320
88
  const { t } = useTranslation(NAVTREE_PLUGIN);
321
- const suppressNextTooltip = useRef<boolean>(false);
322
- const [triggerTooltipOpen, setTriggerTooltipOpen] = useState(false);
323
89
 
324
90
  const monolithicAction = menuActions?.length === 1 && menuActions[0];
325
91
  const baseLabel = toLocalizedString(monolithicAction ? monolithicAction.properties!.label : props.label, t);
326
92
 
327
- return (
328
- <Tooltip.Root
329
- open={triggerTooltipOpen}
330
- onOpenChange={(nextOpen) => {
331
- if (suppressNextTooltip.current) {
332
- setTriggerTooltipOpen(false);
333
- suppressNextTooltip.current = false;
334
- } else {
335
- setTriggerTooltipOpen(nextOpen);
336
- }
337
- }}
338
- >
339
- {baseLabel && (
340
- <Tooltip.Portal>
341
- <Tooltip.Content classNames='z-[31]' side='bottom'>
342
- {baseLabel}
343
- <Tooltip.Arrow />
344
- </Tooltip.Content>
345
- </Tooltip.Portal>
346
- )}
347
- {monolithic && menuActions?.length === 1 ? (
348
- <NavTreeItemMonolithicAction parent={node} {...menuActions[0]} />
349
- ) : props.menuType === 'searchList' ? (
350
- <NavTreeItemActionSearchList
351
- {...props}
352
- menuActions={menuActions}
353
- suppressNextTooltip={suppressNextTooltip}
354
- onAction={(action) => action.data?.(props.caller ? { node, caller: props.caller } : { node })}
355
- />
356
- ) : (
357
- <NavTreeItemActionDropdownMenu
358
- {...props}
359
- parent={node}
360
- menuActions={menuActions}
361
- suppressNextTooltip={suppressNextTooltip}
362
- onAction={(action) => action.data?.(props.caller ? { node, caller: props.caller } : { node })}
363
- />
364
- )}
365
- </Tooltip.Root>
93
+ return monolithic && menuActions?.length === 1 ? (
94
+ <NavTreeItemMonolithicAction baseLabel={baseLabel} parent={node} {...menuActions[0]} />
95
+ ) : (
96
+ <NavTreeItemActionDropdownMenu
97
+ {...props}
98
+ label={baseLabel}
99
+ parent={node}
100
+ menuActions={menuActions}
101
+ onAction={(action) => action.data?.(props.caller ? { node, caller: props.caller } : { node })}
102
+ />
366
103
  );
367
104
  };
@@ -20,7 +20,6 @@ export default [
20
20
  'see release label': 'See release on GitHub',
21
21
  'powered by dxos message': 'Powered by <dxos>DXOS</dxos>',
22
22
  'tree item actions label': 'More actions',
23
- 'tree item searchlist input placeholder': 'Search for actions…',
24
23
  'empty branch message': ' ',
25
24
  },
26
25
  },
package/src/types.ts CHANGED
@@ -47,7 +47,7 @@ export type ActionProperties = SharedProperties & {
47
47
  disposition?: string;
48
48
  hidden?: boolean;
49
49
  caller?: string;
50
- menuType?: 'searchList' | 'dropdown';
50
+ menuType?: 'dropdown';
51
51
  keyBinding?: string | KeyBinding;
52
52
  };
53
53