Skip to content

Commit f12c894

Browse files
committed
docs: fix typo
1 parent c2ae092 commit f12c894

File tree

1 file changed

+48
-28
lines changed

1 file changed

+48
-28
lines changed

docs/rules/anchor-is-valid.md

+48-28
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ This is exacerbated by the expectation sighted users have of how _buttons_ and _
1919
We are aware that sometimes _anchors_ are used instead of _buttons_ to achieve a specific visual design. When using the `<button>` element this can still be achieved with styling but, due to the meaning many people attach to the standard underlined `<a>` due its appearance, please reconsider this in the design.
2020

2121
Consider the following:
22+
2223
```jsx
2324
<a href="javascript:void(0)" onClick={foo} >Perform action</a>
2425
<a href="#" onClick={foo} >Perform action</a>
2526
<a onClick={foo} >Perform action</a>
2627
```
2728

2829
All these _anchor_ implementations indicate that the element is only used to execute JavaScript code. All the above should be replaced with:
30+
2931
```jsx
30-
<button onClick={foo} >Perform action</button>
32+
<button onClick={foo}>Perform action</button>
3133
```
3234

3335
### Case: I want navigable links
@@ -37,52 +39,60 @@ An `<a>` element without an `href` attribute no longer functions as a hyperlink.
3739
To properly function as a hyperlink, the `href` attribute should be present and also contain a valid _URL_. _JavaScript_ strings, empty values or using only **#** are not considered valid `href` values.
3840

3941
Valid `href` attributes values are:
42+
4043
```jsx
4144
<a href="/some/valid/uri" >Navigate to page</a>
4245
<a href="/some/valid/uri#top" >Navigate to page and location</a>
4346
<a href="#top" >Navigate to internal page location</a>
4447
```
4548

46-
4749
### Case: I need the HTML to be interactive, don't I need to use an a tag for that?
48-
An `<a>` tag is not inherently interactive. Without an href attribute, it really is no different to a `<div>`.
50+
51+
An `<a>` tag is not inherently interactive. Without an href attribute, it really is no different than a `<span>`.
4952

5053
Let's look at an example that is not accessible by all users:
54+
5155
```jsx
5256
<a
53-
className={'thing'}
54-
onMouseEnter={() => this.setState({showSomething: true})}>
57+
className={"thing"}
58+
onMouseEnter={() => this.setState({ showSomething: true })}
59+
>
5560
{label}
5661
</a>
5762
```
5863

5964
If you need to create an interface element that the user can click on, consider using a button:
65+
6066
```jsx
6167
<button
62-
className={'thing'}
63-
onClick={() => this.setState({showSomething: true})}>
68+
className={"thing"}
69+
onClick={() => this.setState({ showSomething: true })}
70+
>
6471
{label}
6572
</button>
6673
```
6774

6875
If you want to navigate while providing the user with extra functionality, for example in the `onMouseEnter` event, use an anchor with an `href` attribute containing a URL or path as its value.
76+
6977
```jsx
7078
<a
7179
href={someValidPath}
72-
className={'thing'}
73-
onMouseEnter={() => this.setState({showSomething: true})}>
80+
className={"thing"}
81+
onMouseEnter={() => this.setState({ showSomething: true })}
82+
>
7483
{label}
7584
</a>
7685
```
7786

7887
If you need to create an interface element that the user can mouse over or mouse out of, consider using a div element. In this case, you may need to apply a role of presentation or an interactive role. Interactive ARIA roles include `button`, `link`, `checkbox`, `menuitem`, `menuitemcheckbox`, `menuitemradio`, `option`, `radio`, `searchbox`, `switch` and `textbox`.
88+
7989
```jsx
8090
<div
8191
role="menuitem"
82-
className={'thing'}
83-
onClick={() => this.setState({showSomething: true})}>
84-
onMouseEnter={() => this.setState({showSomething: true})}>
85-
{label}
92+
className={"thing"}
93+
onClick={() => this.setState({ showSomething: true })}
94+
>
95+
onMouseEnter={() => this.setState({ showSomething: true })}>{label}
8696
</div>
8797
```
8898

