@momentum-design/components 0.27.2 → 0.27.3

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.
@@ -13,7 +13,7 @@ import styles from './icon.styles';
13
13
  import { Component } from '../../models';
14
14
  import providerUtils from '../../utils/provider';
15
15
  import IconProvider from '../iconprovider/iconprovider.component';
16
- import { dynamicSVGImport } from './icon.utils';
16
+ import { svgFetch } from './icon.utils';
17
17
  import { DEFAULTS } from './icon.constants';
18
18
  /**
19
19
  * Icon component that dynamically displays SVG icons based on a valid name.
@@ -101,8 +101,8 @@ class Icon extends Component {
101
101
  */
102
102
  async getIconData() {
103
103
  if (this.iconProviderContext.value) {
104
- const { fileExtension, url, cacheName, cacheStrategy } = this.iconProviderContext.value;
105
- if (url && fileExtension && this.name) {
104
+ const { fileExtension, url, cacheName, iconSet, cacheStrategy } = this.iconProviderContext.value;
105
+ if (iconSet === 'custom-icons' && url && fileExtension && this.name) {
106
106
  // function to abort the fetch request and create a new signal
107
107
  // (directly passing the abortcontroller to the fetch request per reference
108
108
  // will not work due to JS call-by-sharing behavior)
@@ -111,25 +111,38 @@ class Icon extends Component {
111
111
  this.abortController = new AbortController();
112
112
  return this.abortController.signal;
113
113
  };
114
- try {
115
- // fetch icon data (including caching logic)
116
- const iconData = await dynamicSVGImport({
117
- url,
118
- name: this.name,
119
- fileExtension,
120
- cacheName,
121
- cacheStrategy,
122
- renewSignal,
123
- });
114
+ // fetch icon data (including caching logic)
115
+ return svgFetch({
116
+ url,
117
+ name: this.name,
118
+ fileExtension,
119
+ cacheName,
120
+ cacheStrategy,
121
+ renewSignal,
122
+ })
123
+ .then((iconData) => {
124
124
  // parse the fetched icon string to an html element and set the attributes
125
125
  const iconElement = this.prepareIconElement(iconData);
126
126
  this.handleIconLoadedSuccess(iconElement);
127
- }
128
- catch (error) {
127
+ })
128
+ .catch((error) => {
129
129
  this.handleIconLoadedFailure(error);
130
- }
130
+ });
131
+ }
132
+ if (iconSet === 'momentum-icons' && this.name) {
133
+ // dynamic import of the lit template from the momentum icons package
134
+ return import(`@momentum-design/icons/dist/ts/${this.name}.ts`)
135
+ .then((module) => {
136
+ this.handleIconLoadedSuccess(module.default());
137
+ })
138
+ .catch((error) => {
139
+ this.handleIconLoadedFailure(error);
140
+ });
131
141
  }
132
142
  }
143
+ const noIconProviderError = new Error('IconProvider not found or not properly set up.');
144
+ this.handleIconLoadedFailure(noIconProviderError);
145
+ return Promise.reject(noIconProviderError);
133
146
  }
134
147
  /**
135
148
  * Sets the iconData state to the fetched icon.
@@ -28,5 +28,5 @@ interface Args {
28
28
  * @returns Response string from the fetch
29
29
  * @throws Error if the response is not ok
30
30
  */
31
- declare const dynamicSVGImport: ({ url, name, fileExtension, cacheStrategy, cacheName, renewSignal, }: Args) => Promise<string>;
32
- export { dynamicSVGImport };
31
+ declare const svgFetch: ({ url, name, fileExtension, cacheStrategy, cacheName, renewSignal, }: Args) => Promise<string>;
32
+ export { svgFetch };
@@ -33,7 +33,7 @@ const fetchIcon = async (request) => fetch(request).then((response) => {
33
33
  * @returns Response string from the fetch
34
34
  * @throws Error if the response is not ok
35
35
  */
36
- const dynamicSVGImport = async ({ url, name, fileExtension, cacheStrategy, cacheName, renewSignal, }) => {
36
+ const svgFetch = async ({ url, name, fileExtension, cacheStrategy, cacheName, renewSignal, }) => {
37
37
  // abort the previous fetch request if it is still pending
38
38
  // and create a new signal
39
39
  const signal = renewSignal();
@@ -76,4 +76,4 @@ const dynamicSVGImport = async ({ url, name, fileExtension, cacheStrategy, cache
76
76
  throw new Error(`Error in caching the Icon ${name}, ${error}`);
77
77
  }));
78
78
  };
79
- export { dynamicSVGImport };
79
+ export { svgFetch };
@@ -1,15 +1,22 @@
1
1
  import { Provider } from '../../models';
2
2
  import IconProviderContext from './iconprovider.context';
3
+ import type { CacheStrategy, IconSet } from './iconprovider.types';
3
4
  /**
4
5
  * IconProvider component, which allows to be consumed from sub components
5
6
  * (see `providerUtils.consume` for how to consume)
6
7
  *
7
- * Bundling icons will be up to the consumer of this component, such
8
- * that only a url has to be passed in from which the icons will be
9
- * fetched.
8
+ * Attribute `iconSet` can be set to either `momentum-icons` or `custom-icons`.
9
+ * If `momentum-icons` is selected, the icons will be fetched from the
10
+ * Momentum Design System icon set per a dynamic JS Import (no need to provide a URL).
11
+ * This requires the consumer to have the `@momentum-designs` package installed and the
12
+ * build tooling needs to support dynamic imports.
10
13
  *
11
- * If `cacheStrategy` is provided, the IconProvider will cache the icons
12
- * in the selected cache (either web-api-cache or in-memory-cache),
14
+ * If `custom-icons` is selected, the icons will be fetched from the provided URL.
15
+ * This requires the consumer to provide a URL from which the icons will be fetched and
16
+ * the consumer needs to make sure to bundle the icons in the application.
17
+ *
18
+ * If `cacheStrategy` is provided (only works with iconSet = `custom-icons`), the
19
+ * IconProvider will cache the icons in the selected cache (either web-api-cache or in-memory-cache),
13
20
  * to avoid fetching the same icon multiple times over the network.
14
21
  * This is useful when the same icon is used multiple times in the application.
15
22
  * To consider:
@@ -28,36 +35,31 @@ declare class IconProvider extends Provider<IconProviderContext> {
28
35
  * Context object of the IconProvider, to be consumed by child components
29
36
  */
30
37
  static get Context(): {
31
- /**
32
- * IconProvider component, which allows to be consumed from sub components
33
- * (see `providerUtils.consume` for how to consume)
34
- *
35
- * Bundling icons will be up to the consumer of this component, such
36
- * that only a url has to be passed in from which the icons will be
37
- * fetched.
38
- *
39
- * If `cacheStrategy` is provided, the IconProvider will cache the icons
40
- * in the selected cache (either web-api-cache or in-memory-cache),
41
- * to avoid fetching the same icon multiple times over the network.
42
- * This is useful when the same icon is used multiple times in the application.
43
- * To consider:
44
- * - The `in-memory-cache` is not persisted and will be lost when the
45
- * IconProvider is removed from the DOM.
46
- * - The `web-api-cache` is persisted, but only works in https environments
47
- * (https://developer.mozilla.org/en-US/docs/Web/API/Cache).
48
- *
49
- * @tagname mdc-iconprovider
50
- *
51
- * @slot - children
52
- */
53
38
  __context__: IconProviderContext;
54
39
  };
40
+ /**
41
+ * Icon set to be used
42
+ *
43
+ * If `momentum-icons` is selected, the icons will be fetched from the
44
+ * Momentum Design System icon set per a dynamic JS Import (no need to provide a URL).
45
+ * This requires the consumer to have the `@momentum-designs` package installed and the
46
+ * build tooling needs to support dynamic imports.
47
+ *
48
+ * If `custom-icons` is selected, the icons will be fetched from the provided URL.
49
+ * This requires the consumer to provide a URL from which the icons will be fetched and
50
+ * the consumer needs to make sure to bundle the icons in the application.
51
+ *
52
+ * @default momentum-icons
53
+ */
54
+ iconSet?: IconSet;
55
55
  /**
56
56
  * Url of where icons will be fetched from
57
+ * (if Icon set is `custom-icons`, this will be the base url)
57
58
  */
58
59
  url?: string;
59
60
  /**
60
61
  * File extension of icons
62
+ * (if Icon set is `custom-icons`, this will be the file extension for icons)
61
63
  * @default svg
62
64
  */
63
65
  fileExtension?: string;
@@ -75,6 +77,8 @@ declare class IconProvider extends Provider<IconProviderContext> {
75
77
  /**
76
78
  * Icons Cache Strategy to use
77
79
  *
80
+ * **Can only be used if Icon set is `custom-icons`**
81
+ *
78
82
  * Choose `in-memory-cache` to cache icons in a JS cache (in-memory cache).
79
83
  * Choose `web-cache-api` to cache icons using the Web Cache API.
80
84
  *
@@ -83,9 +87,9 @@ declare class IconProvider extends Provider<IconProviderContext> {
83
87
  * If not provided or invalid value provided, the icons will not be cached.
84
88
  * @default undefined
85
89
  */
86
- cacheStrategy?: 'in-memory-cache' | 'web-cache-api';
90
+ cacheStrategy?: CacheStrategy;
87
91
  /**
88
- * Icons Cache Name to use
92
+ * Icons Cache Name to use (cache strategy must be provided)
89
93
  *
90
94
  * If provided, Icons inside the provider will be cached in the
91
95
  * cache (determined by `cache-strategy`) with the provided name.
@@ -15,12 +15,18 @@ import { ALLOWED_FILE_EXTENSIONS, DEFAULTS, ALLOWED_LENGTH_UNITS } from './iconp
15
15
  * IconProvider component, which allows to be consumed from sub components
16
16
  * (see `providerUtils.consume` for how to consume)
17
17
  *
18
- * Bundling icons will be up to the consumer of this component, such
19
- * that only a url has to be passed in from which the icons will be
20
- * fetched.
18
+ * Attribute `iconSet` can be set to either `momentum-icons` or `custom-icons`.
19
+ * If `momentum-icons` is selected, the icons will be fetched from the
20
+ * Momentum Design System icon set per a dynamic JS Import (no need to provide a URL).
21
+ * This requires the consumer to have the `@momentum-designs` package installed and the
22
+ * build tooling needs to support dynamic imports.
21
23
  *
22
- * If `cacheStrategy` is provided, the IconProvider will cache the icons
23
- * in the selected cache (either web-api-cache or in-memory-cache),
24
+ * If `custom-icons` is selected, the icons will be fetched from the provided URL.
25
+ * This requires the consumer to provide a URL from which the icons will be fetched and
26
+ * the consumer needs to make sure to bundle the icons in the application.
27
+ *
28
+ * If `cacheStrategy` is provided (only works with iconSet = `custom-icons`), the
29
+ * IconProvider will cache the icons in the selected cache (either web-api-cache or in-memory-cache),
24
30
  * to avoid fetching the same icon multiple times over the network.
25
31
  * This is useful when the same icon is used multiple times in the application.
26
32
  * To consider:
@@ -40,8 +46,24 @@ class IconProvider extends Provider {
40
46
  context: IconProviderContext.context,
41
47
  initialValue: new IconProviderContext(),
42
48
  });
49
+ /**
50
+ * Icon set to be used
51
+ *
52
+ * If `momentum-icons` is selected, the icons will be fetched from the
53
+ * Momentum Design System icon set per a dynamic JS Import (no need to provide a URL).
54
+ * This requires the consumer to have the `@momentum-designs` package installed and the
55
+ * build tooling needs to support dynamic imports.
56
+ *
57
+ * If `custom-icons` is selected, the icons will be fetched from the provided URL.
58
+ * This requires the consumer to provide a URL from which the icons will be fetched and
59
+ * the consumer needs to make sure to bundle the icons in the application.
60
+ *
61
+ * @default momentum-icons
62
+ */
63
+ this.iconSet = DEFAULTS.ICON_SET;
43
64
  /**
44
65
  * File extension of icons
66
+ * (if Icon set is `custom-icons`, this will be the file extension for icons)
45
67
  * @default svg
46
68
  */
47
69
  this.fileExtension = DEFAULTS.FILE_EXTENSION;
@@ -73,6 +95,7 @@ class IconProvider extends Provider {
73
95
  this.fileExtension = DEFAULTS.FILE_EXTENSION;
74
96
  this.context.value.fileExtension = DEFAULTS.FILE_EXTENSION;
75
97
  }
98
+ this.context.value.iconSet = this.iconSet;
76
99
  this.context.value.url = this.url;
77
100
  this.context.value.size = this.size;
78
101
  this.context.value.cacheName = this.cacheName;
@@ -88,6 +111,7 @@ class IconProvider extends Provider {
88
111
  }
89
112
  updateContext() {
90
113
  if (this.context.value.fileExtension !== this.fileExtension
114
+ || this.context.value.iconSet !== this.iconSet
91
115
  || this.context.value.url !== this.url
92
116
  || this.context.value.lengthUnit !== this.lengthUnit
93
117
  || this.context.value.size !== this.size
@@ -98,6 +122,10 @@ class IconProvider extends Provider {
98
122
  }
99
123
  }
100
124
  }
125
+ __decorate([
126
+ property({ type: String, attribute: 'icon-set', reflect: true }),
127
+ __metadata("design:type", String)
128
+ ], IconProvider.prototype, "iconSet", void 0);
101
129
  __decorate([
102
130
  property({ type: String }),
103
131
  __metadata("design:type", String)
@@ -7,5 +7,6 @@ declare const DEFAULTS: {
7
7
  readonly LENGTH_UNIT: "em";
8
8
  readonly SIZE: number;
9
9
  readonly SHOULD_CACHE: false;
10
+ readonly ICON_SET: "momentum-icons";
10
11
  };
11
12
  export { TAG_NAME, DEFAULTS, ALLOWED_FILE_EXTENSIONS, ALLOWED_LENGTH_UNITS, LENGTH_UNIT_SIZE };
@@ -13,5 +13,6 @@ const DEFAULTS = {
13
13
  LENGTH_UNIT: 'em',
14
14
  SIZE: LENGTH_UNIT_SIZE.em,
15
15
  SHOULD_CACHE: false,
16
+ ICON_SET: 'momentum-icons',
16
17
  };
17
18
  export { TAG_NAME, DEFAULTS, ALLOWED_FILE_EXTENSIONS, ALLOWED_LENGTH_UNITS, LENGTH_UNIT_SIZE };
@@ -1,10 +1,12 @@
1
+ import type { IconSet, CacheStrategy } from './iconprovider.types';
1
2
  declare class IconProviderContext {
3
+ iconSet?: IconSet;
2
4
  fileExtension?: string;
3
5
  url?: string;
4
6
  lengthUnit?: string;
5
7
  size?: number;
6
8
  cacheName?: string;
7
- cacheStrategy?: 'in-memory-cache' | 'web-cache-api';
9
+ cacheStrategy?: CacheStrategy;
8
10
  static readonly context: {
9
11
  __context__: IconProviderContext;
10
12
  };
@@ -0,0 +1,3 @@
1
+ type IconSet = 'momentum-icons' | 'custom-icons';
2
+ type CacheStrategy = 'in-memory-cache' | 'web-cache-api';
3
+ export type { IconSet, CacheStrategy };
@@ -0,0 +1 @@
1
+ export {};