Skip to content

Commit

Permalink
Merge pull request #466 from github/kh-use-literal-prop-for-class
Browse files Browse the repository at this point in the history
Use `getLiteralPropValue` for sr-only class
  • Loading branch information
khiga8 authored Jul 18, 2023
2 parents 0c0441f + c2ea115 commit cd17c09
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/rules/a11y-no-visually-hidden-interactive-element.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {getProp, getPropValue} = require('jsx-ast-utils')
const {getProp, getLiteralPropValue} = require('jsx-ast-utils')
const {getElementType} = require('../utils/get-element-type')
const {generateObjSchema} = require('eslint-plugin-jsx-a11y/lib/util/schemas')

Expand Down Expand Up @@ -32,9 +32,12 @@ const checkIfInteractiveElement = (context, node) => {
const checkIfVisuallyHiddenAndInteractive = (context, options, node, isParentVisuallyHidden) => {
const {className, componentName} = options
if (node.type === 'JSXElement') {
const classes = getPropValue(getProp(node.openingElement.attributes, 'className'))
const classes = getLiteralPropValue(getProp(node.openingElement.attributes, 'className'))
const isVisuallyHiddenElement = node.openingElement.name.name === componentName
const hasSROnlyClass = typeof classes !== 'undefined' && classes.includes(className)
let hasSROnlyClass = false
if (classes != null) {
hasSROnlyClass = classes.includes(className)
}
let isHidden = false
if (hasSROnlyClass || isVisuallyHiddenElement || !!isParentVisuallyHidden) {
if (checkIfInteractiveElement(context, node)) {
Expand Down
3 changes: 3 additions & 0 deletions tests/a11y-no-visually-hidden-interactive-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ ruleTester.run('a11y-no-visually-hidden-interactive-element', rule, {
{code: "<span className='sr-only'>Text</span>;"},
{code: "<button className='other'>Submit</button>"},
{code: "<input className='sr-only' />"},
{
code: "<Foo className={({isOn}) => { return isOn || isOnProps ? `${className} selected`.trim() : className ?? ''}}/>",
},
{code: "<a className='other show-on-focus'>skip to main content</a>"},
{code: '<button>Submit</button>'},
{
Expand Down

0 comments on commit cd17c09

Please sign in to comment.