@@ -94,16 +104,17 @@ We recommend, without reserve, that elements resembling anchors should navigate.
94104

95105
However, we understand that developers are not always in total control of the visual design of web applications. In cases where it is imperative to provide an element resembling an anchor that purely acts as a click target with no navigation as result, we would like to recommend a compromise.
96106

97-
Again change the element to a `<button>`:
107+
Again change the element to a `<button>`:
98108

99109
```jsx
100-
<button
110+
<button
101111
type="button"
102-
className="link-button"
103-
onClick={() => this.setState({showSomething: true})}>
104-
Press me, I look like a link
112+
className="link-button"
113+
onClick={() => this.setState({ showSomething: true })}
114+
>
115+
Press me, I look like a link
105116
</button>
106-
```
117+
```
107118

108119
Then use styling to change its appearance to that of a link:
109120

@@ -120,7 +131,7 @@ Then use styling to change its appearance to that of a link:
120131

121132
.link-button:hover,
122133
.link-button:focus {
123-
text-decoration: none;
134+
text-decoration: none;
124135
}
125136
```
126137

@@ -134,13 +145,16 @@ This rule takes one optional object argument of type object:
134145

135146
```json
136147
{
137-
"rules": {
138-
"jsx-a11y/anchor-is-valid": [ "error", {
139-
"components": [ "Link" ],
140-
"specialLink": [ "hrefLeft", "hrefRight" ],
141-
"aspects": [ "noHref", "invalidHref", "preferButton" ]
142-
}]
143-
}
148+
"rules": {
149+
"jsx-a11y/anchor-is-valid": [
150+
"error",
151+
{
152+
"components": ["Link"],
153+
"specialLink": ["hrefLeft", "hrefRight"],
154+
"aspects": ["noHref", "invalidHref", "preferButton"]
155+
}
156+
]
157+
}
144158
}
145159
```
146160

@@ -169,11 +183,12 @@ For the `aspects` option, these strings determine which sub-rules are run. This
169183

170184
The option can be used on its own or with the `components` and `specialLink` options.
171185

172-
If omitted, all sub-rule aspects will be run by default. This is the recommended configuration for all cases except where the rule becomes unusable due to well founded restrictions.
186+
If omitted, all sub-rule aspects will be run by default. This is the recommended configuration for all cases except where the rule becomes unusable due to well founded restrictions.
173187

174188
The option must contain at least one `aspect`.
175189

176190
### Succeed
191+
177192
```jsx
178193
<a href="https://github.com" />
179194
<a href="#section" />
@@ -190,6 +205,7 @@ The option must contain at least one `aspect`.
190205
### Fail
191206

192207
Anchors should be a button:
208+
193209
```jsx
194210
<a onClick={foo} />
195211
<a href="#" onClick={foo} />
@@ -201,13 +217,15 @@ Anchors should be a button:
201217
```
202218

203219
Missing `href` attribute:
220+
204221
```jsx
205222
<a />
206223
<a href={undefined} />
207224
<a href={null} />
208225
```
209226

210227
Invalid `href` attribute:
228+
211229
```jsx
212230
<a href="#" />
213231
<a href={"#"} />
@@ -218,9 +236,11 @@ Invalid `href` attribute:
218236
```
219237

220238
## Accessibility guidelines
239+
221240
- [WCAG 2.1.1](https://www.w3.org/WAI/WCAG21/Understanding/keyboard)
222241

223242
### Resources
243+
224244
- [WebAIM - Introduction to Links and Hypertext](http://webaim.org/techniques/hypertext/)
225245
- [Links vs. Buttons in Modern Web Applications](https://marcysutton.com/links-vs-buttons-in-modern-web-applications/)
226246
- [Using ARIA - Notes on ARIA use in HTML](https://www.w3.org/TR/using-aria/#NOTES)

0 commit comments

Comments
 (0)