@primer/react 0.0.0-20260526190633 → 0.0.0-20260526221749

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.
Files changed (73) hide show
  1. package/CHANGELOG.md +9 -2
  2. package/dist/ActionList/index.d.ts +6 -6
  3. package/dist/ActionMenu/ActionMenu.d.ts.map +1 -1
  4. package/dist/ActionMenu/ActionMenu.js +0 -1
  5. package/dist/CheckboxGroup/CheckboxGroup.js +0 -1
  6. package/dist/DataTable/index.d.ts.map +1 -1
  7. package/dist/DataTable/index.js +0 -1
  8. package/dist/Dialog/Dialog.d.ts +0 -1
  9. package/dist/Dialog/Dialog.d.ts.map +1 -1
  10. package/dist/Dialog/Dialog.js +0 -1
  11. package/dist/FormControl/FormControl.d.ts +3 -3
  12. package/dist/NavList/NavList.d.ts +4 -4
  13. package/dist/NavList/NavList.d.ts.map +1 -1
  14. package/dist/NavList/NavList.js +7 -6
  15. package/dist/PageLayout/PageLayout.d.ts +0 -1
  16. package/dist/PageLayout/PageLayout.d.ts.map +1 -1
  17. package/dist/PageLayout/PageLayout.js +0 -1
  18. package/dist/RadioGroup/RadioGroup.d.ts +3 -4
  19. package/dist/RadioGroup/RadioGroup.d.ts.map +1 -1
  20. package/dist/RadioGroup/RadioGroup.js +0 -1
  21. package/dist/SegmentedControl/SegmentedControl.d.ts +2 -3
  22. package/dist/SegmentedControl/SegmentedControl.d.ts.map +1 -1
  23. package/dist/SegmentedControl/SegmentedControl.js +0 -1
  24. package/dist/Tooltip/Tooltip.js +1 -1
  25. package/dist/UnderlineNav/UnderlineNav-4344d9b0.css +2 -0
  26. package/dist/UnderlineNav/UnderlineNav-4344d9b0.css.map +1 -0
  27. package/dist/UnderlineNav/UnderlineNav.d.ts +3 -1
  28. package/dist/UnderlineNav/UnderlineNav.d.ts.map +1 -1
  29. package/dist/UnderlineNav/UnderlineNav.js +342 -39
  30. package/dist/UnderlineNav/UnderlineNav.module.css.js +2 -2
  31. package/dist/UnderlineNav/UnderlineNavContext.d.ts +11 -1
  32. package/dist/UnderlineNav/UnderlineNavContext.d.ts.map +1 -1
  33. package/dist/UnderlineNav/UnderlineNavContext.js +4 -1
  34. package/dist/UnderlineNav/UnderlineNavItem.d.ts.map +1 -1
  35. package/dist/UnderlineNav/UnderlineNavItem.js +37 -40
  36. package/dist/UnderlineNav/index.d.ts +1 -1
  37. package/dist/UnderlineNav/styles.d.ts +16 -0
  38. package/dist/UnderlineNav/styles.d.ts.map +1 -0
  39. package/dist/UnderlineNav/styles.js +19 -0
  40. package/dist/experimental/UnderlinePanels/UnderlinePanels-e4b325b9.css +2 -0
  41. package/dist/experimental/UnderlinePanels/UnderlinePanels-e4b325b9.css.map +1 -0
  42. package/dist/experimental/UnderlinePanels/UnderlinePanels.js +1 -2
  43. package/dist/experimental/UnderlinePanels/UnderlinePanels.module.css.js +1 -1
  44. package/dist/hooks/index.d.ts +1 -0
  45. package/dist/hooks/index.d.ts.map +1 -1
  46. package/dist/hooks/useSlots.d.ts.map +1 -1
  47. package/dist/hooks/useSlots.js +24 -0
  48. package/dist/index.d.ts +4 -1
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/index.js +3 -0
  51. package/dist/internal/components/CheckboxOrRadioGroup/CheckboxOrRadioGroup.d.ts +3 -3
  52. package/dist/internal/components/CheckboxOrRadioGroup/CheckboxOrRadioGroup.d.ts.map +1 -1
  53. package/dist/internal/components/CheckboxOrRadioGroup/CheckboxOrRadioGroup.js +3 -4
  54. package/dist/internal/components/CheckboxOrRadioGroup/CheckboxOrRadioGroupCaption.js +1 -1
  55. package/dist/internal/components/CheckboxOrRadioGroup/CheckboxOrRadioGroupLabel.js +1 -1
  56. package/dist/internal/components/CheckboxOrRadioGroup/CheckboxOrRadioGroupValidation.js +1 -1
  57. package/dist/internal/components/UnderlineTabbedInterface-4197ee28.css +2 -0
  58. package/dist/internal/components/UnderlineTabbedInterface-4197ee28.css.map +1 -0
  59. package/dist/internal/components/UnderlineTabbedInterface.d.ts +1 -0
  60. package/dist/internal/components/UnderlineTabbedInterface.d.ts.map +1 -1
  61. package/dist/internal/components/UnderlineTabbedInterface.js +60 -66
  62. package/dist/internal/components/UnderlineTabbedInterface.module.css.js +1 -1
  63. package/dist/utils/as-slot.d.ts +20 -0
  64. package/dist/utils/as-slot.d.ts.map +1 -0
  65. package/dist/utils/as-slot.js +30 -0
  66. package/generated/components.json +2 -2
  67. package/package.json +1 -1
  68. package/dist/UnderlineNav/UnderlineNav-4c80468c.css +0 -2
  69. package/dist/UnderlineNav/UnderlineNav-4c80468c.css.map +0 -1
  70. package/dist/experimental/UnderlinePanels/UnderlinePanels-162f9aed.css +0 -2
  71. package/dist/experimental/UnderlinePanels/UnderlinePanels-162f9aed.css.map +0 -1
  72. package/dist/internal/components/UnderlineTabbedInterface-7317470b.css +0 -2
  73. package/dist/internal/components/UnderlineTabbedInterface-7317470b.css.map +0 -1
