-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathPageToggleButton.tsx
More file actions
60 lines (57 loc) · 2.18 KB
/
PageToggleButton.tsx
File metadata and controls
60 lines (57 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Button, ButtonProps, ButtonVariant } from '../../components/Button';
import { PageContextConsumer, PageContextProps } from './PageContext';
export interface PageToggleButtonProps extends ButtonProps {
/** Content of the page toggle button */
children?: React.ReactNode;
/** True if the sidebar is shown */
isSidebarOpen?: boolean;
/** Callback function to handle the sidebar toggle button, managed by the Page component if the Page isManagedSidebar prop is set to true */
onSidebarToggle?: () => void;
/** Button id */
id?: string;
/** Adds an accessible name to the toggle button. */
'aria-label'?: string;
/** Flag indicating whether the hamburger button variation with animations should be used. */
isHamburgerButton?: boolean;
/** 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. */
hamburgerVariant?: 'expand' | 'collapse';
}
export const PageToggleButton: React.FunctionComponent<PageToggleButtonProps> = ({
children,
isSidebarOpen = true,
onSidebarToggle = () => undefined as any,
id = 'nav-toggle',
'aria-label': ariaLabel = 'Side navigation toggle',
isHamburgerButton,
hamburgerVariant,
...props
}: PageToggleButtonProps) => (
<PageContextConsumer>
{({
isManagedSidebar,
onSidebarToggle: managedOnSidebarToggle,
isSidebarOpen: managedIsSidebarOpen
}: PageContextProps) => {
const sidebarToggle = isManagedSidebar ? managedOnSidebarToggle : onSidebarToggle;
const sidebarOpen = isManagedSidebar ? managedIsSidebarOpen : isSidebarOpen;
return (
<Button
id={id}
onClick={sidebarToggle}
aria-label={ariaLabel}
aria-expanded={sidebarOpen ? 'true' : 'false'}
variant={ButtonVariant.plain}
isHamburger={isHamburgerButton}
hamburgerVariant={hamburgerVariant}
{...(isHamburgerButton && {
isExpanded: sidebarOpen
})}
{...props}
>
{!isHamburgerButton && children}
</Button>
);
}}
</PageContextConsumer>
);
PageToggleButton.displayName = 'PageToggleButton';