Skip to content

Commit bbf0352

Browse files
authored
fix(Toolbar): improve numberOfAlwaysVisibleItems behavior with spacers (#5367)
1 parent 49708a5 commit bbf0352

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

packages/main/src/components/DynamicPageTitle/ActionsSpacer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const ActionsSpacer = ({ onClick, noHover }: ActionsSpacerProps) => {
1212
style={{ flexGrow: 1, height: '100%', cursor: noHover ? 'auto' : 'pointer' }}
1313
className="spacer"
1414
onClick={onClick}
15+
data-component-name="ToolbarSpacer"
1516
/>
1617
);
1718
};

packages/main/src/components/Toolbar/index.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ export interface ToolbarPropTypes extends Omit<CommonProps, 'onClick' | 'childre
123123
}) => void;
124124
}
125125

126+
function getSpacerWidths(ref) {
127+
if (!ref) {
128+
return 0;
129+
}
130+
131+
let spacerWidths = 0;
132+
if (ref.dataset.componentName === 'ToolbarSpacer') {
133+
spacerWidths += ref.offsetWidth;
134+
}
135+
return spacerWidths + getSpacerWidths(ref.previousElementSibling);
136+
}
137+
126138
const OVERFLOW_BUTTON_WIDTH = 36 + 8 + 8; // width + padding end + spacing start
127139

128140
/**
@@ -218,10 +230,17 @@ const Toolbar = forwardRef<HTMLDivElement, ToolbarPropTypes>((props, ref) => {
218230
let lastElementResizeObserver;
219231
const lastElement = contentRef.current.children[numberOfAlwaysVisibleItems - 1];
220232
const debouncedObserverFn = debounce(() => {
233+
const spacerWidth = getSpacerWidths(lastElement);
221234
if (isRtl) {
222-
setMinWidth(`${lastElement.offsetParent.offsetWidth - lastElement.offsetLeft + OVERFLOW_BUTTON_WIDTH}px`);
235+
setMinWidth(
236+
`${lastElement.offsetParent.offsetWidth - lastElement.offsetLeft + OVERFLOW_BUTTON_WIDTH - spacerWidth}px`
237+
);
223238
} else {
224-
setMinWidth(`${lastElement.offsetLeft + lastElement.getBoundingClientRect().width + OVERFLOW_BUTTON_WIDTH}px`);
239+
setMinWidth(
240+
`${
241+
lastElement.offsetLeft + lastElement.getBoundingClientRect().width + OVERFLOW_BUTTON_WIDTH - spacerWidth
242+
}px`
243+
);
225244
}
226245
}, 200);
227246
if (numberOfAlwaysVisibleItems && overflowNeeded && lastElement) {

packages/main/src/components/ToolbarSpacer/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type ToolbarSpacerPropTypes = CommonProps;
88
* __Note:__ This component is only compatible with the `Toolbar` component and __not__ with `ToolbarV2`. If you're using `ToolbarV2`, please use `ToolbarSpacerV2` instead.
99
*/
1010
const ToolbarSpacer = forwardRef<HTMLSpanElement, ToolbarSpacerPropTypes>((props, ref) => {
11-
return <span ref={ref} style={{ flexGrow: 1 }} className="spacer" {...props} />;
11+
return <span ref={ref} style={{ flexGrow: 1 }} className="spacer" {...props} data-component-name="ToolbarSpacer" />;
1212
});
1313

1414
ToolbarSpacer.displayName = 'ToolbarSpacer';

0 commit comments

Comments
 (0)