Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/perf-breadcrumbs-cls-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@primer/react': patch
---

perf(Breadcrumbs): Optimize CSS :has() selector and batch offsetWidth reads

- Scope `:has(.MenuOverlay)` to shallow path `> .MenuDetails .MenuOverlay` for O(1) lookup
- Batch all offsetWidth reads in a single pass to avoid layout thrashing
3 changes: 2 additions & 1 deletion packages/react/src/Breadcrumbs/Breadcrumbs.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@
list-style: none;

/* allow menu items to wrap line */
&:has(.MenuOverlay) {
/* PERFORMANCE: MenuOverlay is inside MenuDetails > summary sibling, use shallow path */
&:has(> .MenuDetails .MenuOverlay) {
white-space: normal;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/Breadcrumbs/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ function Breadcrumbs({className, children, style, overflow = 'wrap', variant = '
listElement.children.length === childArray.length
) {
const listElementArray = Array.from(listElement.children) as HTMLElement[]
// Batch all offsetWidth reads in a single pass to avoid layout thrashing
const widths = listElementArray.map(child => child.offsetWidth)
setChildArrayWidths(widths)
setRootItemWidth(listElementArray[0].offsetWidth)
// Use first width from the array instead of reading offsetWidth again
setRootItemWidth(widths[0])
}
}, [childArray, overflowMenuEnabled])

Expand Down
Loading