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

feat: add skeleton to drawer component #81

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Add skeleton component (empty div) to Drawer for improve performance

## [0.18.0] - 2024-11-01

### Added
Expand Down
4 changes: 1 addition & 3 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
"vtex.pixel-manager": "1.x"
},
"mustUpdateAt": "2020-03-28",
"registries": [
"smartcheckout"
],
"registries": ["smartcheckout"],
"$schema": "https://raw.githubusercontent.com/vtex/node-vtex-api/master/gen/manifest.schema"
}
12 changes: 11 additions & 1 deletion react/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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 +160,14 @@ function Drawer(props: Props) {
}
}, [onVisibilityChanged, isMenuOpen])

useEffect(() => {
if (isMenuOpen && !shouldRenderChildrenNow) {
setTimeout(() => {
Copy link
Contributor

@iago1501 iago1501 Nov 6, 2024

Choose a reason for hiding this comment

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

Why is this setTimeout necessary? if the main concern is about conflict, you could check with the prevState from useState set

Copy link
Author

Choose a reason for hiding this comment

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

Thanks to the setTimeout, the drawer will be rendered instanly, reducing the INP and the children are going to be mounted once the drawer is first mounted. I tried with promise but seems that the best approach is using this.

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 +263,8 @@ function Drawer(props: Props) {
className={`${handles.childrenContainer} flex flex-grow-1`}
onClick={handleContainerClick}
>
{shouldRenderChildren && children}
{shouldRenderChildren &&
(shouldRenderChildrenNow ? children : <></>)}
iago1501 marked this conversation as resolved.
Show resolved Hide resolved
</div>
{/* eslint-enable jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
</div>
Expand Down
Loading