@@ -1,16 +1,120 @@
1
- import React, { forwardRef, useState, useRef, useEffect } from 'react';
1
+ import React, { forwardRef, useRef, useState, useCallback } from 'react';
2
+ import { useDevOnlyEffect } from '../internal/hooks/useDevOnlyEffect.js';
3
+ import { UnderlineNavContext } from './UnderlineNavContext.js';
4
+ import { useResizeObserver } from '../hooks/useResizeObserver.js';
2
5
  import VisuallyHidden from '../_VisuallyHidden.js';
3
- import { UnderlineWrapper, UnderlineItemList } from '../internal/components/UnderlineTabbedInterface.js';
6
+ import { dividerStyles, baseMenuMinWidth, menuItemStyles } from './styles.js';
7
+ import { UnderlineWrapper, UnderlineItemList, LoadingCounter, GAP } from '../internal/components/UnderlineTabbedInterface.js';
8
+ import { TriangleDownIcon } from '@primer/octicons-react';
9
+ import { useOnEscapePress } from '../hooks/useOnEscapePress.js';
10
+ import { useOnOutsideClick } from '../hooks/useOnOutsideClick.js';
11
+ import { useId } from '../hooks/useId.js';
12
+ import { ActionList } from '../ActionList/index.js';
4
13
  import { invariant } from '../utils/invariant.js';
5
14
  import classes from './UnderlineNav.module.css.js';
6
- import { UnderlineNavContext } from './UnderlineNavContext.js';
7
- import { useDevOnlyEffect } from '../internal/hooks/useDevOnlyEffect.js';
8
- import { jsxs, jsx } from 'react/jsx-runtime';
15
+ import { getAnchoredPosition } from '@primer/behaviors';
16
+ import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
17
+ import { ButtonComponent } from '../Button/Button.js';
18
+ import CounterLabel from '../CounterLabel/CounterLabel.js';
19
+
20
+ // When page is loaded, we don't have ref for the more button as it is not on the DOM yet.
21
+ // However, we need to calculate number of possible items when the more button present as well. So using the width of the more button as a constant.
22
+ const MORE_BTN_WIDTH = 86;
23
+ // The height is needed to make sure we don't have a layout shift when the more button is the only item in the nav.
24
+ const MORE_BTN_HEIGHT = 45;
25
+ const overflowEffect = (navWidth, moreMenuWidth, childArray, childWidthArray, noIconChildWidthArray, updateListAndMenu) => {
26
+ let iconsVisible = true;
27
+ if (childWidthArray.length === 0) {
28
+ updateListAndMenu({
29
+ items: childArray,
30
+ menuItems: []
31
+ }, iconsVisible, false);
32
+ return;
33
+ }
34
+ const numberOfItemsPossible = calculatePossibleItems(childWidthArray, navWidth);
35
+ const numberOfItemsWithoutIconPossible = calculatePossibleItems(noIconChildWidthArray, navWidth);
36
+ // We need to take more menu width into account when calculating the number of items possible
37
+ const numberOfItemsPossibleWithMoreMenu = calculatePossibleItems(noIconChildWidthArray, navWidth, moreMenuWidth || MORE_BTN_WIDTH);
38
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
+ const items = [];
40
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
+ const menuItems = [];
42
+
43
+ // First, we check if we can fit all the items with their icons
44
+ if (childArray.length <= numberOfItemsPossible) {
45
+ items.push(...childArray);
46
+ } else if (childArray.length <= numberOfItemsWithoutIconPossible) {
47
+ // if we can't fit all the items with their icons, we check if we can fit all the items without their icons
48
+ iconsVisible = false;
49
+ items.push(...childArray);
50
+ } else {
51
+ // if we can't fit all the items without their icons, we keep the icons hidden and show the ones that doesn't fit into the list in the overflow menu
52
+ iconsVisible = false;
9
53
 
54
+ /* Below is an accessibility requirement. Never show only one item in the overflow menu.
55
+ * If there is only one item left to display in the overflow menu according to the calculation,
56
+ * we need to pull another item from the list into the overflow menu.
57
+ */
58
+ const numberOfItemsInMenu = childArray.length - numberOfItemsPossibleWithMoreMenu;
59
+ const numberOfListItems = numberOfItemsInMenu === 1 ? numberOfItemsPossibleWithMoreMenu - 1 : numberOfItemsPossibleWithMoreMenu;
60
+ for (const [index, child] of childArray.entries()) {
61
+ if (index < numberOfListItems) {
62
+ items.push(child);
63
+ } else {
64
+ const ariaCurrent = child.props['aria-current'];
65
+ const isCurrent = Boolean(ariaCurrent) && ariaCurrent !== 'false';
66
+ // We need to make sure to keep the selected item always visible.
67
+ // To do that, we swap the selected item with the last item in the list to make it visible. (When there is at least 1 item in the list to swap.)
68
+ if (isCurrent && numberOfListItems > 0) {
69
+ // If selected item couldn't make in to the list, we swap it with the last item in the list.
70
+ const indexToReplaceAt = numberOfListItems - 1; // because we are replacing the last item in the list
71
+ // splice method modifies the array by removing 1 item here at the given index and replace it with the "child" element then returns the removed item.
72
+ const propsectiveAction = items.splice(indexToReplaceAt, 1, child)[0];
73
+ menuItems.push(propsectiveAction);
74
+ } else {
75
+ menuItems.push(child);
76
+ }
77
+ }
78
+ }
79
+ }
80
+ updateListAndMenu({
81
+ items,
82
+ menuItems
83
+ }, iconsVisible, true);
84
+ };
10
85
  const getValidChildren = children => {
11
86
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
87
  return React.Children.toArray(children).filter(child => /*#__PURE__*/React.isValidElement(child));
13
88
  };
89
+ const calculatePossibleItems = (childWidthArray, navWidth, moreMenuWidth = 0) => {
90
+ const widthToFit = navWidth - moreMenuWidth;
91
+ let breakpoint = childWidthArray.length; // assume all items will fit
92
+ let sumsOfChildWidth = 0;
93
+ for (const [index, childWidth] of childWidthArray.entries()) {
94
+ sumsOfChildWidth = sumsOfChildWidth + childWidth.width + GAP;
95
+ if (sumsOfChildWidth > widthToFit) {
96
+ breakpoint = index;
97
+ break;
98
+ } else {
99
+ continue;
100
+ }
101
+ }
102
+ return breakpoint;
103
+ };
104
+
105
+ // Inline styles converted from baseMenuStyles for use as CSSProperties
106
+ const baseMenuInlineStyles = {
107
+ position: 'absolute',
108
+ zIndex: 1,
109
+ top: '90%',
110
+ boxShadow: '0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24)',
111
+ borderRadius: 12,
112
+ background: 'var(--overlay-bgColor)',
113
+ listStyle: 'none',
114
+ minWidth: `${baseMenuMinWidth}px`,
115
+ maxWidth: '640px',
116
+ right: 0
117
+ };
14
118
  const UnderlineNav = /*#__PURE__*/forwardRef(({
15
119
  as = 'nav',
16
120
  'aria-label': ariaLabel,
@@ -19,7 +123,41 @@ const UnderlineNav = /*#__PURE__*/forwardRef(({
19
123
  className,
20
124
  children
21
125
  }, forwardedRef) => {
126
+ var _listRef$current2;
127
+ const backupRef = useRef(null);
128
+ const navRef = forwardedRef !== null && forwardedRef !== void 0 ? forwardedRef : backupRef;
129
+ const listRef = useRef(null);
130
+ const moreMenuRef = useRef(null);
131
+ const moreMenuBtnRef = useRef(null);
132
+ const containerRef = React.useRef(null);
133
+ const disclosureWidgetId = useId();
134
+ const [isWidgetOpen, setIsWidgetOpen] = useState(false);
135
+ const [iconsVisible, setIconsVisible] = useState(true);
136
+ const [childWidthArray, setChildWidthArray] = useState([]);
137
+ const [noIconChildWidthArray, setNoIconChildWidthArray] = useState([]);
138
+ // Track whether the initial overflow calculation is complete to prevent CLS
139
+ const [isOverflowMeasured, setIsOverflowMeasured] = useState(false);
22
140
  const validChildren = getValidChildren(children);
141
+
142
+ // Responsive props object manages which items are in the list and which items are in the menu.
143
+ const [responsiveProps, setResponsiveProps] = useState({
144
+ items: validChildren,
145
+ menuItems: []
146
+ });
147
+
148
+ // Make sure to have the fresh props data for list items when children are changed (keeping aria-current up-to-date)
149
+ const listItems = responsiveProps.items.map(item => {
150
+ var _validChildren$find;
151
+ return (_validChildren$find = validChildren.find(child => child.key === item.key)) !== null && _validChildren$find !== void 0 ? _validChildren$find : item;
152
+ });
153
+
154
+ // Make sure to have the fresh props data for menu items when children are changed (keeping aria-current up-to-date)
155
+ const menuItems = responsiveProps.menuItems.map(menuItem => {
156
+ var _validChildren$find2;
157
+ return (_validChildren$find2 = validChildren.find(child => child.key === menuItem.key)) !== null && _validChildren$find2 !== void 0 ? _validChildren$find2 : menuItem;
158
+ });
159
+ // This is the case where the viewport is too narrow to show any list item with the more menu. In this case, we only show the dropdown
160
+ const onlyMenuVisible = responsiveProps.items.length === 0;
23
161
  useDevOnlyEffect(() => {
24
162
  // Address illegal state where there are multiple items that have `aria-current='page'` attribute
25
163
  const activeElements = validChildren.filter(child => {
@@ -28,51 +166,216 @@ const UnderlineNav = /*#__PURE__*/forwardRef(({
28
166
  !(activeElements.length <= 1) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Only one current element is allowed') : invariant(false) : void 0;
29
167
  !ariaLabel ? process.env.NODE_ENV !== "production" ? invariant(false, 'Use the `aria-label` prop to provide an accessible label for assistive technology') : invariant(false) : void 0;
30
168
  }, [validChildren, ariaLabel]);
31
- const [hideIcons, setHideIcons] = useState(false);
32
- const scrollContainer = useRef(null);
33
- useEffect(() => {
34
- if (hideIcons || !scrollContainer.current) return;
169
+ function getItemsWidth(itemText) {
170
+ var _noIconChildWidthArra, _noIconChildWidthArra2;
171
+ return (_noIconChildWidthArra = (_noIconChildWidthArra2 = noIconChildWidthArray.find(item => item.text === itemText)) === null || _noIconChildWidthArra2 === void 0 ? void 0 : _noIconChildWidthArra2.width) !== null && _noIconChildWidthArra !== void 0 ? _noIconChildWidthArra : 0;
172
+ }
173
+ const swapMenuItemWithListItem = (prospectiveListItem, indexOfProspectiveListItem, event, callback) => {
174
+ var _navRef$current$getBo, _navRef$current, _listRef$current$getB, _listRef$current;
175
+ // get the selected menu item's width
176
+ const widthToFitIntoList = getItemsWidth(prospectiveListItem.props.children);
177
+ // Check if there is any empty space on the right side of the list
178
+ const availableSpace = ((_navRef$current$getBo = (_navRef$current = navRef.current) === null || _navRef$current === void 0 ? void 0 : _navRef$current.getBoundingClientRect().width) !== null && _navRef$current$getBo !== void 0 ? _navRef$current$getBo : 0) - ((_listRef$current$getB = (_listRef$current = listRef.current) === null || _listRef$current === void 0 ? void 0 : _listRef$current.getBoundingClientRect().width) !== null && _listRef$current$getB !== void 0 ? _listRef$current$getB : 0);
35
179
 
36
- // Putting the resizeobserver on the list ensures it runs if the contents change;
37
- // the container is fullwidth but the list is elastic
38
- const list = scrollContainer.current.querySelector('ul');
39
- if (!list) return;
40
- const observer = new ResizeObserver(() => {
41
- if (scrollContainer.current.scrollWidth > scrollContainer.current.clientWidth) setHideIcons(true);
180
+ // Calculate how many items need to be pulled in to the menu to make room for the selected menu item
181
+ // I.e. if we need to pull 2 items in (index 0 and index 1), breakpoint (index) will return 1.
182
+ const index = getBreakpointForItemSwapping(widthToFitIntoList, availableSpace);
183
+ const indexToSliceAt = responsiveProps.items.length - 1 - index;
184
+ // Form the new list of items
185
+ const itemsLeftInList = [...responsiveProps.items].slice(0, indexToSliceAt);
186
+ const updatedItemList = [...itemsLeftInList, prospectiveListItem];
187
+ // Form the new menu items
188
+ const itemsToAddToMenu = [...responsiveProps.items].slice(indexToSliceAt);
189
+ const updatedMenuItems = [...menuItems];
190
+ // Add itemsToAddToMenu array's items to the menu at the index of the prospectiveListItem and remove 1 count of items (prospectiveListItem)
191
+ updatedMenuItems.splice(indexOfProspectiveListItem, 1, ...itemsToAddToMenu);
192
+ callback({
193
+ items: updatedItemList,
194
+ menuItems: updatedMenuItems
195
+ }, false, true);
196
+ };
197
+ // How many items do we need to pull in to the menu to make room for the selected menu item.
198
+ function getBreakpointForItemSwapping(widthToFitIntoList, availableSpace) {
199
+ let widthToSwap = 0;
200
+ let breakpoint = 0;
201
+ for (const [index, item] of [...responsiveProps.items].reverse().entries()) {
202
+ widthToSwap += getItemsWidth(item.props.children);
203
+ if (widthToFitIntoList < widthToSwap + availableSpace) {
204
+ breakpoint = index;
205
+ break;
206
+ }
207
+ }
208
+ return breakpoint;
209
+ }
210
+ const updateListAndMenu = useCallback((props, displayIcons, overflowMeasured) => {
211
+ setResponsiveProps(props);
212
+ setIconsVisible(displayIcons);
213
+ if (overflowMeasured) {
214
+ setIsOverflowMeasured(true);
215
+ }
216
+ }, []);
217
+ const setChildrenWidth = useCallback(size => {
218
+ setChildWidthArray(arr => {
219
+ const newArr = [...arr, size];
220
+ return newArr;
42
221
  });
43
- observer.observe(list);
44
- return () => observer.disconnect();
45
- }, [hideIcons]);
222
+ }, []);
223
+ const setNoIconChildrenWidth = useCallback(size => {
224
+ setNoIconChildWidthArray(arr => {
225
+ const newArr = [...arr, size];
226
+ return newArr;
227
+ });
228
+ }, []);
229
+ const closeOverlay = React.useCallback(() => {
230
+ setIsWidgetOpen(false);
231
+ }, [setIsWidgetOpen]);
232
+ const focusOnMoreMenuBtn = React.useCallback(() => {
233
+ var _moreMenuBtnRef$curre;
234
+ (_moreMenuBtnRef$curre = moreMenuBtnRef.current) === null || _moreMenuBtnRef$curre === void 0 ? void 0 : _moreMenuBtnRef$curre.focus();
235
+ }, []);
236
+ const onAnchorClick = useCallback(event => {
237
+ if (event.defaultPrevented || event.button !== 0) {
238
+ return;
239
+ }
240
+ setIsWidgetOpen(isWidgetOpen => !isWidgetOpen);
241
+ }, []);
242
+ useOnEscapePress(event => {
243
+ if (isWidgetOpen) {
244
+ event.preventDefault();
245
+ closeOverlay();
246
+ focusOnMoreMenuBtn();
247
+ }
248
+ }, [isWidgetOpen]);
249
+ useOnOutsideClick({
250
+ onClickOutside: closeOverlay,
251
+ containerRef,
252
+ ignoreClickRefs: [moreMenuBtnRef]
253
+ });
254
+ useResizeObserver(resizeObserverEntries => {
255
+ var _moreMenuRef$current$, _moreMenuRef$current;
256
+ const navWidth = resizeObserverEntries[0].contentRect.width;
257
+ const moreMenuWidth = (_moreMenuRef$current$ = (_moreMenuRef$current = moreMenuRef.current) === null || _moreMenuRef$current === void 0 ? void 0 : _moreMenuRef$current.getBoundingClientRect().width) !== null && _moreMenuRef$current$ !== void 0 ? _moreMenuRef$current$ : 0;
258
+ navWidth !== 0 && overflowEffect(navWidth, moreMenuWidth, validChildren, childWidthArray, noIconChildWidthArray, updateListAndMenu);
259
+ }, navRef);
260
+
261
+ // Compute menuInlineStyles if needed
262
+ let menuInlineStyles = {
263
+ ...baseMenuInlineStyles
264
+ };
265
+ if (containerRef.current && listRef.current) {
266
+ const {
267
+ left
268
+ } = getAnchoredPosition(containerRef.current, listRef.current, {
269
+ align: 'start',
270
+ side: 'outside-bottom'
271
+ });
272
+ menuInlineStyles = {
273
+ ...baseMenuInlineStyles,
274
+ right: undefined,
275
+ left
276
+ };
277
+ }
46
278
  return /*#__PURE__*/jsxs(UnderlineNavContext.Provider, {
47
279
  value: {
48
- loadingCounters
280
+ setChildrenWidth,
281
+ setNoIconChildrenWidth,
282
+ loadingCounters,
283
+ iconsVisible
49
284
  },
50
285
  children: [ariaLabel && /*#__PURE__*/jsx(VisuallyHidden, {
51
286
  as: "h2",
52
287
  children: `${ariaLabel} navigation`
53
- }), /*#__PURE__*/jsx("div", {
54
- className: classes.ScrollContainer,
55
- "data-hide-icons": hideIcons,
56
- ref: scrollContainer,
57
- "data-component": "underlinenav-scrollcontainer",
58
- children: /*#__PURE__*/jsx(UnderlineWrapper, {
59
- as: as,
60
- "aria-label": ariaLabel,
61
- className: className,
62
- ref: forwardedRef,
63
- "data-variant": variant,
64
- "data-overflow-mode": "wrap",
65
- style: {
66
- minWidth: 'fit-content'
67
- },
68
- children: /*#__PURE__*/jsx(UnderlineItemList, {
69
- role: "list",
70
- children: children
71
- })
288
+ }), /*#__PURE__*/jsx(UnderlineWrapper, {
289
+ as: as,
290
+ "aria-label": ariaLabel,
291
+ className: className,
292
+ ref: navRef,
293
+ "data-variant": variant,
294
+ "data-overflow-measured": isOverflowMeasured ? 'true' : 'false',
295
+ children: /*#__PURE__*/jsxs(UnderlineItemList, {
296
+ ref: listRef,
297
+ role: "list",
298
+ children: [listItems, menuItems.length > 0 && /*#__PURE__*/jsxs("li", {
299
+ ref: moreMenuRef,
300
+ style: {
301
+ display: 'flex',
302
+ alignItems: 'center',
303
+ height: `${MORE_BTN_HEIGHT}px`
304
+ },
305
+ children: [!onlyMenuVisible && /*#__PURE__*/jsx("div", {
306
+ style: dividerStyles
307
+ }), /*#__PURE__*/jsx(ButtonComponent, {
308
+ ref: moreMenuBtnRef,
309
+ className: classes.MoreButton,
310
+ "aria-controls": disclosureWidgetId,
311
+ "aria-expanded": isWidgetOpen,
312
+ onClick: onAnchorClick,
313
+ trailingAction: TriangleDownIcon,
314
+ children: /*#__PURE__*/jsx("span", {
315
+ children: onlyMenuVisible ? /*#__PURE__*/jsxs(Fragment, {
316
+ children: [/*#__PURE__*/jsxs(VisuallyHidden, {
317
+ as: "span",
318
+ children: [`${ariaLabel}`, "\xA0"]
319
+ }), "Menu"]
320
+ }) : /*#__PURE__*/jsxs(Fragment, {
321
+ children: ["More", /*#__PURE__*/jsxs(VisuallyHidden, {
322
+ as: "span",
323
+ children: ["\xA0", `${ariaLabel} items`]
324
+ })]
325
+ })
326
+ })
327
+ }), /*#__PURE__*/jsx(ActionList, {
328
+ selectionVariant: "single",
329
+ ref: containerRef,
330
+ id: disclosureWidgetId,
331
+ style: {
332
+ ...((_listRef$current2 = listRef.current) !== null && _listRef$current2 !== void 0 && _listRef$current2.clientWidth && listRef.current.clientWidth >= baseMenuMinWidth ? baseMenuInlineStyles : menuInlineStyles),
333
+ display: isWidgetOpen ? 'block' : 'none'
334
+ },
335
+ children: menuItems.map((menuItem, index) => {
336
+ const {
337
+ children: menuItemChildren,
338
+ counter,
339
+ 'aria-current': ariaCurrent,
340
+ onSelect,
341
+ ...menuItemProps
342
+ } = menuItem.props;
343
+
344
+ // This logic is used to pop the selected item out of the menu and into the list when the navigation is control externally
345
+ if (Boolean(ariaCurrent) && ariaCurrent !== 'false') {
346
+ const event = new MouseEvent('click');
347
+ !onlyMenuVisible && swapMenuItemWithListItem(menuItem, index,
348
+ // @ts-ignore - not a big deal because it is internally creating an event but ask help
349
+ event, updateListAndMenu);
350
+ }
351
+ return /*#__PURE__*/jsx(ActionList.LinkItem, {
352
+ style: menuItemStyles,
353
+ onClick: event => {
354
+ // When there are no items in the list, do not run the swap function as we want to keep everything in the menu.
355
+ !onlyMenuVisible && swapMenuItemWithListItem(menuItem, index, event, updateListAndMenu);
356
+ closeOverlay();
357
+ focusOnMoreMenuBtn();
358
+ // fire onSelect event that comes from the UnderlineNav.Item (if it is defined)
359
+ typeof onSelect === 'function' && onSelect(event);
360
+ },
361
+ ...menuItemProps,
362
+ children: /*#__PURE__*/jsxs("span", {
363
+ className: classes.MenuItemContent,
364
+ children: [menuItemChildren, loadingCounters ? /*#__PURE__*/jsx(LoadingCounter, {}) : counter !== undefined && /*#__PURE__*/jsx("span", {
365
+ "data-component": "counter",
366
+ children: /*#__PURE__*/jsx(CounterLabel, {
367
+ children: counter
368
+ })
369
+ })]
370
+ })
371
+ }, menuItemChildren);
372
+ })
373
+ })]
374
+ })]
72
375
  })
73
376
  })]
74
377
  });
75
378
  });
76
379
  UnderlineNav.displayName = 'UnderlineNav';
77
380
 
78
- export { UnderlineNav };
381
+ export { MORE_BTN_WIDTH, UnderlineNav, getValidChildren };
@@ -1,5 +1,5 @@
1
- import './UnderlineNav-4c80468c.css';
1
+ import './UnderlineNav-4344d9b0.css';
2
2
 
3
- var classes = {"ScrollContainer":"prc-UnderlineNav-ScrollContainer-kBvfS","detect-overflow":"prc-UnderlineNav-detect-overflow-6HuSH"};
3
+ var classes = {"MenuItemContent":"prc-UnderlineNav-MenuItemContent-3J7gB","MoreButton":"prc-UnderlineNav-MoreButton-Y8soj"};
4
4
 
5
5
  export { classes as default };
@@ -1,4 +1,14 @@
1
- export declare const UnderlineNavContext: import("react").Context<{
1
+ import type React from 'react';
2
+ export declare const UnderlineNavContext: React.Context<{
3
+ setChildrenWidth: React.Dispatch<{
4
+ text: string;
5
+ width: number;
6
+ }>;
7
+ setNoIconChildrenWidth: React.Dispatch<{
8
+ text: string;
9
+ width: number;
10
+ }>;
2
11
  loadingCounters: boolean;
12
+ iconsVisible: boolean;
3
13
  }>;
4
14
  //# sourceMappingURL=UnderlineNavContext.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"UnderlineNavContext.d.ts","sourceRoot":"","sources":["../../src/UnderlineNav/UnderlineNavContext.tsx"],"names":[],"mappings":"AAEA,eAAO,MAAM,mBAAmB;qBACb,OAAO;EAGxB,CAAA"}
1
+ {"version":3,"file":"UnderlineNavContext.d.ts","sourceRoot":"","sources":["../../src/UnderlineNav/UnderlineNavContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAG9B,eAAO,MAAM,mBAAmB;sBACZ,KAAK,CAAC,QAAQ,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAC,CAAC;4BACvC,KAAK,CAAC,QAAQ,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAC,CAAC;qBACpD,OAAO;kBACV,OAAO;EAMrB,CAAA"}
@@ -1,7 +1,10 @@
1
1
  import { createContext } from 'react';
2
2
 
3
3
  const UnderlineNavContext = /*#__PURE__*/createContext({
4
- loadingCounters: false
4
+ setChildrenWidth: () => null,
5
+ setNoIconChildrenWidth: () => null,
6
+ loadingCounters: false,
7
+ iconsVisible: true
5
8
  });
6
9
 
7
10
  export { UnderlineNavContext };
@@ -1 +1 @@
1
- {"version":3,"file":"UnderlineNavItem.d.ts","sourceRoot":"","sources":["../../src/UnderlineNav/UnderlineNavItem.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAuC,MAAM,OAAO,CAAA;AAC3D,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,wBAAwB,CAAA;AACrD,OAAO,KAA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AC,GAAG,CAAC,CAAA;IAEnE;;OAEG;IACH,aAAa,CAAC,EAAE,KAAK,CAAC,YAAY,CAAA;IAElC;;QAEI;IACJ,EAAE,CAAC,EAAE,KAAK,CAAC,WAAW,GAAG,GAAG,CAAA;IAE5B;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;CAC1B,GAAG,SAAS,CAAA;AAEb,eAAO,MAAM,gBAAgB,EAgFxB,8BAA8B,CAAC,GAAG,EAAE,qBAAqB,CAAC,CAAA"}
@@ -1,42 +1,46 @@
1
1
  import React, { forwardRef, useRef, useContext } from 'react';
2
2
  import { UnderlineNavContext } from './UnderlineNavContext.js';
3
+ import useIsomorphicLayoutEffect from '../utils/useIsomorphicLayoutEffect.js';
3
4
  import { UnderlineItem } from '../internal/components/UnderlineTabbedInterface.js';
4
5
  import classes from './UnderlineNavItem.module.css.js';
5
- import useIsomorphicLayoutEffect from '../utils/useIsomorphicLayoutEffect.js';
6
6
  import { jsx } from 'react/jsx-runtime';
7
7
 
8
- function scrollIntoViewHorizontally(container, descendant) {
9
- // Walk up the offset parent chain to get the true left offset relative to `container`
10
- let offsetLeft = 0;
11
- let el = descendant;
12
- while (el && el !== container) {
13
- offsetLeft += el.offsetLeft;
14
- el = el.offsetParent;
15
- }
16
-
17
- // scrollIntoView would be more convenient but would scroll the entire page unless we pass `options.container`,
18
- // for which browser support is very limited
19
- const descendantLeft = offsetLeft - container.scrollLeft;
20
- const descendantRight = descendantLeft + descendant.offsetWidth;
21
- const containerWidth = container.clientWidth;
22
- if (descendantLeft < 0) container.scrollLeft += descendantLeft;else if (descendantRight > containerWidth) container.scrollLeft += descendantRight - containerWidth;
23
- }
24
- const UnderlineNavItem = /*#__PURE__*/forwardRef((allProps, forwardedRef) => {
25
- const {
26
- as: Component = 'a',
27
- href = '#',
28
- children,
29
- counter,
30
- onSelect,
31
- 'aria-current': ariaCurrent,
32
- icon: Icon,
33
- leadingVisual,
34
- ...props
35
- } = allProps;
36
- const ref = useRef(null);
8
+ const UnderlineNavItem = /*#__PURE__*/forwardRef(({
9
+ as: Component = 'a',
10
+ href = '#',
11
+ children,
12
+ counter,
13
+ onSelect,
14
+ 'aria-current': ariaCurrent,
15
+ icon: Icon,
16
+ leadingVisual,
17
+ ...props
18
+ }, forwardedRef) => {
19
+ const backupRef = useRef(null);
20
+ const ref = forwardedRef !== null && forwardedRef !== void 0 ? forwardedRef : backupRef;
37
21
  const {
38
- loadingCounters
22
+ setChildrenWidth,
23
+ setNoIconChildrenWidth,
24
+ loadingCounters,
25
+ iconsVisible
39
26
  } = useContext(UnderlineNavContext);
27
+ useIsomorphicLayoutEffect(() => {
28
+ if (ref.current) {
29
+ const domRect = ref.current.getBoundingClientRect();
30
+ const icon = Array.from(ref.current.children).find(child => child.getAttribute('data-component') === 'icon');
31
+ const content = Array.from(ref.current.children).find(child => child.getAttribute('data-component') === 'text');
32
+ const text = content.textContent;
33
+ const iconWidthWithMargin = icon ? icon.getBoundingClientRect().width + Number(getComputedStyle(icon).marginRight.slice(0, -2)) + Number(getComputedStyle(icon).marginLeft.slice(0, -2)) : 0;
34
+ setChildrenWidth({
35
+ text,
36
+ width: domRect.width
37
+ });
38
+ setNoIconChildrenWidth({
39
+ text,
40
+ width: domRect.width - iconWidthWithMargin
41
+ });
42
+ }
43
+ }, [ref, setChildrenWidth, setNoIconChildrenWidth]);
40
44
  const keyDownHandler = React.useCallback(event => {
41
45
  if ((event.key === ' ' || event.key === 'Enter') && !event.defaultPrevented && typeof onSelect === 'function') {
42
46
  onSelect(event);
@@ -47,18 +51,10 @@ const UnderlineNavItem = /*#__PURE__*/forwardRef((allProps, forwardedRef) => {
47
51
  onSelect(event);
48
52
  }
49
53
  }, [onSelect]);
50
- useIsomorphicLayoutEffect(() => {
51
- if (ariaCurrent && ariaCurrent !== 'false' && ref.current) {
52
- const scrollContainer = ref.current.closest('[data-component="underlinenav-scrollcontainer"]');
53
- if (!(scrollContainer instanceof HTMLElement)) return;
54
- scrollIntoViewHorizontally(scrollContainer, ref.current);
55
- }
56
- }, [ariaCurrent]);
57
54
  return /*#__PURE__*/jsx("li", {
58
55
  className: classes.UnderlineNavItem,
59
- ref: ref,
60
56
  children: /*#__PURE__*/jsx(UnderlineItem, {
61
- ref: forwardedRef,
57
+ ref: ref,
62
58
  as: Component,
63
59
  href: href,
64
60
  "aria-current": ariaCurrent,
@@ -67,6 +63,7 @@ const UnderlineNavItem = /*#__PURE__*/forwardRef((allProps, forwardedRef) => {
67
63
  counter: counter,
68
64
  icon: leadingVisual !== null && leadingVisual !== void 0 ? leadingVisual : Icon,
69
65
  loadingCounters: loadingCounters,
66
+ iconsVisible: iconsVisible,
70
67
  ...props,
71
68
  children: children
72
69
  })
@@ -1,6 +1,6 @@
1
1
  import type { UnderlineNavProps } from './UnderlineNav';
2
2
  import type { UnderlineNavItemProps } from './UnderlineNavItem';
3
- declare const UnderlineNav: import("react").ForwardRefExoticComponent<UnderlineNavProps & import("react").RefAttributes<HTMLElement>> & {
3
+ declare const UnderlineNav: import("react").ForwardRefExoticComponent<UnderlineNavProps & import("react").RefAttributes<unknown>> & {
4
4
  Item: import("../utils/polymorphic").ForwardRefComponent<"a", UnderlineNavItemProps>;
5
5
  };
6
6
  export { UnderlineNav };
@@ -0,0 +1,16 @@
1
+ export declare const dividerStyles: {
2
+ display: string;
3
+ borderLeft: string;
4
+ width: string;
5
+ borderLeftColor: string;
6
+ marginRight: string;
7
+ height: string;
8
+ };
9
+ export declare const menuItemStyles: {
10
+ '& > span': {
11
+ display: string;
12
+ };
13
+ textDecoration: string;
14
+ };
15
+ export declare const baseMenuMinWidth = 192;
16
+ //# sourceMappingURL=styles.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"styles.d.ts","sourceRoot":"","sources":["../../src/UnderlineNav/styles.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,aAAa;;;;;;;CAOzB,CAAA;AAED,eAAO,MAAM,cAAc;;;;;CAO1B,CAAA;AAED,eAAO,MAAM,gBAAgB,MAAM,CAAA"}
@@ -0,0 +1,19 @@
1
+ const dividerStyles = {
2
+ display: 'inline-block',
3
+ borderLeft: '1px solid',
4
+ width: '1px',
5
+ borderLeftColor: 'var(--borderColor-muted)',
6
+ marginRight: 'var(--base-size-4)',
7
+ height: '24px' // The height of the divider - reference from Figma
8
+ };
9
+ const menuItemStyles = {
10
+ // This is needed to hide the selected check icon on the menu item. https://github.com/primer/react/tree/main/packages/react/src/ActionList/Selection.tsx#L32
11
+ '& > span': {
12
+ display: 'none'
13
+ },
14
+ // To reset the style when the menu items are rendered as react router links
15
+ textDecoration: 'none'
16
+ };
17
+ const baseMenuMinWidth = 192;
18
+
19
+ export { baseMenuMinWidth, dividerStyles, menuItemStyles };
@@ -0,0 +1,2 @@
1
+ .prc-UnderlinePanels-StyledUnderlineWrapper-aiLna{overflow-x:auto;overflow-y:hidden;width:100%;-webkit-overflow-scrolling:auto}.prc-UnderlinePanels-StyledUnderlineWrapper-aiLna[data-icons-visible=false] [data-component=icon]{display:none}
2
+ /*# sourceMappingURL=UnderlinePanels-e4b325b9.css.map */
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/experimental/UnderlinePanels/UnderlinePanels.module.css.js"],"names":[],"mappings":"AAAA,kDAEE,eAAgB,CAChB,iBAAkB,CAFlB,UAAW,CAGX,+BACF,CAEA,kGACE,YACF","file":"UnderlinePanels-e4b325b9.css","sourcesContent":[".StyledUnderlineWrapper {\n width: 100%;\n overflow-x: auto;\n overflow-y: hidden;\n -webkit-overflow-scrolling: auto;\n}\n\n.StyledUnderlineWrapper[data-icons-visible='false'] [data-component='icon'] {\n display: none;\n}\n"]}
@@ -94,7 +94,7 @@ const UnderlinePanels = ({
94
94
  children: [/*#__PURE__*/jsx(UnderlineWrapper, {
95
95
  ref: wrapperRef,
96
96
  slot: "tablist-wrapper",
97
- "data-hide-icons": !iconsVisible,
97
+ "data-icons-visible": iconsVisible,
98
98
  className: clsx(className, classes.StyledUnderlineWrapper),
99
99
  ...props,
100
100
  children: /*#__PURE__*/jsx(UnderlineItemList, {
@@ -215,7 +215,6 @@ var UnderlinePanels_default = Object.assign(UnderlinePanels, {
215
215
  Panel,
216
216
  Tab
217
217
  });
218
- UnderlinePanels.__SLOT__ = Symbol('UnderlinePanels');
219
218
  Tab.__SLOT__ = Symbol('UnderlinePanels.Tab');
220
219
  Panel.__SLOT__ = Symbol('UnderlinePanels.Panel');
221
220