- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 32
          Add rule no-static-element-interactions
          #680
        
          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
          
     Merged
      
      
    
  
     Merged
                    Changes from 4 commits
      Commits
    
    
            Show all changes
          
          
            10 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      8692b56
              
                Add no-static-element-interactions
              
              
                ror-y 3a68a94
              
                Add docs
              
              
                ror-y 7221ce6
              
                Add to recommended
              
              
                ror-y 576d714
              
                Handle custom components
              
              
                ror-y e51a11a
              
                Add updates from review
              
              
                ror-y e4febaa
              
                Run prettier
              
              
                ror-y 10dd24e
              
                yarn install
              
              
                ror-y e6bb1be
              
                Remove package-lock
              
              
                ror-y 0a94838
              
                Update yarn lock part 2
              
              
                ror-y ef4f52f
              
                Update yarn.lock from upstream
              
              
                ror-y File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| /.eslintcache | ||
| /.husky/ | ||
| /.idea/ | ||
| /dist/ | ||
| /node_modules/ | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # no-static-element-interactions | ||
|  | ||
| Static HTML elements do not have semantic meaning. This is clear in the case of `<div>` and `<span>`. It is less so clear in the case of elements that _seem_ semantic, but that do not have a semantic mapping in the accessibility layer. For example `<a>`, `<big>`, `<blockquote>`, `<footer>`, `<picture>`, `<strike>` and `<time>` -- to name a few -- have no semantic layer mapping. They are as void of meaning as `<div>`. | ||
|  | ||
| The [WAI-ARIA `role` attribute](https://www.w3.org/TR/wai-aria-1.1/#usage_intro) confers a semantic mapping to an element. The semantic value can then be expressed to a user via assistive technology. | ||
|  | ||
| In order to add interactivity such as a mouse or key event listener to a static element, that element must be given a role value as well. | ||
|  | ||
| ## How do I resolve this error? | ||
|  | ||
| ### Case: This element acts like a button, link, menuitem, etc | ||
|  | ||
| Indicate the element's role with the `role` attribute: | ||
|  | ||
| ```vue | ||
| <div | ||
| @click="onClickHandler" | ||
| @keypress="onKeyPressHandler" | ||
| role="button" | ||
| tabIndex="0"> | ||
| Save | ||
| </div> | ||
| ``` | ||
|  | ||
| Common interactive roles include: | ||
|  | ||
| 1. `button` | ||
| 1. `link` | ||
| 1. `checkbox` | ||
| 1. `menuitem` | ||
| 1. `menuitemcheckbox` | ||
| 1. `menuitemradio` | ||
| 1. `option` | ||
| 1. `radio` | ||
| 1. `searchbox` | ||
| 1. `switch` | ||
| 1. `textbox` | ||
|  | ||
| Note: Adding a role to your element does **not** add behavior. When a semantic HTML element like `<button>` is used, then it will also respond to Enter key presses when it has focus. The developer is responsible for providing the expected behavior of an element that the role suggests it would have: focusability and key press support. | ||
|  | ||
| Do not use the role `presentation` on the element: it removes the element's semantics, and may also remove its children's semantics, creating big issues with assistive technology. | ||
|  | ||
| Adjust the list of handler prop names in the handlers array to increase or decrease the coverage surface of this rule in your codebase. | ||
|  | ||
| ### Succeed | ||
|  | ||
| ```vue | ||
| <button @click="() => {}" class="foo" /> | ||
| <div class="foo" @click="() => {}" role="button" /> | ||
| <input type="text" @click="() => {}" /> | ||
| ``` | ||
|  | ||
| ### Fail | ||
|  | ||
| ```vue | ||
| <div @click="() => {}" /> | ||
| ``` | ||
|  | ||
| ## Accessibility guidelines | ||
|  | ||
| - [WCAG 4.1.2](https://www.w3.org/WAI/WCAG21/Understanding/name-role-value) | ||
|  | ||
| ### Resources | ||
|  | ||
| - [WAI-ARIA `role` attribute](https://www.w3.org/TR/wai-aria-1.1/#usage_intro) | ||
| - [WAI-ARIA Authoring Practices Guide - Design Patterns and Widgets](https://www.w3.org/TR/wai-aria-practices-1.1/#aria_ex) | ||
| - [Fundamental Keyboard Navigation Conventions](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_generalnav) | ||
| - [Mozilla Developer Network - ARIA Techniques](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_button_role#Keyboard_and_focus) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            84 changes: 84 additions & 0 deletions
          
          84 
        
  src/rules/__tests__/no-static-element-interactions.test.ts
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import rule from "../no-static-element-interactions"; | ||
| import makeRuleTester from "./makeRuleTester"; | ||
|  | ||
| makeRuleTester("no-static-element-interactions", rule, { | ||
| valid: [ | ||
| // Doesn't contain relevant directives | ||
| "<div />", | ||
| "<div id='foo' />", | ||
| "<CoolComponent />", | ||
| "<div class='foo' />", | ||
| "<form @submit='void 0' />", | ||
|  | ||
| // Custom components | ||
| "<Button @click='void 0' />", | ||
| "<v2-button @click='void 0' />", | ||
|  | ||
| // Contains relevant directives | ||
| "<div @click='void 0' role='button' />", | ||
| "<div @contextmenu='void 0' role='button' />", | ||
| "<div @dblclick='void 0' role='button' />", | ||
| "<div @doubleclick='void 0' role='button' />", | ||
| "<div @drag='void 0' role='button' />", | ||
| "<div @dragend='void 0' role='button' />", | ||
| "<div @dragenter='void 0' role='button' />", | ||
| "<div @dragexit='void 0' role='button' />", | ||
| "<div @dragleave='void 0' role='button' />", | ||
| "<div @dragover='void 0' role='button' />", | ||
| "<div @dragstart='void 0' role='button' />", | ||
| "<div @drop='void 0' role='button' />", | ||
| "<div @keydown='void 0' role='button' />", | ||
| "<div @keypress='void 0' role='button' />", | ||
| "<div @keyup='void 0' role='button' />", | ||
| "<div @mousedown='void 0' role='button' />", | ||
| "<div @mouseenter='void 0' role='button' />", | ||
| "<div @mouseleave='void 0' role='button' />", | ||
| "<div @mousemove='void 0' role='button' />", | ||
| "<div @mouseout='void 0' role='button' />", | ||
| "<div @mouseover='void 0' role='button' />", | ||
| "<div @mouseup='void 0' role='button' />", | ||
|  | ||
| // Elements which don't require `role='button'` | ||
| "<button @click='void 0' />", | ||
| "<input @click='void 0'} />", | ||
|  | ||
| // Exception: Elements hidden for screenreaders | ||
| "<div @click='void 0' aria-hidden='true'/>", | ||
|  | ||
| // Exception: `role='presentation'` | ||
| "<div @click='void 0' role='presentation'/>", | ||
| ], | ||
| invalid: [ | ||
| // Contains relevant directives but no `role='button'` | ||
| "<div @click='void 0' />", | ||
| "<div @contextmenu='void 0' />", | ||
| "<div @dblclick='void 0' />", | ||
| "<div @doubleclick='void 0' />", | ||
| "<div @drag='void 0' />", | ||
| "<div @dragend='void 0' />", | ||
| "<div @dragenter='void 0' />", | ||
| "<div @dragexit='void 0' />", | ||
| "<div @dragleave='void 0' />", | ||
| "<div @dragover='void 0' />", | ||
| "<div @dragstart='void 0' />", | ||
| "<div @drop='void 0' />", | ||
| "<div @keydown='void 0' />", | ||
| "<div @keypress='void 0' />", | ||
| "<div @keyup='void 0' />", | ||
| "<div @mousedown='void 0' />", | ||
| "<div @mouseenter='void 0' />", | ||
| "<div @mouseleave='void 0' />", | ||
| "<div @mousemove='void 0' />", | ||
| "<div @mouseout='void 0' />", | ||
| "<div @mouseover='void 0' />", | ||
| "<div @mouseup='void 0' />", | ||
|  | ||
| // Check other element types | ||
| "<a @click='void 0' />", | ||
| "<a @mousedown='void 0' />", | ||
| "<span @click='void 0' />", | ||
| "<span @mousedown='void 0' />", | ||
| "<section @click='void 0' />", | ||
| "<section @mousedown='void 0' />", | ||
| ] | ||
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { Rule } from "eslint"; | ||
| import { | ||
| defineTemplateBodyVisitor, | ||
| getElementAttributeValue, | ||
| hasOnDirectives, | ||
| interactiveHandlers, | ||
| isHiddenFromScreenReader, | ||
| isInteractiveElement, | ||
| isInteractiveRole, | ||
| isPresentationRole, makeDocsURL | ||
| } from "../utils"; | ||
| import { dom } from "aria-query"; | ||
|  | ||
| const rule: Rule.RuleModule = { | ||
| meta: { | ||
| type: "problem", | ||
| docs: { | ||
| url: makeDocsURL("no-static-element-interactions") | ||
| }, | ||
| messages: { | ||
| default: "Visible, non-interactive elements should not have an interactive handler." | ||
| }, | ||
| schema: [] | ||
|  | ||
| }, | ||
| create (context: Rule.RuleContext): Rule.RuleListener { | ||
| return defineTemplateBodyVisitor(context, { | ||
| VElement (node) { | ||
| const role = getElementAttributeValue(node, "role"); | ||
|  | ||
| const domElements = [...dom.keys()]; | ||
|  | ||
| if(!domElements.includes(node.name)) { | ||
| return; | ||
| } | ||
|         
                  ror-y marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| if(isHiddenFromScreenReader(node) || isPresentationRole(node)) { | ||
| return; | ||
| } | ||
|  | ||
| if( | ||
| hasOnDirectives(node, interactiveHandlers) && | ||
| !isInteractiveElement(node) && | ||
| !isInteractiveRole(role) | ||
| ) { | ||
| context.report({ node: node as any, messageId: "default" }); | ||
| } | ||
|  | ||
| } | ||
| }); | ||
| }, | ||
| }; | ||
|  | ||
| export default rule; | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { ARIARoleDefinitionKey, roles } from "aria-query"; | ||
|  | ||
| const interactiveRoles: ARIARoleDefinitionKey[] = []; | ||
|  | ||
| function getInteractiveRoles() { | ||
| for (const [role, definition] of roles.entries()) { | ||
| if ( | ||
| !definition.abstract && | ||
| definition.superClass.some((classes) => classes.includes("widget")) | ||
| ) { | ||
| interactiveRoles.push(role); | ||
| } | ||
| } | ||
|  | ||
| return interactiveRoles | ||
| } | ||
|  | ||
| export default getInteractiveRoles | ||
|         
                  ror-y marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| export default [ | ||
| "click", | ||
| "contextmenu", | ||
| "dblclick", | ||
| "doubleclick", | ||
| "drag", | ||
| "dragend", | ||
| "dragenter", | ||
| "dragexit", | ||
| "dragleave", | ||
| "dragover", | ||
| "dragstart", | ||
| "drop", | ||
| "keydown", | ||
| "keypress", | ||
| "keyup", | ||
| "mousedown", | ||
| "mouseenter", | ||
| "mouseleave", | ||
| "mousemove", | ||
| "mouseout", | ||
| "mouseover", | ||
| "mouseup" | ||
| ] | ||
|         
                  ror-y marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { roles } from "aria-query"; | ||
| import { getInteractiveRoles } from "../utils"; | ||
|  | ||
| function isInteractiveRole(value: any): value is string { | ||
| if (typeof value !== "string") { | ||
| return false; | ||
| } | ||
|  | ||
| return value | ||
| .toLowerCase() | ||
| .split(" ") | ||
| .some( | ||
| (role) => roles.has(role as any) && getInteractiveRoles().includes(role as any) | ||
|         
                  ror-y marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| ); | ||
| } | ||
|  | ||
| export default isInteractiveRole | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.