2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-12 13:47:49 +00:00

feat: add custom loaders to web component

This commit is contained in:
Vjacheslav Trushkin 2024-11-04 09:42:22 +02:00
parent d3f691b65f
commit 8888f2b755
10 changed files with 68 additions and 9 deletions

View File

@ -22,15 +22,36 @@ export function parseIconValue(
value: unknown, value: unknown,
onload: IconOnLoadCallback onload: IconOnLoadCallback
): CurrentIconData { ): CurrentIconData {
// Check if icon name is valid if (typeof value === 'object') {
const name =
typeof value === 'string' ? stringToIcon(value, true, true) : null;
if (!name) {
// Test for serialised object
const data = testIconObject(value); const data = testIconObject(value);
return { return {
value,
data, data,
value,
};
}
if (typeof value !== 'string') {
// Invalid value
return {
value,
};
}
// Check for JSON
if (value.includes('{')) {
const data = testIconObject(value);
if (data) {
return {
data,
value,
};
}
}
// Parse icon name
const name = stringToIcon(value, true, true);
if (!name) {
return {
value,
}; };
} }
@ -38,7 +59,7 @@ export function parseIconValue(
const data = getIconData(name); const data = getIconData(name);
// Icon data exists or icon has no prefix. Do not load icon from API if icon has no prefix // Icon data exists or icon has no prefix. Do not load icon from API if icon has no prefix
if (data !== void 0 || !name.prefix) { if (data !== undefined || !name.prefix) {
return { return {
value, value,
name, name,

View File

@ -11,6 +11,7 @@ export function testIconObject(value: unknown): IconifyIcon | undefined {
...obj, ...obj,
}; };
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) { } catch (err) {
// //
} }

View File

@ -66,6 +66,7 @@ export function defineIconifyIcon(
try { try {
customElements = window.customElements; customElements = window.customElements;
ParentClass = window.HTMLElement; ParentClass = window.HTMLElement;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) { } catch (err) {
return; return;
} }
@ -228,6 +229,7 @@ export function defineIconifyIcon(
if (value && value.slice(0, 1) === '{') { if (value && value.slice(0, 1) === '{') {
try { try {
return JSON.parse(value); return JSON.parse(value);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) { } catch (err) {
// //
} }
@ -284,6 +286,7 @@ export function defineIconifyIcon(
try { try {
(root.lastChild as SVGSVGElement).setCurrentTime(0); (root.lastChild as SVGSVGElement).setCurrentTime(0);
return; return;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) { } catch (err) {
// Failed: setCurrentTime() is not supported // Failed: setCurrentTime() is not supported
} }
@ -468,11 +471,13 @@ export function defineIconifyIcon(
} }
}); });
this._observer.observe(this); this._observer.observe(this);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) { } catch (err) {
// Something went wrong, possibly observer is not supported // Something went wrong, possibly observer is not supported
if (this._observer) { if (this._observer) {
try { try {
this._observer.disconnect(); this._observer.disconnect();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) { } catch (err) {
// //
} }

View File

@ -40,6 +40,10 @@ import {
} from '@iconify/core/lib/api/modules/fetch'; } from '@iconify/core/lib/api/modules/fetch';
import { loadIcons, loadIcon } from '@iconify/core/lib/api/icons'; import { loadIcons, loadIcon } from '@iconify/core/lib/api/icons';
import { sendAPIQuery } from '@iconify/core/lib/api/query'; import { sendAPIQuery } from '@iconify/core/lib/api/query';
import {
setCustomIconLoader,
setCustomIconsLoader,
} from '@iconify/core/lib/api/loaders';
// Cache // Cache
import { initBrowserStorage } from '@iconify/core/lib/browser-storage'; import { initBrowserStorage } from '@iconify/core/lib/browser-storage';
@ -92,6 +96,7 @@ export function exportFunctions(): IconifyExportedFunctions {
let _window: WindowWithIconifyStuff; let _window: WindowWithIconifyStuff;
try { try {
_window = window as WindowWithIconifyStuff; _window = window as WindowWithIconifyStuff;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) { } catch (err) {
// //
} }
@ -120,6 +125,7 @@ export function exportFunctions(): IconifyExportedFunctions {
) { ) {
console.error(err); console.error(err);
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e) { } catch (e) {
console.error(err); console.error(err);
} }
@ -146,6 +152,7 @@ export function exportFunctions(): IconifyExportedFunctions {
if (!addAPIProvider(key, value)) { if (!addAPIProvider(key, value)) {
console.error(err); console.error(err);
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e) { } catch (e) {
console.error(err); console.error(err);
} }
@ -181,6 +188,8 @@ export function exportFunctions(): IconifyExportedFunctions {
loadIcons, loadIcons,
loadIcon, loadIcon,
addAPIProvider, addAPIProvider,
setCustomIconLoader,
setCustomIconsLoader,
appendCustomStyle, appendCustomStyle,
_api, _api,
}; };

View File

@ -11,6 +11,10 @@ import type { IconifyBuilderFunctions } from '@iconify/core/lib/builder/function
import type { IconifyIconBuildResult } from '@iconify/utils/lib/svg/build'; import type { IconifyIconBuildResult } from '@iconify/utils/lib/svg/build';
// API // API
import type {
IconifyCustomIconLoader,
IconifyCustomIconsLoader,
} from '@iconify/core/lib/api/types';
import type { import type {
IconifyAPIFunctions, IconifyAPIFunctions,
IconifyAPIInternalFunctions, IconifyAPIInternalFunctions,
@ -82,6 +86,8 @@ export {
PartialIconifyAPIConfig, PartialIconifyAPIConfig,
IconifyAPIQueryParams, IconifyAPIQueryParams,
IconifyAPICustomQueryParams, IconifyAPICustomQueryParams,
IconifyCustomIconLoader,
IconifyCustomIconsLoader,
}; };
// Builder functions // Builder functions
@ -122,6 +128,8 @@ const {
svgToURL, svgToURL,
loadIcons, loadIcons,
loadIcon, loadIcon,
setCustomIconLoader,
setCustomIconsLoader,
addAPIProvider, addAPIProvider,
_api, _api,
} = IconifyIconComponent; } = IconifyIconComponent;
@ -142,6 +150,8 @@ export {
loadIcons, loadIcons,
loadIcon, loadIcon,
addAPIProvider, addAPIProvider,
setCustomIconLoader,
setCustomIconsLoader,
appendCustomStyle, appendCustomStyle,
_api, _api,
}; };

View File

@ -37,11 +37,16 @@ describe('Testing parseIconValue without API', () => {
}); });
expect(result).toEqual({ expect(result).toEqual({
value, value,
name: {
provider: '',
prefix: '',
name: value,
},
}); });
}); });
it('Icon without prefix', () => { it('Icon without prefix', () => {
const value = 'test'; const value = 'Test';
const result = parseIconValue(value, () => { const result = parseIconValue(value, () => {
throw new Error('callback should not have been called'); throw new Error('callback should not have been called');
}); });

View File

@ -37,6 +37,8 @@ export type {
PartialIconifyAPIConfig, PartialIconifyAPIConfig,
IconifyAPIQueryParams, IconifyAPIQueryParams,
IconifyAPICustomQueryParams, IconifyAPICustomQueryParams,
IconifyCustomIconLoader,
IconifyCustomIconsLoader,
} from 'iconify-icon'; } from 'iconify-icon';
// Builder functions // Builder functions
@ -70,6 +72,8 @@ export {
loadIcons, loadIcons,
loadIcon, loadIcon,
addAPIProvider, addAPIProvider,
setCustomIconLoader,
setCustomIconsLoader,
appendCustomStyle, appendCustomStyle,
_api, _api,
} from 'iconify-icon'; } from 'iconify-icon';

View File

@ -2,3 +2,4 @@
node_modules node_modules
dist dist
lib lib
tsconfig.tsbuildinfo

View File

@ -36,6 +36,8 @@ export type {
PartialIconifyAPIConfig, PartialIconifyAPIConfig,
IconifyAPIQueryParams, IconifyAPIQueryParams,
IconifyAPICustomQueryParams, IconifyAPICustomQueryParams,
IconifyCustomIconLoader,
IconifyCustomIconsLoader,
} from 'iconify-icon'; } from 'iconify-icon';
// Builder functions // Builder functions
@ -69,6 +71,8 @@ export {
loadIcons, loadIcons,
loadIcon, loadIcon,
addAPIProvider, addAPIProvider,
setCustomIconLoader,
setCustomIconsLoader,
appendCustomStyle, appendCustomStyle,
_api, _api,
} from 'iconify-icon'; } from 'iconify-icon';

View File

@ -1 +0,0 @@
{"root":["./src/iconify.tsx"],"version":"5.6.3"}