forked from vaadin/vaadin-checkbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vaadin-checkbox.html
330 lines (285 loc) · 9.36 KB
/
vaadin-checkbox.html
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->
<link rel="import" href="../../polymer/polymer-element.html">
<link rel="import" href="../../polymer/lib/mixins/gesture-event-listeners.html">
<link rel="import" href="../../vaadin-themable-mixin/vaadin-themable-mixin.html">
<link rel="import" href="../../vaadin-control-state-mixin/vaadin-control-state-mixin.html">
<link rel="import" href="../../vaadin-element-mixin/vaadin-element-mixin.html">
<dom-module id="vaadin-checkbox">
<template>
<style>
:host {
display: inline-block;
}
:host([hidden]) {
display: none !important;
}
label {
display: inline-flex;
align-items: baseline;
outline: none;
}
[part="checkbox"] {
position: relative;
display: inline-block;
flex: none;
}
input[type="checkbox"] {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
cursor: inherit;
margin: 0;
}
:host([disabled]) {
-webkit-tap-highlight-color: transparent;
}
</style>
<label>
<span part="checkbox">
<input
type="checkbox"
checked="{{checked::change}}"
disabled$="[[disabled]]"
indeterminate="{{indeterminate::change}}"
role="presentation"
tabindex="-1">
</span>
<span part="label">
<slot></slot>
</span>
</label>
</template>
<script>
(function() {
/**
* `<vaadin-checkbox>` is a Web Component for customized checkboxes.
*
* ```html
* <vaadin-checkbox>
* Make my profile visible
* </vaadin-checkbox>
* ```
*
* ### Styling
*
* The following shadow DOM parts are available for styling:
*
* Part name | Description
* ------------------|----------------
* `checkbox` | The checkbox element
* `label` | The label content element
*
* The following state attributes are available for styling:
*
* Attribute | Description | Part name
* -------------|-------------|--------------
* `active` | Set when the checkbox is pressed down, either with mouse, touch or the keyboard. | `:host`
* `disabled` | Set when the checkbox is disabled. | `:host`
* `focus-ring` | Set when the checkbox is focused using the keyboard. | `:host`
* `focused` | Set when the checkbox is focused. | `:host`
* `indeterminate` | Set when the checkbox is in indeterminate mode. | `:host`
* `checked` | Set when the checkbox is checked. | `:host`
* `empty` | Set when there is no label provided. | `label`
*
* See [ThemableMixin – how to apply styles for shadow parts](https://github.com/vaadin/vaadin-themable-mixin/wiki)
*
* @memberof Vaadin
* @mixes Vaadin.ElementMixin
* @mixes Vaadin.ControlStateMixin
* @mixes Vaadin.ThemableMixin
* @mixes Polymer.GestureEventListeners
* @demo demo/index.html
*/
class CheckboxElement extends
Vaadin.ElementMixin(
Vaadin.ControlStateMixin(
Vaadin.ThemableMixin(
Polymer.GestureEventListeners(Polymer.Element)))) {
static get is() {
return 'vaadin-checkbox';
}
static get version() {
return '2.2.7';
}
static get properties() {
return {
/**
* True if the checkbox is checked.
*/
checked: {
type: Boolean,
value: false,
notify: true,
observer: '_checkedChanged',
reflectToAttribute: true
},
/**
* Indeterminate state of the checkbox when it's neither checked nor unchecked, but undetermined.
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#Indeterminate_state_checkboxes
*/
indeterminate: {
type: Boolean,
notify: true,
observer: '_indeterminateChanged',
reflectToAttribute: true,
value: false
},
/**
* The value given to the data submitted with the checkbox's name to the server when the control is inside a form.
*/
value: {
type: String,
value: 'on'
},
_nativeCheckbox: {
type: Object
}
};
}
constructor() {
super();
/**
* @type {string}
* Name of the element.
*/
this.name;
}
get name() {
return this.checked ? this._storedName : '';
}
set name(name) {
this._storedName = name;
}
ready() {
super.ready();
this.setAttribute('role', 'checkbox');
this._nativeCheckbox = this.shadowRoot.querySelector('input[type="checkbox"]');
this.addEventListener('click', this._handleClick.bind(this));
this._addActiveListeners();
const attrName = this.getAttribute('name');
if (attrName) {
this.name = attrName;
}
this.shadowRoot.querySelector('[part~="label"]').querySelector('slot')
.addEventListener('slotchange', this._updateLabelAttribute.bind(this));
this._updateLabelAttribute();
}
_updateLabelAttribute() {
const label = this.shadowRoot.querySelector('[part~="label"]');
const assignedNodes = label.firstElementChild.assignedNodes();
if (this._isAssignedNodesEmpty(assignedNodes)) {
label.setAttribute('empty', '');
} else {
label.removeAttribute('empty');
}
}
_isAssignedNodesEmpty(nodes) {
// The assigned nodes considered to be empty if there is no slotted content or only one empty text node
return nodes.length === 0 ||
(nodes.length == 1
&& nodes[0].nodeType == Node.TEXT_NODE
&& nodes[0].textContent.trim() === '');
}
_checkedChanged(checked) {
if (this.indeterminate) {
this.setAttribute('aria-checked', 'mixed');
} else {
this.setAttribute('aria-checked', checked);
}
}
_indeterminateChanged(indeterminate) {
if (indeterminate) {
this.setAttribute('aria-checked', 'mixed');
} else {
this.setAttribute('aria-checked', this.checked);
}
}
_addActiveListeners() {
// DOWN
this._addEventListenerToNode(this, 'down', (e) => {
if (this.__interactionsAllowed(e)) {
this.setAttribute('active', '');
}
});
// UP
this._addEventListenerToNode(this, 'up', () => this.removeAttribute('active'));
// KEYDOWN
this.addEventListener('keydown', e => {
if (this.__interactionsAllowed(e) && e.keyCode === 32) {
e.preventDefault();
this.setAttribute('active', '');
}
});
// KEYUP
this.addEventListener('keyup', e => {
if (this.__interactionsAllowed(e) && e.keyCode === 32) {
e.preventDefault();
this._toggleChecked();
this.removeAttribute('active');
if (this.indeterminate) {
this.indeterminate = false;
}
}
});
}
/** @protected */
get focusElement() {
return this.shadowRoot.querySelector('input');
}
/**
* True if users' interactions (mouse or keyboard)
* should toggle the checkbox
*/
__interactionsAllowed(e) {
if (this.disabled) {
return false;
}
// https://github.com/vaadin/vaadin-checkbox/issues/63
if (e.target.localName === 'a') {
return false;
}
return true;
}
_handleClick(e) {
if (this.__interactionsAllowed(e)) {
if (!this.indeterminate) {
if (e.composedPath()[0] !== this._nativeCheckbox) {
e.preventDefault();
this._toggleChecked();
}
} else {
/*
* Required for IE 11 and Edge.
* See issue here: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/7344418/
*/
this.indeterminate = false;
e.preventDefault();
this._toggleChecked();
}
}
}
_toggleChecked() {
this.checked = !this.checked;
this.dispatchEvent(new CustomEvent('change', {composed: false, bubbles: true}));
}
/**
* Fired when the user commits a value change.
*
* @event change
*/
}
customElements.define(CheckboxElement.is, CheckboxElement);
/**
* @namespace Vaadin
*/
window.Vaadin.CheckboxElement = CheckboxElement;
})();
</script>
</dom-module>