@descope/sdk-mixins 0.2.43 → 0.3.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.
package/dist/index.esm.js CHANGED
@@ -24,9 +24,9 @@ const loggerMixin = createSingletonMixin((superclass) => { var _LoggerMixinClass
24
24
  _LoggerMixinClass_wrapLogger = function _LoggerMixinClass_wrapLogger(logger) {
25
25
  return logLevels.reduce((acc, logLevel) => {
26
26
  acc[logLevel] = (...args) => {
27
- var _a;
27
+ var _b;
28
28
  this.onLogEvent(logLevel, args);
29
- (_a = logger[logLevel]) === null || _a === void 0 ? void 0 : _a.call(logger, ...args);
29
+ (_b = logger[logLevel]) === null || _b === void 0 ? void 0 : _b.call(logger, ...args);
30
30
  };
31
31
  return acc;
32
32
  }, {});
@@ -49,9 +49,9 @@ const initLifecycleMixin = createSingletonMixin((superclass) => { var _InitLifec
49
49
  _InitLifecycleMixinClass_isInit.set(this, true);
50
50
  }
51
51
  connectedCallback() {
52
- var _a, _b;
53
- (_a = super.connectedCallback) === null || _a === void 0 ? void 0 : _a.call(this);
54
- if ((_b = this.shadowRoot) === null || _b === void 0 ? void 0 : _b.isConnected) {
52
+ var _b, _c;
53
+ (_b = super.connectedCallback) === null || _b === void 0 ? void 0 : _b.call(this);
54
+ if ((_c = this.shadowRoot) === null || _c === void 0 ? void 0 : _c.isConnected) {
55
55
  // the init function is running once, on the first time the component is connected
56
56
  if (__classPrivateFieldGet(this, _InitLifecycleMixinClass_isInit, "f")) {
57
57
  __classPrivateFieldSet(this, _InitLifecycleMixinClass_isInit, false, "f");
@@ -87,11 +87,11 @@ const observeAttributesMixin = createSingletonMixin((superclass) => {
87
87
  _ObserveAttributesMixinClass_observeMappings.set(this, {});
88
88
  }
89
89
  async init() {
90
- var _a;
91
- await ((_a = super.init) === null || _a === void 0 ? void 0 : _a.call(this));
90
+ var _b;
91
+ await ((_b = super.init) === null || _b === void 0 ? void 0 : _b.call(this));
92
92
  attributesObserver(this, (attrName) => {
93
- var _a;
94
- (_a = __classPrivateFieldGet(this, _ObserveAttributesMixinClass_observeMappings, "f")[attrName]) === null || _a === void 0 ? void 0 : _a.forEach((cb) => {
93
+ var _b;
94
+ (_b = __classPrivateFieldGet(this, _ObserveAttributesMixinClass_observeMappings, "f")[attrName]) === null || _b === void 0 ? void 0 : _b.forEach((cb) => {
95
95
  cb(attrName, this.getAttribute(attrName));
96
96
  });
97
97
  });
@@ -126,8 +126,8 @@ const createValidateAttributesMixin = (mappings) => (superclass) => {
126
126
  this.observeAttributes(mappingsNames, __classPrivateFieldGet(this, _ValidateAttributesMixinClass_instances, "m", _ValidateAttributesMixinClass_handleError).bind(this));
127
127
  }
128
128
  async init() {
129
- var _a;
130
- await ((_a = super.init) === null || _a === void 0 ? void 0 : _a.call(this));
129
+ var _b;
130
+ await ((_b = super.init) === null || _b === void 0 ? void 0 : _b.call(this));
131
131
  // check attributes initial values
132
132
  mappingsNames.forEach((attr) => __classPrivateFieldGet(this, _ValidateAttributesMixinClass_instances, "m", _ValidateAttributesMixinClass_handleError).call(this, attr, this.getAttribute(attr)));
133
133
  }
@@ -156,7 +156,7 @@ const projectIdMixin = createSingletonMixin((superclass) => {
156
156
  const baseUrlMixin = createSingletonMixin((superclass) => {
157
157
  return class BaseUrlMixinClass extends superclass {
158
158
  get baseUrl() {
159
- return this.getAttribute('base-url');
159
+ return this.getAttribute('base-url') || '';
160
160
  }
161
161
  };
162
162
  });
@@ -231,8 +231,8 @@ const debuggerMixin = createSingletonMixin((superclass) => { var _DebuggerMixinC
231
231
  _DebuggerMixinClass_instances.add(this);
232
232
  _DebuggerMixinClass_debuggerEle.set(this, void 0);
233
233
  this.attributeChangedCallback = (attrName, oldValue, newValue) => {
234
- var _a;
235
- (_a = super.attributeChangedCallback) === null || _a === void 0 ? void 0 : _a.call(this, attrName, oldValue, newValue);
234
+ var _b;
235
+ (_b = super.attributeChangedCallback) === null || _b === void 0 ? void 0 : _b.call(this, attrName, oldValue, newValue);
236
236
  if (attrName === 'debug') {
237
237
  __classPrivateFieldGet(this, _DebuggerMixinClass_instances, "m", _DebuggerMixinClass_handleDebugMode).call(this);
238
238
  }
@@ -242,23 +242,23 @@ const debuggerMixin = createSingletonMixin((superclass) => { var _DebuggerMixinC
242
242
  return this.getAttribute('debug') === 'true';
243
243
  }
244
244
  onLogEvent(logLevel, args) {
245
- var _a;
246
- (_a = super.onLogEvent) === null || _a === void 0 ? void 0 : _a.call(this, logLevel, args);
245
+ var _b;
246
+ (_b = super.onLogEvent) === null || _b === void 0 ? void 0 : _b.call(this, logLevel, args);
247
247
  if (logLevel === 'error') {
248
248
  __classPrivateFieldGet(this, _DebuggerMixinClass_instances, "m", _DebuggerMixinClass_updateDebuggerMessages).call(this, args[0] || 'Error', args[1]);
249
249
  }
250
250
  }
251
251
  async init() {
252
- var _a;
253
- await ((_a = super.init) === null || _a === void 0 ? void 0 : _a.call(this));
252
+ var _b;
253
+ await ((_b = super.init) === null || _b === void 0 ? void 0 : _b.call(this));
254
254
  __classPrivateFieldGet(this, _DebuggerMixinClass_instances, "m", _DebuggerMixinClass_handleDebugMode).call(this);
255
255
  }
256
256
  },
257
257
  _DebuggerMixinClass_debuggerEle = new WeakMap(),
258
258
  _DebuggerMixinClass_instances = new WeakSet(),
259
259
  _DebuggerMixinClass_disableDebugger = function _DebuggerMixinClass_disableDebugger() {
260
- var _a;
261
- (_a = __classPrivateFieldGet(this, _DebuggerMixinClass_debuggerEle, "f")) === null || _a === void 0 ? void 0 : _a.remove();
260
+ var _b;
261
+ (_b = __classPrivateFieldGet(this, _DebuggerMixinClass_debuggerEle, "f")) === null || _b === void 0 ? void 0 : _b.remove();
262
262
  __classPrivateFieldSet(this, _DebuggerMixinClass_debuggerEle, null, "f");
263
263
  },
264
264
  _DebuggerMixinClass_enableDebugger = async function _DebuggerMixinClass_enableDebugger() {
@@ -283,9 +283,9 @@ const debuggerMixin = createSingletonMixin((superclass) => { var _DebuggerMixinC
283
283
  __classPrivateFieldGet(this, _DebuggerMixinClass_instances, "m", _DebuggerMixinClass_disableDebugger).call(this);
284
284
  },
285
285
  _DebuggerMixinClass_updateDebuggerMessages = function _DebuggerMixinClass_updateDebuggerMessages(title, description) {
286
- var _a;
286
+ var _b;
287
287
  if (title)
288
- (_a = __classPrivateFieldGet(this, _DebuggerMixinClass_debuggerEle, "f")) === null || _a === void 0 ? void 0 : _a.updateData({ title, description });
288
+ (_b = __classPrivateFieldGet(this, _DebuggerMixinClass_debuggerEle, "f")) === null || _b === void 0 ? void 0 : _b.updateData({ title, description });
289
289
  },
290
290
  _a; });
291
291
 
@@ -339,9 +339,9 @@ const descopeUiMixin = createSingletonMixin((superclass) => {
339
339
  _DescopeUiMixinClass_getComponentsVersion =
340
340
  // eslint-disable-next-line class-methods-use-this
341
341
  async function _DescopeUiMixinClass_getComponentsVersion() {
342
- var _a;
342
+ var _b;
343
343
  const config = await this.config;
344
- const componentsVersion = (_a = config === null || config === void 0 ? void 0 : config.projectConfig) === null || _a === void 0 ? void 0 : _a.componentsVersion;
344
+ const componentsVersion = (_b = config === null || config === void 0 ? void 0 : config.projectConfig) === null || _b === void 0 ? void 0 : _b.componentsVersion;
345
345
  if (!componentsVersion) {
346
346
  this.logger.error('Could not get components version');
347
347
  }
@@ -520,9 +520,9 @@ const modalMixin = createSingletonMixin((superclass) => {
520
520
  });
521
521
  }
522
522
  async init() {
523
- var _a;
523
+ var _b;
524
524
  this.loadDescopeUiComponents([MODAL_ELE_TAG]);
525
- await ((_a = super.init) === null || _a === void 0 ? void 0 : _a.call(this));
525
+ await ((_b = super.init) === null || _b === void 0 ? void 0 : _b.call(this));
526
526
  }
527
527
  },
528
528
  _ModalMixinClass_ModalDriverWrapper = new WeakMap(),
@@ -567,8 +567,8 @@ const notificationsMixin = createSingletonMixin((superclass) => {
567
567
  });
568
568
  }
569
569
  async init() {
570
- var _a;
571
- await ((_a = super.init) === null || _a === void 0 ? void 0 : _a.call(this));
570
+ var _b;
571
+ await ((_b = super.init) === null || _b === void 0 ? void 0 : _b.call(this));
572
572
  this.loadDescopeUiComponents([NOTIFICATION_ELE_TAG]);
573
573
  }
574
574
  },
@@ -577,7 +577,7 @@ const notificationsMixin = createSingletonMixin((superclass) => {
577
577
  });
578
578
 
579
579
  // Those files are saved on a new folder to prevent breaking changes
580
- const THEME_FILENAME = 'theme.json';
580
+ const DEFAULT_STYLE_ID = 'theme';
581
581
 
582
582
  const loadFont = (url) => {
583
583
  const font = document.createElement('link');
@@ -611,27 +611,31 @@ const themeValidation = (_, theme) => (theme || false) &&
611
611
  theme !== 'dark' &&
612
612
  'Supported theme values are "light", "dark", or leave empty for using the OS theme';
613
613
  const themeMixin = createSingletonMixin((superclass) => {
614
- var _ThemeMixinClass_instances, _ThemeMixinClass__themeResource, _ThemeMixinClass_fetchTheme, _ThemeMixinClass_themeResource_get, _ThemeMixinClass_loadGlobalStyle, _ThemeMixinClass_loadComponentsStyle, _ThemeMixinClass_loadFonts, _ThemeMixinClass_applyTheme, _a;
614
+ var _ThemeMixinClass_instances, _ThemeMixinClass_globalStyleTag, _ThemeMixinClass__themeResource, _ThemeMixinClass_fetchTheme, _ThemeMixinClass_themeResource_get, _ThemeMixinClass_loadGlobalStyle, _ThemeMixinClass_loadComponentsStyle, _ThemeMixinClass_getFontsConfig, _ThemeMixinClass_loadFonts, _ThemeMixinClass_applyTheme, _a;
615
615
  const BaseClass = compose(createValidateAttributesMixin({ theme: themeValidation }), staticResourcesMixin, initLifecycleMixin, descopeUiMixin, configMixin, initElementMixin, observeAttributesMixin)(superclass);
616
616
  return _a = class ThemeMixinClass extends BaseClass {
617
617
  constructor() {
618
618
  super(...arguments);
619
619
  _ThemeMixinClass_instances.add(this);
620
+ _ThemeMixinClass_globalStyleTag.set(this, void 0);
620
621
  _ThemeMixinClass__themeResource.set(this, void 0);
621
622
  }
622
623
  get theme() {
623
- var _a, _b;
624
+ var _b, _c;
624
625
  const theme = this.getAttribute('theme');
625
626
  if (theme === 'os') {
626
627
  const isOsDark = window.matchMedia &&
627
- ((_b = (_a = window.matchMedia) === null || _a === void 0 ? void 0 : _a.call(window, '(prefers-color-scheme: dark)')) === null || _b === void 0 ? void 0 : _b.matches);
628
+ ((_c = (_b = window.matchMedia) === null || _b === void 0 ? void 0 : _b.call(window, '(prefers-color-scheme: dark)')) === null || _c === void 0 ? void 0 : _c.matches);
628
629
  return isOsDark ? 'dark' : 'light';
629
630
  }
630
631
  return theme || 'light';
631
632
  }
633
+ get styleId() {
634
+ return this.getAttribute('style-id') || DEFAULT_STYLE_ID;
635
+ }
632
636
  async init() {
633
- var _a;
634
- await ((_a = super.init) === null || _a === void 0 ? void 0 : _a.call(this));
637
+ var _b;
638
+ await ((_b = super.init) === null || _b === void 0 ? void 0 : _b.call(this));
635
639
  __classPrivateFieldGet(this, _ThemeMixinClass_instances, "m", _ThemeMixinClass_loadGlobalStyle).call(this);
636
640
  __classPrivateFieldGet(this, _ThemeMixinClass_instances, "m", _ThemeMixinClass_loadComponentsStyle).call(this);
637
641
  __classPrivateFieldGet(this, _ThemeMixinClass_instances, "m", _ThemeMixinClass_loadFonts).call(this);
@@ -640,13 +644,20 @@ const themeMixin = createSingletonMixin((superclass) => {
640
644
  __classPrivateFieldGet(this, _ThemeMixinClass_instances, "m", _ThemeMixinClass_loadFonts).call(this);
641
645
  __classPrivateFieldGet(this, _ThemeMixinClass_instances, "m", _ThemeMixinClass_applyTheme).call(this);
642
646
  });
647
+ this.observeAttributes(['style-id'], () => {
648
+ __classPrivateFieldSet(this, _ThemeMixinClass__themeResource, null, "f");
649
+ __classPrivateFieldGet(this, _ThemeMixinClass_instances, "m", _ThemeMixinClass_loadFonts).call(this);
650
+ __classPrivateFieldGet(this, _ThemeMixinClass_instances, "m", _ThemeMixinClass_loadGlobalStyle).call(this);
651
+ __classPrivateFieldGet(this, _ThemeMixinClass_instances, "m", _ThemeMixinClass_loadComponentsStyle).call(this);
652
+ });
643
653
  }
644
654
  },
655
+ _ThemeMixinClass_globalStyleTag = new WeakMap(),
645
656
  _ThemeMixinClass__themeResource = new WeakMap(),
646
657
  _ThemeMixinClass_instances = new WeakSet(),
647
658
  _ThemeMixinClass_fetchTheme = async function _ThemeMixinClass_fetchTheme() {
648
659
  try {
649
- const { body: fetchedTheme } = await this.fetchStaticResource(THEME_FILENAME, 'json');
660
+ const { body: fetchedTheme } = await this.fetchStaticResource(`${this.styleId}.json`, 'json');
650
661
  // In development mode, we sometimes want to override the UI components URL
651
662
  // The override components might have a different theme, so we need to merge it with the project theme in order to see the components correctly
652
663
  if (process.env.NODE_ENV === 'development') {
@@ -683,32 +694,42 @@ const themeMixin = createSingletonMixin((superclass) => {
683
694
  return __classPrivateFieldGet(this, _ThemeMixinClass__themeResource, "f");
684
695
  },
685
696
  _ThemeMixinClass_loadGlobalStyle = async function _ThemeMixinClass_loadGlobalStyle() {
686
- var _a, _b;
697
+ var _b, _c;
687
698
  const theme = await __classPrivateFieldGet(this, _ThemeMixinClass_instances, "a", _ThemeMixinClass_themeResource_get);
688
699
  if (!theme)
689
700
  return;
690
- const styleEle = document.createElement('style');
691
- styleEle.innerText =
692
- (((_a = theme === null || theme === void 0 ? void 0 : theme.light) === null || _a === void 0 ? void 0 : _a.globals) || '') + (((_b = theme === null || theme === void 0 ? void 0 : theme.dark) === null || _b === void 0 ? void 0 : _b.globals) || '');
693
- this.shadowRoot.appendChild(styleEle);
701
+ if (!__classPrivateFieldGet(this, _ThemeMixinClass_globalStyleTag, "f")) {
702
+ __classPrivateFieldSet(this, _ThemeMixinClass_globalStyleTag, document.createElement('style'), "f");
703
+ __classPrivateFieldGet(this, _ThemeMixinClass_globalStyleTag, "f").id = 'global-style';
704
+ this.shadowRoot.appendChild(__classPrivateFieldGet(this, _ThemeMixinClass_globalStyleTag, "f"));
705
+ }
706
+ __classPrivateFieldGet(this, _ThemeMixinClass_globalStyleTag, "f").innerText =
707
+ (((_b = theme === null || theme === void 0 ? void 0 : theme.light) === null || _b === void 0 ? void 0 : _b.globals) || '') + (((_c = theme === null || theme === void 0 ? void 0 : theme.dark) === null || _c === void 0 ? void 0 : _c.globals) || '');
694
708
  },
695
709
  _ThemeMixinClass_loadComponentsStyle = async function _ThemeMixinClass_loadComponentsStyle() {
696
- var _a, _b;
710
+ var _b, _c;
697
711
  const theme = Object.assign({}, (await __classPrivateFieldGet(this, _ThemeMixinClass_instances, "a", _ThemeMixinClass_themeResource_get)));
698
712
  if (!theme)
699
713
  return;
700
714
  const descopeUi = await this.descopeUi;
701
715
  if (descopeUi === null || descopeUi === void 0 ? void 0 : descopeUi.componentsThemeManager) {
702
716
  descopeUi.componentsThemeManager.themes = {
703
- light: (_a = theme === null || theme === void 0 ? void 0 : theme.light) === null || _a === void 0 ? void 0 : _a.components,
704
- dark: (_b = theme === null || theme === void 0 ? void 0 : theme.dark) === null || _b === void 0 ? void 0 : _b.components,
717
+ light: (_b = theme === null || theme === void 0 ? void 0 : theme.light) === null || _b === void 0 ? void 0 : _b.components,
718
+ dark: (_c = theme === null || theme === void 0 ? void 0 : theme.dark) === null || _c === void 0 ? void 0 : _c.components,
705
719
  };
706
720
  }
707
721
  },
722
+ _ThemeMixinClass_getFontsConfig = async function _ThemeMixinClass_getFontsConfig() {
723
+ var _b, _c;
724
+ const { projectConfig } = (await this.config) || {};
725
+ const newConfig = (_b = projectConfig === null || projectConfig === void 0 ? void 0 : projectConfig.styles) === null || _b === void 0 ? void 0 : _b[this.styleId];
726
+ const oldConfig = projectConfig === null || projectConfig === void 0 ? void 0 : projectConfig.cssTemplate;
727
+ const config = newConfig || oldConfig;
728
+ const fonts = (_c = config === null || config === void 0 ? void 0 : config[this.theme]) === null || _c === void 0 ? void 0 : _c.fonts;
729
+ return fonts;
730
+ },
708
731
  _ThemeMixinClass_loadFonts = async function _ThemeMixinClass_loadFonts() {
709
- var _a, _b;
710
- const { projectConfig } = await this.config;
711
- const fonts = (_b = (_a = projectConfig === null || projectConfig === void 0 ? void 0 : projectConfig.cssTemplate) === null || _a === void 0 ? void 0 : _a[this.theme]) === null || _b === void 0 ? void 0 : _b.fonts;
732
+ const fonts = await __classPrivateFieldGet(this, _ThemeMixinClass_instances, "m", _ThemeMixinClass_getFontsConfig).call(this);
712
733
  if (fonts) {
713
734
  Object.values(fonts).forEach((font) => {
714
735
  if (font.url) {
@@ -717,6 +738,9 @@ const themeMixin = createSingletonMixin((superclass) => {
717
738
  }
718
739
  });
719
740
  }
741
+ else {
742
+ this.logger.debug('No fonts to load');
743
+ }
720
744
  },
721
745
  _ThemeMixinClass_applyTheme = async function _ThemeMixinClass_applyTheme() {
722
746
  this.rootElement.setAttribute('data-theme', this.theme);
@@ -820,7 +844,7 @@ const formMixin = createSingletonMixin((superclass) => class FormMixinClass exte
820
844
  const baseStaticUrlMixin = createSingletonMixin((superclass) => {
821
845
  return class BaseStaticUrlMixinClass extends superclass {
822
846
  get baseStaticUrl() {
823
- return this.getAttribute('base-static-url');
847
+ return this.getAttribute('base-static-url') || '';
824
848
  }
825
849
  };
826
850
  });