Skip to content

Add new rule a11y-no-title-usage #316

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 7 commits into from
Feb 13, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changeset/nine-mirrors-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-primer-react': major
---

Add `a11y-no-title-usage` that warns against using `title` in some components
40 changes: 40 additions & 0 deletions docs/rules/a11y-no-title-usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## Rule Details

This rule aims to prevent the use of the `title` attribute with some components from `@primer/react`. The `title` attribute is not keyboard accessible, which results in accessibility issues. Instead, we should utilize alternatives that are accessible.

👎 Examples of **incorrect** code for this rule

```jsx
import {RelativeTime} from '@primer/react'

const App = () => <RelativeTime date={new Date('2020-01-01T00:00:00Z')} noTitle={false} />
```

👍 Examples of **correct** code for this rule:

```jsx
import {RelativeTime} from '@primer/react'

const App = () => <RelativeTime date={new Date('2020-01-01T00:00:00Z')} />
```

The noTitle attribute is true by default, so it can be omitted.

## With alternative tooltip
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to have this example ❤️


If you want to still utilize a tooltip in a similar way to how the `title` attribute works, you can use the [Primer `Tooltip`](https://primer.style/components/tooltip/react/beta). If you use the `Tooltip` component, you must use it with an interactive element, such as with a button or a link.

```jsx
import {RelativeTime, Tooltip} from '@primer/react'

const App = () => {
const date = new Date('2020-01-01T00:00:00Z')
return (
<Tooltip text={date.toString()}>
<Link href="#">
<RelativeTime date={date} noTitle={true} />
</Link>
</Tooltip>
)
}
```
1 change: 1 addition & 0 deletions src/configs/recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
'primer-react/a11y-tooltip-interactive-trigger': 'error',
'primer-react/new-color-css-vars': 'error',
'primer-react/a11y-explicit-heading': 'error',
'primer-react/a11y-no-title-usage': 'error',
'primer-react/no-deprecated-props': 'warn',
'primer-react/a11y-remove-disable-tooltip': 'error',
'primer-react/a11y-use-accessible-tooltip': 'error',
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
'a11y-link-in-text-block': require('./rules/a11y-link-in-text-block'),
'a11y-remove-disable-tooltip': require('./rules/a11y-remove-disable-tooltip'),
'a11y-use-accessible-tooltip': require('./rules/a11y-use-accessible-tooltip'),
'a11y-no-title-usage': require('./rules/a11y-no-title-usage'),
'use-deprecated-from-deprecated': require('./rules/use-deprecated-from-deprecated'),
'no-wildcard-imports': require('./rules/no-wildcard-imports'),
'no-unnecessary-components': require('./rules/no-unnecessary-components'),
Expand Down
27 changes: 27 additions & 0 deletions src/rules/__tests__/a11y-no-title-usage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const rule = require('../a11y-no-title-usage')
const {RuleTester} = require('eslint')

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
ecmaFeatures: {
jsx: true,
},
},
})

ruleTester.run('a11y-no-title-usage', rule, {
valid: [
`<RelativeTime date={new Date('2020-01-01T00:00:00Z')} noTitle={true} />`,
`<RelativeTime date={new Date('2020-01-01T00:00:00Z')} noTitle />`,
`<RelativeTime date={new Date('2020-01-01T00:00:00Z')} />`,
],
invalid: [
{
code: `<RelativeTime date={new Date('2020-01-01T00:00:00Z')} noTitle={false} />`,
output: `<RelativeTime date={new Date('2020-01-01T00:00:00Z')} />`,
errors: [{messageId: 'noTitleOnRelativeTime'}],
},
],
})
37 changes: 37 additions & 0 deletions src/rules/a11y-no-title-usage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const url = require('../url')
const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute')

module.exports = {
meta: {
type: 'error',
docs: {
description: 'Disallow usage of title attribute on some components',
recommended: true,
url: url(module),
},
messages: {
noTitleOnRelativeTime: 'Avoid using the title attribute on RelativeTime.',
},
fixable: 'code',
},

create(context) {
return {
JSXOpeningElement(jsxNode) {
const title = getJSXOpeningElementAttribute(jsxNode, 'noTitle')

if (title && title.value && title.value.expression && title.value.expression.value !== true) {
context.report({
node: title,
messageId: 'noTitleOnRelativeTime',
fix(fixer) {
const start = title.range[0] - 1
const end = title.range[1]
return fixer.removeRange([start, end])
},
})
}
},
}
},
}
Loading