@vaadin/component-base 22.0.1 → 22.0.5

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.
package/index.d.ts CHANGED
@@ -5,5 +5,6 @@ export { DisabledMixin } from './src/disabled-mixin.js';
5
5
  export { ElementMixin } from './src/element-mixin.js';
6
6
  export { FocusMixin } from './src/focus-mixin.js';
7
7
  export { KeyboardMixin } from './src/keyboard-mixin.js';
8
+ export { SlotController } from './src/slot-controller.js';
8
9
  export { SlotMixin } from './src/slot-mixin.js';
9
10
  export { TabindexMixin } from './src/tabindex-mixin.js';
package/index.js CHANGED
@@ -5,5 +5,6 @@ export { DisabledMixin } from './src/disabled-mixin.js';
5
5
  export { ElementMixin } from './src/element-mixin.js';
6
6
  export { FocusMixin } from './src/focus-mixin.js';
7
7
  export { KeyboardMixin } from './src/keyboard-mixin.js';
8
+ export { SlotController } from './src/slot-controller.js';
8
9
  export { SlotMixin } from './src/slot-mixin.js';
9
10
  export { TabindexMixin } from './src/tabindex-mixin.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vaadin/component-base",
3
- "version": "22.0.1",
3
+ "version": "22.0.5",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -41,5 +41,5 @@
41
41
  "@vaadin/testing-helpers": "^0.3.2",
42
42
  "sinon": "^9.2.4"
43
43
  },
44
- "gitHead": "2b0a2bff0369d6020f7cc33ad35506aa2d1f6f68"
44
+ "gitHead": "3e1990867670f3de2dec6fa1200d945623b2710c"
45
45
  }
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { Constructor } from '@open-wc/dedupe-mixin';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { GestureEventListeners } from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
 
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { Constructor } from '@open-wc/dedupe-mixin';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
 
package/src/dir-helper.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
 
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { Constructor } from '@open-wc/dedupe-mixin';
package/src/dir-mixin.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { DirHelper } from './dir-helper.js';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { Constructor } from '@open-wc/dedupe-mixin';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import '../custom_typings/vaadin-usage-statistics.js';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { usageStatistics } from '@vaadin/vaadin-usage-statistics/vaadin-usage-statistics.js';
@@ -32,7 +32,7 @@ const registered = new Set();
32
32
  export const ElementMixin = (superClass) =>
