@getflip/swirl-components 0.114.0 → 0.116.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.
Files changed (40) hide show
  1. package/components.json +87 -19
  2. package/dist/cjs/loader.cjs.js +1 -1
  3. package/dist/cjs/swirl-components.cjs.js +1 -1
  4. package/dist/cjs/swirl-file-viewer_7.cjs.entry.js +5 -2
  5. package/dist/cjs/swirl-pdf-reader.cjs.entry.js +6 -1
  6. package/dist/cjs/swirl-theme-provider.cjs.entry.js +42 -285
  7. package/dist/collection/assets/pdfjs/pdf.worker.min.js +1 -1
  8. package/dist/collection/collection-manifest.json +1 -1
  9. package/dist/collection/components/swirl-file-viewer/swirl-file-viewer.js +19 -1
  10. package/dist/collection/components/swirl-file-viewer/viewers/swirl-file-viewer-pdf/swirl-file-viewer-pdf.js +21 -1
  11. package/dist/collection/components/swirl-pdf-reader/swirl-pdf-reader.js +54 -1
  12. package/dist/collection/components/swirl-theme-provider/swirl-theme-provider.js +72 -99
  13. package/dist/collection/components/swirl-theme-provider/swirl-theme-provider.spec.js +67 -18
  14. package/dist/collection/components/swirl-theme-provider/swirl-theme-provider.stories.js +371 -17
  15. package/dist/collection/components/swirl-theme-provider/swirl-theme-provider.types.js +1 -0
  16. package/dist/components/assets/pdfjs/pdf.worker.min.js +1 -1
  17. package/dist/components/swirl-file-viewer-pdf2.js +4 -1
  18. package/dist/components/swirl-file-viewer2.js +3 -1
  19. package/dist/components/swirl-pdf-reader.js +8 -2
  20. package/dist/components/swirl-theme-provider.js +46 -289
  21. package/dist/esm/loader.js +1 -1
  22. package/dist/esm/swirl-components.js +1 -1
  23. package/dist/esm/swirl-file-viewer_7.entry.js +5 -2
  24. package/dist/esm/swirl-pdf-reader.entry.js +7 -2
  25. package/dist/esm/swirl-theme-provider.entry.js +42 -285
  26. package/dist/swirl-components/p-353eab4c.entry.js +1 -0
  27. package/dist/swirl-components/{p-05d56f14.entry.js → p-4368ba9d.entry.js} +2 -2
  28. package/dist/swirl-components/p-bceafa56.entry.js +1 -0
  29. package/dist/swirl-components/swirl-components.esm.js +1 -1
  30. package/dist/types/components/swirl-file-viewer/swirl-file-viewer.d.ts +1 -0
  31. package/dist/types/components/swirl-file-viewer/viewers/swirl-file-viewer-pdf/swirl-file-viewer-pdf.d.ts +6 -0
  32. package/dist/types/components/swirl-pdf-reader/swirl-pdf-reader.d.ts +4 -0
  33. package/dist/types/components/swirl-theme-provider/swirl-theme-provider.d.ts +15 -34
  34. package/dist/types/components/swirl-theme-provider/swirl-theme-provider.types.d.ts +45 -0
  35. package/dist/types/components.d.ts +23 -11
  36. package/dist/types/index.d.ts +1 -1
  37. package/package.json +1 -1
  38. package/vscode-data.json +12 -0
  39. package/dist/swirl-components/p-1a4c6557.entry.js +0 -1
  40. package/dist/swirl-components/p-af636e9c.entry.js +0 -1
@@ -4,247 +4,14 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  const index = require('./index-506fe4ea.js');
6
6
 
