Skip to content

Commit f9e9c44

Browse files
kmcfaulnicolethoen
authored andcommitted
fix(Page): pass sidebar state to button for isHamburger (#11951)
1 parent a32366d commit f9e9c44

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

packages/react-core/src/components/Page/PageToggleButton.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export interface PageToggleButtonProps extends ButtonProps {
1414
'aria-label'?: string;
1515
/** Flag indicating whether the hamburger button variation with animations should be used. */
1616
isHamburgerButton?: boolean;
17+
/** IsHamburgerButton must be true for hamburgerVariant to be have an effect. Adjusts and animates the hamburger icon to indicate what will happen upon clicking the button. */
18+
hamburgerVariant?: 'expand' | 'collapse';
1719
}
1820

1921
export const PageToggleButton: React.FunctionComponent<PageToggleButtonProps> = ({
@@ -23,6 +25,7 @@ export const PageToggleButton: React.FunctionComponent<PageToggleButtonProps> =
2325
id = 'nav-toggle',
2426
'aria-label': ariaLabel = 'Side navigation toggle',
2527
isHamburgerButton,
28+
hamburgerVariant,
2629
...props
2730
}: PageToggleButtonProps) => (
2831
<PageContextConsumer>
@@ -42,6 +45,10 @@ export const PageToggleButton: React.FunctionComponent<PageToggleButtonProps> =
4245
aria-expanded={sidebarOpen ? 'true' : 'false'}
4346
variant={ButtonVariant.plain}
4447
isHamburger={isHamburgerButton}
48+
hamburgerVariant={hamburgerVariant}
49+
{...(isHamburgerButton && {
50+
isExpanded: sidebarOpen
51+
})}
4552
{...props}
4653
>
4754
{!isHamburgerButton && children}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { render, screen } from '@testing-library/react';
2+
import { PageToggleButton } from '../PageToggleButton';
3+
import { PageContextProvider } from '../PageContext';
4+
5+
test('Renders without children', () => {
6+
render(
7+
<div data-testid="container">
8+
<PageToggleButton />
9+
</div>
10+
);
11+
12+
expect(screen.getByTestId('container').firstChild).toBeVisible();
13+
});
14+
15+
test('Renders with children', () => {
16+
render(<PageToggleButton>Test</PageToggleButton>);
17+
18+
expect(screen.getByText('Test')).toBeVisible();
19+
});
20+
21+
test('Throws console error when isHamburger is true and isSidebarOpen is not passed', () => {
22+
const consoleError = jest.spyOn(console, 'error').mockImplementation();
23+
24+
render(<PageToggleButton isHamburger aria-label="Test" />);
25+
26+
expect(consoleError).toHaveBeenCalledWith(
27+
'Button: when the isHamburger property is passed in, you must also pass in a boolean value to the isExpanded property. It is expected that a hamburger button controls the expansion of other content.'
28+
);
29+
});
30+
31+
test('Does not throw console error when isHamburger is true and isSidebarOpen is false', () => {
32+
const consoleError = jest.spyOn(console, 'error').mockImplementation();
33+
34+
render(<PageToggleButton isSidebarOpen={false} isHamburger aria-label="Test" />);
35+
36+
expect(consoleError).not.toHaveBeenCalledWith(
37+
'Button: when the isHamburger property is passed in, you must also pass in a boolean value to the isExpanded property. It is expected that a hamburger button controls the expansion of other content..'
38+
);
39+
});
40+
41+
test('Does not throw console error when isHamburger is true and isSidebarOpen is true', () => {
42+
const consoleError = jest.spyOn(console, 'error').mockImplementation();
43+
44+
render(<PageToggleButton isSidebarOpen isHamburger aria-label="Test" />);
45+
46+
expect(consoleError).not.toHaveBeenCalledWith(
47+
'Button: when the isHamburger property is passed in, you must also pass in a boolean value to the isExpanded property. It is expected that a hamburger button controls the expansion of other content..'
48+
);
49+
});
50+
51+
// assisted by AI/Cursor
52+
test('Does not throw console error when isHamburger is true, isSidebarOpen is not passed, but managedIsSidebarOpen is true', () => {
53+
const consoleError = jest.spyOn(console, 'error').mockImplementation();
54+
55+
const mockPageContext = {
56+
isManagedSidebar: true,
57+
onSidebarToggle: () => null,
58+
isSidebarOpen: true, // managedIsSidebarOpen is true
59+
width: 1024,
60+
height: 768,
61+
getBreakpoint: () => 'lg' as const,
62+
getVerticalBreakpoint: () => 'lg' as const
63+
};
64+
65+
render(
66+
<PageContextProvider value={mockPageContext}>
67+
<PageToggleButton isHamburger aria-label="Test" />
68+
</PageContextProvider>
69+
);
70+
71+
expect(consoleError).not.toHaveBeenCalledWith(
72+
'Button: when the isHamburger property is passed in, you must also pass in a boolean value to the isExpanded property. It is expected that a hamburger button controls the expansion of other content..'
73+
);
74+
});

0 commit comments

Comments
 (0)