@vaadin/component-base 24.2.0-alpha9 → 24.2.0-beta1

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
@@ -4,3 +4,4 @@ export { DirMixin } from './src/dir-mixin.js';
4
4
  export { ElementMixin } from './src/element-mixin.js';
5
5
  export { ResizeMixin } from './src/resize-mixin.js';
6
6
  export { SlotController } from './src/slot-controller.js';
7
+ export { SlotStylesMixin } from './src/slot-styles-mixin.js';
package/index.js CHANGED
@@ -4,3 +4,4 @@ export { DirMixin } from './src/dir-mixin.js';
4
4
  export { ElementMixin } from './src/element-mixin.js';
5
5
  export { ResizeMixin } from './src/resize-mixin.js';
6
6
  export { SlotController } from './src/slot-controller.js';
7
+ export { SlotStylesMixin } from './src/slot-styles-mixin.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vaadin/component-base",
3
- "version": "24.2.0-alpha9",
3
+ "version": "24.2.0-beta1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -42,5 +42,5 @@
42
42
  "@vaadin/testing-helpers": "^0.5.0",
43
43
  "sinon": "^13.0.2"
44
44
  },
45
- "gitHead": "e9765733fea96542e379e02e6f42b07145893140"
45
+ "gitHead": "67c8eef57d1c59e7476e29adaf003cf4548878f2"
46
46
  }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 - 2023 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ export interface CustomElementType extends CustomElementConstructor {
7
+ is: string;
8
+ }
9
+
10
+ export declare function defineCustomElement(CustomElement: CustomElementConstructor): void;
package/src/define.js ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 - 2023 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ export function defineCustomElement(CustomElement) {
7
+ const defined = customElements.get(CustomElement.is);
8
+ if (!defined) {
9
+ customElements.define(CustomElement.is, CustomElement);
10
+ } else {
11
+ const definedVersion = defined.version;
12
+ if (definedVersion && CustomElement.version && definedVersion === CustomElement.version) {
13
+ // Just loading the same thing again
14
+ console.warn(`The component ${CustomElement.is} has been loaded twice`);
15
+ } else {
16
+ console.error(
17
+ `Tried to define ${CustomElement.is} version ${CustomElement.version} when version ${defined.version} is already in use. Something will probably break.`,
18
+ );
19
+ }
20
+ }
21
+ }
@@ -16,6 +16,7 @@ export declare function ElementMixin<T extends Constructor<HTMLElement>>(
16
16
  ): Constructor<DirMixinClass> & Constructor<ElementMixinClass> & T;
17
17
 
18
18
  export declare class ElementMixinClass {
19
+ static is: string;
19
20
  static version: string;
20
21
 
21
22
  protected static finalize(): void;
@@ -45,7 +45,7 @@ const registered = new Set();
45
45
  export const ElementMixin = (superClass) =>
46
46
  class VaadinElementMixin extends DirMixin(superClass) {
47
47
  static get version() {
48
- return '24.2.0-alpha9';
48
+ return '24.2.0-beta1';
49
49
  }
50
50
 
51
51
  /** @protected */
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2023 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+
7
+ /**
8
+ * Convenience method for reading a value from a path.
9
+ */
10
+ export function get(path: string, object: object): unknown;
11
+
12
+ /**
13
+ * Convenience method for setting a value to a path.
14
+ */
15
+ export function set(path: string, value: unknown, object: object): void;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2023 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+
7
+ /**
8
+ * Convenience method for reading a value from a path.
9
+ *
10
+ * @param {string} path
11
+ * @param {object} object
12
+ */
13
+ export function get(path, object) {
14
+ return path.split('.').reduce((obj, property) => (obj ? obj[property] : undefined), object);
15
+ }
16
+
17
+ /**
18
+ * Convenience method for setting a value to a path.
19
+ *
20
+ * @param {string} path
21
+ * @param {unknown} value
22
+ * @param {object} object
23
+ */
24
+ export function set(path, value, object) {
25
+ const pathParts = path.split('.');
26
+ const lastPart = pathParts.pop();
27
+ const target = pathParts.reduce((target, part) => target[part], object);
28
+ target[lastPart] = value;
29
+ }
@@ -4,6 +4,7 @@
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { dedupeMixin } from '@open-wc/dedupe-mixin';
7
+ import { get, set } from './path-utils.js';
7
8
 
8
9
  const caseMap = {};
9
10
 
@@ -92,17 +93,6 @@ const PolylitMixinImplementation = (superclass) => {
92
93
 
93
94
  let result = defaultDescriptor;
94
95
 
95
- if ('value' in options) {
96
- // Set the default value
97
- this.addCheckedInitializer((instance) => {
98
- if (typeof options.value === 'function') {
99
- instance[name] = options.value.call(instance);
100
- } else {
101
- instance[name] = options.value;
102
- }
103
- });
104
- }
105
-
106
96
  if (options.sync) {
107
97
  result = {
108
98
  get: defaultDescriptor.get,
@@ -128,6 +118,10 @@ const PolylitMixinImplementation = (superclass) => {
128
118
  // This is run during construction of the element
129
119
  instance[`_set${upper(name)}`] = function (value) {
130
120
  setter.call(instance, value);
121
+
122
+ if (options.sync) {
123
+ this.performUpdate();
124
+ }
131
125
  };
132
126
  });
133
127
 
@@ -141,6 +135,19 @@ const PolylitMixinImplementation = (superclass) => {
141
135
  };
142
136
  }
143
137
 
138
+ if ('value' in options) {
139
+ // Set the default value
140
+ this.addCheckedInitializer((instance) => {
141
+ const value = typeof options.value === 'function' ? options.value.call(instance) : options.value;
142
+
143
+ if (options.readOnly) {
144
+ instance[`_set${upper(name)}`](value);
145
+ } else {
146
+ instance[name] = value;
147
+ }
148
+ });
149
+ }
150
+
144
151
  if (options.observer) {
145
152
  const method = options.observer;
146
153
 
@@ -272,15 +279,12 @@ const PolylitMixinImplementation = (superclass) => {
272
279
 
273
280
  /** @protected */
274
281
  _get(path, object) {
275
- return path.split('.').reduce((obj, property) => (obj ? obj[property] : undefined), object);
282
+ return get(path, object);
276
283
  }
277
284
 
278
285
  /** @protected */
279
286
  _set(path, value, object) {
280
- const pathParts = path.split('.');
281
- const lastPart = pathParts.pop();
282
- const target = pathParts.reduce((target, part) => target[part], object);
283
- target[lastPart] = value;
287
+ set(path, value, object);
284
288
  }
285
289
  }
286
290
 
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 - 2023 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import type { Constructor } from '@open-wc/dedupe-mixin';
7
+
8
+ /**
9
+ * Mixin to insert styles into the outer scope to handle slotted components.
10
+ * This is useful e.g. to hide native `<input type="number">` controls.
11
+ */
12
+ export declare function SlotStylesMixin<T extends Constructor<HTMLElement>>(
13
+ base: T,
14
+ ): Constructor<SlotStylesMixinClass> & T;
15
+
16
+ export declare class SlotStylesMixinClass {
17
+ /**
18
+ * List of styles to insert into root.
19
+ */
20
+ protected readonly slotStyles: string[];
21
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 - 2023 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
7
+
8
+ const stylesMap = new WeakMap();
9
+
10
+ /**
11
+ * Get all the styles inserted into root.
12
+ * @param {DocumentOrShadowRoot} root
13
+ * @return {Set<string>}
14
+ */
15
+ function getRootStyles(root) {
16
+ if (!stylesMap.has(root)) {
17
+ stylesMap.set(root, new Set());
18
+ }
19
+
20
+ return stylesMap.get(root);
21
+ }
22
+
23
+ /**
24
+ * Insert styles into the root.
25
+ * @param {string} styles
26
+ * @param {DocumentOrShadowRoot} root
27
+ */
28
+ function insertStyles(styles, root) {
29
+ const style = document.createElement('style');
30
+ style.textContent = styles;
31
+
32
+ if (root === document) {
33
+ document.head.appendChild(style);
34
+ } else {
35
+ root.insertBefore(style, root.firstChild);
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Mixin to insert styles into the outer scope to handle slotted components.
41
+ * This is useful e.g. to hide native `<input type="number">` controls.
42
+ *
43
+ * @polymerMixin
44
+ */
45
+ export const SlotStylesMixin = dedupingMixin(
46
+ (superclass) =>
47
+ class SlotStylesMixinClass extends superclass {
48
+ /**
49
+ * List of styles to insert into root.
50
+ * @protected
51
+ */
52
+ get slotStyles() {
53
+ return {};
54
+ }
55
+
56
+ /** @protected */
57
+ connectedCallback() {
58
+ super.connectedCallback();
59
+
60
+ this.__applySlotStyles();
61
+ }
62
+
63
+ /** @private */
64
+ __applySlotStyles() {
65
+ const root = this.getRootNode();
66
+ const rootStyles = getRootStyles(root);
67
+
68
+ this.slotStyles.forEach((styles) => {
69
+ if (!rootStyles.has(styles)) {
70
+ insertStyles(styles, root);
71
+ rootStyles.add(styles);
72
+ }
73
+ });
74
+ }
75
+ },
76
+ );
@@ -23,6 +23,13 @@ type TooltipPosition =
23
23
  * A controller that manages the slotted tooltip element.
24
24
  */
25
25
  export class TooltipController extends SlotController {
26
+ /**
27
+ * An HTML element for linking with the tooltip overlay
28
+ * via `aria-describedby` attribute used by screen readers.
29
+ * When not set, defaults to `target`.
30
+ */
31
+ ariaTarget: HTMLElement | HTMLElement[];
32
+
26
33
  /**
27
34
  * Object with properties passed to `generator`
28
35
  * function to be used for generating tooltip text.
@@ -51,6 +58,12 @@ export class TooltipController extends SlotController {
51
58
  */
52
59
  target: HTMLElement;
53
60
 
61
+ /**
62
+ * Set an HTML element for linking with the tooltip overlay
63
+ * via `aria-describedby` attribute used by screen readers.
64
+ */
65
+ setAriaTarget(ariaTarget: HTMLElement | HTMLElement[]): void;
66
+
54
67
  /**
55
68
  * Set a context object to be used by generator.
56
69
  */
@@ -26,6 +26,10 @@ export class TooltipController extends SlotController {
26
26
  initCustomNode(tooltipNode) {
27
27
  tooltipNode.target = this.target;
28
28
 
29
+ if (this.ariaTarget !== undefined) {
30
+ tooltipNode.ariaTarget = this.ariaTarget;
31
+ }
32
+
29
33
  if (this.context !== undefined) {
30
34
  tooltipNode.context = this.context;
31
35
  }
@@ -45,6 +49,33 @@ export class TooltipController extends SlotController {
45
49
  if (this.shouldShow !== undefined) {
46
50
  tooltipNode.shouldShow = this.shouldShow;
47
51
  }
52
+
53
+ this.__notifyChange();
54
+ }
55
+
56
+ /**
57
+ * Override to notify the host when the tooltip is removed.
58
+ *
59
+ * @param {Node} tooltipNode
60
+ * @protected
61
+ * @override
62
+ */
63
+ teardownNode() {
64
+ this.__notifyChange();
65
+ }
66
+
67
+ /**
68
+ * Set an HTML element for linking with the tooltip overlay
69
+ * via `aria-describedby` attribute used by screen readers.
70
+ * @param {HTMLElement} ariaTarget
71
+ */
72
+ setAriaTarget(ariaTarget) {
73
+ this.ariaTarget = ariaTarget;
74
+
75
+ const tooltipNode = this.node;
76
+ if (tooltipNode) {
77
+ tooltipNode.ariaTarget = ariaTarget;
78
+ }
48
79
  }
49
80
 
50
81
  /**
@@ -127,4 +158,9 @@ export class TooltipController extends SlotController {
127
158
  tooltipNode.target = target;
128
159
  }
129
160
  }
161
+
162
+ /** @private */
163
+ __notifyChange() {
164
+ this.dispatchEvent(new CustomEvent('tooltip-changed', { detail: { node: this.node } }));
165
+ }
130
166
  }