From 71be55b0292773442259bc6a746e542dcbde4ec4 Mon Sep 17 00:00:00 2001 From: Titani Date: Thu, 14 Sep 2023 17:30:16 -0400 Subject: [PATCH] feat(Drawer): Added start and end to position props, updaed resizing to work with RTL --- .../src/components/Drawer/Drawer.tsx | 12 +-- .../components/Drawer/DrawerPanelContent.tsx | 98 ++++++++++++++----- .../src/components/Drawer/examples/Drawer.md | 24 ++--- ...anelRight.tsx => DrawerInlinePanelEnd.tsx} | 2 +- ...nelLeft.tsx => DrawerInlinePanelStart.tsx} | 4 +- ...DrawerPanelLeft.tsx => DrawerPanelEnd.tsx} | 4 +- ...werPanelRight.tsx => DrawerPanelStart.tsx} | 4 +- ...leOnRight.tsx => DrawerResizableAtEnd.tsx} | 6 +- ...eOnLeft.tsx => DrawerResizableAtStart.tsx} | 4 +- 9 files changed, 102 insertions(+), 56 deletions(-) rename packages/react-core/src/components/Drawer/examples/{DrawerInlinePanelRight.tsx => DrawerInlinePanelEnd.tsx} (96%) rename packages/react-core/src/components/Drawer/examples/{DrawerInlinePanelLeft.tsx => DrawerInlinePanelStart.tsx} (92%) rename packages/react-core/src/components/Drawer/examples/{DrawerPanelLeft.tsx => DrawerPanelEnd.tsx} (93%) rename packages/react-core/src/components/Drawer/examples/{DrawerPanelRight.tsx => DrawerPanelStart.tsx} (93%) rename packages/react-core/src/components/Drawer/examples/{DrawerResizableOnRight.tsx => DrawerResizableAtEnd.tsx} (91%) rename packages/react-core/src/components/Drawer/examples/{DrawerResizableOnLeft.tsx => DrawerResizableAtStart.tsx} (96%) diff --git a/packages/react-core/src/components/Drawer/Drawer.tsx b/packages/react-core/src/components/Drawer/Drawer.tsx index e7516cb4d78..c8ffba99f3c 100644 --- a/packages/react-core/src/components/Drawer/Drawer.tsx +++ b/packages/react-core/src/components/Drawer/Drawer.tsx @@ -11,7 +11,7 @@ export enum DrawerColorVariant { export interface DrawerProps extends React.HTMLProps { /** Additional classes added to the Drawer. */ className?: string; - /** Content rendered in the left hand panel */ + /** Content rendered in the drawer panel */ children?: React.ReactNode; /** Indicates if the drawer is expanded */ isExpanded?: boolean; @@ -19,8 +19,8 @@ export interface DrawerProps extends React.HTMLProps { isInline?: boolean; /** Indicates if the drawer will always show both content and panel. */ isStatic?: boolean; - /** Position of the drawer panel */ - position?: 'left' | 'right' | 'bottom'; + /** Position of the drawer panel. left and right are deprecated, use start and end instead. */ + position?: 'start' | 'end' | 'bottom' | 'left' | 'right'; /** Callback when drawer panel is expanded after waiting 250ms for animation to complete. */ onExpand?: (event: KeyboardEvent | React.MouseEvent | React.TransitionEvent) => void; } @@ -39,7 +39,7 @@ export const DrawerContext = React.createContext>({ isExpanded: false, isStatic: false, onExpand: () => {}, - position: 'right', + position: 'end', drawerRef: null, drawerContentRef: null, isInline: false @@ -51,7 +51,7 @@ export const Drawer: React.FunctionComponent = ({ isExpanded = false, isInline = false, isStatic = false, - position = 'right', + position = 'end', onExpand = () => {}, ...props }: DrawerProps) => { @@ -66,7 +66,7 @@ export const Drawer: React.FunctionComponent = ({ isExpanded && styles.modifiers.expanded, isInline && styles.modifiers.inline, isStatic && styles.modifiers.static, - position === 'left' && styles.modifiers.panelLeft, + (position === 'left' || position === 'start') && styles.modifiers.panelLeft, position === 'bottom' && styles.modifiers.panelBottom, className )} diff --git a/packages/react-core/src/components/Drawer/DrawerPanelContent.tsx b/packages/react-core/src/components/Drawer/DrawerPanelContent.tsx index 0a98f7c0fd2..24603b1efdb 100644 --- a/packages/react-core/src/components/Drawer/DrawerPanelContent.tsx +++ b/packages/react-core/src/components/Drawer/DrawerPanelContent.tsx @@ -87,8 +87,8 @@ export const DrawerPanelContent: React.FunctionComponent { let splitterPos; let drawerSize; + const isRTL = window.getComputedStyle(panel.current).getPropertyValue('direction') === 'rtl'; - if (isInline && position === 'right') { - splitterPos = panel.current.getBoundingClientRect().right - splitterRef.current.getBoundingClientRect().left; - drawerSize = drawerRef.current.getBoundingClientRect().right - drawerRef.current.getBoundingClientRect().left; - } else if (isInline && position === 'left') { - splitterPos = splitterRef.current.getBoundingClientRect().right - panel.current.getBoundingClientRect().left; - drawerSize = drawerRef.current.getBoundingClientRect().right - drawerRef.current.getBoundingClientRect().left; - } else if (position === 'right') { - splitterPos = - drawerContentRef.current.getBoundingClientRect().right - splitterRef.current.getBoundingClientRect().left; - drawerSize = - drawerContentRef.current.getBoundingClientRect().right - drawerContentRef.current.getBoundingClientRect().left; - } else if (position === 'left') { - splitterPos = - splitterRef.current.getBoundingClientRect().right - drawerContentRef.current.getBoundingClientRect().left; - drawerSize = - drawerContentRef.current.getBoundingClientRect().right - drawerContentRef.current.getBoundingClientRect().left; + if (isInline && (position === 'end' || position === 'right')) { + if (isRTL) { + splitterPos = panel.current.getBoundingClientRect().left - splitterRef.current.getBoundingClientRect().right; + drawerSize = drawerRef.current.getBoundingClientRect().left - drawerRef.current.getBoundingClientRect().right; + } else { + splitterPos = panel.current.getBoundingClientRect().right - splitterRef.current.getBoundingClientRect().left; + drawerSize = drawerRef.current.getBoundingClientRect().right - drawerRef.current.getBoundingClientRect().left; + } + } else if (isInline && (position === 'start' || position === 'left')) { + if (isRTL) { + splitterPos = splitterRef.current.getBoundingClientRect().left - panel.current.getBoundingClientRect().right; + drawerSize = drawerRef.current.getBoundingClientRect().left - drawerRef.current.getBoundingClientRect().right; + } else { + splitterPos = splitterRef.current.getBoundingClientRect().right - panel.current.getBoundingClientRect().left; + drawerSize = drawerRef.current.getBoundingClientRect().right - drawerRef.current.getBoundingClientRect().left; + } + } else if (position === 'end' || position === 'right') { + if (isRTL) { + splitterPos = + drawerContentRef.current.getBoundingClientRect().left - splitterRef.current.getBoundingClientRect().right; + drawerSize = + drawerContentRef.current.getBoundingClientRect().left - + drawerContentRef.current.getBoundingClientRect().right; + } else { + splitterPos = + drawerContentRef.current.getBoundingClientRect().right - splitterRef.current.getBoundingClientRect().left; + drawerSize = + drawerContentRef.current.getBoundingClientRect().right - + drawerContentRef.current.getBoundingClientRect().left; + } + } else if (position === 'start' || position === 'left') { + if (isRTL) { + splitterPos = + splitterRef.current.getBoundingClientRect().left - drawerContentRef.current.getBoundingClientRect().right; + drawerSize = + drawerContentRef.current.getBoundingClientRect().left - + drawerContentRef.current.getBoundingClientRect().right; + } else { + splitterPos = + splitterRef.current.getBoundingClientRect().right - drawerContentRef.current.getBoundingClientRect().left; + drawerSize = + drawerContentRef.current.getBoundingClientRect().right - + drawerContentRef.current.getBoundingClientRect().left; + } } else if (position === 'bottom') { splitterPos = drawerContentRef.current.getBoundingClientRect().bottom - splitterRef.current.getBoundingClientRect().top; @@ -166,6 +195,8 @@ export const DrawerPanelContent: React.FunctionComponent { + const isRTL = window.getComputedStyle(panel.current).getPropertyValue('direction') === 'rtl'; + e.stopPropagation(); if (!isResizing) { return; @@ -173,17 +204,22 @@ export const DrawerPanelContent: React.FunctionComponent { + const isRTL = window.getComputedStyle(panel.current).getPropertyValue('direction') === 'rtl'; + const key = e.key; if ( key !== 'Escape' && @@ -248,9 +286,17 @@ export const DrawerPanelContent: React.FunctionComponent { +export const DrawerInlinePanelEnd: React.FunctionComponent = () => { const [isExpanded, setIsExpanded] = React.useState(false); const drawerRef = React.useRef(); diff --git a/packages/react-core/src/components/Drawer/examples/DrawerInlinePanelLeft.tsx b/packages/react-core/src/components/Drawer/examples/DrawerInlinePanelStart.tsx similarity index 92% rename from packages/react-core/src/components/Drawer/examples/DrawerInlinePanelLeft.tsx rename to packages/react-core/src/components/Drawer/examples/DrawerInlinePanelStart.tsx index c4ab29674ae..499f1ec7be3 100644 --- a/packages/react-core/src/components/Drawer/examples/DrawerInlinePanelLeft.tsx +++ b/packages/react-core/src/components/Drawer/examples/DrawerInlinePanelStart.tsx @@ -10,7 +10,7 @@ import { Button } from '@patternfly/react-core'; -export const DrawerInlinePanelLeft: React.FunctionComponent = () => { +export const DrawerInlinePanelStart: React.FunctionComponent = () => { const [isExpanded, setIsExpanded] = React.useState(false); const drawerRef = React.useRef(); @@ -47,7 +47,7 @@ export const DrawerInlinePanelLeft: React.FunctionComponent = () => { - + {drawerContent} diff --git a/packages/react-core/src/components/Drawer/examples/DrawerPanelLeft.tsx b/packages/react-core/src/components/Drawer/examples/DrawerPanelEnd.tsx similarity index 93% rename from packages/react-core/src/components/Drawer/examples/DrawerPanelLeft.tsx rename to packages/react-core/src/components/Drawer/examples/DrawerPanelEnd.tsx index 391b1ee953f..657a2bd0830 100644 --- a/packages/react-core/src/components/Drawer/examples/DrawerPanelLeft.tsx +++ b/packages/react-core/src/components/Drawer/examples/DrawerPanelEnd.tsx @@ -10,7 +10,7 @@ import { Button } from '@patternfly/react-core'; -export const DrawerPanelLeft: React.FunctionComponent = () => { +export const DrawerPanelEnd: React.FunctionComponent = () => { const [isExpanded, setIsExpanded] = React.useState(false); const drawerRef = React.useRef(); @@ -47,7 +47,7 @@ export const DrawerPanelLeft: React.FunctionComponent = () => { - + {drawerContent} diff --git a/packages/react-core/src/components/Drawer/examples/DrawerPanelRight.tsx b/packages/react-core/src/components/Drawer/examples/DrawerPanelStart.tsx similarity index 93% rename from packages/react-core/src/components/Drawer/examples/DrawerPanelRight.tsx rename to packages/react-core/src/components/Drawer/examples/DrawerPanelStart.tsx index abb563a9700..df0f1d7802f 100644 --- a/packages/react-core/src/components/Drawer/examples/DrawerPanelRight.tsx +++ b/packages/react-core/src/components/Drawer/examples/DrawerPanelStart.tsx @@ -10,7 +10,7 @@ import { Button } from '@patternfly/react-core'; -export const DrawerPanelRight: React.FunctionComponent = () => { +export const DrawerPanelStart: React.FunctionComponent = () => { const [isExpanded, setIsExpanded] = React.useState(false); const drawerRef = React.useRef(); @@ -47,7 +47,7 @@ export const DrawerPanelRight: React.FunctionComponent = () => { - + {drawerContent} diff --git a/packages/react-core/src/components/Drawer/examples/DrawerResizableOnRight.tsx b/packages/react-core/src/components/Drawer/examples/DrawerResizableAtEnd.tsx similarity index 91% rename from packages/react-core/src/components/Drawer/examples/DrawerResizableOnRight.tsx rename to packages/react-core/src/components/Drawer/examples/DrawerResizableAtEnd.tsx index 59a4cc30851..99ee9bdd29c 100644 --- a/packages/react-core/src/components/Drawer/examples/DrawerResizableOnRight.tsx +++ b/packages/react-core/src/components/Drawer/examples/DrawerResizableAtEnd.tsx @@ -10,7 +10,7 @@ import { Button } from '@patternfly/react-core'; -export const DrawerResizableOnRight: React.FunctionComponent = () => { +export const DrawerResizableAtEnd: React.FunctionComponent = () => { const [isExpanded, setIsExpanded] = React.useState(false); const drawerRef = React.useRef(); @@ -32,7 +32,7 @@ export const DrawerResizableOnRight: React.FunctionComponent = () => { }; const panelContent = ( - + drawer-panel @@ -52,7 +52,7 @@ export const DrawerResizableOnRight: React.FunctionComponent = () => { - + {drawerContent} diff --git a/packages/react-core/src/components/Drawer/examples/DrawerResizableOnLeft.tsx b/packages/react-core/src/components/Drawer/examples/DrawerResizableAtStart.tsx similarity index 96% rename from packages/react-core/src/components/Drawer/examples/DrawerResizableOnLeft.tsx rename to packages/react-core/src/components/Drawer/examples/DrawerResizableAtStart.tsx index 6e11e2525a8..d51cd308ef3 100644 --- a/packages/react-core/src/components/Drawer/examples/DrawerResizableOnLeft.tsx +++ b/packages/react-core/src/components/Drawer/examples/DrawerResizableAtStart.tsx @@ -10,7 +10,7 @@ import { Button } from '@patternfly/react-core'; -export const DrawerResizableOnLeft: React.FunctionComponent = () => { +export const DrawerResizableAtStart: React.FunctionComponent = () => { const [isExpanded, setIsExpanded] = React.useState(false); const drawerRef = React.useRef(); @@ -47,7 +47,7 @@ export const DrawerResizableOnLeft: React.FunctionComponent = () => { - + {drawerContent}