Skip to content

Link API breaks a11y intuition #5533

@danny-andrews-snap

Description

@danny-andrews-snap

Feature request

I want a more intuitive Link API which respects expectations we have around native a tags.

Is your feature request related to a problem? Please describe.

The current link API looks like the following:

<Link href="/about">
  <a>here</a>
</Link>

This is problematic for a few reasons:

  1. It breaks intuition around a11y. When I see an a tag without an href, alarm bells go off, because that is a UX anti-pattern. In fact, this API makes it impossible to appease the anchor-is-valid eslint-plugin-jsx-a11y rule (see https://github.com/evcohen/eslint-plugin-jsx-a11y/issues/402).
  2. It requires a call to cloneElement which should be avoided as its behavior is confusing. (How would I know my a tag will be given an href? Nothing about this API suggests that.)

Describe the solution you'd like

I would like a solution which doesn't require you to type an a tag without an href and also doesn't require you to duplicate the href information.

Describe alternatives you've considered

There are a few good alternatives:

  1. Default to a tag if no children are given:
<Link href="/about" />

This is the approach react-router uses and it works quite well.


  1. Use a render prop:
<Link href="/about">
  {href => <a href={href}>here</a>}
</Link>

This method appeases the anchor-is-valid rule and doesn't require the user to duplicate href information since the build href is passed down to the component they are rendering.


  1. Component injection:
<Link href="/about" component={a} />

A combination of 1 and 2 would be powerful enough to cover all reasonable use cases.

Additional context

6431f5f

Metadata

Metadata

Assignees

Labels

Linking and NavigatingRelated to Next.js linking (e.g., <Link>) and navigation.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions