@tylertech/forge-core 3.0.1-canary.0 → 3.1.0

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.
@@ -44,19 +44,19 @@ export declare function attachLightTemplate<T extends HTMLElement>(componentInst
44
44
  * Attaches a shadow root to the given web component instance.
45
45
  * @param {T} componentInstance A component instance.
46
46
  * @param {string} elementName The name of the element the shadow root is to be attached to.
47
- * @param {string} template The shadow root template HTML string.
47
+ * @param {string | HTMLTemplateElement} template The shadow root template HTML string or element.
48
48
  * @param {string | string[]} styles The shadow root styles string to be encapsulated by this shadow root.
49
49
  * @param {boolean} [delegatesFocus=false] Should the component delegate focus.
50
50
  */
51
- export declare function attachShadowTemplate<T extends HTMLElement>(componentInstance: T, template: string, styles?: string | string[], delegatesFocus?: boolean): void;
51
+ export declare function attachShadowTemplate<T extends HTMLElement>(componentInstance: T, template: string | HTMLTemplateElement, styles?: string | string[], delegatesFocus?: boolean): void;
52
52
  /**
53
53
  * Replaces the template of an existing shadow root with the provided template.
54
54
  * @param {T} componentInstance A component instance.
55
55
  * @param {string} elementName The name of the element the shadow root is to be attached to.
56
- * @param {string} template The shadow root template HTML string.
56
+ * @param {string | HTMLTemplateElement} template The shadow root template HTML string or element.
57
57
  * @param {string | string[]} styles The shadow root styles string to be encapsulated by this shadow root.
58
58
  */
59
- export declare function replaceShadowTemplate<T extends HTMLElement>(componentInstance: T, template: string, styles?: string | string[]): void;
59
+ export declare function replaceShadowTemplate<T extends HTMLElement>(componentInstance: T, template: string | HTMLTemplateElement, styles?: string | string[]): void;
60
60
  /**
61
61
  * Creates and prepares an HTML template element for rendering within a shadow root.
62
62
  * @param {string} elementName The name of the element the shadow root is to be attached to.
@@ -67,9 +67,11 @@ export declare function prepareShadowTemplate(template: string, styles?: string
67
67
  /**
68
68
  * Appends a template to the provided components shadow root.
69
69
  * @param {T} componentInstance A component instance.
70
- * @param {HTMLTemplateElement} templateElement A template element to be cloned.
70
+ * @param {string | HTMLTemplateElement} template A template string or template element to be cloned.
71
71
  */
72
- export declare function setShadowTemplate<T extends HTMLElement>(componentInstance: T, templateElement: HTMLTemplateElement): void;
72
+ export declare function setShadowTemplate<T extends HTMLElement>(componentInstance: T, template: string | HTMLTemplateElement, { force }?: {
73
+ force: boolean;
74
+ }): void;
73
75
  /**
74
76
  * Applies styles to the shadow root of the provided element instance.
75
77
  * @param {T} componentInstance A component instance.
@@ -80,9 +82,9 @@ export declare function setShadowStyles<T extends HTMLElement>(componentInstance
80
82
  force: boolean;
81
83
  }): void;
82
84
  /**
83
- * Reapplies styles to the shadow root of the provided element instance. This function was
85
+ * Re-applies styles to the shadow root of the provided element instance. This function is
84
86
  * intended to be called after an element has been adopted by a new document to reconstruct the
85
- * adopted stylesheet instances within the context of the new document.
87
+ * adopted stylesheet instances within the context (view) of the new document.
86
88
  *
87
89
  * @param componentInstance The component instance to reapply styles to.
88
90
  */
@@ -1,5 +1,5 @@
1
1
  import { replaceElement, isArray, removeAllChildren, walkUpUntil } from '../utils';
2
- import { CUSTOM_ELEMENT_CSS_PROPERTY, CUSTOM_ELEMENT_DEPENDENCIES_PROPERTY, CUSTOM_ELEMENT_NAME_PROPERTY, CUSTOM_ELEMENT_STYLESHEETS_PROPERTY, supportsConstructableStyleSheets } from './constants';
2
+ import { CUSTOM_ELEMENT_CSS_PROPERTY, CUSTOM_ELEMENT_DEPENDENCIES_PROPERTY, CUSTOM_ELEMENT_NAME_PROPERTY, CUSTOM_ELEMENT_STYLESHEETS_PROPERTY, CUSTOM_ELEMENT_TEMPLATE_PROPERTY, supportsConstructableStyleSheets } from './constants';
3
3
  /**
4
4
  * Recursively defines a component as a custom elements and all of its dependencies.
5
5
  * @param component The component to import.
@@ -81,37 +81,35 @@ export function attachLightTemplate(componentInstance, template) {
81
81
  * Attaches a shadow root to the given web component instance.
82
82
  * @param {T} componentInstance A component instance.
83
83
  * @param {string} elementName The name of the element the shadow root is to be attached to.
84
- * @param {string} template The shadow root template HTML string.
84
+ * @param {string | HTMLTemplateElement} template The shadow root template HTML string or element.
85
85
  * @param {string | string[]} styles The shadow root styles string to be encapsulated by this shadow root.
86
86
  * @param {boolean} [delegatesFocus=false] Should the component delegate focus.
87
87
  */
88
88
  export function attachShadowTemplate(componentInstance, template, styles, delegatesFocus = false) {
89
- const templateElement = prepareShadowTemplate(template);
90
89
  componentInstance.attachShadow({ mode: 'open', delegatesFocus });
91
90
  if (styles) {
92
91
  setShadowStyles(componentInstance, styles);
93
92
  }
94
- setShadowTemplate(componentInstance, templateElement);
93
+ setShadowTemplate(componentInstance, template);
95
94
  }
96
95
  /**
97
96
  * Replaces the template of an existing shadow root with the provided template.
98
97
  * @param {T} componentInstance A component instance.
99
98
  * @param {string} elementName The name of the element the shadow root is to be attached to.
100
- * @param {string} template The shadow root template HTML string.
99
+ * @param {string | HTMLTemplateElement} template The shadow root template HTML string or element.
101
100
  * @param {string | string[]} styles The shadow root styles string to be encapsulated by this shadow root.
102
101
  */
103
102
  export function replaceShadowTemplate(componentInstance, template, styles) {
104
103
  if (!componentInstance.shadowRoot) {
105
104
  throw new Error('This element does not contain a shadow root. Did you mean to call `attachShadowTemplate`?');
106
105
  }
107
- const templateElement = prepareShadowTemplate(template);
108
106
  if (componentInstance.shadowRoot.children.length) {
109
107
  removeAllChildren(componentInstance.shadowRoot);
110
108
  }
111
109
  if (styles) {
112
110
  setShadowStyles(componentInstance, styles, { force: true });
113
111
  }
114
- setShadowTemplate(componentInstance, templateElement);
112
+ setShadowTemplate(componentInstance, template, { force: true });
115
113
  }
116
114
  /**
117
115
  * Creates and prepares an HTML template element for rendering within a shadow root.
@@ -139,10 +137,16 @@ export function prepareShadowTemplate(template, styles) {
139
137
  /**
140
138
  * Appends a template to the provided components shadow root.
141
139
  * @param {T} componentInstance A component instance.
142
- * @param {HTMLTemplateElement} templateElement A template element to be cloned.
140
+ * @param {string | HTMLTemplateElement} template A template string or template element to be cloned.
143
141
  */
144
- export function setShadowTemplate(componentInstance, templateElement) {
145
- componentInstance.shadowRoot.appendChild(templateElement.content.cloneNode(true));
142
+ export function setShadowTemplate(componentInstance, template, { force } = { force: false }) {
143
+ const ctor = componentInstance.constructor;
144
+ if (force || !ctor[CUSTOM_ELEMENT_TEMPLATE_PROPERTY]) {
145
+ const templateElement = template instanceof HTMLTemplateElement ? template : parseTemplateString(template);
146
+ ctor[CUSTOM_ELEMENT_TEMPLATE_PROPERTY] = templateElement;
147
+ }
148
+ const resolvedTemplate = ctor[CUSTOM_ELEMENT_TEMPLATE_PROPERTY];
149
+ componentInstance.shadowRoot.appendChild(resolvedTemplate.content.cloneNode(true));
146
150
  }
147
151
  /**
148
152
  * Applies styles to the shadow root of the provided element instance.
@@ -187,22 +191,21 @@ export function setShadowStyles(componentInstance, styles, { force } = { force:
187
191
  }
188
192
  }
189
193
  /**
190
- * Reapplies styles to the shadow root of the provided element instance. This function was
194
+ * Re-applies styles to the shadow root of the provided element instance. This function is
191
195
  * intended to be called after an element has been adopted by a new document to reconstruct the
192
- * adopted stylesheet instances within the context of the new document.
196
+ * adopted stylesheet instances within the context (view) of the new document.
193
197
  *
194
198
  * @param componentInstance The component instance to reapply styles to.
195
199
  */
196
200
  export function readoptStyles(componentInstance) {
197
201
  if (!supportsConstructableStyleSheets ||
198
202
  !componentInstance.shadowRoot ||
199
- !componentInstance.constructor[CUSTOM_ELEMENT_CSS_PROPERTY]) {
203
+ !componentInstance.constructor[CUSTOM_ELEMENT_CSS_PROPERTY] ||
204
+ !componentInstance.ownerDocument.defaultView) {
200
205
  return;
201
206
  }
202
- const cssText = componentInstance.constructor[CUSTOM_ELEMENT_CSS_PROPERTY];
203
- const context = componentInstance.ownerDocument.defaultView ?? window;
204
- const sheet = new context.CSSStyleSheet();
205
- sheet.replaceSync(cssText);
207
+ const sheet = new componentInstance.ownerDocument.defaultView.CSSStyleSheet();
208
+ sheet.replaceSync(componentInstance.constructor[CUSTOM_ELEMENT_CSS_PROPERTY]);
206
209
  componentInstance.shadowRoot.adoptedStyleSheets = [sheet];
207
210
  }
208
211
  /**
@@ -1,5 +1,6 @@
1
1
  export declare const CUSTOM_ELEMENT_NAME_PROPERTY: unique symbol;
2
2
  export declare const CUSTOM_ELEMENT_DEPENDENCIES_PROPERTY: unique symbol;
3
+ export declare const CUSTOM_ELEMENT_TEMPLATE_PROPERTY: unique symbol;
3
4
  export declare const CUSTOM_ELEMENT_CSS_PROPERTY: unique symbol;
4
5
  export declare const CUSTOM_ELEMENT_STYLESHEETS_PROPERTY: unique symbol;
5
6
  /** Whether the browser supports constructable stylesheets */
@@ -1,5 +1,6 @@
1
1
  export const CUSTOM_ELEMENT_NAME_PROPERTY = Symbol('Forge custom element tag name');
2
2
  export const CUSTOM_ELEMENT_DEPENDENCIES_PROPERTY = Symbol('Forge custom element dependencies');
3
+ export const CUSTOM_ELEMENT_TEMPLATE_PROPERTY = Symbol('Forge custom element parsed template');
3
4
  export const CUSTOM_ELEMENT_CSS_PROPERTY = Symbol('Forge custom element CSS text');
4
5
  export const CUSTOM_ELEMENT_STYLESHEETS_PROPERTY = Symbol('Forge custom element CSSStyleSheet instances');
5
6
  /** Whether the browser supports constructable stylesheets */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tylertech/forge-core",
3
- "version": "3.0.1-canary.0",
3
+ "version": "3.1.0",
4
4
  "description": "A library of core web utilities that support Tyler Forge™ based libraries.",
5
5
  "author": "Tyler Technologies, Inc.",
6
6
  "license": "Apache-2.0",