-
-
Notifications
You must be signed in to change notification settings - Fork 44
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
[core] Add useTransitionStatus
and useExecuteIfNotAnimated
Hooks
#396
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f08177a
[core] Add useTransitionStatus Hook
atomiks aab360c
Update implementation
atomiks 3a0c40f
Invert logic
atomiks 317e15c
Update impl
atomiks 1bd9c96
Refactor JSDoc
atomiks de67aaa
Add useAnimationUnmount hook
atomiks 41ca5d4
use client
atomiks 572b39e
Move useTransitionStatus
atomiks 0aee256
Rename anim function
atomiks b5c071c
Update API
atomiks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,49 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { useEventCallback } from './useEventCallback'; | ||
import { ownerWindow } from './owner'; | ||
|
||
/** | ||
* Executes a function only if the given element has no CSS animations or transitions. | ||
* @ignore - internal hook. | ||
*/ | ||
export function useExecuteIfNotAnimated(getElement: () => Element | null | undefined) { | ||
const frame1Ref = React.useRef(-1); | ||
const frame2Ref = React.useRef(-1); | ||
|
||
const cancelFrames = useEventCallback(() => { | ||
cancelAnimationFrame(frame1Ref.current); | ||
cancelAnimationFrame(frame2Ref.current); | ||
}); | ||
|
||
React.useEffect(() => cancelFrames, [cancelFrames]); | ||
|
||
return useEventCallback((fnToExecute: () => void) => { | ||
cancelFrames(); | ||
|
||
const element = getElement(); | ||
|
||
if (!element) { | ||
return; | ||
} | ||
|
||
// Wait for the CSS styles to be applied to determine if the animation has been removed in the | ||
// [data-instant] state. This allows the close animation to play if the `delay` instantType is | ||
// applying to the same element. | ||
// Notes: | ||
// - A single `requestAnimationFrame` is sometimes unreliable. | ||
// - `queueMicrotask` does not work. | ||
frame1Ref.current = requestAnimationFrame(() => { | ||
frame2Ref.current = requestAnimationFrame(() => { | ||
const computedStyles = ownerWindow(element).getComputedStyle(element); | ||
const hasNoAnimation = | ||
['', 'none'].includes(computedStyles.animationName) || | ||
['', '0s'].includes(computedStyles.animationDuration); | ||
const hasNoTransition = ['', '0s'].includes(computedStyles.transitionDuration); | ||
if (hasNoAnimation && hasNoTransition) { | ||
fnToExecute(); | ||
} | ||
}); | ||
}); | ||
}); | ||
} |
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,52 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { useEnhancedEffect } from './useEnhancedEffect'; | ||
|
||
export type TransitionStatus = 'entering' | 'exiting' | undefined; | ||
|
||
/** | ||
* Provides a status string for CSS animations. | ||
* @param open - a boolean that determines if the element is open. | ||
* @ignore - internal hook. | ||
*/ | ||
export function useTransitionStatus(open: boolean) { | ||
const [transitionStatus, setTransitionStatus] = React.useState<TransitionStatus>(); | ||
const [mounted, setMounted] = React.useState(open); | ||
|
||
if (open && !mounted) { | ||
setMounted(true); | ||
} | ||
|
||
if (!open && mounted && transitionStatus !== 'exiting') { | ||
setTransitionStatus('exiting'); | ||
} | ||
|
||
if (!open && !mounted && transitionStatus === 'exiting') { | ||
setTransitionStatus(undefined); | ||
} | ||
|
||
useEnhancedEffect(() => { | ||
if (!open) { | ||
return undefined; | ||
} | ||
|
||
setTransitionStatus('entering'); | ||
|
||
const frame = requestAnimationFrame(() => { | ||
setTransitionStatus(undefined); | ||
}); | ||
|
||
return () => { | ||
cancelAnimationFrame(frame); | ||
}; | ||
}, [open]); | ||
|
||
return React.useMemo( | ||
() => ({ | ||
mounted, | ||
setMounted, | ||
transitionStatus, | ||
}), | ||
[mounted, transitionStatus], | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to call
setMounted(true)
from outside of this hook? If not, we could return anonAnimationEnd
callback that will callsetMounted(false)
(and maybe alsosetTransitionStatus(undefined)
?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setMounted
gives them the freedom to perform more complex checks and keeps its responsibility minimal. For example, due to the wrapper in anchored popups, the checks inanimationend
/transitionend
may differ. This gives full control.Example: