Skip to content

Commit

Permalink
feat: add skeleton to drawer component
Browse files Browse the repository at this point in the history
  • Loading branch information
Enrique Pérez authored and Enrique Pérez committed Nov 6, 2024
1 parent ac51544 commit 176d046
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
13 changes: 12 additions & 1 deletion react/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import useLockScroll from './modules/useLockScroll'
import DrawerCloseButton from './DrawerCloseButton'
import { DrawerContextProvider } from './DrawerContext'
import { isElementInsideLink } from './modules/isElementInsideLink'
import Skeleton from './Skeleton'

const Swipable = React.lazy(() => import('./Swipable'))

Expand Down Expand Up @@ -138,6 +139,7 @@ function Drawer(props: Props) {
const { state: menuState, openMenu, closeMenu } = useMenuState()
const { isOpen: isMenuOpen, hasBeenOpened: hasMenuBeenOpened } = menuState
const [isMoving, setIsMoving] = useState(false)
const [shouldRenderChildrenNow, setShouldRenderChildrenNow] = useState(false)

// Always add the listener for 'openDrawer' events, since they're sent by
// the drawer-trigger block.
Expand All @@ -159,6 +161,14 @@ function Drawer(props: Props) {
}
}, [onVisibilityChanged, isMenuOpen])

useEffect(() => {
if (isMenuOpen && !shouldRenderChildrenNow) {
setTimeout(() => {
setShouldRenderChildrenNow(true)
}, 0)
}
}, [isMenuOpen, shouldRenderChildrenNow])

const handleContainerClick: MouseEventHandler<HTMLElement> = event => {
// target is the clicked element
// currentTarget is the element which was attached the event (e.g. the container)
Expand Down Expand Up @@ -254,7 +264,8 @@ function Drawer(props: Props) {
className={`${handles.childrenContainer} flex flex-grow-1`}
onClick={handleContainerClick}
>
{shouldRenderChildren && children}
{shouldRenderChildren &&
(shouldRenderChildrenNow ? children : <Skeleton />)}
</div>
{/* eslint-enable jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
</div>
Expand Down
27 changes: 27 additions & 0 deletions react/Skeleton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react'
import { useCssHandles } from 'vtex.css-handles'

const CSS_HANDLES = ['skeletonContainer', 'skeletonItem'] as const

function Skeleton() {
const handles = useCssHandles(CSS_HANDLES)

return (
<div className={`${handles.skeletonContainer} pa4`}>
<div
className={`${handles.skeletonItem} mb4 bg-muted-5 br2`}
style={{ height: '20px', width: '60%' }}
/>
<div
className={`${handles.skeletonItem} mb4 bg-muted-5 br2`}
style={{ height: '20px', width: '80%' }}
/>
<div
className={`${handles.skeletonItem} mb4 bg-muted-5 br2`}
style={{ height: '20px', width: '40%' }}
/>
</div>
)
}

export default Skeleton

0 comments on commit 176d046

Please sign in to comment.