You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/rules/a11y-link-in-text-block.md
+54-6Lines changed: 54 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,26 @@
1
-
# EXPERIMENTAL: Require `inline` prop on `<Link>` in text block
1
+
# EXPERIMENTAL: Require `inline` prop on `<Link>` in text block and convert HTML anchors to Link components
2
2
3
3
This is an experimental rule. If you suspect any false positives reported by this rule, please file an issue so we can make this rule better.
4
4
5
5
## Rule Details
6
6
7
7
The `Link` component should have the `inline` prop when it is used within a text block and has no styles (aside from color) to distinguish itself from surrounding plain text.
8
8
9
+
Additionally, HTML anchor elements (`<a>`) in text blocks should be converted to use the `Link` component from `@primer/react` to maintain consistent styling and accessibility.
10
+
9
11
Related: [WCAG 1.4.1 Use of Color issues](https://www.w3.org/WAI/WCAG21/Understanding/use-of-color.html)
10
12
11
-
The lint rule will flag any `<Link>` without the `inline` property (equal to `true`) detected with string nodes on either side.
13
+
The lint rule will flag:
14
+
- Any `<Link>` without the `inline` property (equal to `true`) detected with string nodes on either side.
15
+
- Any HTML `<a>` elements detected within a text block, with an autofix to convert them to `Link` components.
12
16
13
17
There are certain edge cases that the linter skips to avoid false positives including:
14
18
15
-
-`<Link className="...">` because there may be distinguishing styles applied.
19
+
-`<Link className="...">`or `<a className="...">`because there may be distinguishing styles applied.
16
20
-`<Link sx={{fontWeight:...}}>` or `<Link sx={{fontFamily:...}}>` because these technically may provide sufficient distinguishing styling.
17
-
-`<Link>` where the only adjacent text is a period, since that can't really be considered a text block.
18
-
-`<Link>` where the children is a JSX component, rather than a string literal, because then it might be an icon link rather than a text link.
19
-
-`<Link>` that are nested inside of headings as these have often been breadcrumbs.
21
+
-`<Link>`or `<a>`where the only adjacent text is a period, since that can't really be considered a text block.
22
+
-`<Link>`or `<a>`where the children is a JSX component, rather than a string literal, because then it might be an icon link rather than a text link.
23
+
-`<Link>`or `<a>`that are nested inside of headings as these have often been breadcrumbs.
20
24
21
25
This rule will not catch all instances of link in text block due to the limitations of static analysis, so be sure to also have in-browser checks in place such as the [link-in-text-block Axe rule](https://dequeuniversity.com/rules/axe/4.9/link-in-text-block) for additional coverage.
22
26
@@ -46,6 +50,26 @@ function ExampleComponent() {
46
50
}
47
51
```
48
52
53
+
```jsx
54
+
functionExampleComponent() {
55
+
return (
56
+
<SomeComponent>
57
+
Please <a href="https://github.com">visit our site</a>for more information.
58
+
</SomeComponent>
59
+
)
60
+
}
61
+
```
62
+
63
+
```jsx
64
+
functionExampleComponent() {
65
+
return (
66
+
<p>
67
+
Learn more about <a href="https://github.com/pricing">GitHub plans</a> and pricing options.
68
+
</p>
69
+
)
70
+
}
71
+
```
72
+
49
73
👍 Examples of **correct** code for this rule:
50
74
51
75
```jsx
@@ -68,6 +92,30 @@ function ExampleComponent() {
68
92
}
69
93
```
70
94
95
+
```jsx
96
+
import {Link} from'@primer/react'
97
+
98
+
functionExampleComponent() {
99
+
return (
100
+
<SomeComponent>
101
+
Please <Link href="https://github.com">visit our site</Link>for more information.
102
+
</SomeComponent>
103
+
)
104
+
}
105
+
```
106
+
107
+
```jsx
108
+
import {Link} from'@primer/react'
109
+
110
+
functionExampleComponent() {
111
+
return (
112
+
<p>
113
+
Learn more about <Link href="https://github.com/pricing" inline>GitHub plans</Link> and pricing options.
114
+
</p>
115
+
)
116
+
}
117
+
```
118
+
71
119
This rule will skip `Link`s containing JSX elements to minimize potential false positives because it is possible the JSX element sufficiently distinguishes the link from surrounding text.
0 commit comments