Skip to content
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

Enabling accessibility #80

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ os:
- linux

node_js:
- '8'
- '10'
- '14'

install:
- npm install --legacy-peer-deps

before_install:
- npm install -g npm
Expand Down
3 changes: 2 additions & 1 deletion src/Nav.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ class Nav extends PureComponent {
{ [styles.expanded]: expanded }
)}
>
{React.Children.map(children, child => {
{React.Children.map(children, (child, key) => {
if (React.isValidElement(child) && this.isNavItem(child)) {
return this.renderNavItem(child, {
tabIndex: child.props.tabIndexPosition || key,
onSelect,
selected: currentSelected,
expanded: (!!child.props.expanded) ||
Expand Down
99 changes: 67 additions & 32 deletions src/NavItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ class NavItem extends PureComponent {
navitemClassName: PropTypes.string,
navitemStyle: PropTypes.object,
subnavClassName: PropTypes.string,
subnavStyle: PropTypes.object
subnavStyle: PropTypes.object,

tabIndex: PropTypes.string
};
static defaultProps = {
componentClass: 'div',
Expand Down Expand Up @@ -86,6 +88,20 @@ class NavItem extends PureComponent {
}
};

preventDefault = (event) => {
event.preventDefault();
};

onKeyDown = (event) => {
const SPACEBAR_CODE = 32;
const ENTER_KEY = 13;
const isSpaceKey = event.keyCode === SPACEBAR_CODE;
const isEnterKey = event.keyCode === ENTER_KEY;
if (isSpaceKey || isEnterKey) {
event.target.click();
}
}

render() {
const {
componentType, // eslint-disable-line
Expand All @@ -99,6 +115,7 @@ class NavItem extends PureComponent {

// Nav props
selected,
tabIndex,

// Sub navigation item
subnav,
Expand Down Expand Up @@ -141,25 +158,32 @@ class NavItem extends PureComponent {
})}
style={style}
>
<div
{...props}
className={cx(navitemClassName, styles.navitem)}
style={navitemStyle}
disabled={disabled}
role="menuitem"
tabIndex="-1"
<a
href="#"
onClick={chainedFunction(
onClick,
this.handleSelect
this.handleSelect,
this.preventDefault,
)}
onKeyDown={this.onKeyDown}
aria-label={navText.props.title}
>
{navIcon &&
<div {...navIconProps} className={cx(navIconClassName, styles.navicon)} />
}
{navText &&
<div {...navTextProps} className={cx(navTextClassName, styles.navtext)} />
}
</div>
<div
{...props}
className={cx(navitemClassName, styles.navitem)}
style={navitemStyle}
disabled={disabled}
role="menuitem"
tabIndex="-1"
>
{navIcon &&
<div {...navIconProps} className={cx(navIconClassName, styles.navicon)} />
}
{navText &&
<div {...navTextProps} className={cx(navTextClassName, styles.navtext)} />
}
</div>
</a>
</Component>
);
}
Expand Down Expand Up @@ -208,26 +232,37 @@ class NavItem extends PureComponent {
})}
style={style}
>
<div
{...props}
className={cx(navitemClassName, styles.navitem)}
style={navitemStyle}
disabled={disabled}
role="menuitem"
tabIndex="-1"
<a
href="#"
onClick={chainedFunction(
onClick,
(navItems.length === 0) ? this.handleSelect : noop
this.handleSelect,
this.preventDefault,
)}
onKeyDown={this.onKeyDown}
aria-label={navText.props.title}
>
{navIcon &&
<div {...navIconProps} className={cx(navIconClassName, styles.navicon)} />
}
{navText &&
<div {...navTextProps} className={cx(navTextClassName, styles.navtext)} />
}
{others}
</div>
<div
{...props}
className={cx(navitemClassName, styles.navitem)}
style={navitemStyle}
disabled={disabled}
role="menuitem"
tabIndex={tabIndex}
onClick={chainedFunction(
onClick,
(navItems.length === 0) ? this.handleSelect : noop
)}
>
{navIcon &&
<div {...navIconProps} className={cx(navIconClassName, styles.navicon)} />
}
{navText &&
<div {...navTextProps} className={cx(navTextClassName, styles.navtext)} />
}
{others}
</div>
</a>
{(navItems.length > 0) &&
<div
role="menu"
Expand Down
36 changes: 18 additions & 18 deletions src/sidenav-navitem.styl
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,49 @@
clear: both;
position: relative;

&.highlighted > .navitem {
&.highlighted > a > .navitem {
cursor: default;
}

&:hover > .navitem::after {
&:hover > a > .navitem::after {
background: #fff;
opacity: .15;
}

&.highlighted > .navitem::after,
&.highlighted > a > .navitem::after,
&:hover.highlighted > .navitem::after {
background: #000;
opacity: .2;
}

&.highlighted.expanded > .navitem::after,
&:hover.highlighted.expanded > .navitem::after {
&.highlighted.expanded > a > .navitem::after,
&:hover.highlighted.expanded > a > .navitem::after {
background: #000;
opacity: .25;
}

&.highlighted.selected.expanded > .navitem::after,
&:hover.highlighted.selected.expanded > .navitem::after {
&.highlighted.selected.expanded > a > .navitem::after,
&:hover.highlighted.selected.expanded > a > .navitem::after {
background: #000;
opacity: .2;
}

&:hover > .navitem .navicon,
&.highlighted > .navitem .navicon {
&:hover > a > .navitem .navicon,
&.highlighted > a > .navitem .navicon {
opacity: 1;
}

&:hover > .navitem .navicon,
&:hover > .navitem .navtext,
&.highlighted > .navitem .navicon,
&.highlighted > .navitem .navtext {
&:hover > a > .navitem .navicon,
&:hover > a > .navitem .navtext,
&.highlighted > a > .navitem .navicon,
&.highlighted > a > .navitem .navtext {
color: #fff;
> * {
color: #fff;
}
}

> .navitem {
> a > .navitem {
position: relative;
display: block;
line-height: 50px;
Expand Down Expand Up @@ -73,15 +73,15 @@
}
}

> .navitem .navicon,
> .navitem .navtext {
> a > .navitem .navicon,
> a > .navitem .navtext {
color: #f9dcdd;
> * {
color: #f9dcdd;
}
}

> .navitem .navicon {
> a > .navitem .navicon {
display: block;
float: left;
width: 64px;
Expand All @@ -96,7 +96,7 @@
text-align: center;
}

> .navitem .navicon + .navtext {
> a > .navitem .navicon + .navtext {
width: 0;
visibility: hidden;
opacity: 0;
Expand Down