Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
react-aria-modal
Advanced tools
A fully flexible and accessible React modal built according WAI-ARIA Authoring Practices
A fully flexible and accessible React modal built according WAI-ARIA Authoring Practices.
"Flexible" mostly means that this module provides minimal inline styles to get the thing working, but does not provide "complete" modal styling that would get in you way. You get to (have to) style the dialog yourself. Essentially, this module provides a "smart" minimally styled component to wrap you "dumb" fully styled component.
This module is built on top of some vanilla JS modules that could be used by non-React libraries:
(It doesn't directly depend on focus-trap, but uses focus-trap-react, a focus-trap wrapper which could be used by other React libraries.)
If you like this kind of module (accessible, flexible, unstyled) you should also check out these projects:
npm install react-aria-modal
Just provide the right props (see below) and pass the content of the modal as this component's child.
Look in demo/js/
for example code. (And see what they look like here But here's a simple example.
var AriaModal = require('react-aria-modal');
var DemoOne = React.createClass({
getInitialState: function() {
return { modalActive: false };
},
activateModal: function() {
this.setState({ modalActive: true });
},
deactivateModal: function() {
this.setState({ modalActive: false });
},
render: function() {
return (
<div>
<button onClick={this.activateModal}>
activate modal
</button>
<AriaModal
active={this.state.modalActive}
titleText='demo one'
onExit={this.deactivateModal}
initialFocus='#demo-one-deactivate'
>
<div className='modal-dialog'>
<p>
Here is a modal.
</p>
<p>
<button
id='demo-one-deactivate'
onClick={this.deactivateModal}
>
deactivate modal
</button>
</p>
</div>
</AriaModal>
</div>
)
},
});
The modal can be activated in a couple of ways:
active
proptrue
as the active
propSimilarly, the modal can be deactivated in a couple of ways:
false
as the active
propPass your dialog element as the child. And that's it.
When the modal is active, you'll notice the following:
role
of dialog
(or alertdialog
) and an aria-label
or aria-labelledby
attribute.document.body
, not inserted directly into the HTML source order, as you might assume; but it should still update correctly. (This makes positioning easier (no weird nested z-index troubles).)Type: Function
, required
This function needs to handles the state change of exiting (or deactivating) the modal.
Maybe it's just a wrapper around setState()
; or maybe you use some more involved Flux-inspired state management — whatever the case, this module leaves the state management up to you instead of making assumptions. That also makes it easier to create your own "close modal" buttons; because you have the function that closes the modal right there, written by you, at your disposal.
Type: Boolean
If true
, the modal will receive a role
of alertdialog
, instead of its default dialog
.
Type: String
By default, when the modal activates its first focusable child will receive focus. If, instead, you want to identify a specific element that should receive initial focus, pass a selector string to this prop. (That selector is passed to document.querySelector()
to find the DOM node.)
Demo example 3 and an additional example below illustrate a good method if you want no initial visible focus. (Add tabIndex='0'
to the modal's content and give it outline: 0;
.)
Type: String
The id of the element that should be used as the modal's accessible title. This value is passed to the modal's aria-labelledby
attribute.
You must use either titleId
or titleText
, but not both.
Type: String
A string to use as the modal's accessible title. This value is passed to the modal's aria-label
attribute.
You must use either titleId
or titleText
, but not both.
Type: String
Apply a class to the underlay in order to custom-style it.
This module does apply various inline styles, though, so be aware that overriding some styles might be difficult. If, for example, you want to change the underlay's color, you should probably use the underlayColor
prop instead of a class.
Type: Boolean
, Default true
By default, a click on the underlay will exit the modal. Pass false
, and clicking on the underlay will do nothing.
Type: String
(color value), Default: rgba(0,0,0,0.5)
If you want to change about the underlay's color, you can do that with this prop.
Type: Boolean
If true
, the modal's contents will be vertically (as well as horizontally) centered.
An alert dialog that itself receives initial focus (but has no visible outline) and does not exit when the underlay is clicked, and is vertically centered:
var AriaModal = require('react-aria-modal');
var MyModal = React.createClass({
..
render: function() {
return (
<AriaModal
onExit={this.myExitHandler}
alert={true}
titleId='modal-title'
underlayClickExists={false}
verticallyCenter={true}
>
<div
tabIndex='0'
style={{ outline: 0 }}
className='my-modal-dialog'
>
<h2 id='modal-title'>Alert!</h2>
..
</div>
</AriaModal>
)
}
})
FAQs
A fully accessible and flexible React modal built according WAI-ARIA Authoring Practices
We found that react-aria-modal demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.