33
33
  class VaadinElementMixin extends DirMixin(superClass) {
34
34
  static get version() {
35
- return '22.0.1';
35
+ return '22.0.5';
36
36
  }
37
37
 
38
38
  /** @protected */
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { Constructor } from '@open-wc/dedupe-mixin';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { Constructor } from '@open-wc/dedupe-mixin';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
@@ -0,0 +1,57 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { ReactiveController } from 'lit';
7
+
8
+ export class SlotController extends EventTarget implements ReactiveController {
9
+ constructor(
10
+ host: HTMLElement,
11
+ slotName: string,
12
+ slotFactory?: () => HTMLElement,
13
+ slotInitializer?: (host: HTMLElement, node: HTMLElement) => void
14
+ );
15
+
16
+ hostConnected(): void;
17
+
18
+ /**
19
+ * The controller host element.
20
+ */
21
+ host: HTMLElement;
22
+
23
+ /**
24
+ * The slotted node managed by the controller.
25
+ */
26
+ node: HTMLElement;
27
+
28
+ /**
29
+ * Return a reference to the node managed by the controller.
30
+ */
31
+ getSlotChild(): Node;
32
+
33
+ protected initialized: boolean;
34
+
35
+ protected defaultNode: Node;
36
+
37
+ protected defaultId: string;
38
+
39
+ protected attachDefaultNode(): Node | undefined;
40
+
41
+ protected initNode(node: Node): void;
42
+
43
+ /**
44
+ * Override to initialize the newly added custom node.
45
+ */
46
+ protected initCustomNode(node: Node): void;
47
+
48
+ /**
49
+ * Override to cleanup slotted node when it's removed.
50
+ */
51
+ protected teardownNode(node: Node): void;
52
+
53
+ /**
54
+ * Setup the observer to manage slot content changes.
55
+ */
56
+ protected observe(): void;
57
+ }
@@ -0,0 +1,173 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { dashToCamelCase } from '@polymer/polymer/lib/utils/case-map.js';
7
+ import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nodes-observer.js';
8
+
9
+ /**
10
+ * A controller for providing content to slot element and observing changes.
11
+ */
12
+ export class SlotController extends EventTarget {
13
+ constructor(host, slotName, slotFactory, slotInitializer) {
14
+ super();
15
+
16
+ this.host = host;
17
+ this.slotName = slotName;
18
+ this.slotFactory = slotFactory;
19
+ this.slotInitializer = slotInitializer;
20
+ this.defaultId = SlotController.generateId(slotName, host);
21
+ }
22
+
23
+ /**
24
+ * Ensure that every instance has unique ID.
25
+ *
26
+ * @param {string} slotName
27
+ * @param {HTMLElement} host
28
+ * @return {string}
29
+ * @protected
30
+ */
31
+ static generateId(slotName, host) {
32
+ const prefix = slotName || 'default';
33
+
34
+ // Support dash-case slot names e.g. "error-message"
35
+ const field = `${dashToCamelCase(prefix)}Id`;
36
+
37
+ // Maintain the unique ID counter for a given prefix.
38
+ this[field] = 1 + this[field] || 0;
39
+
40
+ return `${prefix}-${host.localName}-${this[field]}`;
41
+ }
42
+
43
+ hostConnected() {
44
+ if (!this.initialized) {
45
+ let node = this.getSlotChild();
46
+
47
+ if (!node) {
48
+ node = this.attachDefaultNode();
49
+ } else {
50
+ this.node = node;
51
+ this.initCustomNode(node);
52
+ }
53
+
54
+ this.initNode(node);
55
+
56
+ // TODO: Consider making this behavior opt-in to improve performance.
57
+ this.observe();
58
+
59
+ this.initialized = true;
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Create and attach default node using the slot factory.
65
+ * @return {Node | undefined}
66
+ * @protected
67
+ */
68
+ attachDefaultNode() {
69
+ const { host, slotName, slotFactory } = this;
70
+
71
+ // Check if the node was created previously and if so, reuse it.
72
+ let node = this.defaultNode;
73
+
74
+ // Slot factory is optional, some slots don't have default content.
75
+ if (!node && slotFactory) {
76
+ node = slotFactory(host);
77
+ if (node instanceof Element) {
78
+ if (slotName !== '') {
79
+ node.setAttribute('slot', slotName);
80
+ }
81
+ this.node = node;
82
+ this.defaultNode = node;
83
+ }
84
+ }
85
+
86
+ if (node) {
87
+ host.appendChild(node);
88
+ }
89
+
90
+ return node;
91
+ }
92
+
93
+ /**
94
+ * Return a reference to the node managed by the controller.
95
+ * @return {Node}
96
+ */
97
+ getSlotChild() {
98
+ const { slotName } = this;
99
+ return Array.from(this.host.childNodes).find((node) => {
100
+ // Either an element (any slot) or a text node (only un-named slot).
101
+ return (
102
+ (node.nodeType === Node.ELEMENT_NODE && node.slot === slotName) ||
103
+ (node.nodeType === Node.TEXT_NODE && node.textContent.trim() && slotName === '')
104
+ );
105
+ });
106
+ }
107
+
108
+ /**
109
+ * @param {Node} node
110
+ * @protected
111
+ */
112
+ initNode(node) {
113
+ const { slotInitializer } = this;
114
+ // Don't try to bind `this` to initializer (normally it's arrow function).
115
+ // Instead, pass the host as a first argument to access component's state.
116
+ if (slotInitializer) {
117
+ slotInitializer(this.host, node);
118
+ }
119
+ }
120
+
121
+ /**
122
+ * Override to initialize the newly added custom node.
123
+ *
124
+ * @param {Node} _node
125
+ * @protected
126
+ */
127
+ initCustomNode(_node) {}
128
+
129
+ /**
130
+ * Override to teardown slotted node when it's removed.
131
+ *
132
+ * @param {Node} _node
133
+ * @protected
134
+ */
135
+ teardownNode(_node) {}
136
+
137
+ /**
138
+ * Setup the observer to manage slot content changes.
139
+ * @protected
140
+ */
141
+ observe() {
142
+ const { slotName } = this;
143
+ const selector = slotName === '' ? 'slot:not([name])' : `slot[name=${slotName}]`;
144
+ const slot = this.host.shadowRoot.querySelector(selector);
145
+
146
+ this.__slotObserver = new FlattenedNodesObserver(slot, (info) => {
147
+ // TODO: support default slot with multiple nodes (e.g. confirm-dialog)
148
+ const current = this.node;
149
+ const newNode = info.addedNodes.find((node) => node !== current);
150
+
151
+ if (info.removedNodes.length) {
152
+ info.removedNodes.forEach((node) => {
153
+ this.teardownNode(node);
154
+ });
155
+ }
156
+
157
+ if (newNode) {
158
+ // Custom node is added, remove the current one.
159
+ if (current && current.isConnected) {
160
+ this.host.removeChild(current);
161
+ }
162
+
163
+ this.node = newNode;
164
+
165
+ if (newNode !== this.defaultNode) {
166
+ this.initCustomNode(newNode);
167
+
168
+ this.initNode(newNode);
169
+ }
170
+ }
171
+ });
172
+ }
173
+ }
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { Constructor } from '@open-wc/dedupe-mixin';
package/src/slot-mixin.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { Constructor } from '@open-wc/dedupe-mixin';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { DisabledMixin } from './disabled-mixin.js';
@@ -56,7 +56,9 @@ export const TabindexMixin = (superclass) =>
56
56
  super._disabledChanged(disabled, oldDisabled);
57
57
 
58
58
  if (disabled) {
59
- this.__lastTabIndex = this.tabindex;
59
+ if (this.tabindex !== undefined) {
60
+ this.__lastTabIndex = this.tabindex;
61
+ }
60
62
  this.tabindex = -1;
61
63
  } else if (oldDisabled) {
62
64
  this.tabindex = this.__lastTabIndex;
package/src/templates.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
 
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { animationFrame, timeOut } from './async.js';