This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
New Drawer / Sidebar #285
Draft
kylerberry
wants to merge
4
commits into
main
Choose a base branch
from
sidebar-hotness
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
New Drawer / Sidebar #285
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react'; | ||
import { Drawer } from './Drawer'; | ||
import { Story } from '@storybook/react'; | ||
import { DrawerProps } from './Drawer.types'; | ||
import styled from 'styled-components'; | ||
|
||
export default { | ||
title: 'components/Drawer', | ||
component: Drawer, | ||
argTypes: { | ||
isOpen: { control: 'boolean', defaultValue: false }, | ||
placement: { options: ['left', 'right'], defaultValue: 'left' }, | ||
size: { | ||
options: ['xs', 'sm', 'md', 'lg', 'xl'], | ||
defaultValue: 'md', | ||
}, | ||
}, | ||
}; | ||
|
||
const Template: Story<DrawerProps> = ({ | ||
isOpen, | ||
placement, | ||
size, | ||
}) => { | ||
return ( | ||
<Drawer isOpen={isOpen} placement={placement} size={size}> | ||
<DrawerContent>Hi! I'm Drawer Content!</DrawerContent> | ||
</Drawer> | ||
); | ||
}; | ||
|
||
const DrawerContent = styled.div` | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
right: 0; | ||
bottom: 0; | ||
background: white; | ||
color: black; | ||
`; | ||
|
||
export const DrawerTemplate = Template.bind({}); | ||
DrawerTemplate.storyName = 'Drawer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import styled from 'styled-components'; | ||
import { rem } from 'polished'; | ||
import { DrawerSize } from './Drawer.types'; | ||
|
||
export const DRAWER_WIDTH_XS = 276; | ||
export const DRAWER_WIDTH_SM = 308; | ||
export const DRAWER_WIDTH_MD = 340; | ||
export const DRAWER_WIDTH_LG = 372; | ||
export const DRAWER_WIDTH_XL = 404; | ||
|
||
export const DRAWER_SIZE = { | ||
xs: DRAWER_WIDTH_XS, | ||
sm: DRAWER_WIDTH_SM, | ||
md: DRAWER_WIDTH_MD, | ||
lg: DRAWER_WIDTH_LG, | ||
xl: DRAWER_WIDTH_XL, | ||
}; | ||
|
||
function panelSize(size: DrawerSize) { | ||
switch (size) { | ||
case 'xs': | ||
return rem(DRAWER_WIDTH_XS); | ||
case 'md': | ||
return rem(DRAWER_WIDTH_MD); | ||
case 'lg': | ||
return rem(DRAWER_WIDTH_LG); | ||
case 'xl': | ||
return rem(DRAWER_WIDTH_XL); | ||
case 'sm': | ||
default: | ||
return rem(DRAWER_WIDTH_SM); | ||
} | ||
} | ||
|
||
export const WithSize = styled.div<{ size: DrawerSize }>` | ||
position: relative; | ||
width: ${({ size }) => panelSize(size)}; | ||
height: 100vh; | ||
`; | ||
|
||
export const WithPlacement = styled.div<{ left: boolean }>` | ||
position: fixed; | ||
${({ left }) => | ||
left | ||
? ` | ||
left: 0; | ||
right: initial;` | ||
: ` | ||
right: 0; | ||
left: initial; | ||
`} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from 'react'; | ||
import { DrawerProps, Minors } from './Drawer.types'; | ||
import { AnimatePresence, motion } from 'framer-motion'; | ||
import { withIris } from '../../utils'; | ||
import { DrawerContainer } from './Minors/DrawerContainer'; | ||
import { getAnimation } from './util'; | ||
|
||
const DrawerComponent = ({ | ||
placement, | ||
isOpen, | ||
size = 'md', | ||
// autoFocus, | ||
// initialFocusRef, | ||
// finalFocusRef, | ||
// returnFocusOnClose, | ||
// preserveScrollBarGap, | ||
children, | ||
}: DrawerProps) => { | ||
return ( | ||
<AnimatePresence> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can give this an |
||
{isOpen && ( | ||
<motion.div {...getAnimation(size, placement)}> | ||
<DrawerContainer size={size} placement={placement}> | ||
{children} | ||
</DrawerContainer> | ||
</motion.div> | ||
)} | ||
</AnimatePresence> | ||
); | ||
}; | ||
|
||
export const Drawer = withIris<HTMLDivElement, DrawerProps, Minors>( | ||
DrawerComponent | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React, { HTMLProps } from 'react'; | ||
import { IrisProps, MinorComponent } from '../../utils'; | ||
|
||
export type DrawerSize = 'xs' | 'sm' | 'md' | 'lg' | 'xl'; | ||
|
||
export interface FocusableElement { | ||
focus(options?: FocusOptions): void; | ||
} | ||
|
||
export type DrawerProps = IrisProps<{ | ||
/** | ||
* Whether the drawer is visible or not | ||
* | ||
* @default false | ||
*/ | ||
isOpen: boolean; | ||
/** | ||
* The placement of the drawer | ||
* | ||
* @default "left" | ||
*/ | ||
placement?: 'left' | 'right'; | ||
/** | ||
* Width of the drawer | ||
* | ||
* @default "md" | ||
*/ | ||
size?: DrawerSize; | ||
/** | ||
* If `true`, the modal will autofocus the first enabled and interactive | ||
* element within the `ModalContent` | ||
* | ||
* @default true | ||
*/ | ||
autoFocus?: boolean; | ||
/** | ||
* The `ref` of element to receive focus when the modal opens. | ||
*/ | ||
initialFocusRef?: React.RefObject<FocusableElement>; | ||
/** | ||
* The `ref` of element to receive focus when the modal closes. | ||
*/ | ||
finalFocusRef?: React.RefObject<FocusableElement>; | ||
/** | ||
* If `true`, the modal will return focus to the element that triggered it when it closes. | ||
* | ||
* @default true | ||
*/ | ||
returnFocusOnClose?: boolean; | ||
/** | ||
* If `true`, a `padding-right` will be applied to the body element | ||
* that's equal to the width of the scrollbar. | ||
* | ||
* This can help prevent some unpleasant flickering effect | ||
* and content adjustment when the modal opens | ||
* | ||
* @default true | ||
*/ | ||
preserveScrollBarGap?: boolean; | ||
}>; | ||
|
||
export interface Minors { | ||
DrawerHeader: MinorComponent<HTMLProps<HTMLHeadingElement>>; | ||
DrawerContent: MinorComponent<HTMLProps<HTMLDivElement>>; | ||
DrawerBody: MinorComponent<HTMLProps<HTMLDivElement>>; | ||
DrawerFooter: MinorComponent<HTMLProps<HTMLDivElement>>; | ||
DrawerOverlay: MinorComponent<HTMLProps<HTMLDivElement>>; | ||
DrawerCloseButton: MinorComponent<HTMLProps<HTMLButtonElement>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react'; | ||
import { WithPlacement, WithSize } from '../Drawer.styles'; | ||
import { DrawerSize } from '../Drawer.types'; | ||
|
||
interface Props { | ||
size: DrawerSize; | ||
placement: 'left' | 'right'; | ||
} | ||
|
||
export const DrawerContainer = ({ | ||
size, | ||
placement, | ||
children, | ||
}: React.PropsWithChildren<Props>) => { | ||
return ( | ||
<WithPlacement left={placement === 'left'}> | ||
<WithSize size={size}>{children}</WithSize> | ||
</WithPlacement> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { DRAWER_SIZE } from './Drawer.styles'; | ||
import { DrawerSize } from './Drawer.types'; | ||
|
||
export function getAnimation( | ||
size: DrawerSize, | ||
placement: 'left' | 'right' | ||
) { | ||
const left = placement === 'left'; | ||
return { | ||
initial: { x: left ? -DRAWER_SIZE[size] : DRAWER_SIZE[size] }, | ||
animate: { x: 0 }, | ||
exit: { x: left ? -DRAWER_SIZE[size] : DRAWER_SIZE[size] }, | ||
transition: { | ||
type: 'tween', | ||
duration: (DRAWER_SIZE[size] / 6 + 90) / 1000, | ||
}, | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add a spread prop and pass it to
motion.div
to expose motion props to the parent.