7
- /**
8
- * A simple guard function:
9
- *
10
- * ```js
11
- * Math.min(Math.max(low, value), high)
12
- * ```
13
- */
14
- function guard(low, high, value) {
15
- return Math.min(Math.max(low, value), high);
16
- }
17
-
18
- class ColorError extends Error {
19
- constructor(color) {
20
- super(`Failed to parse color: "${color}"`);
21
- }
22
-
23
- }
24
-
25
- var ColorError$1 = ColorError;
26
-
27
- /**
28
- * Parses a color into red, gree, blue, alpha parts
29
- *
30
- * @param color the input color. Can be a RGB, RBGA, HSL, HSLA, or named color
31
- */
32
-
33
- function parseToRgba(color) {
34
- if (typeof color !== 'string') throw new ColorError$1(color);
35
- if (color.trim().toLowerCase() === 'transparent') return [0, 0, 0, 0];
36
- let normalizedColor = color.trim();
37
- normalizedColor = namedColorRegex.test(color) ? nameToHex(color) : color;
38
- const reducedHexMatch = reducedHexRegex.exec(normalizedColor);
39
-
40
- if (reducedHexMatch) {
41
- const arr = Array.from(reducedHexMatch).slice(1);
42
- return [...arr.slice(0, 3).map(x => parseInt(r(x, 2), 16)), parseInt(r(arr[3] || 'f', 2), 16) / 255];
43
- }
44
-
45
- const hexMatch = hexRegex.exec(normalizedColor);
46
-
47
- if (hexMatch) {
48
- const arr = Array.from(hexMatch).slice(1);
49
- return [...arr.slice(0, 3).map(x => parseInt(x, 16)), parseInt(arr[3] || 'ff', 16) / 255];
50
- }
51
-
52
- const rgbaMatch = rgbaRegex.exec(normalizedColor);
53
-
54
- if (rgbaMatch) {
55
- const arr = Array.from(rgbaMatch).slice(1);
56
- return [...arr.slice(0, 3).map(x => parseInt(x, 10)), parseFloat(arr[3] || '1')];
57
- }
58
-
59
- const hslaMatch = hslaRegex.exec(normalizedColor);
60
-
61
- if (hslaMatch) {
62
- const [h, s, l, a] = Array.from(hslaMatch).slice(1).map(parseFloat);
63
- if (guard(0, 100, s) !== s) throw new ColorError$1(color);
64
- if (guard(0, 100, l) !== l) throw new ColorError$1(color);
65
- return [...hslToRgb(h, s, l), a || 1];
66
- }
67
-
68
- throw new ColorError$1(color);
69
- }
70
-
71
- function hash(str) {
72
- let hash = 5381;
73
- let i = str.length;
74
-
75
- while (i) {
76
- hash = hash * 33 ^ str.charCodeAt(--i);
77
- }
78
- /* JavaScript does bitwise operations (like XOR, above) on 32-bit signed
79
- * integers. Since we want the results to be always positive, convert the
80
- * signed int to an unsigned by doing an unsigned bitshift. */
81
-
82
-
83
- return (hash >>> 0) % 2341;
84
- }
85
-
86
- const colorToInt = x => parseInt(x.replace(/_/g, ''), 36);
87
-
88
- const compressedColorMap = '1q29ehhb 1n09sgk7 1kl1ekf_ _yl4zsno 16z9eiv3 1p29lhp8 _bd9zg04 17u0____ _iw9zhe5 _to73___ _r45e31e _7l6g016 _jh8ouiv _zn3qba8 1jy4zshs 11u87k0u 1ro9yvyo 1aj3xael 1gz9zjz0 _3w8l4xo 1bf1ekf_ _ke3v___ _4rrkb__ 13j776yz _646mbhl _nrjr4__ _le6mbhl 1n37ehkb _m75f91n _qj3bzfz 1939yygw 11i5z6x8 _1k5f8xs 1509441m 15t5lwgf _ae2th1n _tg1ugcv 1lp1ugcv 16e14up_ _h55rw7n _ny9yavn _7a11xb_ 1ih442g9 _pv442g9 1mv16xof 14e6y7tu 1oo9zkds 17d1cisi _4v9y70f _y98m8kc 1019pq0v 12o9zda8 _348j4f4 1et50i2o _8epa8__ _ts6senj 1o350i2o 1mi9eiuo 1259yrp0 1ln80gnw _632xcoy 1cn9zldc _f29edu4 1n490c8q _9f9ziet 1b94vk74 _m49zkct 1kz6s73a 1eu9dtog _q58s1rz 1dy9sjiq __u89jo3 _aj5nkwg _ld89jo3 13h9z6wx _qa9z2ii _l119xgq _bs5arju 1hj4nwk9 1qt4nwk9 1ge6wau6 14j9zlcw 11p1edc_ _ms1zcxe _439shk6 _jt9y70f _754zsow 1la40eju _oq5p___ _x279qkz 1fa5r3rv _yd2d9ip _424tcku _8y1di2_ _zi2uabw _yy7rn9h 12yz980_ __39ljp6 1b59zg0x _n39zfzp 1fy9zest _b33k___ _hp9wq92 1il50hz4 _io472ub _lj9z3eo 19z9ykg0 _8t8iu3a 12b9bl4a 1ak5yw0o _896v4ku _tb8k8lv _s59zi6t _c09ze0p 1lg80oqn 1id9z8wb _238nba5 1kq6wgdi _154zssg _tn3zk49 _da9y6tc 1sg7cv4f _r12jvtt 1gq5fmkz 1cs9rvci _lp9jn1c _xw1tdnb 13f9zje6 16f6973h _vo7ir40 _bt5arjf _rc45e4t _hr4e100 10v4e100 _hc9zke2 _w91egv_ _sj2r1kk 13c87yx8 _vqpds__ _ni8ggk8 _tj9yqfb 1ia2j4r4 _7x9b10u 1fc9ld4j 1eq9zldr _5j9lhpx _ez9zl6o _md61fzm'.split(' ').reduce((acc, next) => {
89
- const key = colorToInt(next.substring(0, 3));
90
- const hex = colorToInt(next.substring(3)).toString(16); // NOTE: padStart could be used here but it breaks Node 6 compat
91
- // https://github.com/ricokahler/color2k/issues/351
92
-
93
- let prefix = '';
94
-
95
- for (let i = 0; i < 6 - hex.length; i++) {
96
- prefix += '0';
97
- }
98
-
99
- acc[key] = `${prefix}${hex}`;
100
- return acc;
101
- }, {});
102
- /**
103
- * Checks if a string is a CSS named color and returns its equivalent hex value, otherwise returns the original color.
104
- */
105
-
106
- function nameToHex(color) {
107
- const normalizedColorName = color.toLowerCase().trim();
108
- const result = compressedColorMap[hash(normalizedColorName)];
109
- if (!result) throw new ColorError$1(color);
110
- return `#${result}`;
111
- }
112
-
113
- const r = (str, amount) => Array.from(Array(amount)).map(() => str).join('');
114
-
115
- const reducedHexRegex = new RegExp(`^#${r('([a-f0-9])', 3)}([a-f0-9])?$`, 'i');
116
- const hexRegex = new RegExp(`^#${r('([a-f0-9]{2})', 3)}([a-f0-9]{2})?$`, 'i');
117
- const rgbaRegex = new RegExp(`^rgba?\\(\\s*(\\d+)\\s*${r(',\\s*(\\d+)\\s*', 2)}(?:,\\s*([\\d.]+))?\\s*\\)$`, 'i');
118
- const hslaRegex = /^hsla?\(\s*([\d.]+)\s*,\s*([\d.]+)%\s*,\s*([\d.]+)%(?:\s*,\s*([\d.]+))?\s*\)$/i;
119
- const namedColorRegex = /^[a-z]+$/i;
120
-
121
- const roundColor = color => {
122
- return Math.round(color * 255);
123
- };
124
-
125
- const hslToRgb = (hue, saturation, lightness) => {
126
- let l = lightness / 100;
127
-
128
- if (saturation === 0) {
129
- // achromatic
130
- return [l, l, l].map(roundColor);
131
- } // formulae from https://en.wikipedia.org/wiki/HSL_and_HSV
132
-
133
-
134
- const huePrime = (hue % 360 + 360) % 360 / 60;
135
- const chroma = (1 - Math.abs(2 * l - 1)) * (saturation / 100);
136
- const secondComponent = chroma * (1 - Math.abs(huePrime % 2 - 1));
137
- let red = 0;
138
- let green = 0;
139
- let blue = 0;
140
-
141
- if (huePrime >= 0 && huePrime < 1) {
142
- red = chroma;
143
- green = secondComponent;
144
- } else if (huePrime >= 1 && huePrime < 2) {
145
- red = secondComponent;
146
- green = chroma;
147
- } else if (huePrime >= 2 && huePrime < 3) {
148
- green = chroma;
149
- blue = secondComponent;
150
- } else if (huePrime >= 3 && huePrime < 4) {
151
- green = secondComponent;
152
- blue = chroma;
153
- } else if (huePrime >= 4 && huePrime < 5) {
154
- red = secondComponent;
155
- blue = chroma;
156
- } else if (huePrime >= 5 && huePrime < 6) {
157
- red = chroma;
158
- blue = secondComponent;
159
- }
160
-
161
- const lightnessModification = l - chroma / 2;
162
- const finalRed = red + lightnessModification;
163
- const finalGreen = green + lightnessModification;
164
- const finalBlue = blue + lightnessModification;
165
- return [finalRed, finalGreen, finalBlue].map(roundColor);
166
- };
167
-
168
- // taken from:
169
- /**
170
- * Parses a color in hue, saturation, lightness, and the alpha channel.
171
- *
172
- * Hue is a number between 0 and 360, saturation, lightness, and alpha are
173
- * decimal percentages between 0 and 1
174
- */
175
-
176
- function parseToHsla(color) {
177
- const [red, green, blue, alpha] = parseToRgba(color).map((value, index) => // 3rd index is alpha channel which is already normalized
178
- index === 3 ? value : value / 255);
179
- const max = Math.max(red, green, blue);
180
- const min = Math.min(red, green, blue);
181
- const lightness = (max + min) / 2; // achromatic
182
-
183
- if (max === min) return [0, 0, lightness, alpha];
184
- const delta = max - min;
185
- const saturation = lightness > 0.5 ? delta / (2 - max - min) : delta / (max + min);
186
- const hue = 60 * (red === max ? (green - blue) / delta + (green < blue ? 6 : 0) : green === max ? (blue - red) / delta + 2 : (red - green) / delta + 4);
187
- return [hue, saturation, lightness, alpha];
188
- }
189
-
190
- /**
191
- * Takes in hsla parts and constructs an hsla string
192
- *
193
- * @param hue The color circle (from 0 to 360) - 0 (or 360) is red, 120 is green, 240 is blue
194
- * @param saturation Percentage of saturation, given as a decimal between 0 and 1
195
- * @param lightness Percentage of lightness, given as a decimal between 0 and 1
196
- * @param alpha Percentage of opacity, given as a decimal between 0 and 1
197
- */
198
-
199
- function hsla(hue, saturation, lightness, alpha) {
200
- return `hsla(${(hue % 360).toFixed()}, ${guard(0, 100, saturation * 100).toFixed()}%, ${guard(0, 100, lightness * 100).toFixed()}%, ${parseFloat(guard(0, 1, alpha).toFixed(3))})`;
201
- }
202
-
203
- /**
204
- * Takes in rgba parts and returns an rgba string
205
- *
206
- * @param red The amount of red in the red channel, given in a number between 0 and 255 inclusive
207
- * @param green The amount of green in the red channel, given in a number between 0 and 255 inclusive
208
- * @param blue The amount of blue in the red channel, given in a number between 0 and 255 inclusive
209
- * @param alpha Percentage of opacity, given as a decimal between 0 and 1
210
- */
211
-
212
- function rgba(red, green, blue, alpha) {
213
- return `rgba(${guard(0, 255, red).toFixed()}, ${guard(0, 255, green).toFixed()}, ${guard(0, 255, blue).toFixed()}, ${parseFloat(guard(0, 1, alpha).toFixed(3))})`;
214
- }
215
-
216
- /**
217
- * Takes in any color and returns it as an rgba string.
218
- */
219
-
220
- function toRgba(color) {
221
- return rgba(...parseToRgba(color));
222
- }
223
-
224
7
  const swirlThemeProviderCss = ".sc-swirl-theme-provider-h{display:block}";
