-
-
Notifications
You must be signed in to change notification settings - Fork 132
[core] Import @floating-ui/react
#2002
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
Open
romgrk
wants to merge
41
commits into
mui:master
Choose a base branch
from
romgrk:refactor-floating-ui-react
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
a08a114
refactor: initial import
romgrk 714591a
perf: mergeProps
romgrk 6e77a0d
lint
romgrk a66ee2f
lint
romgrk 94b90d9
lint
romgrk 77ac4ec
lint
romgrk ab4a5e6
refactor: rm useClick, mkdir interactions
romgrk 43309cc
fix
romgrk 07acc18
lint
romgrk 357024d
refactor: use BUI hooks
romgrk b4a34ad
refactor: rm log
romgrk 5072ffc
refactor: safeReact
romgrk 6a3a244
refactor: merge platform detect code
romgrk 7c5713f
lint
romgrk 5d59083
revert: docs draft
romgrk dec7604
refactor: FocusGuard
romgrk 0e4f86f
lint
romgrk 080dc69
Merge branch 'master' into refactor-floating-ui-react
romgrk 91a1740
lint
romgrk 576af67
deps: remove use-isomorphic-layout-effect
romgrk 386491d
ci: run (empty commit)
romgrk f7c633b
test: add useInteractions, useClientPoint
romgrk b75795a
lint
romgrk 5273876
lint
romgrk 22baeb0
Merge branch 'master' into refactor-floating-ui-react
romgrk 6493fa1
lint
romgrk 1356f70
refactor: add tests, move useClick
romgrk 1ab5b82
lint
romgrk 314d9f0
lint
romgrk 59be3cb
lint
romgrk 2b30b2b
lint
romgrk 7c546c3
fix: import upstream fixes
romgrk 5925241
test: make jsdom work
romgrk 04e3f19
Merge branch 'master' into refactor-floating-ui-react
romgrk 4d3ebb3
lint
romgrk 2899a41
refactor: use BUI timeout utilities
romgrk 281ddcc
lint
romgrk 86956f3
fix: weakmap fix
romgrk 0c635c2
test: add useTypeahead
romgrk 9d4f0b2
Merge branch 'master' into refactor-floating-ui-react
romgrk d75ca04
lint
romgrk 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
229 changes: 229 additions & 0 deletions
229
packages/react/src/floating-ui-react/components/FloatingDelayGroup.tsx
This file contains hidden or 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,229 @@ | ||
import * as React from 'react'; | ||
import { useTimeout, Timeout } from '../../utils/useTimeout'; | ||
import { useModernLayoutEffect } from '../../utils/useModernLayoutEffect'; | ||
|
||
import { getDelay } from '../hooks/useHover'; | ||
import type { FloatingRootContext, Delay } from '../types'; | ||
|
||
interface ContextValue { | ||
hasProvider: boolean; | ||
timeoutMs: number; | ||
delayRef: React.MutableRefObject<Delay>; | ||
initialDelayRef: React.MutableRefObject<Delay>; | ||
timeout: Timeout; | ||
currentIdRef: React.MutableRefObject<any>; | ||
currentContextRef: React.MutableRefObject<{ | ||
onOpenChange: (open: boolean) => void; | ||
setIsInstantPhase: (value: boolean) => void; | ||
} | null>; | ||
} | ||
|
||
const FloatingDelayGroupContext = React.createContext<ContextValue>({ | ||
hasProvider: false, | ||
timeoutMs: 0, | ||
delayRef: { current: 0 }, | ||
initialDelayRef: { current: 0 }, | ||
timeout: new Timeout(), | ||
currentIdRef: { current: null }, | ||
currentContextRef: { current: null }, | ||
}); | ||
|
||
export interface FloatingDelayGroupProps { | ||
children?: React.ReactNode; | ||
/** | ||
* The delay to use for the group when it's not in the instant phase. | ||
*/ | ||
delay: Delay; | ||
/** | ||
* An optional explicit timeout to use for the group, which represents when | ||
* grouping logic will no longer be active after the close delay completes. | ||
* This is useful if you want grouping to “last” longer than the close delay, | ||
* for example if there is no close delay at all. | ||
*/ | ||
timeoutMs?: number; | ||
} | ||
|
||
/** | ||
* Experimental next version of `FloatingDelayGroup` to become the default | ||
* in the future. This component is not yet stable. | ||
* Provides context for a group of floating elements that should share a | ||
* `delay`. Unlike `FloatingDelayGroup`, `useDelayGroup` with this | ||
* component does not cause a re-render of unrelated consumers of the | ||
* context when the delay changes. | ||
* @see https://floating-ui.com/docs/FloatingDelayGroup | ||
romgrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @internal | ||
*/ | ||
export function FloatingDelayGroup(props: FloatingDelayGroupProps): React.JSX.Element { | ||
const { children, delay, timeoutMs = 0 } = props; | ||
|
||
const delayRef = React.useRef(delay); | ||
const initialDelayRef = React.useRef(delay); | ||
const currentIdRef = React.useRef<string | null>(null); | ||
const currentContextRef = React.useRef(null); | ||
const timeout = useTimeout(); | ||
|
||
return ( | ||
<FloatingDelayGroupContext.Provider | ||
value={React.useMemo( | ||
() => ({ | ||
hasProvider: true, | ||
delayRef, | ||
initialDelayRef, | ||
currentIdRef, | ||
timeoutMs, | ||
currentContextRef, | ||
timeout, | ||
}), | ||
[timeoutMs, timeout], | ||
)} | ||
> | ||
{children} | ||
</FloatingDelayGroupContext.Provider> | ||
); | ||
} | ||
|
||
interface UseDelayGroupOptions { | ||
/** | ||
* Whether delay grouping should be enabled. | ||
* @default true | ||
*/ | ||
enabled?: boolean; | ||
} | ||
|
||
interface UseDelayGroupReturn { | ||
/** | ||
* The delay reference object. | ||
*/ | ||
delayRef: React.MutableRefObject<Delay>; | ||
/** | ||
* Whether animations should be removed. | ||
*/ | ||
isInstantPhase: boolean; | ||
/** | ||
* Whether a `<FloatingDelayGroup>` provider is present. | ||
*/ | ||
hasProvider: boolean; | ||
} | ||
|
||
/** | ||
* Enables grouping when called inside a component that's a child of a | ||
* `FloatingDelayGroup`. | ||
* @see https://floating-ui.com/docs/FloatingDelayGroup | ||
* @internal | ||
*/ | ||
export function useDelayGroup( | ||
context: FloatingRootContext, | ||
options: UseDelayGroupOptions = {}, | ||
): UseDelayGroupReturn { | ||
const { open, onOpenChange, floatingId } = context; | ||
const { enabled = true } = options; | ||
|
||
const groupContext = React.useContext(FloatingDelayGroupContext); | ||
const { | ||
currentIdRef, | ||
delayRef, | ||
timeoutMs, | ||
initialDelayRef, | ||
currentContextRef, | ||
hasProvider, | ||
timeout, | ||
} = groupContext; | ||
|
||
const [isInstantPhase, setIsInstantPhase] = React.useState(false); | ||
|
||
useModernLayoutEffect(() => { | ||
function unset() { | ||
setIsInstantPhase(false); | ||
currentContextRef.current?.setIsInstantPhase(false); | ||
currentIdRef.current = null; | ||
currentContextRef.current = null; | ||
delayRef.current = initialDelayRef.current; | ||
} | ||
|
||
if (!enabled) { | ||
return undefined; | ||
} | ||
if (!currentIdRef.current) { | ||
return undefined; | ||
} | ||
|
||
if (!open && currentIdRef.current === floatingId) { | ||
setIsInstantPhase(false); | ||
|
||
if (timeoutMs) { | ||
timeout.start(timeoutMs, unset); | ||
return () => { | ||
timeout.clear(); | ||
}; | ||
} | ||
|
||
unset(); | ||
} | ||
return undefined; | ||
}, [ | ||
enabled, | ||
open, | ||
floatingId, | ||
currentIdRef, | ||
delayRef, | ||
timeoutMs, | ||
initialDelayRef, | ||
currentContextRef, | ||
timeout, | ||
]); | ||
|
||
useModernLayoutEffect(() => { | ||
if (!enabled) { | ||
return; | ||
} | ||
if (!open) { | ||
return; | ||
} | ||
|
||
const prevContext = currentContextRef.current; | ||
const prevId = currentIdRef.current; | ||
|
||
currentContextRef.current = { onOpenChange, setIsInstantPhase }; | ||
currentIdRef.current = floatingId; | ||
delayRef.current = { | ||
open: 0, | ||
close: getDelay(initialDelayRef.current, 'close'), | ||
}; | ||
|
||
if (prevId !== null && prevId !== floatingId) { | ||
timeout.clear(); | ||
setIsInstantPhase(true); | ||
prevContext?.setIsInstantPhase(true); | ||
prevContext?.onOpenChange(false); | ||
} else { | ||
setIsInstantPhase(false); | ||
prevContext?.setIsInstantPhase(false); | ||
} | ||
}, [ | ||
enabled, | ||
open, | ||
floatingId, | ||
onOpenChange, | ||
currentIdRef, | ||
delayRef, | ||
timeoutMs, | ||
initialDelayRef, | ||
currentContextRef, | ||
timeout, | ||
]); | ||
|
||
useModernLayoutEffect(() => { | ||
return () => { | ||
currentContextRef.current = null; | ||
}; | ||
}, [currentContextRef]); | ||
|
||
return React.useMemo( | ||
() => ({ | ||
hasProvider, | ||
delayRef, | ||
isInstantPhase, | ||
}), | ||
[hasProvider, delayRef, isInstantPhase], | ||
); | ||
} |
Oops, something went wrong.
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.
This is
NextFloatingDelayGroup
.