Security News
RubyGems.org Adds New Maintainer Role
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.
react-aria-menubutton
Advanced tools
A menu button that
test/
);Please check out the demo.
The primary goal of this project is to build a React component that follows every detail of the WAI-ARIA Menu Button Design Pattern. This is kind of hard (because of keyboard interaction and focus management), so I wanted to share what I'd learned and (hopefully) learn more from others. Follow the link and read about the keyboard interactions and ARIA attributes. Quotations from this spec are scattered in comments throughout the source code, so it's clear which code addresses which requirements.
The demo also lists all of the interactions that are built in.
If you think that this component does not satisfy the spec or if you know of other ways to make it even more accessible, please file an issue.
npm install react-aria-menubutton
There is only one dependency: React 0.13.x.
There are two ways to use this module:
Either way, what is exposed is the function createAriaMenuButton([options])
, which returns the component you want, tailored with your options.
Using CommonJS, for example, you can simply require()
the module to get the factory:
var createAriaMenuButton = require('react-aria-menubutton');
var AriaMenuButton = createAriaMenuButton();
React.render(
<AriaMenuButton id='myMenuButton'
handleSelection={mySelectionHandler}
items={myItems}
triggerContent='Click me'
closeOnSelection={true}
transition={true} />,
document.getElementById('container')
);
Using globals/UMD, you must do the following:
dist/
directoryFor example:
<script src="react.min.js"></script>
<script src="node_modules/react-aria-menu-button/dist/createAriaMenuButton.min.js"></script>
<script>
var AriaMenuButton = createAriaMenuButton();
// ...
</script>
It is not a goal of this module to share some new neat dropdown style. This module's focus is on functionality — especially accessibility. However, in order for this thing to look like a menu button, it will need to be styled like a menu button. Therefore, I'm trying to give you, the user, the means to write your own styles, rather than forcing you into anything.
Towards that end, the elements of AriaMenuButton
are marked with classes that follow SUIT CSS conventions; so the whole thing is very easily themeable.
Within css/
, there is a base.css
stylesheet that provides some minimal rule sets that can get you started.
There are also a few ready-made themes that match the styles of popular frameworks.
All of them are on display in the demo, so have a look.
The following classes are used:
.AriaMenuButton {}
.AriaMenuButton-trigger {}
.AriaMenuButton-trigger.is-open {}
.AriaMenuButton-menuWrapper {}
.AriaMenuButton-menuWrapper--trans {}
.AriaMenuButton-menu {}
.AriaMenuButton-menu--flushRight {}
.AriaMenuButton-li {}
.AriaMenuButton-menuItem {}
.AriaMenuButton-menuItem.is-selected {}
You can customize these class names in SUIT-compliant ways by passing componentName
and namespace
options to `createAriaMenuButton([options]).
This will replace AriaMenuButton
in the class name with the component name of your choice. For example, if you pass { componentName: 'Dropdown' }
, your classes will be
.Dropdown {}
.Dropdown-trigger {}
.Dropdown-trigger.is-open {}
.Dropdown-menuWrapper {}
/* ... and so on */
This will add you namespace to the front of every class name, including the state classes. For example, if you pass { namespace: 'up' }
, you classes will be
.up-AriaMenuButton {}
.up-AriaMenuButton-trigger {}
.up-AriaMenuButton-trigger.up-is-open {}
.up-AriaMenuButton-menuWrapper {}
And you can, of course, specify both a component name and a namespace. Passing { componentName: 'Down', namespace: 'lo' }
, you'll get
.lo-Down {}
.lo-Down-trigger {}
.lo-Down-trigger.lo-is-open {}
.lo-Down-menuWrapper {}
If you require()
this module with CommonJS, you will receive the the function createAriaMenuButton()
.
If you are not using CommonJS, the same function will be globally exposed.
Returns a React component, an AriaMenuButton
, as described below.
/* little example */
var createAriaMenuButton = require('react-aria-menubutton');
var MySpecialButton = createAriaMenuButton({
componentName: 'MySpecialButton',
namespace: 'me'
});
transition
is true — because you want to use React's CSSTransitionGroup to animate the opening and closing of the menu — you need to pass in React.addons
here. For example,
var React = require('react/addons');
var createAriaMenuButton = require('react-aria-menubutton');
var AriaMenuButton = createAriaMenuButton({ reactAddons: React.addons });
A React component that takes the following props:
A callback to run when the user makes a selection (i.e. clicks or presses Enter or Space on a menu item).
It will be passed the value of the selected menu item (as defined in the items
prop).
While the menu's open/closed-state is handled internally, the selection-state is up to you: do with it what you will.
Each item has the following properties:
content
is an element, include a text
property to be used when letter keys are pressed. For example, if your item essentially says "horse" but the content
prop is a complicated element with icons and sub-elements and whatnot, provide test: 'horse'
and the component will know that when the user types "h" this item should be focused.handleSelection()
function when this item is selected. If no value
is provided, the item's content
will be passed to the selection handler.The content to be displayed in the menu button — the "trigger". This is inserted directly into the button via JSX, so it can be a string or a React element with mind-blowing innards, whatever you need.
If true
, the menu will close when a selection is made.
If true
, the menu will receive the modifier class
AriaMenuButton-menu--flushRight
.
All of the provided styles (in css/
) by default position the menu flush with the left side of the button.
But if the button were at the right edge of the screen, you'd want the menu flushed with its right side, instead, so that the menu didn't extend offscreen to the right.
That's why we have this option.
A base id for the component. This will be used to generate ids for all clickable elements besides menu items (which each get their own id). The ids might be useful for testing.
If true
, the menu will be start open when AriaMenuButton
mounts.
This is useful for testing, but probably not useful for you (unless you are contributing).
The currently selected value.
The item that has this value will receive the state class is-selected
, which CSS can use for special standout styling.
If true
, the menu will be wrapped in a ReactCSSTransitionGroup
.
This wrapping element will have the classes AriaMenuButton-menuWrapper
and AriaMenuButton-menuWrapper--trans
.
Your CSS, then, can respond to the changing state classes is-enter
, is-enter-active
, is-leave
, and is-leave-active
to apply a CSS transition of one kind or another.
The second example in the demo exemplifies a transition.
Note this: If you want to use transition
, the component will need access to React with addons, since ReactCSSTransitionGroup
is an addon. This is done by passing React.addons
as the option reactAddons
, as described above.
0.4.0
dist-modules/
with (transpiled) CommonJS modules and dist/
with a UMD library that expects React to be global.FAQs
A fully accessible and flexible React-powered menu button
The npm package react-aria-menubutton receives a total of 9,972 weekly downloads. As such, react-aria-menubutton popularity was classified as popular.
We found that react-aria-menubutton demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 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
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.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.