225
8
 
226
9
  const preferredThemeStorageKey = "swirl-preferred-theme";
227
- const tenantColorMapping = {
228
- "--s-action-primary-default": "primary",
229
- "--s-action-primary-hovered": "primaryHovered",
230
- "--s-action-primary-pressed": "primaryPressed",
231
- "--s-text-on-action-primary": "primaryContrast",
232
- "--s-icon-on-action-primary": "primaryContrast",
233
- "--s-surface-highlight-default": "secondary",
234
- "--s-surface-highlight-hovered": "secondaryHovered",
235
- "--s-surface-highlight-pressed": "secondaryPressed",
236
- "--s-on-surface-highlight-default": "secondaryHovered",
237
- "--s-text-on-surface-highlight": "secondaryContrast",
238
- "--s-icon-on-surface-highlight": "secondaryContrast",
239
- "--s-text-highlight": "text",
240
- "--s-interactive-primary-default": "text",
241
- "--s-interactive-primary-hovered": "textHovered",
242
- "--s-interactive-primary-pressed": "textPressed",
243
- };
244
10
  const SwirlThemeProvider = class {
245
11
  constructor(hostRef) {
246
12
  index.registerInstance(this, hostRef);
247
13
  this.themeChange = index.createEvent(this, "themeChange", 7);
14
+ this.setDesignTokens = [];
248
15
  this.config = undefined;
249
16
  }
250
17
  componentWillLoad() {
@@ -258,27 +25,27 @@ const SwirlThemeProvider = class {
258
25
  this.updateAppTheme();
259
26
  }
260
27
  /**
261
- * Returns the active app theme.
28
+ * Returns the active OS theme.
262
29
  * @returns SwirlTheme
263
30
  */
264
- async getActiveTheme() {
265
- return this.appTheme;
31
+ async getActiveOSTheme() {
32
+ return this.appOSTheme;
266
33
  }
267
34
  /**
268
- * Returns the user's preferred theme stored in local storage.
35
+ * Returns the user's preferred OS theme stored in local storage.
269
36
  * @returns SwirlTheme
270
37
  */
271
- async getPreferredTheme() {
38
+ async getPreferredOSTheme() {
272
39
  if (!Boolean(this.resolvedConfig.storage)) {
273
40
  return;
274
41
  }
275
42
  return this.resolvedConfig.storage.getItem(preferredThemeStorageKey);
276
43
  }
277
44
  /**
278
- * Sets the user's preferred theme and stores it in local storage. Overrides
279
- * the OS theme.
45
+ * Sets the user's preferred OS theme and stores it in local storage. Overrides
46
+ * the system theme.
280
47
  */
281
- async setPreferredTheme(theme) {
48
+ async setPreferredOSTheme(theme) {
282
49
  if (!Boolean(this.resolvedConfig.storage)) {
283
50
  return;
284
51
  }
@@ -286,9 +53,9 @@ const SwirlThemeProvider = class {
286
53
  this.updateAppTheme();
287
54
  }
288
55
  /**
289
- * Resets the user's preferred theme, using the OS theme instead.
56
+ * Resets the user's preferred OS theme, using the system theme instead.
290
57
  */
291
- async resetPreferredTheme() {
58
+ async resetPreferredOSTheme() {
292
59
  if (!Boolean(this.resolvedConfig.storage)) {
293
60
  return;
294
61
  }
@@ -315,8 +82,8 @@ const SwirlThemeProvider = class {
315
82
  });
316
83
  }
317
84
  async updateAppTheme() {
318
- this.appTheme = (await this.getPreferredTheme()) || this.osTheme;
319
- if (this.appTheme === "dark") {
85
+ this.appOSTheme = (await this.getPreferredOSTheme()) || this.osTheme;
86
+ if (this.appOSTheme === "dark") {
320
87
  document.documentElement.classList.remove("theme-light");
321
88
  document.documentElement.classList.add("theme-dark");
322
89
  }
@@ -325,59 +92,49 @@ const SwirlThemeProvider = class {
325
92
  document.documentElement.classList.add("theme-light");
326
93
  }
327
94
  this.updateTenantVariables();
95
+ this.updateTenantAssets();
328
96
  const themeChangeEventData = {
329
- activeTheme: await this.getActiveTheme(),
330
- preferredTheme: await this.getPreferredTheme(),
97
+ activeTheme: await this.getActiveOSTheme(),
98
+ preferredTheme: await this.getPreferredOSTheme(),
331
99
  };
332
- if (!Boolean(this.recentThemeChangeEventData) ||
100
+ if (!Boolean(this.recentOSThemeChangeEventData) ||
333
101
  themeChangeEventData.activeTheme !==
334
- this.recentThemeChangeEventData.activeTheme ||
102
+ this.recentOSThemeChangeEventData.activeTheme ||
335
103
  themeChangeEventData.preferredTheme !==
336
- this.recentThemeChangeEventData.preferredTheme) {
337
- this.recentThemeChangeEventData = themeChangeEventData;
338
- this.themeChange.emit(this.recentThemeChangeEventData);
104
+ this.recentOSThemeChangeEventData.preferredTheme) {
105
+ this.recentOSThemeChangeEventData = themeChangeEventData;
106
+ this.themeChange.emit(this.recentOSThemeChangeEventData);
107
+ }
108
+ }
109
+ updateTenantAssets() {
110
+ const theme = this.resolvedConfig?.themes?.[this.appOSTheme];
111
+ if (!Boolean(theme)) {
112
+ return;
339
113
  }
114
+ document.head
115
+ .querySelector('link[rel="icon"]')
116
+ ?.setAttribute("href", theme.favicon.link);
340
117
  }
341
118
  updateTenantVariables() {
342
- const tenantTheme = this.resolvedConfig?.tenantColors;
343
- if (!Boolean(tenantTheme)) {
119
+ const theme = this.resolvedConfig?.themes?.[this.appOSTheme];
120
+ if (!Boolean(theme)) {
344
121
  this.resetTenantVariables();
345
122
  return;
346
123
  }
347
124
  const rootElement = this.resolvedConfig.rootElement;
348
- // generate state colors (hovered, pressed) from base colors
349
- const primaryHsla = parseToHsla(tenantTheme.primary);
350
- const secondaryHsla = parseToHsla(tenantTheme.secondary);
351
- const textHsla = parseToHsla(tenantTheme.text);
352
- const primaryHovered = toRgba(hsla(primaryHsla[0], primaryHsla[1] - 0.21, primaryHsla[2] + 0.09, 1));
353
- const primaryPressed = toRgba(hsla(primaryHsla[0], primaryHsla[1] - 0.2, primaryHsla[2] + 0.17, 1));
354
- const secondaryHovered = toRgba(hsla(secondaryHsla[0], secondaryHsla[1], secondaryHsla[2] + 0.07, 1));
355
- const secondaryPressed = toRgba(hsla(secondaryHsla[0], secondaryHsla[1], secondaryHsla[2] + 0.15, 1));
356
- const textHovered = toRgba(hsla(textHsla[0], textHsla[1] - 0.34, textHsla[2] + 0.1, 1));
357
- const textPressed = toRgba(hsla(textHsla[0], textHsla[1] - 0.48, textHsla[2] + 0.2, 1));
358
- const tenantThemeWithGeneratedStateColors = {
359
- ...tenantTheme,
360
- primaryHovered: primaryHovered,
361
- primaryPressed: primaryPressed,
362
- secondaryHovered: secondaryHovered,
363
- secondaryPressed: secondaryPressed,
364
- textHovered: textHovered,
365
- textPressed: textPressed,
366
- };
367
- // set custom properties for tenant theme colors
368
- Object.entries(tenantColorMapping).forEach(([key, value]) => rootElement.style.setProperty(key, tenantThemeWithGeneratedStateColors[value]));
125
+ this.setDesignTokens = [];
126
+ theme.design_tokens.forEach((token) => {
127
+ const propertyName = `--s-${token.id}`;
128
+ rootElement.style.setProperty(propertyName, `rgba(${token.color.r}, ${token.color.g}, ${token.color.b}, ${token.color.a})`);
129
+ this.setDesignTokens.push(propertyName);
130
+ });
369
131
  }
370
132
  resetTenantVariables() {
371
133
  const rootElement = this.resolvedConfig.rootElement;
372
- [
373
- ...Object.keys(tenantColorMapping),
374
- "primaryHovered",
375
- "primaryPressed",
376
- "secondaryHovered",
377
- "secondaryPressed",
378
- "textHovered",
379
- "textPressed",
380
- ].forEach((key) => rootElement.style.removeProperty(key));
134
+ this.setDesignTokens.forEach((property) => {
135
+ rootElement.style.removeProperty(property);
136
+ });
137
+ this.setDesignTokens = [];
381
138
  }
382
139
  render() {
383
140
  return (index.h(index.Host, null, index.h("slot", null)));