@spectrum-web-components/action-group 0.9.0 → 0.10.1-devmode.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,326 +1,309 @@
1
- /*
2
- Copyright 2020 Adobe. All rights reserved.
3
- This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License. You may obtain a copy
5
- of the License at http://www.apache.org/licenses/LICENSE-2.0
6
-
7
- Unless required by applicable law or agreed to in writing, software distributed under
8
- the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- OF ANY KIND, either express or implied. See the License for the specific language
10
- governing permissions and limitations under the License.
11
- */
12
- import { __decorate } from "tslib";
13
- import { html, SpectrumElement, } from '@spectrum-web-components/base';
14
- import { property } from '@spectrum-web-components/base/src/decorators.js';
15
- import { RovingTabindexController } from '@spectrum-web-components/reactive-controllers/src/RovingTabindex.js';
16
- import { MutationController } from '@lit-labs/observers/mutation_controller.js';
17
- import styles from './action-group.css.js';
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __decorateClass = (decorators, target, key, kind) => {
4
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
5
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
6
+ if (decorator = decorators[i])
7
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
8
+ if (kind && result)
9
+ __defProp(target, key, result);
10
+ return result;
11
+ };
12
+ import {
13
+ html,
14
+ SpectrumElement
15
+ } from "@spectrum-web-components/base";
16
+ import { property } from "@spectrum-web-components/base/src/decorators.js";
17
+ import { RovingTabindexController } from "@spectrum-web-components/reactive-controllers/src/RovingTabindex.js";
18
+ import { MutationController } from "@lit-labs/observers/mutation_controller.js";
19
+ import styles from "./action-group.css.js";
18
20
  const EMPTY_SELECTION = [];
