@react-spectrum/tree 3.1.11 → 3.2.0

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/src/TreeView.tsx DELETED
@@ -1,345 +0,0 @@
1
- /*
2
- * Copyright 2024 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import {AriaTreeProps} from '@react-aria/tree';
14
- import {
15
- ButtonContext,
16
- Tree,
17
- TreeItem,
18
- TreeItemContent,
19
- TreeItemContentProps,
20
- TreeItemContentRenderProps,
21
- TreeItemProps,
22
- TreeItemRenderProps,
23
- TreeRenderProps,
24
- useContextProps
25
- } from 'react-aria-components';
26
- import {Checkbox} from '@react-spectrum/checkbox';
27
- import ChevronLeftMedium from '@spectrum-icons/ui/ChevronLeftMedium';
28
- import ChevronRightMedium from '@spectrum-icons/ui/ChevronRightMedium';
29
- import {DOMRef, Expandable, Key, SelectionBehavior, SpectrumSelectionProps, StyleProps} from '@react-types/shared';
30
- import {focusRing, style} from '@react-spectrum/style-macro-s1' with {type: 'macro'};
31
- import {isAndroid} from '@react-aria/utils';
32
- import React, {createContext, JSX, JSXElementConstructor, ReactElement, ReactNode, useRef} from 'react';
33
- import {SlotProvider, useDOMRef, useStyleProps} from '@react-spectrum/utils';
34
- import {useButton} from '@react-aria/button';
35
- import {useLocale} from '@react-aria/i18n';
36
-
37
- export interface SpectrumTreeViewProps<T> extends Omit<AriaTreeProps<T>, 'children' | 'render'>, StyleProps, SpectrumSelectionProps, Expandable {
38
- /** Provides content to display when there are no items in the tree. */
39
- renderEmptyState?: () => JSX.Element,
40
- /**
41
- * Handler that is called when a user performs an action on an item. The exact user event depends on
42
- * the collection's `selectionStyle` prop and the interaction modality.
43
- */
44
- onAction?: (key: Key) => void,
45
- /**
46
- * The contents of the tree.
47
- */
48
- children?: ReactNode | ((item: T) => ReactNode)
49
- }
50
-
51
- export interface SpectrumTreeViewItemProps extends Omit<TreeItemProps, 'className' | 'style' | 'render' | 'value' | 'onHoverStart' | 'onHoverEnd' | 'onHoverChange' | 'onClick'> {
52
- /** Rendered contents of the tree item or child items. */
53
- children: ReactNode
54
- }
55
-
56
- interface TreeRendererContextValue {
57
- renderer?: (item) => ReactElement<any, string | JSXElementConstructor<any>>
58
- }
59
- const TreeRendererContext = createContext<TreeRendererContextValue>({});
60
-
61
- // TODO: add animations for rows appearing and disappearing
62
-
63
- // TODO: the below is needed so the borders of the top and bottom row isn't cut off if the TreeView is wrapped within a container by always reserving the 2px needed for the
64
- // keyboard focus ring. Perhaps find a different way of rendering the outlines since the top of the item doesn't
65
- // scroll into view due to how the ring is offset. Alternatively, have the tree render the top/bottom outline like it does in Listview
66
- const tree = style<Pick<TreeRenderProps, 'isEmpty'>>({
67
- ...focusRing(),
68
- outlineOffset: '[-2px]', // make certain we are visible inside overflow hidden containers
69
- height: 'full',
70
- width: 'full',
71
- borderWidth: 2,
72
- boxSizing: 'border-box',
73
- borderXWidth: 0,
74
- borderStyle: 'solid',
75
- borderColor: {
76
- default: 'transparent',
77
- forcedColors: 'Background'
78
- },
79
- justifyContent: {
80
- isEmpty: 'center'
81
- },
82
- alignItems: {
83
- isEmpty: 'center'
84
- },
85
- display: {
86
- isEmpty: 'flex'
87
- },
88
- overflow: 'auto'
89
- });
90
-
91
- /**
92
- * A tree view provides users with a way to navigate nested hierarchical information.
93
- */
94
- export const TreeView = React.forwardRef(function TreeView<T extends object>(props: SpectrumTreeViewProps<T>, ref: DOMRef<HTMLDivElement>) {
95
- let {children, selectionStyle, UNSAFE_className} = props;
96
-
97
- let renderer;
98
- if (typeof children === 'function') {
99
- renderer = children;
100
- }
101
-
102
- let {styleProps} = useStyleProps(props);
103
- let domRef = useDOMRef(ref);
104
- let selectionBehavior = selectionStyle === 'highlight' ? 'replace' : 'toggle';
105
-
106
- return (
107
- <TreeRendererContext.Provider value={{renderer}}>
108
- <Tree {...props} {...styleProps} className={renderProps => (UNSAFE_className ?? '') + tree(renderProps)} selectionBehavior={selectionBehavior as SelectionBehavior} ref={domRef}>
109
- {props.children}
110
- </Tree>
111
- </TreeRendererContext.Provider>
112
- );
113
- }) as <T>(props: SpectrumTreeViewProps<T> & {ref?: DOMRef<HTMLDivElement>}) => ReactElement;
114
-
115
- interface TreeRowRenderProps extends TreeItemRenderProps {
116
- isLink?: boolean
117
- }
118
-
119
- const treeRow = style<TreeRowRenderProps>({
120
- position: 'relative',
121
- display: 'flex',
122
- height: 10,
123
- width: 'full',
124
- boxSizing: 'border-box',
125
- fontSize: 'base',
126
- fontWeight: 'normal',
127
- lineHeight: 200,
128
- color: 'body',
129
- outlineStyle: 'none',
130
- cursor: {
131
- default: 'default',
132
- isLink: 'pointer'
133
- },
134
- // TODO: not sure where to get the equivalent colors here, for instance isHovered is spectrum 600 with 10% opacity but I don't think that exists in the theme
135
- backgroundColor: {
136
- isHovered: '[var(--spectrum-table-row-background-color-hover)]',
137
- isFocusVisibleWithin: '[var(--spectrum-table-row-background-color-hover)]',
138
- isPressed: '[var(--spectrum-table-row-background-color-down)]',
139
- isSelected: '[var(--spectrum-table-row-background-color-selected)]'
140
- }
141
- });
142
-
143
- const treeCellGrid = style({
144
- display: 'grid',
145
- width: 'full',
146
- alignItems: 'center',
147
- gridTemplateColumns: ['minmax(0, auto)', 'minmax(0, auto)', 'minmax(0, auto)', 10, 'minmax(0, auto)', '1fr', 'minmax(0, auto)', 'auto'],
148
- gridTemplateRows: '1fr',
149
- gridTemplateAreas: [
150
- 'drag-handle checkbox level-padding expand-button icon content actions actionmenu'
151
- ],
152
- color: {
153
- isDisabled: {
154
- default: 'gray-400',
155
- forcedColors: 'GrayText'
156
- }
157
- }
158
- });
159
-
160
- // TODO: These styles lose against the spectrum class names, so I've did unsafe for the ones that get overridden
161
- const treeCheckbox = style({
162
- gridArea: 'checkbox',
163
- transitionDuration: '160ms',
164
- marginStart: 3,
165
- marginEnd: 0
166
- });
167
-
168
- const treeIcon = style({
169
- gridArea: 'icon',
170
- marginEnd: 2
171
- });
172
-
173
- const treeContent = style<Pick<TreeItemContentRenderProps, 'isDisabled'>>({
174
- gridArea: 'content',
175
- textOverflow: 'ellipsis',
176
- whiteSpace: 'nowrap',
177
- overflow: 'hidden'
178
- });
179
-
180
- const treeActions = style({
181
- gridArea: 'actions',
182
- flexGrow: 0,
183
- flexShrink: 0,
184
- /* TODO: I made this one up, confirm desired behavior. These paddings are to make sure the action group has enough padding for the focus ring */
185
- marginStart: .5,
186
- marginEnd: 1
187
- });
188
-
189
- const treeActionMenu = style({
190
- gridArea: 'actionmenu',
191
- width: 8
192
- });
193
-
194
- const treeRowOutline = style({
195
- content: '',
196
- display: 'block',
197
- position: 'absolute',
198
- insetStart: 0,
199
- insetEnd: 0,
200
- top: {
201
- default: 0,
202
- isFocusVisible: '[-2px]',
203
- isSelected: {
204
- default: '[-1px]',
205
- isFocusVisible: '[-2px]'
206
- },
207
- isFirst: 0
208
- },
209
- bottom: 0,
210
- pointerEvents: 'none',
211
- forcedColorAdjust: 'none',
212
-
213
- boxShadow: {
214
- isFocusVisible: '[inset 2px 0 0 0 var(--spectrum-alias-focus-color), inset -2px 0 0 0 var(--spectrum-alias-focus-color), inset 0 -2px 0 0 var(--spectrum-alias-focus-color), inset 0 2px 0 0 var(--spectrum-alias-focus-color)]',
215
- isSelected: {
216
- default: '[inset 1px 0 0 0 var(--spectrum-alias-focus-color), inset -1px 0 0 0 var(--spectrum-alias-focus-color), inset 0 -1px 0 0 var(--spectrum-alias-focus-color), inset 0 1px 0 0 var(--spectrum-alias-focus-color)]',
217
- isFocusVisible: '[inset 2px 0 0 0 var(--spectrum-alias-focus-color), inset -2px 0 0 0 var(--spectrum-alias-focus-color), inset 0 -2px 0 0 var(--spectrum-alias-focus-color), inset 0 2px 0 0 var(--spectrum-alias-focus-color)]'
218
- },
219
- forcedColors: {
220
- isFocusVisible: '[inset 2px 0 0 0 Highlight, inset -2px 0 0 0 Highlight, inset 0 -2px 0 0 Highlight, inset 0 2px 0 0 Highlight]',
221
- isSelected: {
222
- default: '[inset 1px 0 0 0 Highlight, inset -1px 0 0 0 Highlight, inset 0 -1px 0 0 Highlight, inset 0 1px 0 0 Highlight]',
223
- isFocusVisible: '[inset 2px 0 0 0 Highlight, inset -2px 0 0 0 Highlight, inset 0 -2px 0 0 Highlight, inset 0 2px 0 0 Highlight]'
224
- }
225
- }
226
- }
227
- });
228
-
229
- export const TreeViewItem = (props: SpectrumTreeViewItemProps): ReactNode => {
230
- let {
231
- href
232
- } = props;
233
-
234
- return (
235
- <TreeItem
236
- {...props}
237
- className={renderProps => treeRow({
238
- ...renderProps,
239
- isLink: !!href
240
- })} />
241
- );
242
- };
243
-
244
- export interface SpectrumTreeViewItemContentProps extends Omit<TreeItemContentProps, 'children'> {
245
- /** Rendered contents of the tree item or child items. */
246
- children: ReactNode
247
- }
248
-
249
-
250
- export const TreeViewItemContent = (props: SpectrumTreeViewItemContentProps): ReactNode => {
251
- let {
252
- children
253
- } = props;
254
-
255
- return (
256
- <TreeItemContent>
257
- {({isExpanded, hasChildItems, level, selectionMode, selectionBehavior, isDisabled, isSelected, isFocusVisible, state, id}) => {
258
- let isFirst = state.collection.getFirstKey() === id;
259
- return (
260
- <div className={treeCellGrid({isDisabled})}>
261
- {selectionMode !== 'none' && selectionBehavior === 'toggle' && (
262
- // TODO: add transition?
263
- <Checkbox
264
- isEmphasized
265
- UNSAFE_className={treeCheckbox()}
266
- UNSAFE_style={{paddingInlineEnd: '0px'}}
267
- slot="selection" />
268
- )}
269
- <div style={{gridArea: 'level-padding', marginInlineEnd: `calc(${level - 1} * var(--spectrum-global-dimension-size-200))`}} />
270
- {/* TODO: revisit when we do async loading, at the moment hasChildItems will only cause the chevron to be rendered, no aria/data attributes indicating the row's expandability are added */}
271
- {hasChildItems && <ExpandableRowChevron isDisabled={isDisabled} isExpanded={isExpanded} />}
272
- <SlotProvider
273
- slots={{
274
- text: {UNSAFE_className: treeContent({isDisabled})},
275
- // Note there is also an issue here where these icon props are making into the action menu's icon. Resolved by 8ab0ffb276ff437a65b365c9a3be0323a1b24656
276
- // but could crop up later for other components
277
- icon: {UNSAFE_className: treeIcon(), size: 'S'},
278
- actionButton: {UNSAFE_className: treeActions(), isQuiet: true},
279
- actionGroup: {
280
- UNSAFE_className: treeActions(),
281
- isQuiet: true,
282
- density: 'compact',
283
- buttonLabelBehavior: 'hide',
284
- isDisabled,
285
- overflowMode: 'collapse'
286
- },
287
- actionMenu: {UNSAFE_className: treeActionMenu(), UNSAFE_style: {marginInlineEnd: '.5rem'}, isQuiet: true}
288
- }}>
289
- {children}
290
- </SlotProvider>
291
- <div className={treeRowOutline({isFocusVisible, isSelected, isFirst})} />
292
- </div>
293
- );
294
- }}
295
- </TreeItemContent>
296
- );
297
- };
298
-
299
- interface ExpandableRowChevronProps {
300
- isExpanded?: boolean,
301
- isDisabled?: boolean,
302
- isRTL?: boolean
303
- }
304
-
305
- const expandButton = style<ExpandableRowChevronProps>({
306
- gridArea: 'expand-button',
307
- height: 'full',
308
- aspectRatio: 'square',
309
- display: 'flex',
310
- flexWrap: 'wrap',
311
- alignContent: 'center',
312
- justifyContent: 'center',
313
- outlineStyle: 'none',
314
- transform: {
315
- isExpanded: {
316
- default: 'rotate(90deg)',
317
- isRTL: 'rotate(-90deg)'
318
- }
319
- },
320
- transition: '[transform ease var(--spectrum-global-animation-duration-100)]'
321
- });
322
-
323
- function ExpandableRowChevron(props: ExpandableRowChevronProps) {
324
- let expandButtonRef = useRef(null);
325
- let [fullProps, ref] = useContextProps({...props, slot: 'chevron'}, expandButtonRef, ButtonContext);
326
- let {isExpanded, isDisabled} = fullProps;
327
- let {direction} = useLocale();
328
-
329
- // Will need to keep the chevron as a button for iOS VO at all times since VO doesn't focus the cell. Also keep as button if cellAction is defined by the user in the future
330
- let {buttonProps} = useButton({
331
- ...fullProps,
332
- elementType: 'span'
333
- }, ref);
334
-
335
- return (
336
- <span
337
- {...buttonProps}
338
- ref={ref}
339
- // Override tabindex so that grid keyboard nav skips over it. Needs -1 so android talkback can actually "focus" it
340
- tabIndex={isAndroid() && !isDisabled ? -1 : undefined}
341
- className={expandButton({isExpanded, isDisabled, isRTL: direction === 'rtl'})}>
342
- {direction === 'ltr' ? <ChevronRightMedium /> : <ChevronLeftMedium />}
343
- </span>
344
- );
345
- }