| 1 |
import { createContext } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Page/page'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { PageContextConsumer } from './PageContext'; |
| 5 |
|
| 6 |
export interface PageSidebarProps extends React.HTMLProps<HTMLDivElement> { |
| 7 |
/** Additional classes added to the page sidebar */ |
| 8 |
className?: string; |
| 9 |
/** Content rendered inside the page sidebar (e.g. <PageSidebarBody /> */ |
| 10 |
children?: React.ReactNode; |
| 11 |
/** |
| 12 |
* If true, manages the sidebar open/close state and there is no need to pass the isSidebarOpen boolean into |
| 13 |
* the sidebar component or add a callback onSidebarToggle function into the Masthead component |
| 14 |
*/ |
| 15 |
isManagedSidebar?: boolean; |
| 16 |
/** Programmatically manage if the sidebar is shown, if isManagedSidebar is set to true in the Page component, this prop is managed */ |
| 17 |
isSidebarOpen?: boolean; |
| 18 |
/** Sidebar id */ |
| 19 |
id?: string; |
| 20 |
} |
| 21 |
|
| 22 |
export interface PageSidebarContextProps { |
| 23 |
isSidebarOpen: boolean; |
| 24 |
} |
| 25 |
export const pageSidebarContextDefaults: PageSidebarContextProps = { |
| 26 |
isSidebarOpen: true |
| 27 |
}; |
| 28 |
export const PageSidebarContext = createContext<Partial<PageSidebarContextProps>>(pageSidebarContextDefaults); |
| 29 |
|
| 30 |
export const PageSidebar: React.FunctionComponent<PageSidebarProps> = ({ |
| 31 |
className = '', |
| 32 |
children, |
| 33 |
isSidebarOpen = true, |
| 34 |
id = 'page-sidebar', |
| 35 |
...props |
| 36 |
}: PageSidebarProps) => ( |
| 37 |
<PageContextConsumer> |
| 38 |
{({ isManagedSidebar, isSidebarOpen: managedIsNavOpen, isMobile }) => { |
| 39 |
const sidebarOpen = isManagedSidebar ? managedIsNavOpen : isSidebarOpen; |
| 40 |
|
| 41 |
return ( |
| 42 |
<div |
| 43 |
id={id} |
| 44 |
className={css( |
| 45 |
styles.pageSidebar, |
| 46 |
sidebarOpen && isMobile && styles.modifiers.expanded, |
| 47 |
!sidebarOpen && styles.modifiers.collapsed, |
| 48 |
className |
| 49 |
)} |
| 50 |
aria-hidden={!sidebarOpen} |
| 51 |
{...props} |
| 52 |
> |
| 53 |
<PageSidebarContext.Provider value={{ isSidebarOpen: sidebarOpen }}> |
| 54 |
<div className={css(styles.pageSidebarMain)}>{children}</div> |
| 55 |
</PageSidebarContext.Provider> |
| 56 |
</div> |
| 57 |
); |
| 58 |
}} |
| 59 |
</PageContextConsumer> |
| 60 |
); |
| 61 |
PageSidebar.displayName = 'PageSidebar'; |