-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add DialogContainer #965
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
Closed
Closed
Add DialogContainer #965
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c00a43b
semi-working
jluyau b6266f8
Fixes
jluyau c6147ce
checkpoint
jluyau 6ea4f64
Merge branch 'main' into dialog-container
jluyau f826ad0
Fixes
jluyau ebb4ce7
Merge branch 'main' into dialog-container
dannify 02d921d
Merge branch 'main' into dialog-container
jluyau f078254
Updates
jluyau 280ad0b
Merge branch 'dialog-container' of github.com:jluyau/react-spectrum i…
jluyau 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,93 @@ | ||
import {DialogContainerContext} from './DialogContainerContext'; | ||
import React, {Fragment, ReactElement, useContext, useEffect, useRef, useState} from 'react'; | ||
import {OverlayTriggerState, useOverlayTriggerState} from '@react-stately/overlays'; | ||
import {SpectrumDialogClose, SpectrumDialogProps} from '@react-types/dialog'; | ||
import {unwrapDOMRef, useMediaQuery} from '@react-spectrum/utils'; | ||
import {Modal, Popover, Tray} from '@react-spectrum/overlays'; | ||
import {DOMRefValue} from '@react-types/shared'; | ||
import {useOverlayPosition, useOverlayTrigger} from '@react-aria/overlays'; | ||
import {PressResponder} from '@react-aria/interactions'; | ||
import {DialogContext} from './context'; | ||
import {DialogTriggerBase} from './DialogTrigger'; | ||
import {Dialog} from './Dialog'; | ||
|
||
|
||
export function DialogContainer(props): ReactElement { | ||
let { | ||
children, | ||
type = 'modal', | ||
mobileType = type === 'popover' ? 'modal' : type, | ||
isDismissable | ||
} = props; | ||
// NOTE: content needs to be saved as function in order to save in useState (since it's a function as child) | ||
let [overlays, setOverlays] = useState([]); | ||
let [overlayProps, setOverlayProps] = useState(props); | ||
|
||
// On small devices, show a modal or tray instead of a popover. | ||
// TODO: DNA variable? | ||
let isMobile = useMediaQuery('(max-width: 700px)'); | ||
if (isMobile) { | ||
// handle cases where desktop popovers need a close button for the mobile modal view | ||
if (type !== 'modal' && mobileType === 'modal') { | ||
isDismissable = true; | ||
} | ||
|
||
type = mobileType; | ||
} | ||
|
||
let state = useOverlayTriggerState(overlayProps); | ||
|
||
let addContent = content => { | ||
let newOverlayContent = [...overlays]; | ||
newOverlayContent.unshift(content); | ||
setOverlays(newOverlayContent); | ||
}; | ||
|
||
let overlayContent = overlays.length ? overlays[0] : null; | ||
let context = { | ||
type, | ||
onClose: state.close, | ||
isDismissable | ||
}; | ||
let onClose = () => { | ||
let newOverlays = [...overlays]; | ||
newOverlays.shift(); | ||
setOverlays(newOverlays); | ||
state.close(); | ||
}; | ||
|
||
let renderOverlay = () => { | ||
switch (type) { | ||
case 'fullscreen': | ||
case 'fullscreenTakeover': | ||
return ( | ||
<Modal isOpen={state.isOpen} isDismissable={false} onClose={onClose} type={type}> | ||
{typeof overlayContent === 'function' ? overlayContent(onClose) : overlayContent} | ||
</Modal> | ||
); | ||
case 'modal': | ||
return ( | ||
<Modal isOpen={state.isOpen} isDismissable={isDismissable} onClose={onClose}> | ||
{typeof overlayContent === 'function' ? overlayContent(onClose) : overlayContent} | ||
</Modal> | ||
); | ||
case 'tray': | ||
return ( | ||
<Tray isOpen={state.isOpen} onClose={onClose}> | ||
{typeof overlayContent === 'function' ? overlayContent(onClose) : overlayContent} | ||
</Tray> | ||
); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<DialogContainerContext.Provider value={{isDismissable, state, overlayContent, addContent, setOverlayProps, type}}> | ||
{children} | ||
</DialogContainerContext.Provider> | ||
<DialogContext.Provider value={context}> | ||
{renderOverlay()} | ||
</DialogContext.Provider> | ||
</> | ||
); | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/@react-spectrum/dialog/src/DialogContainerContext.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,3 @@ | ||
import React from 'react'; | ||
|
||
export const DialogContainerContext = React.createContext(null); |
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
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.
Inline this function?