2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-10 23:20:59 +00:00
iconify/components/react/src/iconify.ts

383 lines
8.1 KiB
TypeScript
Raw Normal View History

2024-04-27 19:17:17 +00:00
import {
useEffect,
useState,
forwardRef,
createElement,
type Ref,
} from 'react';
import type { IconifyJSON, IconifyIcon } from '@iconify/types';
// Core
import type { IconifyIconName } from '@iconify/utils/lib/icon/name';
2022-06-19 16:16:11 +00:00
import type { IconifyIconSize } from '@iconify/utils/lib/customisations/defaults';
import type { IconifyStorageFunctions } from '@iconify/core/lib/storage/functions';
import {
iconLoaded,
getIcon,
addIcon,
addCollection,
getIconData,
allowSimpleNames,
} from '@iconify/core/lib/storage/functions';
import { listIcons } from '@iconify/core/lib/storage/storage';
import type { IconifyBuilderFunctions } from '@iconify/core/lib/builder/functions';
import { iconToSVG as buildIcon } from '@iconify/utils/lib/svg/build';
import { replaceIDs } from '@iconify/utils/lib/svg/id';
import { calculateSize } from '@iconify/utils/lib/svg/size';
import type { IconifyIconBuildResult } from '@iconify/utils/lib/svg/build';
// API
import type {
IconifyAPIFunctions,
IconifyAPIInternalFunctions,
IconifyAPIQueryParams,
IconifyAPICustomQueryParams,
} from '@iconify/core/lib/api/functions';
import type {
IconifyAPIModule,
IconifyAPISendQuery,
IconifyAPIPrepareIconsQuery,
} from '@iconify/core/lib/api/modules';
import { setAPIModule } from '@iconify/core/lib/api/modules';
import type {
PartialIconifyAPIConfig,
IconifyAPIConfig,
GetAPIConfig,
} from '@iconify/core/lib/api/config';
import {
addAPIProvider,
getAPIConfig,
listAPIProviders,
} from '@iconify/core/lib/api/config';
import {
fetchAPIModule,
setFetch,
getFetch,
} from '@iconify/core/lib/api/modules/fetch';
import type {
IconifyIconLoaderCallback,
IconifyIconLoaderAbort,
} from '@iconify/core/lib/api/icons';
2022-01-13 20:19:49 +00:00
import { loadIcons, loadIcon } from '@iconify/core/lib/api/icons';
import { sendAPIQuery } from '@iconify/core/lib/api/query';
// Cache
import { initBrowserStorage } from '@iconify/core/lib/browser-storage';
import { toggleBrowserCache } from '@iconify/core/lib/browser-storage/functions';
import type {
IconifyBrowserCacheType,
IconifyBrowserCacheFunctions,
} from '@iconify/core/lib/browser-storage/functions';
// Properties
import type {
IconifyIconOnLoad,
IconifyIconCustomisations,
IconifyIconProps,
IconifyRenderMode,
IconProps,
2024-04-27 19:17:17 +00:00
IconElement,
} from './props';
// Render SVG
import { render } from './render';
import { defaultIconProps } from '@iconify/utils/lib/icon/defaults';
/**
* Export required types
*/
// Function sets
export {
IconifyStorageFunctions,
IconifyBuilderFunctions,
IconifyBrowserCacheFunctions,
IconifyAPIFunctions,
IconifyAPIInternalFunctions,
};
// JSON stuff
export { IconifyIcon, IconifyJSON, IconifyIconName };
// Customisations and icon props
export {
IconifyIconCustomisations,
IconifyIconSize,
IconifyRenderMode,
IconifyIconProps,
IconProps,
IconifyIconOnLoad,
};
// API
export {
IconifyAPIConfig,
IconifyIconLoaderCallback,
IconifyIconLoaderAbort,
IconifyAPIModule,
GetAPIConfig,
IconifyAPIPrepareIconsQuery,
IconifyAPISendQuery,
2021-05-01 20:39:56 +00:00
PartialIconifyAPIConfig,
IconifyAPIQueryParams,
IconifyAPICustomQueryParams,
};
// Builder functions
2022-06-19 16:16:11 +00:00
export { IconifyIconBuildResult };
/* Browser cache */
export { IconifyBrowserCacheType };
/**
* Enable cache
*/
function enableCache(storage: IconifyBrowserCacheType): void {
toggleBrowserCache(storage, true);
}
/**
* Disable cache
*/
function disableCache(storage: IconifyBrowserCacheType): void {
toggleBrowserCache(storage, false);
}
/**
* Initialise stuff
*/
// Enable short names
allowSimpleNames(true);
// Set API module
setAPIModule('', fetchAPIModule);
/**
* Browser stuff
*/
if (typeof document !== 'undefined' && typeof window !== 'undefined') {
// Set cache and load existing cache
initBrowserStorage();
interface WindowWithIconifyStuff {
IconifyPreload?: IconifyJSON[] | IconifyJSON;
IconifyProviders?: Record<string, PartialIconifyAPIConfig>;
}
const _window = window as WindowWithIconifyStuff;
// Load icons from global "IconifyPreload"
if (_window.IconifyPreload !== void 0) {
const preload = _window.IconifyPreload;
const err = 'Invalid IconifyPreload syntax.';
if (typeof preload === 'object' && preload !== null) {
(preload instanceof Array ? preload : [preload]).forEach((item) => {
try {
if (
// Check if item is an object and not null/array
typeof item !== 'object' ||
item === null ||
item instanceof Array ||
// Check for 'icons' and 'prefix'
typeof item.icons !== 'object' ||
typeof item.prefix !== 'string' ||
// Add icon set
!addCollection(item)
) {
console.error(err);
}
} catch (e) {
console.error(err);
}
});
}
}
// Set API from global "IconifyProviders"
if (_window.IconifyProviders !== void 0) {
const providers = _window.IconifyProviders;
if (typeof providers === 'object' && providers !== null) {
for (let key in providers) {
const err = 'IconifyProviders[' + key + '] is invalid.';
try {
const value = providers[key];
if (
typeof value !== 'object' ||
!value ||
value.resources === void 0
) {
continue;
}
if (!addAPIProvider(key, value)) {
console.error(err);
}
} catch (e) {
console.error(err);
}
}
}
}
}
/**
* Component
*/
interface InternalIconProps extends IconProps {
2024-04-27 19:17:17 +00:00
_ref?: Ref<IconElement> | null;
}
2024-04-27 19:17:17 +00:00
function IconComponent(props: InternalIconProps): JSX.Element {
interface State {
// Currently rendered icon
name: string;
2024-04-27 19:17:17 +00:00
// Icon data, null if missing
data?: IconifyIcon | null;
}
2024-04-27 19:17:17 +00:00
const [mounted, setMounted] = useState(!!props.ssr);
2024-04-27 19:17:17 +00:00
interface AbortState {
callback?: IconifyIconLoaderAbort;
}
2024-04-27 19:17:17 +00:00
const [abort, setAbort] = useState<AbortState>({});
const [state, setState] = useState<State>({
name: '',
});
// Cancel loading
function cleanup() {
const callback = abort.callback;
if (callback) {
callback();
setAbort({});
}
}
2024-04-27 19:17:17 +00:00
// Update state
function updateState() {
const name = props.icon;
if (typeof name === 'object') {
// Icon as object
cleanup();
setState({
name: '',
data: name,
});
return;
}
2024-04-27 19:17:17 +00:00
// New icon or got icon data
const data = getIconData(name);
if (state.name !== name || data !== state.data) {
cleanup();
setState({
name,
data,
});
2024-04-27 19:17:17 +00:00
if (data === undefined) {
// Load icon, update state when done
const callback = loadIcons([name], updateState);
setAbort({
callback,
});
} else if (data) {
// Icon data is available: trigger onLoad callback if present
props.onLoad?.(name);
}
}
}
2024-04-27 19:17:17 +00:00
// Mounted state, cleanup for loader
useEffect(() => {
setMounted(true);
return cleanup;
}, []);
// Icon changed or component mounted
2024-04-27 19:17:17 +00:00
useEffect(() => {
if (mounted) {
updateState();
}
}, [props.icon, mounted]);
2024-04-27 19:17:17 +00:00
// Render icon
const { name, data } = state;
if (!data) {
return props.children
? (props.children as JSX.Element)
: createElement('span', {});
}
2024-04-27 19:17:17 +00:00
return render(
{
...defaultIconProps,
...data,
},
props,
name
);
}
2024-04-27 19:17:17 +00:00
// Component type
type IconComponentType = (props: IconProps) => JSX.Element;
/**
* Block icon
*
* @param props - Component properties
*/
2024-04-27 19:17:17 +00:00
export const Icon = forwardRef<IconElement, IconProps>((props, ref) =>
IconComponent({
2021-08-16 07:55:28 +00:00
...props,
_ref: ref,
2024-04-27 19:17:17 +00:00
})
) as IconComponentType;
/**
* Inline icon (has negative verticalAlign that makes it behave like icon font)
*
* @param props - Component properties
*/
2024-04-27 19:17:17 +00:00
export const InlineIcon = forwardRef<IconElement, IconProps>((props, ref) =>
IconComponent({
inline: true,
...props,
_ref: ref,
})
) as IconComponentType;
/**
* Internal API
*/
const _api: IconifyAPIInternalFunctions = {
getAPIConfig,
setAPIModule,
sendAPIQuery,
setFetch,
getFetch,
listAPIProviders,
};
/**
* Export functions
*/
// IconifyAPIInternalFunctions
export { _api };
// IconifyAPIFunctions
2022-01-13 20:19:49 +00:00
export { addAPIProvider, loadIcons, loadIcon };
// IconifyStorageFunctions
export {
iconLoaded,
iconLoaded as iconExists, // deprecated, kept to avoid breaking changes
getIcon,
listIcons,
addIcon,
addCollection,
};
// IconifyBuilderFunctions
export { replaceIDs, calculateSize, buildIcon };
// IconifyBrowserCacheFunctions
export { enableCache, disableCache };