Skip to content

Commit

Permalink
feat: drawer (#1105)
Browse files Browse the repository at this point in the history
* feat: add drawer size variant

* chore: adjust button margin

* chore: adjust button margin

* chore: adjust button margin
  • Loading branch information
danimuller20 authored Jun 14, 2024
1 parent 2504cdd commit 1c749e0
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 11 deletions.
15 changes: 13 additions & 2 deletions packages/ocean-core/src/components/_drawer.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@
top: 0;
transition: all 335ms ease-out 0ms;
visibility: hidden;
width: 378px;

&--small {
width: 378px;
}

&--large {
width: 476px;
}

&--right {
left: auto;
Expand Down Expand Up @@ -60,7 +67,7 @@

.ods-drawer__content--header {
display: flex;
height: 48px;
min-height: 48px;
padding: 0 24px;

&--left {
Expand Down Expand Up @@ -89,4 +96,8 @@
background-color: $color-interface-light-pure;
}
}

@media (max-width: 767px) {
padding: 0 16px;
}
}
12 changes: 7 additions & 5 deletions packages/ocean-react/src/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface DrawerProps {
iconAlignment?: 'right' | 'left';
anchorEl?: RefObject<HTMLDivElement> | null;
onMouseOutDrawer?: (event?: MouseEvent<HTMLDivElement>) => void;
size?: 'small' | 'large';
}

const Drawer = ({
Expand All @@ -32,9 +33,9 @@ const Drawer = ({
iconAlignment = 'right',
anchorEl,
onMouseOutDrawer,
size = 'small',
}: DrawerProps): React.ReactElement => {
const drawerRef = useRef<HTMLDivElement>(null);

const handleOverlayClose = (event: MouseEvent<HTMLDivElement>) => {
if (event.target === event.currentTarget) {
event.preventDefault();
Expand All @@ -48,11 +49,11 @@ const Drawer = ({
const position = anchorEl.current.getBoundingClientRect();

if (align === 'right') {
const size = window.innerWidth - position.left;
const positionSize = window.innerWidth - position.left;

return {
width: `${size}px`,
right: `${size}px`,
width: `${positionSize}px`,
right: `${positionSize}px`,
left: 'auto',
};
}
Expand Down Expand Up @@ -90,7 +91,8 @@ const Drawer = ({
className={classNames(
'ods-drawer',
open && 'ods-drawer--open',
`ods-drawer--${align}`
`ods-drawer--${align}`,
`ods-drawer--${size}`
)}
onMouseLeave={onMouseOutDrawer}
>
Expand Down
66 changes: 63 additions & 3 deletions packages/ocean-react/src/Drawer/__tests__/Drawer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('renders drawer component properly', () => {

expect(document.querySelector('.ods-drawer')).toMatchInlineSnapshot(`
<div
class="ods-drawer ods-drawer--open ods-drawer--right"
class="ods-drawer ods-drawer--open ods-drawer--right ods-drawer--small"
>
<div
class="ods-drawer__content--header ods-drawer__content--header--right"
Expand Down Expand Up @@ -55,7 +55,7 @@ test('renders drawer attached to div on right', () => {

expect(document.querySelector('.ods-drawer')).toMatchInlineSnapshot(`
<div
class="ods-drawer ods-drawer--open ods-drawer--right"
class="ods-drawer ods-drawer--open ods-drawer--right ods-drawer--small"
>
<div
class="ods-drawer__content--header ods-drawer__content--header--right"
Expand Down Expand Up @@ -85,7 +85,67 @@ test('renders drawer attached to div on left', () => {

expect(document.querySelector('.ods-drawer')).toMatchInlineSnapshot(`
<div
class="ods-drawer ods-drawer--open ods-drawer--left"
class="ods-drawer ods-drawer--open ods-drawer--left ods-drawer--small"
>
<div
class="ods-drawer__content--header ods-drawer__content--header--right"
>
<button
class="ods-btn ods-btn--md ods-btn--primary"
type="button"
>
mock-x-outline-xvg
</button>
</div>
<p>
Drawer content!
</p>
</div>
`);
});

test('renders drawer small drawer', () => {
render(
<AttachedDrawer size="small">
<p>Drawer content!</p>
</AttachedDrawer>
);

fireEvent.click(screen.getByRole('button', { name: 'Open drawer' }));

expect(document.querySelector('.ods-drawer')).toMatchInlineSnapshot(`
<div
class="ods-drawer ods-drawer--open ods-drawer--right ods-drawer--small"
>
<div
class="ods-drawer__content--header ods-drawer__content--header--right"
>
<button
class="ods-btn ods-btn--md ods-btn--primary"
type="button"
>
mock-x-outline-xvg
</button>
</div>
<p>
Drawer content!
</p>
</div>
`);
});

test('renders drawer large drawer', () => {
render(
<AttachedDrawer size="large">
<p>Drawer content!</p>
</AttachedDrawer>
);

fireEvent.click(screen.getByRole('button', { name: 'Open drawer' }));

expect(document.querySelector('.ods-drawer')).toMatchInlineSnapshot(`
<div
class="ods-drawer ods-drawer--open ods-drawer--right ods-drawer--large"
>
<div
class="ods-drawer__content--header ods-drawer__content--header--right"
Expand Down
3 changes: 3 additions & 0 deletions packages/ocean-react/src/Drawer/examples/AttachedDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ export interface SimpleDrawerProps {
open?: boolean;
align?: 'left' | 'right';
iconAlignment?: 'left' | 'right';
size?: 'small' | 'large';
}

const SimpleDrawer = ({
children,
open,
align,
iconAlignment,
size,
}: SimpleDrawerProps): React.ReactElement => {
const anchorRef = useRef<HTMLDivElement>(null);
const [isOpen, setIsOpen] = useState(false);
Expand Down Expand Up @@ -63,6 +65,7 @@ const SimpleDrawer = ({
iconAlignment={iconAlignment}
align={align}
anchorEl={anchorRef}
size={size}
>
{children}
</Drawer>
Expand Down
3 changes: 3 additions & 0 deletions packages/ocean-react/src/Drawer/examples/SimpleDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ export interface SimpleDrawerProps {
open?: boolean;
align?: 'left' | 'right';
iconAlignment?: 'left' | 'right';
size?: 'small' | 'large';
}

const SimpleDrawer = ({
children,
open,
align,
iconAlignment,
size,
}: SimpleDrawerProps): React.ReactElement => {
const [isOpen, setIsOpen] = useState(false);

Expand All @@ -33,6 +35,7 @@ const SimpleDrawer = ({
overlayClose={toggleOverlayClose}
iconAlignment={iconAlignment}
align={align}
size={size}
>
{children}
</Drawer>
Expand Down
16 changes: 15 additions & 1 deletion packages/ocean-react/src/Drawer/stories/Drawer.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import { Drawer } from '@useblu/ocean-react';
| .ods-drawer\_\overlay--open | Transitional styles applied to the overlay after open. |
| .ods-drawer | Styles applied to the drawer content. |
| .ods-drawer--open | Transitional styles applied to the drawer content after open. |
| .ods-drawer--small | Styled applied to drawer size |
| .ods-drawer--large | Styled applied to drawer size |
| .ods-drawer\_\_content--header | Styles applied to the drawer content header. |

## Playground
Expand All @@ -49,6 +51,7 @@ import { Drawer } from '@useblu/ocean-react';
open: false,
align: 'right',
iconAlignment: 'right',
size: 'small',
}}
parameters={{
controls: { exclude: /^onMouseOutDrawer*/ },
Expand All @@ -68,6 +71,11 @@ import { Drawer } from '@useblu/ocean-react';
options: ['right', 'left'],
control: { type: 'select' },
},
size: {
name: 'size',
options: ['small', 'large'],
control: { type: 'radio' },
},
}}
>
{(props) => (
Expand Down Expand Up @@ -129,7 +137,7 @@ import { Drawer } from '@useblu/ocean-react';
variant="secondary"
size="md"
blocked
style={{ margin: '0 8px 0 0' }}
style={{ margin: '0 12px 0 0' }}
>
Default
</Button>
Expand All @@ -152,6 +160,7 @@ import { Drawer } from '@useblu/ocean-react';
open: false,
align: 'right',
iconAlignment: 'right',
size: 'small',
}}
parameters={{
controls: { exclude: /^onMouseOutDrawer*/ },
Expand All @@ -171,6 +180,11 @@ import { Drawer } from '@useblu/ocean-react';
options: ['right', 'left'],
control: { type: 'select' },
},
size: {
name: 'size',
options: ['small', 'large'],
control: { type: 'radio' },
},
}}
>
{(props) => (
Expand Down

0 comments on commit 1c749e0

Please sign in to comment.