Skip to content

Commit 5213519

Browse files
committed
fix: CSidebarNavItem/CSidebarNavDropdown: accessibility fixes #141
- add tabIndex and aria-attribute to CSidebarNavDropdown - Make links not focusable when they are in closed CSidebarNavDropdown
1 parent 4e35ed8 commit 5213519

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/template/CSidebarNavDropdown.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import CIcon from '@coreui/icons-react'
1111
import { useLocation } from 'react-router-dom'
1212

1313
import { Context } from './CSidebar'
14+
export const DropdownContext = React.createContext({})
1415

1516
export const iconProps = (icon) => {
1617
if (typeof icon === 'object') {
@@ -100,13 +101,20 @@ const CSidebarNavDropdown = props => {
100101
{...attributes}
101102
ref={ref}
102103
>
103-
<a className="c-sidebar-nav-dropdown-toggle" onClick={toggle} >
104+
<a
105+
className="c-sidebar-nav-dropdown-toggle"
106+
tabIndex="0"
107+
onClick={toggle}
108+
aria-label="menu dropdown"
109+
>
104110
{ icon && (isValidElement(icon) ? icon : <CIcon {...iconProps(icon)}/>) }
105111
{ fontIcon && <i className={iconClasses}/> }
106112
{ name }
107113
</a>
108114
<ul className="c-sidebar-nav-dropdown-items">
109-
{children}
115+
<DropdownContext.Provider value={{ isOpen }}>
116+
{children}
117+
</DropdownContext.Provider>
110118
</ul>
111119
</li>
112120
)

src/template/CSidebarNavItem.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import React, { isValidElement } from 'react'
1+
import React, { isValidElement, useContext } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44
import { CLink, CBadge } from '../index'
55
import CIcon from '@coreui/icons-react'
66
import { iconProps } from './CSidebarNavDropdown'
77

8+
import { DropdownContext } from './CSidebarNavDropdown'
9+
810
//component - CoreUI / CSidebarNavItem
911
const CSidebarNavItem = props => {
1012

@@ -23,7 +25,7 @@ const CSidebarNavItem = props => {
2325
...rest
2426
} = props
2527

26-
//render
28+
const { isOpen } = useContext(DropdownContext)
2729
const classes = classNames(
2830
'c-sidebar-nav-item',
2931
className
@@ -43,6 +45,7 @@ const CSidebarNavItem = props => {
4345
className={linkClasses}
4446
{...routerLinkProps}
4547
{...rest}
48+
tabIndex={ isOpen === false ? -1 : 0 }
4649
>
4750
{ icon && (isValidElement(icon) ? icon : <CIcon {...iconProps(icon)}/>) }
4851
{ fontIcon && <i className={`c-sidebar-nav-icon ${fontIcon}`}/>}

0 commit comments

Comments
 (0)