Skip to content

Commit 07fa8fd

Browse files
committed
Toolbar: apply responsive width styles via setBreakpointCssVars; keep flexGrow via formatBreakpointMods; update ToolbarItem tests; remove extra demo
1 parent b5a2db6 commit 07fa8fd

File tree

3 files changed

+43
-61
lines changed

3 files changed

+43
-61
lines changed

packages/react-core/src/components/Toolbar/ToolbarItem.tsx

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import styles from '@patternfly/react-styles/css/components/Toolbar/toolbar';
22
import { css } from '@patternfly/react-styles';
3-
import { formatBreakpointMods, toCamel } from '../../helpers/util';
3+
import { formatBreakpointMods, setBreakpointCssVars, toCamel } from '../../helpers/util';
4+
import c_toolbar__item_Width from '@patternfly/react-tokens/dist/esm/c_toolbar__item_Width';
5+
import c_toolbar__item_m_w_sm_Width from '@patternfly/react-tokens/dist/esm/c_toolbar__item_m_w_sm_Width';
6+
import c_toolbar__item_m_w_md_Width from '@patternfly/react-tokens/dist/esm/c_toolbar__item_m_w_md_Width';
7+
import c_toolbar__item_m_w_lg_Width from '@patternfly/react-tokens/dist/esm/c_toolbar__item_m_w_lg_Width';
8+
import c_toolbar__item_m_w_xl_Width from '@patternfly/react-tokens/dist/esm/c_toolbar__item_m_w_xl_Width';
9+
import c_toolbar__item_m_w_2xl_Width from '@patternfly/react-tokens/dist/esm/c_toolbar__item_m_w_2xl_Width';
10+
import c_toolbar__item_m_w_3xl_Width from '@patternfly/react-tokens/dist/esm/c_toolbar__item_m_w_3xl_Width';
11+
import c_toolbar__item_m_w_4xl_Width from '@patternfly/react-tokens/dist/esm/c_toolbar__item_m_w_4xl_Width';
412
import { Divider } from '../Divider';
513
import { PageContext } from '../Page/PageContext';
614

@@ -206,6 +214,7 @@ export const ToolbarItem: React.FunctionComponent<ToolbarItemProps> = ({
206214
widths,
207215
flexGrow,
208216
role,
217+
style,
209218
...props
210219
}: ToolbarItemProps) => {
211220
if (variant === ToolbarItemVariant.separator) {
@@ -243,27 +252,38 @@ export const ToolbarItem: React.FunctionComponent<ToolbarItemProps> = ({
243252
formatBreakpointMods(rowGap, styles, '', getBreakpoint(width)),
244253
formatBreakpointMods(rowWrap, styles, '', getBreakpoint(width)),
245254
className,
246-
widths &&
247-
Object.entries(widths).reduce(
248-
(acc, [bp, size]) => ({
249-
...acc,
250-
[`pf-m-w-${size}${bp !== 'default' ? `-on-${bp}` : ''}`]: true
251-
}),
252-
{}
253-
),
254-
flexGrow &&
255-
Object.entries(flexGrow).reduce(
256-
(acc, [bp]) => ({
257-
...acc,
258-
[`pf-m-flex-grow${bp !== 'default' ? `-on-${bp}` : ''}`]: true
259-
}),
260-
{}
261-
)
255+
formatBreakpointMods(flexGrow, styles, '', getBreakpoint(width))
262256
)}
257+
style={{
258+
...style,
259+
...(widths
260+
? setBreakpointCssVars(
261+
Object.entries(widths).reduce(
262+
(acc, [bp, size]) => {
263+
if (!size) {
264+
return acc;
265+
}
266+
const valueMap: Record<string, string> = {
267+
sm: (c_toolbar__item_m_w_sm_Width as any)?.value,
268+
md: (c_toolbar__item_m_w_md_Width as any)?.value,
269+
lg: (c_toolbar__item_m_w_lg_Width as any)?.value,
270+
xl: (c_toolbar__item_m_w_xl_Width as any)?.value,
271+
'2xl': (c_toolbar__item_m_w_2xl_Width as any)?.value,
272+
'3xl': (c_toolbar__item_m_w_3xl_Width as any)?.value,
273+
'4xl': (c_toolbar__item_m_w_4xl_Width as any)?.value
274+
};
275+
const tokenValue = valueMap[size as keyof typeof valueMap];
276+
return tokenValue ? { ...acc, [bp]: tokenValue } : acc;
277+
},
278+
{} as Record<string, string>
279+
),
280+
(c_toolbar__item_Width as any).name
281+
)
282+
: undefined)
283+
}}
263284
{...(variant === 'label' && { 'aria-hidden': true })}
264285
id={id}
265286
role={role}
266-
data-testid="toolbaritem"
267287
{...props}
268288
>
269289
{children}

packages/react-core/src/components/Toolbar/__tests__/ToolbarItem.test.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { render, screen } from '@testing-library/react';
22
import { ToolbarItem } from '../ToolbarItem';
3+
import c_toolbar__item_Width from '@patternfly/react-tokens/dist/esm/c_toolbar__item_Width';
34

45
describe('ToolbarItem', () => {
56
it('should render with pf-m-overflow-container when isOverflowContainer is set', () => {
@@ -43,14 +44,15 @@ describe('ToolbarItem', () => {
4344
const sizes = ['sm', 'md', 'lg', 'xl', '2xl', '3xl', '4xl'];
4445

4546
describe.each(bps)('widths at various breakpoints', (bp) => {
46-
it.each(sizes)(`should render with pf-m-w-%s when widths is set to %s at ${bp}`, (size) => {
47+
it.each(sizes)(`applies width CSS var when widths is set to %s at ${bp}`, (size) => {
4748
render(
4849
<ToolbarItem data-testid="toolbaritem" widths={{ [bp]: size }}>
4950
Test
5051
</ToolbarItem>
5152
);
52-
const bpWidthClass = bp === 'default' ? `pf-m-w-${size}` : `pf-m-w-${size}-on-${bp}`;
53-
expect(screen.getByTestId('toolbaritem')).toHaveClass(bpWidthClass);
53+
const styleAttr = screen.getByTestId('toolbaritem').getAttribute('style') || '';
54+
const cssVarName = `${(c_toolbar__item_Width as any).name}${bp === 'default' ? '' : `-on-${bp}`}`;
55+
expect(styleAttr).toContain(cssVarName);
5456
});
5557
});
5658
});

packages/react-integration/demo-app-ts/src/components/demos/ToolbarDemo.tsx

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)