-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlink.tsx
More file actions
65 lines (58 loc) · 2.26 KB
/
link.tsx
File metadata and controls
65 lines (58 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import type { AnchorHTMLAttributes } from 'react';
import { clsx } from 'clsx';
import { forwardRef } from 'react';
export interface LinkProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
/**
* Alternative for `aria-current`.
* Marks the link as **the current item** in a set related elements. Use the
* `current` prop in favor of `aria-current` for the proper styling.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-current aria-current on MDN}
*/
current?: AnchorHTMLAttributes<HTMLAnchorElement>['aria-current'];
/**
* The URL that the Link points to.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/a#href href on MDN}
*/
href: AnchorHTMLAttributes<HTMLAnchorElement>['href'];
/**
* Link around `inline-block`, `inline-flex` and `inline-grid` elements like
* _image_ or _badge_. Changing the `display` from `inline` to `inline-block`
* for the link ensures the focus outline is rendered around the entire box
* in all browsers.
*/
inlineBox?: boolean;
/**
* Alternative for `aria-disabled`. Marks the Link as **disabled**. Use the
* `disabled` prop in favor of `aria-disabled` since it ensures the proper
* styling, `role`, `href` and `tabindex` attributes.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-disabled aria-disabled on MDN}
*/
disabled?: boolean;
}
export const Link = forwardRef<HTMLAnchorElement, LinkProps>(function Link(props, forwardedRef) {
const { children, className, current, disabled, href, inlineBox, ...restProps } = props;
return (
<a
aria-current={current || undefined}
aria-disabled={disabled}
className={clsx(
'nl-link',
{
['nl-link--current']: current,
['nl-link--disabled']: disabled,
['nl-link--inline-box']: inlineBox,
},
className,
)}
href={disabled ? undefined : href}
role={disabled ? 'link' : undefined}
ref={forwardedRef}
tabIndex={disabled ? 0 : undefined}
// Let restProps override aria-current, aria-disabled, tabindex and role
{...restProps}
>
{children}
</a>
);
});
Link.displayName = 'Link';