-
Notifications
You must be signed in to change notification settings - Fork 25
/
button-subtle.js
239 lines (205 loc) · 7.35 KB
/
button-subtle.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import '../icons/icon.js';
import '../tooltip/tooltip.js';
import { css, html, LitElement, nothing } from 'lit';
import { ButtonMixin } from './button-mixin.js';
import { buttonStyles } from './button-styles.js';
import { classMap } from 'lit/directives/class-map.js';
import { getUniqueId } from '../../helpers/uniqueId.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { labelStyles } from '../typography/styles.js';
/**
* A button component that can be used just like the native button, but for advanced or de-emphasized actions.
* @slot - Default content placed inside of the button
* @slot icon - Optional slot for a custom icon
*/
class ButtonSubtle extends ButtonMixin(LitElement) {
static get properties() {
return {
/**
* ACCESSIBILITY: A description to be added to the button for accessibility when text on button does not provide enough context
* @type {string}
*/
description: { type: String },
/**
* Aligns the leading edge of text if value is set to "text"
* @type {'text'|''}
*/
hAlign: { type: String, reflect: true, attribute: 'h-align' },
/**
* Preset icon key (e.g. "tier1:gear")
* @type {string}
*/
icon: { type: String, reflect: true },
/**
* Indicates that the icon should be rendered on right
* @type {boolean}
*/
iconRight: { type: Boolean, reflect: true, attribute: 'icon-right' },
/**
* Whether to render the slimmer version of the button
* @type {boolean}
*/
slim: { type: Boolean, reflect: true },
/**
* ACCESSIBILITY: REQUIRED: Text for the button
* @type {string}
*/
text: { type: String, reflect: true },
_hasCustomIcon: { state: true }
};
}
static get styles() {
return [ labelStyles, buttonStyles,
css`
:host {
--d2l-count-badge-background-color: var(--d2l-color-celestine);
--d2l-count-badge-foreground-color: #ffffff;
display: inline-block;
}
:host([hidden]) {
display: none;
}
button {
--d2l-button-subtle-padding-inline-start: 0.6rem;
--d2l-button-subtle-padding-inline-end: 0.6rem;
background-color: transparent;
border-color: transparent;
font-family: inherit;
padding-block-end: 0;
padding-block-start: 0;
padding-inline-end: var(--d2l-button-subtle-padding-inline-end);
padding-inline-start: var(--d2l-button-subtle-padding-inline-start);
position: relative;
}
:host([slim]) button {
--d2l-button-subtle-padding-inline-start: 0.5rem;
--d2l-button-subtle-padding-inline-end: 0.5rem;
min-height: 1.5rem;
}
:host([slim]) button.d2l-button-subtle-has-icon {
--d2l-button-subtle-padding-inline-start: 0.4rem;
--d2l-button-subtle-padding-inline-end: 0.5rem;
}
:host([slim][icon-right]) button.d2l-button-subtle-has-icon {
--d2l-button-subtle-padding-inline-start: 0.5rem;
--d2l-button-subtle-padding-inline-end: 0.4rem;
}
:host([h-align="text"]) button {
inset-inline-start: calc(var(--d2l-button-subtle-padding-inline-start) * -1);
}
/* Firefox includes a hidden border which messes up button dimensions */
button::-moz-focus-inner {
border: 0;
}
button[disabled]:hover,
button[disabled]:focus,
:host([active]) button[disabled] {
background-color: transparent;
}
button:hover,
button:focus,
:host([active]) button {
background-color: var(--d2l-color-gypsum);
}
.d2l-button-subtle-content {
color: var(--d2l-color-celestine);
}
button:hover:not([disabled]) .d2l-button-subtle-content,
button:focus:not([disabled]) .d2l-button-subtle-content,
:host([active]:not([disabled])) button .d2l-button-subtle-content {
color: var(--d2l-color-celestine-minus-1);
}
button:hover:not([disabled]),
button:focus:not([disabled]),
:host([active]:not([disabled])) {
--d2l-count-badge-background-color: var(--d2l-color-celestine-minus-1);
}
.d2l-button-subtle-has-icon .d2l-button-subtle-content-wrapper {
padding-inline: 1.2rem 0;
}
:host([icon-right]) .d2l-button-subtle-has-icon .d2l-button-subtle-content-wrapper {
padding-inline: 0 1.2rem;
}
slot[name="icon"]::slotted(*) {
display: none;
}
d2l-icon.d2l-button-subtle-icon,
slot[name="icon"]::slotted(d2l-icon-custom) {
color: var(--d2l-color-celestine);
display: inline-block;
height: 0.9rem;
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 0.9rem;
}
button:hover:not([disabled]) d2l-icon.d2l-button-subtle-icon,
button:focus:not([disabled]) d2l-icon.d2l-button-subtle-icon,
:host([active]:not([disabled])) button d2l-icon.d2l-button-subtle-icon,
button:hover:not([disabled]) slot[name="icon"]::slotted(d2l-icon-custom),
button:focus:not([disabled]) slot[name="icon"]::slotted(d2l-icon-custom),
:host([active]:not([disabled])) slot[name="icon"]::slotted(d2l-icon-custom) {
color: var(--d2l-color-celestine-minus-1);
}
:host([icon-right]) .d2l-button-subtle-has-icon d2l-icon.d2l-button-subtle-icon,
:host([icon-right]) slot[name="icon"]::slotted(d2l-icon-custom) {
inset-inline-end: var(--d2l-button-subtle-padding-inline-end);
}
:host([disabled]) button {
cursor: default;
opacity: 0.5;
}
`
];
}
constructor() {
super();
this.iconRight = false;
this.slim = false;
this._buttonId = getUniqueId();
this._describedById = getUniqueId();
this._hasCustomIcon = false;
}
render() {
const icon = this.icon ? html`<d2l-icon icon="${this.icon}" class="d2l-button-subtle-icon"></d2l-icon>` : nothing;
const buttonClasses = {
'd2l-button-subtle-has-icon': this._hasCustomIcon || this.icon,
'd2l-label-text': true
};
return html`
<button
aria-describedby="${ifDefined(this.description ? this._describedById : undefined)}"
aria-disabled="${ifDefined(this.disabled && this.disabledTooltip ? 'true' : undefined)}"
aria-expanded="${ifDefined(this.ariaExpanded)}"
aria-haspopup="${ifDefined(this.ariaHaspopup)}"
aria-label="${ifDefined(this.ariaLabel)}"
?autofocus="${this.autofocus}"
class="${classMap(buttonClasses)}"
?disabled="${this.disabled && !this.disabledTooltip}"
form="${ifDefined(this.form)}"
formaction="${ifDefined(this.formaction)}"
formenctype="${ifDefined(this.formenctype)}"
formmethod="${ifDefined(this.formmethod)}"
?formnovalidate="${this.formnovalidate}"
formtarget="${ifDefined(this.formtarget)}"
id="${this._buttonId}"
name="${ifDefined(this.name)}"
type="${this._getType()}">
<slot name="icon" @slotchange="${this._handleIconSlotChange}">${icon}</slot>
<span class="d2l-button-subtle-content-wrapper">
<span class="d2l-button-subtle-content">${this.text}</span>
<slot></slot>
</span>
</button>
${this.description ? html`<span id="${this._describedById}" hidden>${this.description}</span>` : null}
${this.disabled && this.disabledTooltip ? html`<d2l-tooltip class="vdiff-target" for="${this._buttonId}">${this.disabledTooltip}</d2l-tooltip>` : ''}
`;
}
_handleIconSlotChange(e) {
const icon = e && e.target && e.target.assignedNodes({ flatten: true }).filter((node) => {
return node.nodeType === Node.ELEMENT_NODE && node.tagName === 'D2L-ICON-CUSTOM';
});
this._hasCustomIcon = icon.length === 1;
}
}
customElements.define('d2l-button-subtle', ButtonSubtle);