This repository has been archived by the owner on Mar 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
[WIP] feat: add dialog component #251
Draft
schne324
wants to merge
1
commit into
develop
Choose a base branch
from
dialog
base: develop
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.
Draft
Changes from all commits
Commits
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
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
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,112 @@ | ||
import React, { useState } from 'react'; | ||
import { | ||
Dialog, | ||
DialogHeading, | ||
DialogContent, | ||
DialogFooter, | ||
DialogActions, | ||
Button | ||
} from 'src/'; | ||
import DemoComponent from 'demo/Demo'; | ||
|
||
const Demo = () => { | ||
const [modalShow, setModalShow] = useState(false); | ||
const onModalClose = () => setModalShow(false); | ||
const onModalLauncherClick = () => setModalShow(true); | ||
const [alertShow, setAlertShow] = useState(false); | ||
const onAlertClose = () => setAlertShow(false); | ||
const onAlertLauncherClick = () => setAlertShow(true); | ||
|
||
return ( | ||
<DemoComponent | ||
component={Dialog} | ||
states={[ | ||
{ | ||
DEMO_renderBefore: <h3>Modal Dialog</h3>, | ||
children: ( | ||
<div> | ||
<DialogContent>Hello worlds</DialogContent> | ||
<DialogFooter> | ||
<Button onClick={onModalClose}>ok</Button> | ||
<Button variant="secondary" onClick={onModalClose}> | ||
cancel | ||
</Button> | ||
</DialogFooter> | ||
</div> | ||
), | ||
onClose: onModalClose, | ||
show: modalShow, | ||
heading: <DialogHeading>Hello</DialogHeading>, | ||
DEMO_renderAfter: ( | ||
<Button onClick={onModalLauncherClick}>Launch Modal Dialog!</Button> | ||
) | ||
}, | ||
{ | ||
DEMO_renderBefore: <h3>Alert Dialog</h3>, | ||
alert: true, | ||
show: alertShow, | ||
onClose: onAlertClose, | ||
children: ( | ||
<div> | ||
Do you accept the terms? | ||
<DialogActions> | ||
<Button onClick={onAlertClose}>Accept</Button> | ||
<Button onClick={onAlertClose} variant="secondary"> | ||
Decline | ||
</Button> | ||
</DialogActions> | ||
</div> | ||
), | ||
DEMO_renderAfter: ( | ||
<Button variant="secondary" onClick={onAlertLauncherClick}> | ||
Launch Alert Dialog! | ||
</Button> | ||
) | ||
} | ||
]} | ||
propDocs={{}} | ||
/> | ||
); | ||
}; | ||
|
||
export default Demo; | ||
|
||
/* | ||
<div> | ||
<Dialog | ||
show={modalShow} | ||
onClose={onModalClose} | ||
heading={<DialogHeading>Testing123</DialogHeading>} | ||
> | ||
<DialogContent> | ||
<p>Hello worlds</p> | ||
</DialogContent> | ||
<DialogFooter> | ||
<Button onClick={onModalClose}>ok</Button> | ||
<Button variant="secondary" onClick={onModalClose}> | ||
cancel | ||
</Button> | ||
</DialogFooter> | ||
</Dialog> | ||
<Button onClick={onModalLauncherClick}>Launch Modal Dialog!</Button> | ||
</div> | ||
<div> | ||
<Dialog | ||
alert | ||
show={alertShow} | ||
onClose={onAlertClose} | ||
heading={<DialogHeading>Testing123</DialogHeading>} | ||
> | ||
Do you accept the terms? | ||
<DialogActions> | ||
<Button onClick={onAlertClose}>Accept</Button> | ||
<Button onClick={onAlertClose} variant="secondary"> | ||
Decline | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
<Button variant="secondary" onClick={onAlertLauncherClick}> | ||
Launch Alert Dialog! | ||
</Button> | ||
</div> | ||
*/ |
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
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,105 @@ | ||
import React, { useEffect, useState, createRef } from 'react'; | ||
import FocusTrap from 'focus-trap-react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
import Offscreen from '../Offscreen'; | ||
import Scrim from '../Scrim'; | ||
import AriaIsolate from '../../utils/aria-isolate'; | ||
|
||
const Dialog = ({ | ||
show, | ||
onClose, | ||
forceAction, | ||
className, | ||
closeButtonText, | ||
children, | ||
heading, | ||
alert, | ||
...other | ||
}) => { | ||
const dialog = createRef(); | ||
const [deactivate, setDeactivate] = useState(); | ||
useEffect( | ||
() => { | ||
if (show) { | ||
const isolator = new AriaIsolate(dialog.current); | ||
setDeactivate(() => isolator.deactivate.bind(isolator)); | ||
isolator.activate(); | ||
return; | ||
} | ||
|
||
if (!deactivate) { | ||
return; | ||
} | ||
|
||
deactivate(); | ||
}, | ||
[show] | ||
); | ||
|
||
return show ? ( | ||
<FocusTrap | ||
focusTrapOptions={{ | ||
clickOutsideDeactivates: true, | ||
onDeactivate: onClose, | ||
escapeDeactivates: !forceAction, | ||
initialFocus: alert | ||
? '.dqpl-dialog-inner' | ||
: '.dqpl-dialog-show .dqpl-modal-heading', | ||
allowOutsideClick: () => false | ||
}} | ||
> | ||
<div | ||
role={alert ? 'alertdialog' : 'dialog'} | ||
className={classNames(className, { | ||
'dqpl-modal': !alert, | ||
'dqpl-alert': alert, | ||
'dqpl-dialog-show': show | ||
})} | ||
ref={dialog} | ||
{...other} | ||
> | ||
<div className="dqpl-dialog-inner" tabIndex={-1}> | ||
{alert ? ( | ||
<div className="dqpl-content">{children}</div> | ||
) : ( | ||
<> | ||
<div className="dqpl-modal-header"> | ||
{heading} | ||
{!forceAction && ( | ||
<button | ||
className="dqpl-close dqpl-icon" | ||
type="button" | ||
onClick={onClose} | ||
> | ||
<div className="fa fa-close" aria-hidden="true" /> | ||
<Offscreen>{closeButtonText}</Offscreen> | ||
</button> | ||
)} | ||
</div> | ||
{children} | ||
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. Just being nitpicky here, but do you think this should be wrapped with a class? Alert has |
||
</> | ||
)} | ||
</div> | ||
</div> | ||
<Scrim show={show} /> | ||
</FocusTrap> | ||
) : null; | ||
}; | ||
|
||
Dialog.displayName = 'Dialog'; | ||
Dialog.defaultProps = { | ||
closeButtonText: 'Close' | ||
}; | ||
Dialog.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
onClose: PropTypes.func.isRequired, | ||
show: PropTypes.bool, | ||
forceAction: PropTypes.bool, | ||
className: PropTypes.string, | ||
closeButtonText: PropTypes.string, | ||
heading: PropTypes.node, | ||
alert: PropTypes.bool | ||
}; | ||
|
||
export default Dialog; |
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,12 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
|
||
const DialogActions = ({ className, ...other }) => ( | ||
<div className={classNames('dqpl-buttons', className)} {...other} /> | ||
); | ||
DialogActions.displayName = 'DialogActions'; | ||
DialogActions.propTypes = { | ||
className: PropTypes.string | ||
}; | ||
export default DialogActions; |
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,12 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
|
||
const DialogContent = ({ className, ...other }) => ( | ||
<div className={classNames('dqpl-content', className)} {...other} /> | ||
); | ||
DialogContent.displayName = 'DialogContent'; | ||
DialogContent.propTypes = { | ||
className: PropTypes.string | ||
}; | ||
export default DialogContent; |
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,12 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
|
||
const DialogFooter = ({ className, ...other }) => ( | ||
<div className={classNames('dqpl-modal-footer', className)} {...other} /> | ||
); | ||
DialogFooter.displayName = 'DialogFooter'; | ||
DialogFooter.propTypes = { | ||
className: PropTypes.string | ||
}; | ||
export default DialogFooter; |
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,25 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
|
||
const DialogHeading = ({ level, className, ...other }) => { | ||
const Heading = `h${level}`; | ||
return ( | ||
<Heading | ||
tabIndex={-1} | ||
className={classNames('dqpl-modal-heading', className)} | ||
{...other} | ||
/> | ||
); | ||
}; | ||
|
||
DialogHeading.displayName = 'DialogHeading'; | ||
DialogHeading.defaultProps = { | ||
level: 2 | ||
}; | ||
DialogHeading.propTypes = { | ||
level: PropTypes.number, | ||
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. Consider using |
||
className: PropTypes.string | ||
}; | ||
|
||
export default DialogHeading; |
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,5 @@ | ||
export default from './Dialog'; | ||
export { default as DialogHeading } from './DialogHeading'; | ||
export { default as DialogContent } from './DialogContent'; | ||
export { default as DialogFooter } from './DialogFooter'; | ||
export { default as DialogActions } from './DialogActions'; |
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
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.
We have
ClickOutsideListener
used elsewhere. It would be nice to converge on a single thing. I think we should evaluate the other places we useClickOutsideListener
to see if they need focus traps and adjust accordingly, maybe as a tech debt thing.