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

Improved Types and Concise Code #39

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Changes from all commits
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
96 changes: 45 additions & 51 deletions src/index.tsx
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,67 @@ import React, { useEffect, useMemo, useRef } from 'react'
import type { CSSProperties } from 'react'
import './styles.scss'

type IDirection = 'left' | 'right' | 'top' | 'bottom'

type Props = {
open: boolean
onClose?: () => void
direction: 'left' | 'right' | 'top' | 'bottom'
direction: IDirection
lockBackgroundScroll?: boolean
children?: React.ReactNode
duration?: number
overlayOpacity?: number
overlayColor?: String
enableOverlay?: boolean
style?: React.CSSProperties
style?: CSSProperties
zIndex?: number
size?: number | string
className?: string
customIdSuffix?: string
overlayClassName?: string
}

type DirectionStyle = Pick<
CSSProperties,
'top' | 'left' | 'right' | 'bottom' | 'width' | 'height' | 'transform'
>
const getDirectionStyle = (
dir: string,
dir: IDirection,
size?: number | string,
): {} | React.CSSProperties => {
switch (dir) {
case 'left':
return {
top: 0,
left: 0,
transform: 'translate3d(-100%, 0, 0)',
width: size,
height: '100vh',
}
case 'right':
return {
top: 0,
right: 0,
transform: 'translate3d(100%, 0, 0)',
width: size,
height: '100vh',
}
case 'bottom':
return {
left: 0,
right: 0,
bottom: 0,
transform: 'translate3d(0, 100%, 0)',
width: '100%',
height: size,
}
case 'top':
return {
left: 0,
right: 0,
top: 0,
transform: 'translate3d(0, -100%, 0)',
width: '100%',
height: size,
}

default:
return {}
): DirectionStyle => {
const directionStyle: Record<IDirection, DirectionStyle> = {
left: {
top: 0,
left: 0,
transform: 'translate3d(-100%, 0, 0)',
width: size,
height: '100vh',
},
right: {
top: 0,
right: 0,
transform: 'translate3d(100%, 0, 0)',
width: size,
height: '100vh',
},
bottom: {
left: 0,
right: 0,
bottom: 0,
transform: 'translate3d(0, 100%, 0)',
width: '100%',
height: size,
},
top: {
left: 0,
right: 0,
top: 0,
transform: 'translate3d(0, -100%, 0)',
width: '100%',
height: size,
},
}
return directionStyle[dir]
}

const EZDrawer: React.FC<Props> = (props) => {
Expand All @@ -89,16 +89,10 @@ const EZDrawer: React.FC<Props> = (props) => {
useEffect(() => {
const updatePageScroll = () => {
bodyRef.current = window.document.querySelector('body')

if (bodyRef.current && lockBackgroundScroll) {
if (open) {
bodyRef.current.style.overflow = 'hidden'
} else {
bodyRef.current.style.overflow = ''
}
bodyRef.current.style.overflow = open ? 'hidden' : ''
}
}

updatePageScroll()
}, [open])

Expand All @@ -107,8 +101,8 @@ const EZDrawer: React.FC<Props> = (props) => {
}, [customIdSuffix])

const overlayStyles: CSSProperties = {
backgroundColor: `${overlayColor}`,
opacity: `${overlayOpacity}`,
backgroundColor: overlayColor.toString(),
opacity: overlayOpacity,
zIndex: zIndex,
}

Expand Down