Skip to content

Commit

Permalink
feat(drawer): close on overlay click (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
StewartJingga authored Apr 25, 2024
1 parent ec7457d commit 6565493
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 58 deletions.
49 changes: 0 additions & 49 deletions packages/apsara-ui/src/Drawer/Drawer.stories.mdx

This file was deleted.

23 changes: 23 additions & 0 deletions packages/apsara-ui/src/Drawer/Drawer.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useState } from 'react';
import Drawer from './Drawer';

export const Basic = () => {
const [open, setOpen] = useState(false);
return (
<>
<button type="button" onClick={() => setOpen(true)}>Click to open Drawer</button>
<Drawer
open={open}
onClose={() => setOpen(false)}
>
<p>This is sample drawer content</p>
<button type="button" onClick={() => console.log('test')}>Test</button>
</Drawer>
</>
);
}

export default {
title: "Overlay/Drawer",
component: Drawer,
};
9 changes: 7 additions & 2 deletions packages/apsara-ui/src/Drawer/Drawer.styles.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import styled from "styled-components";

export const Overlay = styled.div`
animation: opacity 200ms ease-out;
export const Wrapper = styled.div`
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
z-index: 1000;
`;

export const Overlay = styled.div`
animation: opacity 200ms ease-out;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.3);
`;

Expand Down
18 changes: 13 additions & 5 deletions packages/apsara-ui/src/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
import React, { useContext } from "react";
import { ThemeContext } from "styled-components";
import Icon from "../Icon";
import { Overlay, Body } from "./Drawer.styles";
import { Overlay, Body, Wrapper } from "./Drawer.styles";

type DrawerProps = {
onClose?: () => void;
onClose: () => void;
onOverlayClick?: () => void;
disableCloseOnOverlayClick?: boolean; // default: true
open?: boolean;
className?: string;
position?: "left" | "right";
children?: React.ReactNode;
};

const Drawer = ({ onClose, open = false, children, className = "", position = "right" }: DrawerProps) => {
const Drawer = ({ onOverlayClick, disableCloseOnOverlayClick, onClose, open = false, children, className = "", position = "right" }: DrawerProps) => {
const theme = useContext(ThemeContext);

if (!open) return null;
return (
<Overlay>
<Wrapper>
<Overlay onClick={() => {
if (!disableCloseOnOverlayClick) onClose();
if (onOverlayClick) onOverlayClick();
}}>
</Overlay>
<Body className={className} position={position}>
<Icon name="cross" onClick={onClose} color={theme?.drawer?.close} />
{children}
</Body>
</Overlay>
</Wrapper>

);
};

Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"target": "es6",
"module": "esnext",
"lib": ["es6", "dom", "es2016", "es2017", "es2019"],
"jsx": "react",
"jsx": "react-jsx",
"sourceMap": true,

/* Strict Type-Checking Options */
Expand All @@ -22,7 +22,6 @@
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,

/* Module Resolution Options */
"moduleResolution": "node",
Expand Down

0 comments on commit 6565493

Please sign in to comment.