iconly
v3.0.2
Published
Iconly is designed to load and cache SVG icons in the browser, using IndexedDB to store the data. It retrieves the icons from a given SVG file, stores them in IndexedDB, and inserts them into the DOM for easy access and use.
Maintainers
Readme
~2kB gzipped
Demo
Install
$ npm install iconlyImport
import { createIconly } from 'iconly';Upgrading to v3
- The API is now factory-based: use
createIconly()instead ofnew Iconly(). init()returns aResult<void>and never throws.- The injected sprite wrapper is now
[data-iconly="iconset"]insidecontainer(instead of#iconset).
See CHANGELOG.md for details.
Usage
const iconLoader = createIconly({
file: './sprite.svg',
version: '1.0',
debug: true,
storage: 'indexeddb',
});
const result = await iconLoader.init();
if (!result.ok) {
console.error(result.error);
}After init(), the SVG sprite is injected into the container inside <div data-iconly="iconset" aria-hidden="true">...</div>.
<svg>
<use href="#icon-name"></use>
</svg>File with icons
<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none" fill="none">
<symbol id="icon-name" viewBox="0 0 300 300">
<path ... />
</symbol>
...
</svg>Options
| Option | Type | Default | Description |
|:------------------:|:---------------------------------------:|:---------------:|:----------------------------------------------------------------|
| file | string | './icons.svg' | The URL of the SVG file containing the icons. |
| version | string | '1.0' | The version of the icon set. |
| debug | boolean | false | If true, debug callbacks and logger debug output are enabled. |
| container | string \| HTMLElement | document.body | The container element where the icons will be injected. |
| storage | 'indexeddb' \| 'memory' \| 'session' | 'indexeddb' | Storage strategy used for caching icon data. |
| dbName | string | 'iconlyDB' | IndexedDB database name (only for indexeddb). |
| storeName | string | 'icons' | IndexedDB store name (only for indexeddb). |
| sessionKeyPrefix | string | 'iconly' | Prefix for SessionStorage keys (only for session). |
| logger | { debug?, error? } | undefined | Optional logger implementation for debug/error output. |
| onError | (error) => void | undefined | Optional callback invoked on errors. |
| onDebug | (...args) => void | undefined | Optional callback invoked for debug messages. |
Error handling
init()never throws; it returns aResultwithok: falseanderrordetails.- When
debugisfalse, debug messages are suppressed (noonDebugcalls and nologger.debug). - Errors still trigger
onErrorandlogger.error(if provided), even whendebugisfalse. - You can cancel a fetch by calling
iconLoader.abort(), which results in afetch_abortederror code.
License
MIT
