File size: 6,666 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {
	__experimentalText as Text,
	__experimentalHStack as HStack,
	Button,
	VisuallyHidden,
} from '@wordpress/components';
import { useResizeObserver, useMergeRefs } from '@wordpress/compose';
import { useI18n } from '@wordpress/react-i18n';
import clsx from 'clsx';
import React, { useState, forwardRef, useRef, useMemo } from 'react';
import Menu from '../menu';
import { BreadcrumbProps, BreadcrumbItemProps, Item } from './types';
import './style.scss';

function defaultRenderItemLink( props: BreadcrumbItemProps ) {
	return <a { ...props }>{ props.label }</a>;
}

function BreadcrumbsMenu( {
	items,
	renderItemLink,
}: {
	items: BreadcrumbItemProps[];
	renderItemLink: NonNullable< BreadcrumbProps[ 'renderItemLink' ] >;
} ) {
	const { __ } = useI18n();

	return (
		<li className="a8c-components-breadcrumbs__item-wrapper">
			<Menu placement="bottom-start">
				<Menu.TriggerButton
					className="a8c-components-breadcrumbs__item"
					render={
						<Button size="compact" text={ __( '…' ) } label={ __( 'More breadcrumb items' ) } />
					}
				/>
				<Menu.Popover>
					{ items.map( ( item, index ) => {
						return (
							<Menu.Item key={ `${ item.label }-${ index }` } render={ renderItemLink( item ) }>
								<Menu.ItemLabel>{ item.label }</Menu.ItemLabel>
							</Menu.Item>
						);
					} ) }
				</Menu.Popover>
			</Menu>
		</li>
	);
}

function BreadcrumbItem( {
	item,
	renderItemLink,
}: {
	item: Item;
	renderItemLink: NonNullable< BreadcrumbProps[ 'renderItemLink' ] >;
} ) {
	const itemProps = useMemo(
		() => ( {
			...item,
			className: 'a8c-components-breadcrumbs__item',
		} ),
		[ item ]
	);
	return (
		<li className="a8c-components-breadcrumbs__item-wrapper">{ renderItemLink( itemProps ) }</li>
	);
}

function BreadcrumbCurrentItem( {
	item: { label },
	visible = false,
}: {
	item: BreadcrumbItemProps;
	visible: boolean;
} ) {
	const content = (
		<Text as="span" className="a8c-components-breadcrumbs__item" aria-current="page">
			{ label }
		</Text>
	);
	return visible ? (
		<li className="a8c-components-breadcrumbs__item-wrapper is-current">{ content }</li>
	) : (
		<VisuallyHidden as="li">{ content }</VisuallyHidden>
	);
}

const BreadcrumbsNav = forwardRef<
	HTMLElement,
	BreadcrumbProps & {
		isOffscreen?: boolean;
	}
>( function BreadcrumbsNav(
	{
		isOffscreen,
		items,
		showCurrentItem = false,
		variant = 'default',
		renderItemLink = defaultRenderItemLink,
		...props
	},
	ref
) {
	// Always show the first item. The last item (current page) is rendered
	// conditionally based on the `showCurrentItem` prop.
	const hasMiddleItems = items.length > 3;
	const firstItem = items[ 0 ];
	const middleItems = hasMiddleItems ? items.slice( 1, -2 ) : [];
	// Always show the parent item if there are more than 2 items. If there
	// are only 2 items, the parent item is the first item and is already shown.
	const parentItem = items.length > 2 && items[ items.length - 2 ];
	/**
	 * As the container shrinks, multiple breadcrumb items between the first and
	 * last visible item should collapse into a dropdown menu to avoid wrapping.
	 * The current approach is to keep a ref of the `offScreen (full-width)`
	 * container and observe for `inlineSize` changes. If the `offScreen` container
	 * would overflow, we should render the compact variant.
	 * Noting that we prioritize the `isCompact` prop over the `width` checks.
	 */
	const isCompact = ! isOffscreen && hasMiddleItems && variant === 'compact';
	return (
		<nav
			className={ clsx( 'a8c-components-breadcrumbs', { 'is-offscreen': isOffscreen } ) }
			ref={ ref }
			{ ...( isOffscreen && { 'aria-hidden': true, inert: '' } ) }
			{ ...props }
		>
			<HStack
				as="ul"
				className="a8c-components-breadcrumbs__list"
				spacing={ 0 }
				justify="flex-start"
				expanded={ false }
			>
				<BreadcrumbItem item={ firstItem } renderItemLink={ renderItemLink } />
				{ isCompact ? (
					<BreadcrumbsMenu items={ middleItems } renderItemLink={ renderItemLink } />
				) : (
					middleItems.map( ( item, index ) => (
						<BreadcrumbItem
							key={ `${ item.label }-${ index }` }
							item={ item }
							renderItemLink={ renderItemLink }
						/>
					) )
				) }
				{ parentItem && <BreadcrumbItem item={ parentItem } renderItemLink={ renderItemLink } /> }
				<BreadcrumbCurrentItem item={ items[ items.length - 1 ] } visible={ showCurrentItem } />
			</HStack>
		</nav>
	);
} );

function UnforwardedBreadcrumbs(
	{ items, renderItemLink, 'aria-label': ariaLabel, ...props }: BreadcrumbProps,
	ref: React.ForwardedRef< HTMLElement >
) {
	const { __ } = useI18n();
	const computedAriaLabel = ariaLabel ?? __( 'Breadcrumbs' );

	const offScreenWidth = useRef( 0 );
	const containerWidth = useRef( 0 );
	const [ shouldRenderCompact, setShouldRenderCompact ] = useState( false );

	const computeShouldRenderCompact = () => {
		setShouldRenderCompact( offScreenWidth.current > containerWidth.current );
	};
	const offScreenRef = useResizeObserver( ( resizeObserverEntries ) => {
		offScreenWidth.current = resizeObserverEntries[ 0 ].borderBoxSize[ 0 ].inlineSize;
		computeShouldRenderCompact();
	} );
	const containerRef = useResizeObserver( ( resizeObserverEntries ) => {
		containerWidth.current = resizeObserverEntries[ 0 ].borderBoxSize[ 0 ].inlineSize;
		computeShouldRenderCompact();
	} );

	const mergedRefs = useMergeRefs( [ ref, containerRef ] );
	if ( ! items.length || items.length === 1 ) {
		return null;
	}

	const computedVariant = shouldRenderCompact ? 'compact' : props.variant;

	return (
		<>
			<BreadcrumbsNav
				ref={ offScreenRef }
				items={ items }
				renderItemLink={ renderItemLink }
				{ ...props }
				variant={ computedVariant }
				isOffscreen
			/>
			<BreadcrumbsNav
				ref={ mergedRefs }
				items={ items }
				renderItemLink={ renderItemLink }
				{ ...props }
				variant={ computedVariant }
				aria-label={ computedAriaLabel }
			/>
		</>
	);
}

/**
 * The `Breadcrumbs` component provides a secondary navigation aid that shows
 * users their current location within a site's or application's hierarchy.
 * It helps users understand the structure of the site, retrace their steps,
 * and easily navigate to higher-level pages.
 *
 * For accessibility, **it is important that the current page is included as the
 * final item in the breadcrumb trail**. This ensures screen reader users
 * receive the full navigational context.
 *
 * Note: for the Breadcrumbs component to work properly in compact mode, make
 * sure that the implicit component's width is not affected by changes in the
 * document's body padding.
 */
export const Breadcrumbs = forwardRef( UnforwardedBreadcrumbs );