forked from chakra-ui/chakra-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request chakra-ui#5442 from chakra-ui/fix/lazy-animation
fix: lazy popover and menu unmounting
- Loading branch information
Showing
12 changed files
with
110 additions
and
21 deletions.
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,7 @@ | ||
--- | ||
"@chakra-ui/popover": patch | ||
"@chakra-ui/menu": patch | ||
--- | ||
|
||
Fix issue where the content of a lazy popover or menu gets unmounted before | ||
(framer-motion) animation ends leading to a janky user experience. |
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,6 @@ | ||
--- | ||
"@chakra-ui/hooks": minor | ||
--- | ||
|
||
Add `useAnimationState` hook to help track motion component animations. Used in | ||
popopover and menu lazy modes |
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
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
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
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
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,41 @@ | ||
import { getOwnerWindow } from "@chakra-ui/utils" | ||
import { RefObject, useEffect, useState } from "react" | ||
import { useEventListener } from "./use-event-listener" | ||
|
||
export type UseAnimationStateProps = { | ||
isOpen: boolean | ||
ref: RefObject<HTMLElement> | ||
} | ||
|
||
export function useAnimationState(props: UseAnimationStateProps) { | ||
const { isOpen, ref } = props | ||
|
||
const [mounted, setMounted] = useState(isOpen) | ||
const [once, setOnce] = useState(false) | ||
|
||
useEffect(() => { | ||
if (!once) { | ||
setMounted(isOpen) | ||
setOnce(true) | ||
} | ||
}, [isOpen, once, mounted]) | ||
|
||
useEventListener( | ||
"animationend", | ||
() => { | ||
setMounted(isOpen) | ||
}, | ||
() => ref.current, | ||
) | ||
|
||
const hidden = isOpen ? false : !mounted && once | ||
|
||
return { | ||
present: !hidden, | ||
onComplete() { | ||
const win = getOwnerWindow(ref.current) | ||
const evt = new win.CustomEvent("animationend", { bubbles: true }) | ||
ref.current?.dispatchEvent(evt) | ||
}, | ||
} | ||
} |
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,4 @@ | ||
{ | ||
"main": "dist/chakra-ui-hooks-use-animation-state.cjs.js", | ||
"module": "dist/chakra-ui-hooks-use-animation-state.esm.js" | ||
} |
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
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
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
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