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