-
Notifications
You must be signed in to change notification settings - Fork 466
feat(headless): dialog stacking state and alertdialog role #9427
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
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --- | ||
| --- |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| 'use client'; | ||
|
|
||
| import { createContext, useCallback, useContext, useLayoutEffect, useMemo, useState } from 'react'; | ||
|
|
||
| /** | ||
| * How a dialog root reaches the dialog root it renders inside, so the two can style the stack | ||
| * they form: the one on top drops its backdrop, the one beneath recedes behind it. | ||
| * | ||
| * Deliberately separate from `isNested`, which reports any FLOATING ancestor — a Menu or a | ||
| * Popover counts. Stacking styles cannot key on that: a dialog opened from a menu item has a | ||
| * floating ancestor but sits on the bare page, and must still paint its own scrim. | ||
| */ | ||
| export interface DialogNestingContextValue { | ||
| /** | ||
| * Whether the surrounding dialog is still covering the page — open, or closed but still | ||
| * mounted for its exit transition. Not the raw `open` flag: a child that un-suppressed its | ||
| * backdrop the instant the parent started closing would paint a second scrim over the | ||
| * parent's still-fading one. | ||
| */ | ||
| open: boolean; | ||
| /** | ||
| * Called by a dialog rendered inside this one, for as long as it is open. Returns the release. | ||
| * Stable for the lifetime of the root, so registering never churns. | ||
| */ | ||
| registerStackedChild: () => () => void; | ||
| } | ||
|
|
||
| export const DialogNestingContext = createContext<DialogNestingContextValue | null>(null); | ||
|
|
||
| /** What a root learns about the stack it belongs to. */ | ||
| export interface DialogNesting { | ||
| /** Whether this dialog is layered over an open dialog. */ | ||
| isStacked: boolean; | ||
| /** | ||
| * How many open dialogs are stacked directly on this one. Counts DIRECT children only — a | ||
| * three-deep stack reports 1 at both lower levels rather than 2 and 1 — which is enough for | ||
| * the single recede step that exists today. Making it cumulative means propagating the count | ||
| * back up the chain, and getting that to settle when two levels mount in one commit. | ||
| */ | ||
| stackedChildCount: number; | ||
| /** Provided to this root's children, so a dialog inside it registers against this one. */ | ||
| context: DialogNestingContextValue; | ||
| } | ||
|
|
||
| /** | ||
| * Joins a dialog root to the stack it belongs to, in both directions: up, to report itself to | ||
| * the dialog it renders inside, and down, to count the dialogs that render inside it. | ||
| */ | ||
| export function useDialogNesting(open: boolean, mounted: boolean): DialogNesting { | ||
| const parent = useContext(DialogNestingContext); | ||
| const [stackedChildCount, setStackedChildCount] = useState(0); | ||
|
|
||
| const registerStackedChild = useCallback(() => { | ||
| setStackedChildCount(count => count + 1); | ||
| let released = false; | ||
| return () => { | ||
| if (released) { | ||
| return; | ||
| } | ||
| released = true; | ||
| setStackedChildCount(count => count - 1); | ||
| }; | ||
| }, []); | ||
|
|
||
| const registerWithParent = parent?.registerStackedChild; | ||
|
|
||
| // Gated on `open` rather than on being mounted: a closing dialog stays mounted for the length | ||
| // of its exit transition, and the surface beneath has to come forward WITH it rather than | ||
| // after it. Depends on the registration function, not the whole context value, so a parent | ||
| // opening or closing does not re-register. | ||
| useLayoutEffect(() => { | ||
| if (!open || !registerWithParent) { | ||
| return; | ||
| } | ||
| return registerWithParent(); | ||
| }, [open, registerWithParent]); | ||
|
|
||
| const covering = open || mounted; | ||
|
|
||
| const context = useMemo<DialogNestingContextValue>( | ||
| () => ({ open: covering, registerStackedChild }), | ||
| [covering, registerStackedChild], | ||
| ); | ||
|
|
||
| return { | ||
| // A parent that is closed AND gone is not something to sit on top of: the child owns the scrim, | ||
| // which is what a confirmation root mounted beside its dialog's portal relies on. | ||
| isStacked: parent !== null && parent.open, | ||
| stackedChildCount, | ||
| context, | ||
| }; | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.