19
- /**
20
- * @element sp-action-group
21
- * @slot - the sp-action-button elements that make up the group
22
- *
23
- * @fires change - Announces that selection state has been changed by user
24
- */
25
21
  export class ActionGroup extends SpectrumElement {
26
- constructor() {
27
- super();
28
- this._buttons = [];
29
- this._buttonSelector = 'sp-action-button';
30
- this.rovingTabindexController = new RovingTabindexController(this, {
31
- focusInIndex: (elements) => {
32
- let firstEnabledIndex = -1;
33
- const firstSelectedIndex = elements.findIndex((el, index) => {
34
- if (!elements[firstEnabledIndex] && !el.disabled) {
35
- firstEnabledIndex = index;
36
- }
37
- return el.selected && !el.disabled;
38
- });
39
- return elements[firstSelectedIndex]
40
- ? firstSelectedIndex
41
- : firstEnabledIndex;
42
- },
43
- elements: () => this.buttons,
44
- isFocusableElement: (el) => !el.disabled,
22
+ constructor() {
23
+ super();
24
+ this._buttons = [];
25
+ this._buttonSelector = "sp-action-button";
26
+ this.rovingTabindexController = new RovingTabindexController(this, {
27
+ focusInIndex: (elements) => {
28
+ let firstEnabledIndex = -1;
29
+ const firstSelectedIndex = elements.findIndex((el, index) => {
30
+ if (!elements[firstEnabledIndex] && !el.disabled) {
31
+ firstEnabledIndex = index;
32
+ }
33
+ return el.selected && !el.disabled;
45
34
  });
46
- this.compact = false;
47
- this.emphasized = false;
48
- this.justified = false;
49
- this.label = '';
50
- this.quiet = false;
51
- this.vertical = false;
52
- this._selected = EMPTY_SELECTION;
53
- this.manageButtons = () => {
54
- const slot = this.shadowRoot.querySelector('slot');
55
- if (!slot)
56
- return;
57
- const assignedElements = slot.assignedElements({ flatten: true });
58
- const buttons = assignedElements.reduce((acc, el) => {
59
- if (el.matches(this._buttonSelector)) {
60
- acc.push(el);
61
- }
62
- else {
63
- const buttonDescendents = Array.from(el.querySelectorAll(`:scope > ${this._buttonSelector}`));
64
- acc.push(...buttonDescendents);
65
- }
66
- return acc;
67
- }, []);
68
- this.buttons = buttons;
69
- // <selected> element merges selected so following paradigm here
70
- const currentlySelectedButtons = [];
71
- this.buttons.forEach((button) => {
72
- if (button.selected) {
73
- currentlySelectedButtons.push(button.value);
74
- }
75
- });
76
- this.setSelected(this.selected.concat(currentlySelectedButtons));
77
- this.manageChildren();
78
- this.manageSelects();
79
- };
80
- new MutationController(this, {
81
- config: {
82
- childList: true,
83
- subtree: true,
84
- },
85
- callback: () => {
86
- this.manageButtons();
87
- },
88
- });
89
- }
90
- static get styles() {
91
- return [styles];
92
- }
93
- set buttons(tabs) {
94
- if (tabs === this.buttons)
95
- return;
96
- this._buttons = tabs;
97
- this.rovingTabindexController.clearElementCache();
98
- }
99
- get buttons() {
100
- return this._buttons;
101
- }
102
- set selected(selected) {
103
- this.requestUpdate('selected', this._selected);
104
- this._selected = selected;
105
- this.updateComplete.then(() => {
106
- this.applySelects();
107
- this.manageChildren();
108
- });
109
- }
110
- get selected() {
111
- return this._selected;
112
- }
113
- dispatchChange(old) {
114
- const applyDefault = this.dispatchEvent(new Event('change', {
115
- bubbles: true,
116
- composed: true,
117
- cancelable: true,
118
- }));
119
- if (!applyDefault) {
120
- this.setSelected(old);
121
- this.buttons.map((button) => {
122
- button.selected = this.selected.includes(button.value);
123
- });
35
+ return elements[firstSelectedIndex] ? firstSelectedIndex : firstEnabledIndex;
36
+ },
37
+ elements: () => this.buttons,
38
+ isFocusableElement: (el) => !el.disabled
39
+ });
40
+ this.compact = false;
41
+ this.emphasized = false;
42
+ this.justified = false;
43
+ this.label = "";
44
+ this.quiet = false;
45
+ this.vertical = false;
46
+ this._selected = EMPTY_SELECTION;
47
+ this.manageButtons = () => {
48
+ const slot = this.shadowRoot.querySelector("slot");
49
+ if (!slot)
50
+ return;
51
+ const assignedElements = slot.assignedElements({ flatten: true });
52
+ const buttons = assignedElements.reduce((acc, el) => {
53
+ if (el.matches(this._buttonSelector)) {
54
+ acc.push(el);
55
+ } else {
56
+ const buttonDescendents = Array.from(el.querySelectorAll(`:scope > ${this._buttonSelector}`));
57
+ acc.push(...buttonDescendents);
124
58
  }
59
+ return acc;
60
+ }, []);
61
+ this.buttons = buttons;
62
+ const currentlySelectedButtons = [];
63
+ this.buttons.forEach((button) => {
64
+ if (button.selected) {
65
+ currentlySelectedButtons.push(button.value);
66
+ }
67
+ });
68
+ this.setSelected(this.selected.concat(currentlySelectedButtons));
69
+ this.manageChildren();
70
+ this.manageSelects();
71
+ };
72
+ new MutationController(this, {
73
+ config: {
74
+ childList: true,
75
+ subtree: true
76
+ },
77
+ callback: () => {
78
+ this.manageButtons();
79
+ }
80
+ });
81
+ }
82
+ static get styles() {
83
+ return [styles];
84
+ }
85
+ set buttons(tabs) {
86
+ if (tabs === this.buttons)
87
+ return;
88
+ this._buttons = tabs;
89
+ this.rovingTabindexController.clearElementCache();
90
+ }
91
+ get buttons() {
92
+ return this._buttons;
93
+ }
94
+ set selected(selected) {
95
+ this.requestUpdate("selected", this._selected);
96
+ this._selected = selected;
97
+ this.updateComplete.then(() => {
98
+ this.applySelects();
99
+ this.manageChildren();
100
+ });
101
+ }
102
+ get selected() {
103
+ return this._selected;
104
+ }
105
+ dispatchChange(old) {
106
+ const applyDefault = this.dispatchEvent(new Event("change", {
107
+ bubbles: true,
108
+ composed: true,
109
+ cancelable: true
110
+ }));
111
+ if (!applyDefault) {
112
+ this.setSelected(old);
113
+ this.buttons.map((button) => {
114
+ button.selected = this.selected.includes(button.value);
115
+ });
125
116
  }
126
- setSelected(selected, announce) {
127
- if (selected === this.selected)
128
- return;
129
- const old = this.selected;
130
- this.requestUpdate('selected', old);
131
- this._selected = selected;
132
- if (!announce)
133
- return;
134
- this.dispatchChange(old);
135
- }
136
- focus(options) {
137
- this.rovingTabindexController.focus(options);
138
- }
139
- deselectSelectedButtons() {
140
- const selected = [
141
- ...this.querySelectorAll('[selected]'),
142
- ];
143
- selected.forEach((el) => {
144
- el.selected = false;
145
- el.tabIndex = -1;
146
- el.setAttribute('aria-checked', 'false');
147
- });
117
+ }
118
+ setSelected(selected, announce) {
119
+ if (selected === this.selected)
120
+ return;
121
+ const old = this.selected;
122
+ this.requestUpdate("selected", old);
123
+ this._selected = selected;
124
+ if (!announce)
125
+ return;
126
+ this.dispatchChange(old);
127
+ }
128
+ focus(options) {
129
+ this.rovingTabindexController.focus(options);
130
+ }
131
+ deselectSelectedButtons() {
132
+ const selected = [
133
+ ...this.querySelectorAll("[selected]")
134
+ ];
135
+ selected.forEach((el) => {
136
+ el.selected = false;
137
+ el.tabIndex = -1;
138
+ el.setAttribute("aria-checked", "false");
139
+ });
140
+ }
141
+ handleClick(event) {
142
+ const target = event.target;
143
+ if (typeof target.value === "undefined") {
144
+ return;
148
145
  }
149
- handleClick(event) {
150
- const target = event.target;
151
- if (typeof target.value === 'undefined') {
152
- return;
153
- }
154
- switch (this.selects) {
155
- case 'single': {
156
- this.deselectSelectedButtons();
157
- target.selected = true;
158
- target.tabIndex = 0;
159
- target.setAttribute('aria-checked', 'true');
160
- this.setSelected([target.value], true);
161
- target.focus();
162
- break;
163
- }
164
- case 'multiple': {
165
- const selected = [...this.selected];
166
- target.selected = !target.selected;
167
- target.setAttribute('aria-checked', target.selected ? 'true' : 'false');
168
- if (target.selected) {
169
- selected.push(target.value);
170
- }
171
- else {
172
- selected.splice(this.selected.indexOf(target.value), 1);
173
- }
174
- this.setSelected(selected, true);
175
- this.buttons.forEach((button) => {
176
- button.tabIndex = -1;
177
- });
178
- target.tabIndex = 0;
179
- break;
180
- }
181
- default:
182
- break;
146
+ switch (this.selects) {
147
+ case "single": {
148
+ this.deselectSelectedButtons();
149
+ target.selected = true;
150
+ target.tabIndex = 0;
151
+ target.setAttribute("aria-checked", "true");
152
+ this.setSelected([target.value], true);
153
+ target.focus();
154
+ break;
155
+ }
156
+ case "multiple": {
157
+ const selected = [...this.selected];
158
+ target.selected = !target.selected;
159
+ target.setAttribute("aria-checked", target.selected ? "true" : "false");
160
+ if (target.selected) {
161
+ selected.push(target.value);
162
+ } else {
163
+ selected.splice(this.selected.indexOf(target.value), 1);
183
164
  }
165
+ this.setSelected(selected, true);
166
+ this.buttons.forEach((button) => {
167
+ button.tabIndex = -1;
168
+ });
169
+ target.tabIndex = 0;
170
+ break;
171
+ }
172
+ default:
173
+ break;
184
174
  }
185
- async applySelects() {
186
- await this.manageSelects(true);
175
+ }
176
+ async applySelects() {
177
+ await this.manageSelects(true);
178
+ }
179
+ async manageSelects(applied) {
180
+ if (!this.buttons.length) {
181
+ return;
187
182
  }
188
- async manageSelects(applied) {
189
- if (!this.buttons.length) {
190
- return;
191
- }
192
- const options = this.buttons;
193
- switch (this.selects) {
194
- case 'single': {
195
- this.setAttribute('role', 'radiogroup');
196
- const selections = [];
197
- const updates = options.map(async (option) => {
198
- await option.updateComplete;
199
- option.setAttribute('role', 'radio');
200
- option.setAttribute('aria-checked', option.selected ? 'true' : 'false');
201
- if (option.selected) {
202
- selections.push(option);
203
- }
204
- });
205
- if (applied)
206
- break;
207
- await Promise.all(updates);
208
- const selected = selections.map((button) => {
209
- return button.value;
210
- });
211
- this.setSelected(selected || EMPTY_SELECTION);
212
- break;
213
- }
214
- case 'multiple': {
215
- this.setAttribute('role', 'group');
216
- const selection = [];
217
- const selections = [];
218
- const updates = options.map(async (option) => {
219
- await option.updateComplete;
220
- option.setAttribute('role', 'checkbox');
221
- option.setAttribute('aria-checked', option.selected ? 'true' : 'false');
222
- if (option.selected) {
223
- selection.push(option.value);
224
- selections.push(option);
225
- }
226
- });
227
- if (applied)
228
- break;
229
- await Promise.all(updates);
230
- const selected = !!selection.length
231
- ? selection
232
- : EMPTY_SELECTION;
233
- this.setSelected(selected);
234
- break;
183
+ const options = this.buttons;
184
+ switch (this.selects) {
185
+ case "single": {
186
+ this.setAttribute("role", "radiogroup");
187
+ const selections = [];
188
+ const updates = options.map(async (option) => {
189
+ await option.updateComplete;
190
+ option.setAttribute("role", "radio");
191
+ option.setAttribute("aria-checked", option.selected ? "true" : "false");
192
+ if (option.selected) {
193
+ selections.push(option);
194
+ }
195
+ });
196
+ if (applied)
197
+ break;
198
+ await Promise.all(updates);
199
+ const selected = selections.map((button) => {
200
+ return button.value;
201
+ });
202
+ this.setSelected(selected || EMPTY_SELECTION);
203
+ break;
204
+ }
205
+ case "multiple": {
206
+ this.setAttribute("role", "group");
207
+ const selection = [];
208
+ const selections = [];
209
+ const updates = options.map(async (option) => {
210
+ await option.updateComplete;
211
+ option.setAttribute("role", "checkbox");
212
+ option.setAttribute("aria-checked", option.selected ? "true" : "false");
213
+ if (option.selected) {
214
+ selection.push(option.value);
215
+ selections.push(option);
216
+ }
217
+ });
218
+ if (applied)
219
+ break;
220
+ await Promise.all(updates);
221
+ const selected = !!selection.length ? selection : EMPTY_SELECTION;
222
+ this.setSelected(selected);
223
+ break;
224
+ }
225
+ default:
226
+ if (this.selected.length) {
227
+ const selections = [];
228
+ const updates = options.map(async (option) => {
229
+ await option.updateComplete;
230
+ option.setAttribute("aria-checked", option.selected ? "true" : "false");
231
+ option.setAttribute("role", "button");
232
+ if (option.selected) {
233
+ selections.push(option);
235
234
  }
236
- default:
237
- // if user defines .selected
238
- if (this.selected.length) {
239
- const selections = [];
240
- const updates = options.map(async (option) => {
241
- await option.updateComplete;
242
- option.setAttribute('aria-checked', option.selected ? 'true' : 'false');
243
- option.setAttribute('role', 'button');
244
- if (option.selected) {
245
- selections.push(option);
246
- }
247
- });
248
- if (applied)
249
- break;
250
- await Promise.all(updates);
251
- this.setSelected(selections.map((button) => {
252
- return button.value;
253
- }));
254
- }
255
- else {
256
- this.buttons.forEach((option) => {
257
- option.setAttribute('role', 'button');
258
- });
259
- this.removeAttribute('role');
260
- break;
261
- }
235
+ });
236
+ if (applied)
237
+ break;
238
+ await Promise.all(updates);
239
+ this.setSelected(selections.map((button) => {
240
+ return button.value;
241
+ }));
242
+ } else {
243
+ this.buttons.forEach((option) => {
244
+ option.setAttribute("role", "button");
245
+ });
246
+ this.removeAttribute("role");
247
+ break;
262
248
  }
263
249
  }
264
- render() {
265
- return html `
250
+ }
251
+ render() {
252
+ return html`
266
253
  <slot role="presentation" @slotchange=${this.manageButtons}></slot>
267
254
  `;
255
+ }
256
+ firstUpdated(changes) {
257
+ super.firstUpdated(changes);
258
+ this.addEventListener("click", this.handleClick);
259
+ }
260
+ updated(changes) {
261
+ super.updated(changes);
262
+ if (changes.has("selects")) {
263
+ this.manageSelects();
264
+ this.manageChildren();
268
265
  }
269
- firstUpdated(changes) {
270
- super.firstUpdated(changes);
271
- this.addEventListener('click', this.handleClick);
266
+ if (changes.has("quiet") && this.quiet || changes.has("emphasized") && this.emphasized) {
267
+ this.manageChildren();
272
268
  }
273
- updated(changes) {
274
- super.updated(changes);
275
- if (changes.has('selects')) {
276
- this.manageSelects();
277
- this.manageChildren();
278
- }
279
- if ((changes.has('quiet') && this.quiet) ||
280
- (changes.has('emphasized') && this.emphasized)) {
281
- this.manageChildren();
282
- }
283
- // Update `aria-label` when `label` available or not first `updated`
284
- if (changes.has('label') &&
285
- (this.label || typeof changes.get('label') !== 'undefined')) {
286
- if (this.label.length) {
287
- this.setAttribute('aria-label', this.label);
288
- }
289
- else {
290
- this.removeAttribute('aria-label');
291
- }
292
- }
293
- }
294
- manageChildren() {
295
- this.buttons.forEach((button) => {
296
- button.quiet = this.quiet;
297
- button.emphasized = this.emphasized;
298
- button.selected = this.selected.includes(button.value);
299
- });
269
+ if (changes.has("label") && (this.label || typeof changes.get("label") !== "undefined")) {
270
+ if (this.label.length) {
271
+ this.setAttribute("aria-label", this.label);
272
+ } else {
273
+ this.removeAttribute("aria-label");
274
+ }
300
275
  }
276
+ }
277
+ manageChildren() {
278
+ this.buttons.forEach((button) => {
279
+ button.quiet = this.quiet;
280
+ button.emphasized = this.emphasized;
281
+ button.selected = this.selected.includes(button.value);
282
+ });
283
+ }
301
284
  }
302
- __decorate([
303
- property({ type: Boolean, reflect: true })
304
- ], ActionGroup.prototype, "compact", void 0);
305
- __decorate([
306
- property({ type: Boolean, reflect: true })
307
- ], ActionGroup.prototype, "emphasized", void 0);
308
- __decorate([
309
- property({ type: Boolean, reflect: true })
310
- ], ActionGroup.prototype, "justified", void 0);
311
- __decorate([
312
- property({ type: String })
313
- ], ActionGroup.prototype, "label", void 0);
314
- __decorate([
315
- property({ type: Boolean, reflect: true })
316
- ], ActionGroup.prototype, "quiet", void 0);
317
- __decorate([
318
- property({ type: String })
319
- ], ActionGroup.prototype, "selects", void 0);
320
- __decorate([
321
- property({ type: Boolean, reflect: true })
322
- ], ActionGroup.prototype, "vertical", void 0);
323
- __decorate([
324
- property({ type: Array })
325
- ], ActionGroup.prototype, "selected", null);
326
- //# sourceMappingURL=ActionGroup.js.map
285
+ __decorateClass([
286
+ property({ type: Boolean, reflect: true })
287
+ ], ActionGroup.prototype, "compact", 2);
288
+ __decorateClass([
289
+ property({ type: Boolean, reflect: true })
290
+ ], ActionGroup.prototype, "emphasized", 2);
291
+ __decorateClass([
292
+ property({ type: Boolean, reflect: true })
293
+ ], ActionGroup.prototype, "justified", 2);
294
+ __decorateClass([
295
+ property({ type: String })
296
+ ], ActionGroup.prototype, "label", 2);
297
+ __decorateClass([
298
+ property({ type: Boolean, reflect: true })
299
+ ], ActionGroup.prototype, "quiet", 2);
300
+ __decorateClass([
301
+ property({ type: String })
302
+ ], ActionGroup.prototype, "selects", 2);
303
+ __decorateClass([
304
+ property({ type: Boolean, reflect: true })
305
+ ], ActionGroup.prototype, "vertical", 2);
306
+ __decorateClass([
307
+ property({ type: Array })
308
+ ], ActionGroup.prototype, "selected", 1);
309
+ //# sourceMappingURL=ActionGroup.js.map