Skip to content

Commit

Permalink
chore: improve useValidAnchor doc (biomejs#1955)
Browse files Browse the repository at this point in the history
  • Loading branch information
togami2864 authored Mar 3, 2024
1 parent 4ed7ac1 commit 41f97ce
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
11 changes: 8 additions & 3 deletions crates/biome_js_analyze/src/analyzers/a11y/use_valid_anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ declare_rule! {
///
/// There are **many reasons** why an anchor should not have a logic with an incorrect `href` attribute:
/// - it can disrupt the correct flow of the user navigation e.g. a user that wants to open the link
/// in another tab, but the default "click" behaviour is prevented
/// - it can source of invalid links, and crawlers can't navigate the website, risking to penalise
/// in another tab, but the default "click" behavior is prevented
/// - it can source of invalid links, and crawlers can't navigate the website, risking to penalize
/// SEO ranking
///
///
/// For a detailed explanation, check out https://marcysutton.com/links-vs-buttons-in-modern-web-applications
///
/// ## Examples
///
/// ### Invalid
Expand All @@ -43,7 +46,9 @@ declare_rule! {
/// ```jsx,expect_diagnostic
/// <a href="javascript:void(0)">navigate here</a>
/// ```
/// ```jsx,expect_diagnostic
/// <a onClick={something}>navigate here</a>
/// ```
/// ### Valid
///
/// ```jsx
Expand Down
24 changes: 22 additions & 2 deletions website/src/content/docs/linter/rules/use-valid-anchor.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ used for user interaction.
There are **many reasons** why an anchor should not have a logic with an incorrect `href` attribute:

- it can disrupt the correct flow of the user navigation e.g. a user that wants to open the link
in another tab, but the default "click" behaviour is prevented
- it can source of invalid links, and crawlers can't navigate the website, risking to penalise
in another tab, but the default "click" behavior is prevented
- it can source of invalid links, and crawlers can't navigate the website, risking to penalize
SEO ranking

For a detailed explanation, check out https://marcysutton.com/links-vs-buttons-in-modern-web-applications

## Examples

### Invalid
Expand Down Expand Up @@ -109,6 +111,24 @@ SEO ranking

</code></pre>

```jsx
<a onClick={something}>navigate here</a>
```

<pre class="language-text"><code class="language-text">a11y/useValidAnchor.js:1:4 <a href="https://biomejs.dev/linter/rules/use-valid-anchor">lint/a11y/useValidAnchor</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

<strong><span style="color: Tomato;"> </span></strong><strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Use a </span><span style="color: Tomato;"><strong>button</strong></span><span style="color: Tomato;"> element instead of an </span><span style="color: Tomato;"><strong>a</strong></span><span style="color: Tomato;"> element.</span>

<strong><span style="color: Tomato;"> </span></strong><strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>&lt;a onClick={something}&gt;navigate here&lt;/a&gt;
<strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong>
<strong>2 │ </strong>

<strong><span style="color: lightgreen;"> </span></strong><strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Anchor elements should only be used for default sections or page navigation</span>

<strong><span style="color: lightgreen;"> </span></strong><strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Check </span><span style="color: lightgreen;"><a href="https://marcysutton.com/links-vs-buttons-in-modern-web-applications">this thorough explanation</a></span><span style="color: lightgreen;"> to better understand the context.</span>

</code></pre>

### Valid

```jsx
Expand Down

0 comments on commit 41f97ce

Please sign in to comment.