-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
68 lines (59 loc) · 2.04 KB
/
index.ts
File metadata and controls
68 lines (59 loc) · 2.04 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
66
67
68
import linkCss from '@nl-design-system-candidate/link-css/link.css?inline';
import { html, LitElement, nothing, unsafeCSS } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
@customElement('clippy-link')
export class ClippyLink extends LitElement {
@property() href = '';
@property() target = '';
@property() rel = '';
@property({ attribute: 'inline-box', type: Boolean }) inlineBox = false;
@property({ type: Boolean }) disabled = false;
@property({ attribute: 'aria-current' }) ariaCurrentValue: string = '';
@property({ attribute: false }) override className = '';
static override readonly styles = [unsafeCSS(linkCss)];
override render() {
const disabled = this.disabled;
// When disabled, remove href/target/rel and keep it focusable for accessibility.
const href = disabled ? nothing : this.href || nothing;
const target = disabled || !this.target ? nothing : this.target;
const rel = disabled || !this.rel ? nothing : this.rel;
const role = disabled ? 'link' : nothing;
const tabIndex = disabled ? 0 : nothing;
const ariaCurrent = this.ariaCurrentValue || nothing;
const classes = {
'nl-link': true,
'nl-link--current': Boolean(this.ariaCurrentValue),
'nl-link--disabled': disabled,
'nl-link--inline-box': this.inlineBox,
};
const hostClasses = (this.className || '')
.trim()
.split(/\s+/)
.filter(Boolean)
.reduce<Record<string, boolean>>((acc, name) => {
acc[name] = true;
return acc;
}, {});
const mergedClasses = { ...classes, ...hostClasses };
return html`
<a
class=${classMap(mergedClasses)}
href=${href}
target=${target}
rel=${rel}
aria-disabled=${disabled || nothing}
aria-current=${ariaCurrent}
role=${role}
tabindex=${tabIndex}
>
<slot></slot>
</a>
`;
}
}
declare global {
interface HTMLElementTagNameMap {
'clippy-link': ClippyLink;
}
}