Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improved drawer types & fixed drawer bugs #68

Merged
merged 3 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/drawer/Drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,23 @@ const PLACEMENTS = {

export type TPlacement = keyof typeof PLACEMENTS;

const useDrawer = createHook<
DialogOptions & { placement?: TPlacement },
DialogHTMLProps
>({
export type DrawerOptions = DialogOptions & { placement?: TPlacement };

export type DrawerHTMLProps = DialogHTMLProps;

export type DrawerProps = DrawerOptions & DrawerHTMLProps;

const useDrawer = createHook<DrawerOptions, DrawerHTMLProps>({
name: "Drawer",
compose: [useDialog],
compose: useDialog,
keys: [...DRAWER_KEYS, "placement"],
anuraghazra marked this conversation as resolved.
Show resolved Hide resolved

useProps({ placement = "left" }, { style: htmlStyles, ...htmlProps }) {
return {
style: {
...PLACEMENTS[placement],
position: "fixed",
htmlStyles,
...htmlStyles,
},
...htmlProps,
};
Expand Down
34 changes: 22 additions & 12 deletions src/drawer/DrawerCloseButton.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import { callAllHandlers } from "@chakra-ui/utils";
import { createHook, createComponent } from "reakit-system";
import { useButton, DialogStateReturn, ButtonProps } from "reakit";
import {
useButton,
ButtonProps,
useDialogDisclosure,
DialogDisclosureOptions,
DialogDisclosureHTMLProps,
} from "reakit";

import { DRAWER_KEYS } from "./__keys";
import { DRAWER_CLOSE_BUTTON_KEYS } from "./__keys";

export type DrawerCloseButtonOptions = DialogDisclosureOptions;

export type DrawerCloseButtonHTMLProps = ButtonProps &
DialogDisclosureHTMLProps;

export type DrawerCloseButtonProps = DrawerCloseButtonOptions &
DrawerCloseButtonHTMLProps;

export const useDrawerCloseButton = createHook<
Pick<DialogStateReturn, "hide">,
ButtonProps
DrawerCloseButtonOptions,
DrawerCloseButtonHTMLProps
>({
name: "DrawerCloseButton",
compose: useButton,
keys: [...DRAWER_KEYS, "hide"],
compose: [useButton, useDialogDisclosure],
anuraghazra marked this conversation as resolved.
Show resolved Hide resolved
keys: DRAWER_CLOSE_BUTTON_KEYS,

useProps({ hide }, { onClick: htmlOnClick, ...htmlProps }) {
return {
onClick: callAllHandlers(hide, htmlOnClick),
...htmlProps,
};
useProps(options, htmlProps) {
return htmlProps;
},
});

Expand Down
34 changes: 3 additions & 31 deletions src/drawer/__keys.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
const DRAWER_STATE_KEYS = [
"baseId",
"unstable_idCountRef",
"visible",
"animated",
"animating",
"setBaseId",
"show",
"hide",
"toggle",
"setVisible",
"setAnimated",
"stopAnimation",
"modal",
"unstable_disclosureRef",
"setModal",
] as const;

export const DRAWER_KEYS = [
...DRAWER_STATE_KEYS,
"hideOnEsc",
"hideOnClickOutside",
"preventBodyScroll",
"unstable_initialFocusRef",
"unstable_finalFocusRef",
"unstable_orphan",
"unstable_autoFocusOnShow",
"unstable_autoFocusOnHide",
] as const;
export const DRAWER_BACKDROP_KEYS = DRAWER_STATE_KEYS;
export const DRAWER_DISCLOSURE_KEYS = DRAWER_BACKDROP_KEYS;
// Automatically generated
export const DRAWER_KEYS = ["placement"] as const;
export const DRAWER_CLOSE_BUTTON_KEYS = [] as const;
10 changes: 6 additions & 4 deletions src/drawer/stories/Drawer.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,21 @@ export const Default = () => {
return (
<div>
<DrawerDisclosure {...dialog}>Open Drawer</DrawerDisclosure>
<select onBlur={e => setPlacement(e.target.value as TPlacement)}>
<select
defaultValue={placement}
onBlur={e => setPlacement(e.target.value as TPlacement)}
>
<option value="top">Top</option>
<option value="bottom">Bottom</option>
<option value="right">Right</option>
<option selected value="left">
Left
</option>
<option value="left">Left</option>
</select>
<DrawerBackdrop className={backdropStyles} {...dialog}>
<Drawer
{...dialog}
placement={placement}
aria-label="Hello world"
style={{ color: "red" }}
className={css`
opacity: 0;
padding: 10px;
Expand Down