-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add close through a child function to dialogs #234
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
c02b5fc
starting work on closable dialogs
snowystinger e574357
Works, now for types and dismiss button
snowystinger 4f0b9a2
Merge branch 'master' into function-as-a-child-dialog-triggers
snowystinger c2fb8ed
update to yarn lock because apparently it didn't resolve correctly wh…
snowystinger 70fa9ba
update a bunch of stories and add a test
snowystinger 9af5066
Merge branch 'master' into function-as-a-child-dialog-triggers
snowystinger 36216d6
Hook up dismissable
snowystinger ccd6901
appease typescript
snowystinger 699faaa
Fix other isDismissable instances
snowystinger c0ea798
Merge branch 'master' into function-as-a-child-dialog-triggers
snowystinger a0e593f
kill me, i hate typescript
snowystinger c6618ff
finally fixed all the things
snowystinger 57297a8
Add a test for isDismissable behaviors
snowystinger a74def2
write tests for alert dialog
snowystinger 8181837
Merge branch 'master' into function-as-a-child-dialog-triggers
LFDanLu 215eed5
Addressing review comments
LFDanLu c5ac3f5
Merge branch 'master' of https://github.com/adobe-private/react-spect…
LFDanLu 5e196aa
Merge branch 'master' of https://github.com/adobe-private/react-spect…
LFDanLu b6ed84f
fixing some of the types and missed merge stuff
LFDanLu 99fac46
Making dismissible modals (ONLY) closable by underlay click
LFDanLu edb4f4d
adding tests for useOverlay closeOnInteractOutside
LFDanLu ce2801e
Merge branch 'master' of https://github.com/adobe-private/react-spect…
LFDanLu 5a934ce
fixing lint
LFDanLu 76773da
updating test as per review
LFDanLu fb92718
Merge branch 'master' into function-as-a-child-dialog-triggers
snowystinger 528bb83
Addressing review comments
LFDanLu e155974
Merge branch 'master' into function-as-a-child-dialog-triggers
LFDanLu 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,8 +14,8 @@ import {DialogContext} from './context'; | |
| import {DOMRefValue} from '@react-types/shared'; | ||
| import {Modal, Overlay, Popover, Tray} from '@react-spectrum/overlays'; | ||
| import {PressResponder} from '@react-aria/interactions'; | ||
| import React, {Fragment, useRef} from 'react'; | ||
| import {SpectrumDialogTriggerProps} from '@react-types/dialog'; | ||
| import React, {Fragment, ReactElement, useRef} from 'react'; | ||
| import {SpectrumDialogClose, SpectrumDialogProps, SpectrumDialogTriggerProps} from '@react-types/dialog'; | ||
| import {unwrapDOMRef, useMediaQuery} from '@react-spectrum/utils'; | ||
| import {useControlledState} from '@react-stately/utils'; | ||
| import {useOverlayPosition, useOverlayTrigger} from '@react-aria/overlays'; | ||
|
|
@@ -27,9 +27,14 @@ export function DialogTrigger(props: SpectrumDialogTriggerProps) { | |
| mobileType = type === 'popover' ? 'modal' : type, | ||
| hideArrow, | ||
| targetRef, | ||
| isDismissable, | ||
| ...positionProps | ||
| } = props; | ||
| let [trigger, content] = React.Children.toArray(children); | ||
| if (!Array.isArray(children) || children.length > 2) { | ||
| throw new Error('DialogTrigger must have exactly 2 children'); | ||
| } | ||
| // if a function is passed as the second child, it won't appear in toArray | ||
| let [trigger, content] = children as [ReactElement, SpectrumDialogClose]; | ||
|
|
||
| // On small devices, show a modal or tray instead of a popover. | ||
| // TODO: DNA variable? | ||
|
|
@@ -66,20 +71,20 @@ export function DialogTrigger(props: SpectrumDialogTriggerProps) { | |
| case 'fullscreen': | ||
| case 'fullscreenTakeover': | ||
| return ( | ||
| <Modal isOpen={isOpen} onClose={onClose} type={type}> | ||
| {content} | ||
| <Modal isOpen={isOpen} isDismissable={false} onClose={onClose} type={type}> | ||
| {typeof content === 'function' ? content(onClose) : content} | ||
| </Modal> | ||
| ); | ||
| case 'modal': | ||
| return ( | ||
| <Modal isOpen={isOpen} onClose={onClose}> | ||
| {content} | ||
| <Modal isOpen={isOpen} isDismissable={isDismissable} onClose={onClose}> | ||
| {typeof content === 'function' ? content(onClose) : content} | ||
| </Modal> | ||
| ); | ||
| case 'tray': | ||
| return ( | ||
| <Tray isOpen={isOpen} onClose={onClose}> | ||
| {content} | ||
| {typeof content === 'function' ? content(onClose) : content} | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm... thought tray's couldn't have the buttons? I'm fine to just have it all done the same though, seems useful |
||
| </Tray> | ||
| ); | ||
| } | ||
|
|
@@ -91,6 +96,7 @@ export function DialogTrigger(props: SpectrumDialogTriggerProps) { | |
| isOpen={isOpen} | ||
| onPress={onPress} | ||
| onClose={onClose} | ||
| isDismissable={isDismissable} | ||
| trigger={trigger} | ||
| overlay={renderOverlay()} /> | ||
| ); | ||
|
|
@@ -111,7 +117,7 @@ function PopoverTrigger({isOpen, onPress, onClose, targetRef, trigger, content, | |
| shouldFlip: props.shouldFlip, | ||
| isOpen | ||
| }); | ||
|
|
||
| let {triggerAriaProps, overlayAriaProps} = useOverlayTrigger({ | ||
| ref: triggerRef, | ||
| type: 'dialog', | ||
|
|
@@ -145,10 +151,23 @@ function PopoverTrigger({isOpen, onPress, onClose, targetRef, trigger, content, | |
| ); | ||
| } | ||
|
|
||
| function DialogTriggerBase({type, isOpen, onPress, onClose, dialogProps = {}, triggerProps = {}, overlay, trigger}) { | ||
| interface SpectrumDialogTriggerBase { | ||
| type?: 'modal' | 'popover' | 'tray' | 'fullscreen' | 'fullscreenTakeover', | ||
| isOpen?: boolean, | ||
| onPress?: any, | ||
| onClose?: () => void, | ||
| isDismissable?: boolean | ||
| dialogProps?: SpectrumDialogProps | {}, | ||
| triggerProps?: any, | ||
| overlay: ReactElement, | ||
| trigger: ReactElement | ||
| } | ||
|
|
||
| function DialogTriggerBase({type, isOpen, onPress, onClose, isDismissable, dialogProps = {}, triggerProps = {}, overlay, trigger}: SpectrumDialogTriggerBase) { | ||
| let context = { | ||
| type, | ||
| onClose, | ||
| isDismissable, | ||
| ...dialogProps | ||
| }; | ||
|
|
||
|
|
||
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.
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.
I'm not a fan of asking the people who create modals to write
(onClose) => {<content><Button onPress={close}>Close</Button></content>}. It feels like we're asking them to write jsx that isn't just elements.The first idea I thought of was a modal context to pass the onClose to button children which is also problematic, but it doesn't make the end user jump through any weird hoops.
I'm concerned that this will be a support issue of us having to train everyone to write things this way.
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 a pretty common React pattern called 'Function as children' so it shouldn't be hard to understand.
This should also be documentable via our prop tables.
There isn't really a great way to do this via context without creating <DialogButton or something equivalent that can pick up a different context than the slots and knows which of the three (four) possible buttons it is.
We went through many options before arriving at this one. I think Devon might have it written down.