Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for React Drawer like a Champ!
Alex Suarez
Alex Suarez

Posted on

     

React Drawer like a Champ!

Hi there!!! Hope everything goes fine with you on the other side of the screen wherever you are!

I just found out today about this amazing app for VSCode (Thanks to Cody, the master of Tests!), CodeSpan, and man! I can't hold myself so I decided to write an article with some cool screenshots taken with this app, and what a better example to show than a drawer that I'm building from scratch for the latest component library I'm building!

Here we go...

Oh, wait I'm using TS and styled-system to build this out so "Box" component may look a little weird, if I don't, let you know about this. Now ... here we go ...

React Component!

code1

So nothing fancy here, just a Box component with some props, typed by this interface right here

code12png

What is missing? The direction and that is where the magic is ...

code5png

So with the helper from above you and based on the isOpen prop you can translate your drawer from the selected direction using the direction props spreading the helper in the component style like this ...

code4png

So that's it ... Or since I've always hated those dev who share only images... here is your code!

Component

importReactfrom'react'import{Box}from'../../structural'import{directions}from'./directions'exportdefaultfunctionDrawer({children,isOpen=false,speed=0.5,width='100vw',height='100vh',bgColor='white',direction='right',zIndex=99,className,style}:DrawerProps){return(<BoxclassName={className}position='fixed'height={height}width={width}backgroundColor={bgColor}zIndex={zIndex}boxShadow='0 0 15px 3px rgba(0, 0, 0, 0.1)'style={{transition:`all${speed}s ease`,...directions(isOpen)[direction],...style}}data-testid='drawer'>{children}</Box>)}exportinterfaceDrawerProps{children?:React.ReactNodeisOpen?:booleanspeed?:numberwidth?:number|stringheight?:number|stringbgColor?:stringzIndex?:numberdirection?:'top'|'left'|'right'|'bottom'}
Enter fullscreen modeExit fullscreen mode

Direction helper

exportconstdirections=(isOpen:boolean)=>({right:{transform:isOpen?'translateX(-100%)':'translateX(100%)',top:0,left:'100%'},left:{transform:isOpen?'translateX(100%)':'translateX(-100%)',top:0,right:'100%'},top:{transform:isOpen?'translateY(100%)':'translateY(-100%)',left:0,bottom:'100%'},bottom:{transform:isOpen?'translateY(-100%)':'translateY(100%)',left:0,top:'100%'}})
Enter fullscreen modeExit fullscreen mode

Now

Now? You know how to build a Drawer that opens from every side of your window! :)

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
longvd89 profile image
longvd89
  • Joined

What is box component

CollapseExpand
 
alexandprivate profile image
Alex Suarez
Senior Frontend Engineer
  • Location
    Amsterdam, Netherlands
  • Work
    Senior Frontend Engineer
  • Joined

Hi it's a simple component styled with styled components and styled system that's exposes some css properties as props, you can simple use a div and move this values I set via props in the style object, for more info you may look atstyled-system.com/ sorry for the confusion :)

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Senior Frontend Engineer
  • Location
    Amsterdam, Netherlands
  • Work
    Senior Frontend Engineer
  • Joined

More fromAlex Suarez

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp