Skip to content

Commit

Permalink
feat(Drawer): Added start and end to position props, updated resizing…
Browse files Browse the repository at this point in the history
… to work with RTL (#9627)

* feat(Drawer): Added start and end to position props, updaed resizing to work with RTL

* update logic for newsize
  • Loading branch information
tlabaj authored Sep 20, 2023
1 parent cfd927c commit e12b872
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 56 deletions.
12 changes: 6 additions & 6 deletions packages/react-core/src/components/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ export enum DrawerColorVariant {
export interface DrawerProps extends React.HTMLProps<HTMLDivElement> {
/** 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;
/** Indicates if the content element and panel element are displayed side by side. */
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;
}
Expand All @@ -39,7 +39,7 @@ export const DrawerContext = React.createContext<Partial<DrawerContextProps>>({
isExpanded: false,
isStatic: false,
onExpand: () => {},
position: 'right',
position: 'end',
drawerRef: null,
drawerContentRef: null,
isInline: false
Expand All @@ -51,7 +51,7 @@ export const Drawer: React.FunctionComponent<DrawerProps> = ({
isExpanded = false,
isInline = false,
isStatic = false,
position = 'right',
position = 'end',
onExpand = () => {},
...props
}: DrawerProps) => {
Expand All @@ -66,7 +66,7 @@ export const Drawer: React.FunctionComponent<DrawerProps> = ({
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
)}
Expand Down
98 changes: 72 additions & 26 deletions packages/react-core/src/components/Drawer/DrawerPanelContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
const previouslyFocusedElement = React.useRef(null);
let currWidth: number = 0;
let panelRect: DOMRect;
let right: number;
let left: number;
let end: number;
let start: number;
let bottom: number;
let setInitialVals: boolean = true;

Expand All @@ -108,23 +108,52 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
const calcValueNow = () => {
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;
Expand Down Expand Up @@ -166,24 +195,31 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
};

const handleControlMove = (e: MouseEvent | TouchEvent, controlPosition: number) => {
const isRTL = window.getComputedStyle(panel.current).getPropertyValue('direction') === 'rtl';

e.stopPropagation();
if (!isResizing) {
return;
}

if (setInitialVals) {
panelRect = panel.current.getBoundingClientRect();
right = panelRect.right;
left = panelRect.left;
if (isRTL) {
start = panelRect.right;
end = panelRect.left;
} else {
end = panelRect.right;
start = panelRect.left;
}
bottom = panelRect.bottom;
setInitialVals = false;
}
const mousePos = controlPosition;
let newSize = 0;
if (position === 'right') {
newSize = right - mousePos;
} else if (position === 'left') {
newSize = mousePos - left;
if (position === 'end' || position === 'right') {
newSize = isRTL ? mousePos - end : end - mousePos;
} else if (position === 'start' || position === 'left') {
newSize = isRTL ? start - mousePos : mousePos - start;
} else {
newSize = bottom - mousePos;
}
Expand Down Expand Up @@ -225,6 +261,8 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
const callbackMouseUp = React.useCallback(handleMouseup, []);

const handleKeys = (e: React.KeyboardEvent) => {
const isRTL = window.getComputedStyle(panel.current).getPropertyValue('direction') === 'rtl';

const key = e.key;
if (
key !== 'Escape' &&
Expand All @@ -248,9 +286,17 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
newSize = position === 'bottom' ? panelRect.height : panelRect.width;
let delta = 0;
if (key === 'ArrowRight') {
delta = position === 'left' ? increment : -increment;
if (isRTL) {
delta = position === 'left' || position === 'start' ? -increment : increment;
} else {
delta = position === 'left' || position === 'start' ? increment : -increment;
}
} else if (key === 'ArrowLeft') {
delta = position === 'left' ? -increment : increment;
if (isRTL) {
delta = position === 'left' || position === 'start' ? increment : -increment;
} else {
delta = position === 'left' || position === 'start' ? -increment : increment;
}
} else if (key === 'ArrowUp') {
delta = increment;
} else if (key === 'ArrowDown') {
Expand Down
24 changes: 12 additions & 12 deletions packages/react-core/src/components/Drawer/examples/Drawer.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ section: components

```

### Panel on right
### Panel at end

```ts file="./DrawerPanelRight.tsx"
```ts file="./DrawerPanelEnd.tsx"

```

### Panel on left
### Panel at start

```ts file="./DrawerPanelLeft.tsx"
```ts file="./DrawerPanelStart.tsx"

```

Expand All @@ -50,15 +50,15 @@ section: components

```

### Inline panel on right
### Inline panel at end

```ts file="./DrawerInlinePanelRight.tsx"
```ts file="./DrawerInlinePanelEnd.tsx"

```

### Inline panel on left
### Inline panel at start

```ts file="./DrawerInlinePanelLeft.tsx"
```ts file="./DrawerInlinePanelStart.tsx"

```

Expand Down Expand Up @@ -100,15 +100,15 @@ section: components

```

### Resizable on right
### Resizable at end

```ts file="DrawerResizableOnRight.tsx"
```ts file="DrawerResizableAtEnd.tsx"

```

### Resizable on left
### Resizable at start

```ts file="DrawerResizableOnLeft.tsx"
```ts file="DrawerResizableAtStart.tsx"

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Button
} from '@patternfly/react-core';

export const DrawerInlinePanelRight: React.FunctionComponent = () => {
export const DrawerInlinePanelEnd: React.FunctionComponent = () => {
const [isExpanded, setIsExpanded] = React.useState(false);
const drawerRef = React.useRef<HTMLDivElement>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLDivElement>();

Expand Down Expand Up @@ -47,7 +47,7 @@ export const DrawerInlinePanelLeft: React.FunctionComponent = () => {
<Button aria-expanded={isExpanded} onClick={onClick}>
Toggle drawer
</Button>
<Drawer isExpanded={isExpanded} isInline position="left" onExpand={onExpand}>
<Drawer isExpanded={isExpanded} isInline position="start" onExpand={onExpand}>
<DrawerContent panelContent={panelContent}>
<DrawerContentBody>{drawerContent}</DrawerContentBody>
</DrawerContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLDivElement>();

Expand Down Expand Up @@ -47,7 +47,7 @@ export const DrawerPanelLeft: React.FunctionComponent = () => {
<Button aria-expanded={isExpanded} onClick={onClick}>
Toggle drawer
</Button>
<Drawer isExpanded={isExpanded} position="left" onExpand={onExpand}>
<Drawer isExpanded={isExpanded} position="end" onExpand={onExpand}>
<DrawerContent panelContent={panelContent}>
<DrawerContentBody>{drawerContent}</DrawerContentBody>
</DrawerContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLDivElement>();

Expand Down Expand Up @@ -47,7 +47,7 @@ export const DrawerPanelRight: React.FunctionComponent = () => {
<Button aria-expanded={isExpanded} onClick={onClick}>
Toggle drawer
</Button>
<Drawer isExpanded={isExpanded} position="right" onExpand={onExpand}>
<Drawer isExpanded={isExpanded} position="start" onExpand={onExpand}>
<DrawerContent panelContent={panelContent}>
<DrawerContentBody>{drawerContent}</DrawerContentBody>
</DrawerContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLDivElement>();

Expand All @@ -32,7 +32,7 @@ export const DrawerResizableOnRight: React.FunctionComponent = () => {
};

const panelContent = (
<DrawerPanelContent isResizable onResize={onResize} id="right-resize-panel" defaultSize={'500px'} minSize={'150px'}>
<DrawerPanelContent isResizable onResize={onResize} id="end-resize-panel" defaultSize={'500px'} minSize={'150px'}>
<DrawerHead>
<span tabIndex={isExpanded ? 0 : -1} ref={drawerRef}>
drawer-panel
Expand All @@ -52,7 +52,7 @@ export const DrawerResizableOnRight: React.FunctionComponent = () => {
<Button aria-expanded={isExpanded} onClick={onClick}>
Toggle drawer
</Button>
<Drawer isExpanded={isExpanded} onExpand={onExpand} position="right">
<Drawer isExpanded={isExpanded} onExpand={onExpand} position="end">
<DrawerContent panelContent={panelContent}>
<DrawerContentBody>{drawerContent}</DrawerContentBody>
</DrawerContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLDivElement>();

Expand Down Expand Up @@ -47,7 +47,7 @@ export const DrawerResizableOnLeft: React.FunctionComponent = () => {
<Button aria-expanded={isExpanded} onClick={onClick}>
Toggle drawer
</Button>
<Drawer isExpanded={isExpanded} onExpand={onExpand} position="left">
<Drawer isExpanded={isExpanded} onExpand={onExpand} position="start">
<DrawerContent panelContent={panelContent}>
<DrawerContentBody>{drawerContent}</DrawerContentBody>
</DrawerContent>
Expand Down

0 comments on commit e12b872

Please sign in to comment.