-
Notifications
You must be signed in to change notification settings - Fork 25
/
link.js
177 lines (166 loc) · 4.5 KB
/
link.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import '../colors/colors.js';
import '../icons/icon.js';
import { css, html, LitElement, nothing, unsafeCSS } from 'lit';
import { classMap } from 'lit/directives/class-map.js';
import { FocusMixin } from '../../mixins/focus/focus-mixin.js';
import { getFocusPseudoClass } from '../../helpers/focus.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { LocalizeCoreElement } from '../../helpers/localize-core-element.js';
import { offscreenStyles } from '../offscreen/offscreen.js';
import { styleMap } from 'lit/directives/style-map.js';
export const linkStyles = css`
.d2l-link, .d2l-link:visited, .d2l-link:active, .d2l-link:link {
color: var(--d2l-color-celestine);
cursor: pointer;
outline-style: none;
text-decoration: none;
}
:host([skeleton]) .d2l-link.d2l-skeletize::before {
bottom: 0.2rem;
top: 0.2rem;
}
.d2l-link:hover {
color: var(--d2l-color-celestine-minus-1);
text-decoration: underline;
}
.d2l-link:${unsafeCSS(getFocusPseudoClass())} {
border-radius: 2px;
outline: 2px solid var(--d2l-color-celestine);
outline-offset: 1px;
text-decoration: underline;
}
.d2l-link.d2l-link-main {
font-weight: 700;
}
.d2l-link.d2l-link-small {
font-size: 0.7rem;
letter-spacing: 0.01rem;
line-height: 1.05rem;
}
:host([skeleton]) .d2l-link.d2l-link-small.d2l-skeletize::before {
bottom: 0.15rem;
top: 0.15rem;
}
@media print {
.d2l-link, .d2l-link:visited, .d2l-link:active, .d2l-link:link {
color: var(--d2l-color-ferrite);
}
}
`;
/**
* This component can be used just like the native anchor tag.
* @slot - The content (e.g., text) that when selected causes navigation
*/
class Link extends LocalizeCoreElement(FocusMixin(LitElement)) {
static get properties() {
return {
/**
* ACCESSIBILITY: Label to provide more context for screen reader users when the link text is not enough
* @type {string}
*/
ariaLabel: { type: String, attribute: 'aria-label' },
/**
* Download a URL instead of navigating to it
* @type {boolean}
*/
download: { type: Boolean },
/**
* REQUIRED: URL or URL fragment of the link
* @type {string}
*/
href: { type: String },
/**
* Whether to apply the "main" link style
* @type {boolean}
*/
main: { type: Boolean, reflect: true },
/**
* The number of lines to display before truncating text with an ellipsis. The text will not be truncated unless a value is specified.
* @type {number}
*/
lines: { type: Number },
/**
* Whether to apply the "small" link style
* @type {boolean}
*/
small: { type: Boolean, reflect: true },
/**
* Where to display the linked URL
* @type {string}
*/
target: { type: String }
};
}
static get styles() {
return [ linkStyles, offscreenStyles,
css`
:host {
display: inline;
}
:host([hidden]) {
display: none;
}
:host([small]) {
/* needed to keep host element same height as link */
font-size: 0.7rem;
line-height: 1.05rem;
}
a {
display: inherit;
}
a.truncate {
-webkit-box-orient: vertical;
display: -webkit-box;
overflow: hidden;
overflow-wrap: anywhere;
}
d2l-icon {
color: var(--d2l-color-celestine);
height: 0.95em;
margin-inline-start: 0.315em;
vertical-align: inherit;
width: 0.95em;
}
a:hover d2l-icon {
--d2l-icon-fill-color: var(--d2l-color-celestine-minus-1);
}
@media print {
d2l-icon {
display: none;
}
}
`
];
}
constructor() {
super();
this.download = false;
this.main = false;
this.small = false;
this.lines = 0;
}
static get focusElementSelector() {
return '.d2l-link';
}
render() {
const linkClasses = {
'd2l-link': true,
'd2l-link-main': this.main,
'd2l-link-small': this.small,
'truncate': this.lines > 0
};
const styles = { webkitLineClamp: this.lines || null };
const newWindowElements = (this.target === '_blank')
? html`<span style="white-space: nowrap; line-height: 0;"><span style="font-size: 0;"> </span><d2l-icon icon="tier1:new-window"></d2l-icon></span><span class="d2l-offscreen">${this.localize('components.link.open-in-new-window')}</span>`
: nothing;
return html`<a
aria-label="${ifDefined(this.ariaLabel)}"
class="${classMap(linkClasses)}"
style="${styleMap(styles)}"
?download="${this.download}"
href="${ifDefined(this.href)}"
target="${ifDefined(this.target)}"
><slot></slot>${newWindowElements}</a>`;
}
}
customElements.define('d2l-link', Link);