@@ -81,8 +81,9 @@ const getValidChildren = (children: React.ReactNode) => {
81
81
82
82
function Breadcrumbs ( { className, children, sx : sxProp , overflow = 'wrap' , hideRoot = true } : BreadcrumbsProps ) {
83
83
const containerRef = useRef < HTMLElement > ( null )
84
+ const menuButtonRef = useRef < HTMLButtonElement > ( null )
84
85
const [ effectiveHideRoot , setEffectiveHideRoot ] = useState < boolean > ( hideRoot )
85
- let effectiveOverflow = 'wrap'
86
+ // let effectiveOverflow = 'wrap'
86
87
const childArray = useMemo ( ( ) => getValidChildren ( children ) , [ children ] )
87
88
88
89
const rootItem = childArray [ 0 ]
@@ -93,9 +94,13 @@ function Breadcrumbs({className, children, sx: sxProp, overflow = 'wrap', hideRo
93
94
const [ menuItems , setMenuItems ] = useState < React . ReactElement [ ] > ( [ ] )
94
95
const [ rootItemWidth , setRootItemWidth ] = useState < number > ( 0 )
95
96
96
- if ( typeof window !== 'undefined' ) {
97
- effectiveOverflow = overflow
98
- }
97
+ // Menu button width with fallback
98
+ const MENU_BUTTON_FALLBACK_WIDTH = 32 // Design system small IconButton
99
+ const [ menuButtonWidth , setMenuButtonWidth ] = useState ( MENU_BUTTON_FALLBACK_WIDTH )
100
+
101
+ // if (typeof window !== 'undefined') {
102
+ // effectiveOverflow = overflow
103
+ // }
99
104
const MIN_VISIBLE_ITEMS = ! effectiveHideRoot ? 3 : 4
100
105
101
106
useEffect ( ( ) => {
@@ -108,9 +113,19 @@ function Breadcrumbs({className, children, sx: sxProp, overflow = 'wrap', hideRo
108
113
}
109
114
} , [ childArray . length ] )
110
115
116
+ // Measure actual menu button width when it exists
117
+ useEffect ( ( ) => {
118
+ if ( menuButtonRef . current ) {
119
+ const measuredWidth = menuButtonRef . current . offsetWidth
120
+ if ( measuredWidth > 0 ) {
121
+ setMenuButtonWidth ( measuredWidth )
122
+ }
123
+ }
124
+ } , [ menuItems . length ] ) // Re-measure when menu button appears/disappears
125
+
111
126
const calculateOverflow = useCallback (
112
127
( availableWidth : number ) => {
113
- const MENU_BUTTON_WIDTH = 50 // Approximate width of "..." button
128
+ const MENU_BUTTON_WIDTH = menuButtonWidth // Use measured width with fallback
114
129
115
130
const calculateVisibleItemsWidth = ( w : number [ ] ) => {
116
131
const widths = w . reduce ( ( sum , width ) => sum + width + 16 , 0 )
@@ -165,7 +180,16 @@ function Breadcrumbs({className, children, sx: sxProp, overflow = 'wrap', hideRo
165
180
effectiveHideRoot : eHideRoot ,
166
181
}
167
182
} ,
168
- [ MIN_VISIBLE_ITEMS , childArray , childArrayWidths , effectiveHideRoot , hideRoot , overflow , rootItemWidth ] ,
183
+ [
184
+ MIN_VISIBLE_ITEMS ,
185
+ childArray ,
186
+ childArrayWidths ,
187
+ effectiveHideRoot ,
188
+ hideRoot ,
189
+ overflow ,
190
+ rootItemWidth ,
191
+ menuButtonWidth ,
192
+ ] ,
169
193
)
170
194
171
195
const handleResize = useCallback (
@@ -197,9 +221,9 @@ function Breadcrumbs({className, children, sx: sxProp, overflow = 'wrap', hideRo
197
221
198
222
// Determine final children to render
199
223
const finalChildren = React . useMemo ( ( ) => {
200
- if ( effectiveOverflow === 'wrap' || menuItems . length === 0 ) {
224
+ if ( overflow === 'wrap' || menuItems . length === 0 ) {
201
225
return visibleItems . map ( ( child , index ) => (
202
- < li className = { classes . ItemWrapper } key = { `visible + ${ index } ` } >
226
+ < li className = { classes . BreadcrumbsItem } key = { `visible + ${ index } ` } >
203
227
{ child }
204
228
</ li >
205
229
) )
@@ -210,22 +234,23 @@ function Breadcrumbs({className, children, sx: sxProp, overflow = 'wrap', hideRo
210
234
effectiveMenuItems = [ ...menuItems . slice ( 1 ) ]
211
235
}
212
236
const menuElement = (
213
- < li className = { classes . ItemWrapper } key = "breadcrumbs-menu" >
237
+ < li className = { classes . BreadcrumbsItem } key = "breadcrumbs-menu" >
214
238
< BreadcrumbsMenuItem
239
+ ref = { menuButtonRef }
215
240
items = { effectiveMenuItems }
216
241
aria-label = { `${ effectiveMenuItems . length } more breadcrumb items` }
217
242
/>
218
243
</ li >
219
244
)
220
245
221
246
const visibleElements = visibleItems . map ( ( child , index ) => (
222
- < li className = { classes . ItemWrapper } key = { `visible + ${ index } ` } >
247
+ < li className = { classes . BreadcrumbsItem } key = { `visible + ${ index } ` } >
223
248
{ child }
224
249
</ li >
225
250
) )
226
251
227
252
const rootElement = (
228
- < li className = { classes . ItemWrapper } key = { `rootElement` } >
253
+ < li className = { classes . BreadcrumbsItem } key = { `rootElement` } >
229
254
{ rootItem }
230
255
</ li >
231
256
)
@@ -238,7 +263,7 @@ function Breadcrumbs({className, children, sx: sxProp, overflow = 'wrap', hideRo
238
263
// Show: [root breadcrumb, overflow menu, leaf breadcrumb]
239
264
return [ rootElement , menuElement , ...visibleElements ]
240
265
}
241
- } , [ effectiveOverflow , menuItems , visibleItems , rootItem , effectiveHideRoot ] )
266
+ } , [ overflow , menuItems , effectiveHideRoot , visibleItems , rootItem ] )
242
267
243
268
return (
244
269
< BoxWithFallback
@@ -247,7 +272,7 @@ function Breadcrumbs({className, children, sx: sxProp, overflow = 'wrap', hideRo
247
272
aria-label = "Breadcrumbs"
248
273
sx = { sxProp }
249
274
ref = { containerRef }
250
- data-overflow = { effectiveOverflow }
275
+ data-overflow = { overflow }
251
276
>
252
277
< BreadcrumbsList > { finalChildren } </ BreadcrumbsList >
253
278
</ BoxWithFallback >
0 commit comments