@descope-ui/common 0.0.10 → 0.0.12

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/CHANGELOG.md CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
4
4
 
5
+ ## [0.0.12](https://github.com/descope/web-components-ui/compare/@descope-ui/common-0.0.11...@descope-ui/common-0.0.12) (2025-05-28)
6
+
7
+ ## [0.0.11](https://github.com/descope/web-components-ui/compare/@descope-ui/common-0.0.10...@descope-ui/common-0.0.11) (2025-04-29)
8
+
9
+
10
+ ### Bug Fixes
11
+
12
+ * CSSStyleSheet fallback for older browser ([#639](https://github.com/descope/web-components-ui/issues/639)) ([1ff86cc](https://github.com/descope/web-components-ui/commit/1ff86ccd6d5a00de025207d99f034772b92ac910))
13
+
5
14
  ## [0.0.10](https://github.com/descope/web-components-ui/compare/@descope-ui/common-0.0.9...@descope-ui/common-0.0.10) (2025-04-16)
6
15
 
7
16
  ## [0.0.9](https://github.com/descope/web-components-ui/compare/@descope-ui/common-0.0.8...@descope-ui/common-0.0.9) (2025-03-06)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@descope-ui/common",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "dependencies": {
5
5
  "element-internals-polyfill": "^1.3.9",
6
6
  "color": "^4.2.3",
@@ -45,19 +45,19 @@ export const observeChildren = (ele, callback) => {
45
45
 
46
46
  const createSyncAttrsCb =
47
47
  (srcEle, targetEle, mapAttrs = {}) =>
48
- (attrNames) => {
49
- attrNames.forEach((attrName) => {
50
- const targetAttrName = mapAttrs[attrName] || attrName;
51
- const srcAttrVal = srcEle.getAttribute(attrName);
52
- if (srcAttrVal !== null) {
53
- if (targetEle.getAttribute(targetAttrName) !== srcAttrVal) {
54
- targetEle.setAttribute(targetAttrName, srcAttrVal);
48
+ (attrNames) => {
49
+ attrNames.forEach((attrName) => {
50
+ const targetAttrName = mapAttrs[attrName] || attrName;
51
+ const srcAttrVal = srcEle.getAttribute(attrName);
52
+ if (srcAttrVal !== null) {
53
+ if (targetEle.getAttribute(targetAttrName) !== srcAttrVal) {
54
+ targetEle.setAttribute(targetAttrName, srcAttrVal);
55
+ }
56
+ } else {
57
+ targetEle.removeAttribute(targetAttrName);
55
58
  }
56
- } else {
57
- targetEle.removeAttribute(targetAttrName);
58
- }
59
- });
60
- };
59
+ });
60
+ };
61
61
 
62
62
  export const syncAttrs = (ele1, ele2, options) => {
63
63
  observeAttributes(ele1, createSyncAttrsCb(ele1, ele2), options);
@@ -94,8 +94,16 @@ export const forwardProps = (src, target, props = []) => {
94
94
  Object.defineProperties(target, config);
95
95
  };
96
96
 
97
- export const injectStyle = (cssString, ref, {prepend = false} = {}) => {
98
- const style = new CSSStyleSheet();
97
+ export const injectStyle = (cssString, ref, { prepend = false } = {}) => {
98
+
99
+ let style;
100
+ try {
101
+ style = new CSSStyleSheet();
102
+ } catch (e) {
103
+ // fallback for browsers that don't support CSSStyleSheet
104
+ return generateStyleTagFallback(cssString, ref, { prepend });
105
+ }
106
+
99
107
  const _ref = ref?.shadowRoot || ref;
100
108
  if (cssString) {
101
109
  style.replaceSync(cssString);
@@ -109,3 +117,33 @@ export const injectStyle = (cssString, ref, {prepend = false} = {}) => {
109
117
 
110
118
  return style;
111
119
  }
120
+
121
+ // we should mimic the CSSStyleSheet API for the fns we are using
122
+ class CSSStyleSheetMock {
123
+ constructor(cssString, ref, { prepend = false } = {}) {
124
+ this.styleEle = document.createElement('style');
125
+ this.styleEle.textContent = cssString;
126
+ this.styleEle.setAttribute('nonce', window.DESCOPE_NONCE);
127
+ this.ref = ref?.shadowRoot || ref;
128
+
129
+ if (!this.ref) {
130
+ return
131
+ }
132
+
133
+ if (prepend) {
134
+ this.ref.prepend(this.styleEle);
135
+ } else {
136
+ this.ref.append(this.styleEle);
137
+ }
138
+ }
139
+
140
+ replaceSync(cssString) {
141
+ this.styleEle.textContent = cssString;
142
+ }
143
+
144
+ get cssRules() {
145
+ return this.styleEle.sheet?.cssRules;
146
+ }
147
+ }
148
+
149
+ const generateStyleTagFallback = (cssString, ref, { prepend = false } = {}) => new CSSStyleSheetMock(cssString, ref, { prepend });
@@ -1,8 +1,19 @@
1
- import { BASE_THEME_SECTION, CSS_SELECTOR_SPECIFIER_MULTIPLY } from '../../../constants';
1
+ import {
2
+ BASE_THEME_SECTION,
3
+ CSS_SELECTOR_SPECIFIER_MULTIPLY,
4
+ } from '../../../constants';
2
5
  import { kebabCaseJoin } from '../../../utils';
3
- import { getCssVarName, injectStyle, observeAttributes } from '../../../componentsHelpers';
6
+ import {
7
+ getCssVarName,
8
+ injectStyle,
9
+ observeAttributes,
10
+ } from '../../../componentsHelpers';
4
11
  import { componentsThemeManager } from '../../../themeHelpers';
5
- import { createStyle, createCssVarsList, createClassSelectorSpecifier } from './helpers';
12
+ import {
13
+ createStyle,
14
+ createCssVarsList,
15
+ createClassSelectorSpecifier,
16
+ } from './helpers';
6
17
 
7
18
  const STYLE_OVERRIDE_ATTR_PREFIX = 'st';
8
19
 
@@ -54,8 +65,10 @@ export const createStyleMixin =
54
65
  this.#baseSelector = baseSelector ?? this.baseSelector;
55
66
  this.#getRootElement = getRootElement;
56
67
 
57
- this.#styleAttributes = Object.keys(CustomStyleMixinClass.cssVarList).map((key) =>
58
- kebabCaseJoin(STYLE_OVERRIDE_ATTR_PREFIX, componentNameSuffix, key)
68
+ this.#styleAttributes = Object.keys(
69
+ CustomStyleMixinClass.cssVarList,
70
+ ).map((key) =>
71
+ kebabCaseJoin(STYLE_OVERRIDE_ATTR_PREFIX, componentNameSuffix, key),
59
72
  );
60
73
  }
61
74
 
@@ -65,14 +78,23 @@ export const createStyleMixin =
65
78
  }
66
79
 
67
80
  #onComponentThemeChange() {
68
- this.#themeStyleEle.replaceSync(this.#componentTheme[this.#themeSection]);
81
+ this.#themeStyleEle.replaceSync(
82
+ this.#componentTheme[this.#themeSection],
83
+ );
84
+ this.currentThemeName = componentsThemeManager.currentThemeName;
85
+ // `onThemeChange` - This function is a hook for the component, so it can be implemented in the component
86
+ // and is executed once theme is change.
87
+ this.onThemeChange?.(componentsThemeManager.currentThemeName);
69
88
  }
70
89
 
71
90
  #createComponentTheme() {
72
- this.#themeStyleEle = injectStyle('', this.#rootElement, { prepend: true });
73
- this.#disconnectThemeManager = componentsThemeManager.onCurrentThemeChange(
74
- this.#onComponentThemeChange.bind(this)
75
- );
91
+ this.#themeStyleEle = injectStyle('', this.#rootElement, {
92
+ prepend: true,
93
+ });
94
+ this.#disconnectThemeManager =
95
+ componentsThemeManager.onCurrentThemeChange(
96
+ this.#onComponentThemeChange.bind(this),
97
+ );
76
98
  this.#onComponentThemeChange();
77
99
  }
78
100
 
@@ -80,10 +102,13 @@ export const createStyleMixin =
80
102
  if (this.#styleAttributes.length) {
81
103
  const classSpecifier = createClassSelectorSpecifier(
82
104
  componentName,
83
- CSS_SELECTOR_SPECIFIER_MULTIPLY
105
+ CSS_SELECTOR_SPECIFIER_MULTIPLY,
84
106
  );
85
107
 
86
- this.#overrideStyleEle = injectStyle(`:host(${classSpecifier}) {}`, this.#rootElement);
108
+ this.#overrideStyleEle = injectStyle(
109
+ `:host(${classSpecifier}) {}`,
110
+ this.#rootElement,
111
+ );
87
112
  }
88
113
  }
89
114
 
@@ -94,7 +119,7 @@ export const createStyleMixin =
94
119
 
95
120
  const varName = getCssVarName(
96
121
  componentName,
97
- attrName.replace(new RegExp(`^${STYLE_OVERRIDE_ATTR_PREFIX}-`), '')
122
+ attrName.replace(new RegExp(`^${STYLE_OVERRIDE_ATTR_PREFIX}-`), ''),
98
123
  );
99
124
 
100
125
  if (value) style?.setProperty(varName, value);
@@ -117,7 +142,7 @@ export const createStyleMixin =
117
142
  const style = createStyle(
118
143
  kebabCaseJoin(componentName, this.#componentNameSuffix),
119
144
  this.#baseSelector,
120
- mappings
145
+ mappings,
121
146
  );
122
147
 
123
148
  injectStyle(style, this.#rootElement, { prepend: true });
@@ -125,13 +150,16 @@ export const createStyleMixin =
125
150
  }
126
151
 
127
152
  #addClassName(className) {
128
- (this.#rootElement.classList || this.#rootElement.host.classList).add(className);
153
+ (this.#rootElement.classList || this.#rootElement.host.classList).add(
154
+ className,
155
+ );
129
156
  }
130
157
 
131
158
  async init() {
132
159
  super.init?.();
133
160
  if (this.shadowRoot.isConnected) {
134
- this.#rootElement = (await this.#getRootElement?.(this)) || this.shadowRoot;
161
+ this.#rootElement =
162
+ (await this.#getRootElement?.(this)) || this.shadowRoot;
135
163
 
136
164
  this.#addClassName(componentName);
137
165