| 1 |
import { createContext, useRef } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Drawer/drawer'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
|
| 5 |
export enum DrawerColorVariant { |
| 6 |
default = 'default', |
| 7 |
secondary = 'secondary', |
| 8 |
/** |
| 9 |
* @deprecated `DrawerColorVariant.noBackground` is deprecated. Use the `isPlain` prop on `DrawerPanelContent` and the `DrawerSection`instead. |
| 10 |
*/ |
| 11 |
noBackground = 'no-background' |
| 12 |
} |
| 13 |
|
| 14 |
export interface DrawerProps extends React.HTMLProps<HTMLDivElement> { |
| 15 |
/** Additional classes added to the Drawer. */ |
| 16 |
className?: string; |
| 17 |
/** Content rendered in the drawer panel */ |
| 18 |
children?: React.ReactNode; |
| 19 |
/** Indicates if the drawer is expanded */ |
| 20 |
isExpanded?: boolean; |
| 21 |
/** Indicates if the content element and panel element are displayed side by side. */ |
| 22 |
isInline?: boolean; |
| 23 |
/** @beta Indicates if the drawer will have pill styles */ |
| 24 |
isPill?: boolean; |
| 25 |
/** Indicates if the drawer will always show both content and panel. */ |
| 26 |
isStatic?: boolean; |
| 27 |
/** Position of the drawer panel. left and right are deprecated, use start and end instead. */ |
| 28 |
position?: 'start' | 'end' | 'bottom' | 'left' | 'right'; |
| 29 |
/** Callback when drawer panel is expanded after waiting 250ms for animation to complete. */ |
| 30 |
onExpand?: (event: KeyboardEvent | React.MouseEvent | React.TransitionEvent) => void; |
| 31 |
} |
| 32 |
|
| 33 |
export interface DrawerContextProps { |
| 34 |
isExpanded: boolean; |
| 35 |
isStatic: boolean; |
| 36 |
onExpand?: (event: KeyboardEvent | React.MouseEvent | React.TransitionEvent) => void; |
| 37 |
position?: string; |
| 38 |
drawerRef?: React.RefObject<HTMLDivElement | null>; |
| 39 |
drawerContentRef?: React.RefObject<HTMLDivElement | null>; |
| 40 |
isInline: boolean; |
| 41 |
} |
| 42 |
|
| 43 |
export const DrawerContext = createContext<Partial<DrawerContextProps>>({ |
| 44 |
isExpanded: false, |
| 45 |
isStatic: false, |
| 46 |
onExpand: () => {}, |
| 47 |
position: 'end', |
| 48 |
drawerRef: null, |
| 49 |
drawerContentRef: null, |
| 50 |
isInline: false |
| 51 |
}); |
| 52 |
|
| 53 |
export const Drawer: React.FunctionComponent<DrawerProps> = ({ |
| 54 |
className = '', |
| 55 |
children, |
| 56 |
isExpanded = false, |
| 57 |
isInline = false, |
| 58 |
isPill = false, |
| 59 |
isStatic = false, |
| 60 |
position = 'end', |
| 61 |
onExpand = () => {}, |
| 62 |
...props |
| 63 |
}: DrawerProps) => { |
| 64 |
const drawerRef = useRef<HTMLDivElement>(undefined); |
| 65 |
const drawerContentRef = useRef<HTMLDivElement>(undefined); |
| 66 |
|
| 67 |
return ( |
| 68 |
<DrawerContext.Provider value={{ isExpanded, isStatic, onExpand, position, drawerRef, drawerContentRef, isInline }}> |
| 69 |
<div |
| 70 |
className={css( |
| 71 |
styles.drawer, |
| 72 |
isExpanded && styles.modifiers.expanded, |
| 73 |
isInline && styles.modifiers.inline, |
| 74 |
isPill && styles.modifiers.pill, |
| 75 |
isStatic && styles.modifiers.static, |
| 76 |
(position === 'left' || position === 'start') && styles.modifiers.panelLeft, |
| 77 |
position === 'bottom' && styles.modifiers.panelBottom, |
| 78 |
className |
| 79 |
)} |
| 80 |
ref={drawerRef} |
| 81 |
{...props} |
| 82 |
> |
| 83 |
{children} |
| 84 |
</div> |
| 85 |
</DrawerContext.Provider> |
| 86 |
); |
| 87 |
}; |
| 88 |
Drawer.displayName = 'Drawer'; |