-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathBreadcrumbs.tsx
481 lines (448 loc) · 14.9 KB
/
Breadcrumbs.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {ActionButton} from './ActionButton';
import {
Breadcrumb as AriaBreadcrumb,
BreadcrumbsProps as AriaBreadcrumbsProps,
CollectionRenderer,
CollectionRendererContext,
ContextValue,
DefaultCollectionRenderer,
HeadingContext,
Link,
LinkRenderProps,
Provider,
Breadcrumbs as RACBreadcrumbs
} from 'react-aria-components';
import {AriaBreadcrumbItemProps, useLocale} from 'react-aria';
import ChevronIcon from '../ui-icons/Chevron';
import {Collection, DOMRef, DOMRefValue, LinkDOMProps, Node} from '@react-types/shared';
import {createContext, forwardRef, Fragment, ReactNode, RefObject, useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react';
import {focusRing, size, style} from '../style' with { type: 'macro' };
import FolderIcon from '../s2wf-icons/S2_Icon_FolderBreadcrumb_20_N.svg';
import {forwardRefType} from './types';
import {getAllowedOverrides, StyleProps} from './style-utils' with {type: 'macro'};
import {inertValue, useLayoutEffect} from '@react-aria/utils';
// @ts-ignore
import intlMessages from '../intl/*.json';
import {Menu, MenuItem, MenuTrigger} from './Menu';
import {Text} from './Content';
import {useDOMRef, useResizeObserver} from '@react-spectrum/utils';
import {useLocalizedStringFormatter} from '@react-aria/i18n';
import {useSpectrumContextProps} from './useSpectrumContextProps';
const MIN_VISIBLE_ITEMS = 1;
const MAX_VISIBLE_ITEMS = 4;
interface BreadcrumbsStyleProps {
/**
* Size of the Breadcrumbs including spacing and layout.
*
* @default 'M'
*/
size?: 'M' | 'L',
/** Whether the breadcrumbs are disabled. */
isDisabled?: boolean
/**
* Whether to place the last Breadcrumb item onto a new line.
*/
// TODO: isMultiline?: boolean
/** Whether to always show the root item if the items are collapsed. */
// TODO: showRoot?: boolean,
}
export interface BreadcrumbsProps<T> extends Omit<AriaBreadcrumbsProps<T>, 'children' | 'items' | 'style' | 'className'>, BreadcrumbsStyleProps, StyleProps {
/** The children of the Breadcrumbs. */
children: ReactNode
}
export const BreadcrumbsContext = createContext<ContextValue<Partial<BreadcrumbsProps<any>>, DOMRefValue<HTMLOListElement>>>(null);
const wrapper = style<BreadcrumbsStyleProps>({
position: 'relative',
display: 'flex',
justifyContent: 'start',
listStyleType: 'none',
flexWrap: 'nowrap',
flexGrow: 1,
flexShrink: 0,
flexBasis: 0,
gap: {
size: {
// TODO: why do these scale but other spacings don't?
M: size(6), // breadcrumbs-text-to-separator-medium
L: size(9) // breadcrumbs-text-to-separator-large
}
},
padding: 0,
transition: 'default',
marginTop: 0,
marginBottom: 0,
marginStart: {
size: {
M: size(6),
L: size(9)
}
}
}, getAllowedOverrides());
const InternalBreadcrumbsContext = createContext<Partial<BreadcrumbsProps<any>>>({});
/** Breadcrumbs show hierarchy and navigational context for a user's location within an application. */
export const Breadcrumbs = /*#__PURE__*/ (forwardRef as forwardRefType)(function Breadcrumbs<T extends object>(props: BreadcrumbsProps<T>, ref: DOMRef<HTMLOListElement>) {
[props, ref] = useSpectrumContextProps(props, ref, BreadcrumbsContext);
let domRef = useDOMRef(ref);
let {
UNSAFE_className = '',
UNSAFE_style,
styles,
size = 'M',
children,
isDisabled,
...otherProps
} = props;
return (
<Provider
values={[
[InternalBreadcrumbsContext, {size, isDisabled}]
]}>
<CollapsingCollection containerRef={domRef} onAction={props.onAction}>
<RACBreadcrumbs
{...otherProps}
isDisabled={isDisabled}
ref={domRef}
style={UNSAFE_style}
className={UNSAFE_className + wrapper({
size
}, styles)}>
{children}
</RACBreadcrumbs>
</CollapsingCollection>
</Provider>
);
});
let BreadcrumbMenu = (props: {items: Array<Node<any>>, onAction: BreadcrumbsProps<unknown>['onAction']}) => {
let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-spectrum/s2');
let {items, onAction} = props;
let {direction} = useLocale();
let {size, isDisabled} = useContext(InternalBreadcrumbsContext);
let label = stringFormatter.format('breadcrumbs.more');
return (
<CollectionRendererContext.Provider value={DefaultCollectionRenderer}>
<li className={breadcrumbStyles({size, isDisabled, isMenu: true})}>
<MenuTrigger>
<ActionButton isDisabled={isDisabled} isQuiet aria-label={label}><FolderIcon /></ActionButton>
<Menu items={items} onAction={onAction}>
{(item: Node<any>) => (
<MenuItem
{...item.props.originalProps}
key={item.key}>
<Text slot="label">
{item.props.children({size, isCurrent: false, isMenu: true})}
</Text>
</MenuItem>
)}
</Menu>
</MenuTrigger>
<ChevronIcon
size={size}
className={chevronStyles({direction, isMenu: true})} />
</li>
</CollectionRendererContext.Provider>
);
};
let HiddenBreadcrumbs = function (props: {listRef: RefObject<HTMLDivElement | null>, items: Array<Node<any>>, size: string}) {
let {listRef, items, size} = props;
return (
<div
// @ts-ignore
inert={inertValue(true)}
ref={listRef}
className={style({
display: '[inherit]',
gap: '[inherit]',
flexWrap: '[inherit]',
position: 'absolute',
top: 0,
bottom: 0,
start: 0,
end: 0,
visibility: 'hidden',
overflow: 'hidden',
opacity: 0
})}>
{items.map((item, idx) => {
// pull off individual props as an allow list, don't want refs or other props getting through
return (
<div
data-hidden-breadcrumb
style={item.props.UNSAFE_style}
key={item.key}
className={item.props.className({size, isCurrent: idx === items.length - 1})}>
{item.props.children({size, isCurrent: idx === items.length - 1})}
</div>
);
})}
<ActionButton data-hidden-button isQuiet><FolderIcon /></ActionButton>
</div>
);
};
const breadcrumbStyles = style<BreadcrumbsStyleProps & {isMenu?: boolean, isCurrent?: boolean}>({
display: 'flex',
alignItems: 'center',
justifyContent: 'start',
height: 'control',
transition: 'default',
position: 'relative',
flexShrink: 0,
color: {
default: 'neutral',
isDisabled: 'disabled',
forcedColors: {
default: 'ButtonText',
isDisabled: 'GrayText'
}
},
borderStyle: 'none',
marginStart: {
// adjusts with the parent flex gap
isMenu: size(-6)
}
});
const chevronStyles = style({
scale: {
direction: {
rtl: -1
}
},
marginStart: {
default: 'text-to-visual',
isMenu: 0
},
color: {
default: 'neutral',
forcedColors: {
default: 'LinkText'
}
},
'--iconPrimary': {
type: 'fill',
value: 'currentColor'
}
});
const linkStyles = style<LinkRenderProps & {size?: 'M' | 'L', isCurrent?: boolean}>({
...focusRing(),
borderRadius: 'sm',
font: 'control',
color: {
default: 'neutral-subdued',
isDisabled: 'disabled',
isCurrent: 'neutral',
forcedColors: {
default: 'LinkText',
isDisabled: 'GrayText',
isCurrent: 'GrayText'
}
},
transition: 'default',
textDecoration: {
default: 'none',
isHovered: 'underline',
isFocusVisible: 'underline',
isDisabled: 'none'
},
cursor: {
default: 'pointer',
isDisabled: 'default'
},
outlineColor: {
default: 'focus-ring',
forcedColors: 'Highlight'
},
disableTapHighlight: true
});
const currentStyles = style<{size: string}>({
font: 'control',
fontWeight: 'bold'
});
// TODO: support user heading size customization, for now just set it to large
const heading = style({
margin: 0,
font: 'heading-lg',
fontWeight: 'extra-bold'
});
export interface BreadcrumbProps extends Omit<AriaBreadcrumbItemProps, 'children' | 'style' | 'className' | 'autoFocus' | 'onClick'>, LinkDOMProps {
/** The children of the breadcrumb item. */
children: ReactNode
}
/** An individual Breadcrumb for Breadcrumbs. */
export const Breadcrumb = /*#__PURE__*/ (forwardRef as forwardRefType)(function Breadcrumb({children, ...props}: BreadcrumbProps, ref: DOMRef<HTMLLIElement>) {
let {href, target, rel, download, ping, referrerPolicy} = props;
let {size = 'M'} = useContext(InternalBreadcrumbsContext) ?? {};
let domRef = useDOMRef(ref);
let {direction} = useLocale();
return (
<AriaBreadcrumb
{...props}
ref={domRef}
// @ts-ignore
originalProps={props}
className={({isCurrent, isDisabled}) => breadcrumbStyles({size, isCurrent, isDisabled})}>
{({
isCurrent,
isDisabled,
// @ts-ignore
isMenu
}) => {
if (isMenu) {
return children;
}
return (
isCurrent ?
<div
className={currentStyles({size})}>
<Provider
values={[
[HeadingContext, {className: heading}]
]}>
{children}
</Provider>
</div>
: (
<>
<Link
style={({isFocusVisible}) => ({clipPath: isFocusVisible ? 'none' : 'margin-box'})}
href={href}
target={target}
rel={rel}
download={download}
ping={ping}
referrerPolicy={referrerPolicy}
isDisabled={isDisabled || isCurrent}
className={({isFocused, isFocusVisible, isHovered, isDisabled, isPressed}) => linkStyles({isFocused, isFocusVisible, isHovered, isDisabled, size, isPressed, isCurrent})}>
{children}
</Link>
<ChevronIcon
size="M"
className={chevronStyles({direction})} />
</>
)
);
}}
</AriaBreadcrumb>
);
});
// Context for passing the count for the custom renderer
let CollapseContext = createContext<{
containerRef: RefObject<HTMLOListElement | null>,
onAction: BreadcrumbsProps<unknown>['onAction']
} | null>(null);
function CollapsingCollection({children, containerRef, onAction}) {
return (
<CollapseContext.Provider value={{containerRef, onAction}}>
<CollectionRendererContext.Provider value={CollapsingCollectionRenderer}>
{children}
</CollectionRendererContext.Provider>
</CollapseContext.Provider>
);
}
let CollapsingCollectionRenderer: CollectionRenderer = {
CollectionRoot({collection}) {
return useCollectionRender(collection);
},
CollectionBranch({collection}) {
return useCollectionRender(collection);
}
};
let useCollectionRender = (collection: Collection<Node<unknown>>) => {
let {containerRef, onAction} = useContext(CollapseContext) ?? {};
let [visibleItems, setVisibleItems] = useState(collection.size);
let {size = 'M'} = useContext(InternalBreadcrumbsContext);
let children = useMemo(() => {
let result: Node<any>[] = [];
for (let key of collection.getKeys()) {
result.push(collection.getItem(key)!);
}
return result;
}, [collection]);
let listRef = useRef<HTMLDivElement | null>(null);
let updateOverflow = useCallback(() => {
let currListRef: HTMLDivElement | null = listRef.current;
if (!currListRef) {
setVisibleItems(collection.size);
return;
}
let container = currListRef.parentElement;
if (!container) {
setVisibleItems(collection.size);
return;
}
let listItems = Array.from(currListRef.querySelectorAll('[data-hidden-breadcrumb]')) as HTMLLIElement[];
let folder = currListRef.querySelector('button') as HTMLButtonElement;
if (listItems.length <= 0) {
setVisibleItems(collection.size);
return;
}
let containerWidth = container.offsetWidth;
let containerGap = parseInt(getComputedStyle(container).gap, 10);
let folderGap = parseInt(getComputedStyle(folder).marginInlineStart, 10);
let newVisibleItems = 0;
let maxVisibleItems = MAX_VISIBLE_ITEMS;
let widths: Array<number> = [];
let totalWidth = 0;
for (let breadcrumb of listItems) {
let width = breadcrumb.offsetWidth + 1; // offsetWidth is rounded down
widths.push(width);
totalWidth += width;
}
// can we fit all the items without collapsing
if (totalWidth <= containerWidth - (collection.size * containerGap) && collection.size <= MAX_VISIBLE_ITEMS) {
setVisibleItems(collection.size);
return;
}
// we know there is always at least one item because of the listItems.length check up above
let widthOfFirst = widths.shift()!;
let availableWidth = containerWidth - widthOfFirst - folderGap - folder.offsetWidth - containerGap;
maxVisibleItems -= 2; // account for the first item and folder
for (let width of widths.reverse()) {
availableWidth -= width;
if (availableWidth <= 0) {
break;
}
availableWidth -= containerGap;
newVisibleItems++;
}
setVisibleItems(Math.max(MIN_VISIBLE_ITEMS, Math.min(maxVisibleItems, newVisibleItems)));
}, [collection.size, setVisibleItems]);
// making bad assumption that i can listen to containerRef when it's declared in the parent?
useResizeObserver({ref: containerRef, onResize: updateOverflow});
useLayoutEffect(() => {
if (collection.size > 0) {
queueMicrotask(updateOverflow);
}
}, [collection.size, updateOverflow]);
useEffect(() => {
// Recalculate visible tags when fonts are loaded.
document.fonts?.ready.then(() => updateOverflow());
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
let sliceIndex = collection.size - visibleItems;
return (
<>
<HiddenBreadcrumbs items={children} size={size} listRef={listRef} />
{visibleItems < collection.size && collection.size > 2 ? (
<>
{children[0].render?.(children[0])}
<BreadcrumbMenu items={children.slice(1, sliceIndex)} onAction={onAction} />
{children.slice(sliceIndex).map(node => <Fragment key={node.key}>{node.render?.(node)}</Fragment >)}
</>
) : (
<>
{children.map(node => <Fragment key={node.key}>{node.render?.(node)}</Fragment>)}
</>
)}
</>
);
};