@nysds/nys-icon 1.19.4 → 1.20.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.
- package/dist/icon-cache.d.ts +18 -0
- package/dist/icon-library-registry.d.ts +64 -0
- package/dist/index.d.ts +4 -0
- package/dist/nys-icon.d.ts +164 -0
- package/dist/nys-icon.figma.d.ts +1 -0
- package/dist/nys-icon.js +267 -229
- package/dist/nys-icon.js.map +1 -1
- package/dist/{nys-icon.library-3w_7rLli.js → nys-icon.library-Bu3noJqZ.js} +2 -2
- package/dist/{nys-icon.library-3w_7rLli.js.map → nys-icon.library-Bu3noJqZ.js.map} +1 -1
- package/dist/nys-icon.library.d.ts +2 -0
- package/package.json +2 -1
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Icon Cache
|
|
3
|
+
*
|
|
4
|
+
* Shared SVG sanitize/parse pipeline and caches. URL-based icons (custom
|
|
5
|
+
* libraries) are fetched once per URL; inline SVG sources (the built-in
|
|
6
|
+
* NYSDS set) are parsed once per source string. Each consumer gets a
|
|
7
|
+
* cloned SVGElement via `cloneNode(true)` so DOM nodes are never shared.
|
|
8
|
+
*/
|
|
9
|
+
/** Fetch and parse an SVG from a URL. Returns a cloned SVGElement (safe for multiple consumers). */
|
|
10
|
+
export declare function fetchIcon(url: string): Promise<SVGElement>;
|
|
11
|
+
/**
|
|
12
|
+
* Parse an inline SVG source string. Results are cached by source content,
|
|
13
|
+
* so repeated renders of the same icon sanitize/parse once. Returns a
|
|
14
|
+
* cloned SVGElement (safe for multiple consumers).
|
|
15
|
+
*/
|
|
16
|
+
export declare function parseIcon(source: string): SVGElement;
|
|
17
|
+
/** Clear one URL entry, or all cached icons (URL and inline) when no URL is given. */
|
|
18
|
+
export declare function clearIconCache(url?: string): void;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Icon Library Registry
|
|
3
|
+
*
|
|
4
|
+
* Global registry for icon libraries. The "default" library resolves the
|
|
5
|
+
* standard NYSDS icon set from an inline SVG map shipped as JavaScript
|
|
6
|
+
* (`nys-icon.library.ts`), loaded lazily as a separate chunk — no base-URL
|
|
7
|
+
* discovery, no per-icon fetch, no browser globals. Custom libraries
|
|
8
|
+
* (Font Awesome, Material Icons, etc.) register at runtime via
|
|
9
|
+
* `registerIconLibrary()` and typically resolve to URLs.
|
|
10
|
+
*
|
|
11
|
+
* The registry and watcher maps are stored on `globalThis` so that even
|
|
12
|
+
* when bundlers (Storybook Vite, etc.) create duplicate module
|
|
13
|
+
* instances, every copy shares a single source of truth. `globalThis`
|
|
14
|
+
* (rather than `window`) keeps module import side-effect-safe in
|
|
15
|
+
* Node/SSR environments, where `window` does not exist.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* How a resolver locates an icon: a URL string (legacy shorthand for
|
|
19
|
+
* `{ type: "url" }`), an explicit URL, or inline SVG source. Resolvers may
|
|
20
|
+
* return synchronously or via a Promise.
|
|
21
|
+
*/
|
|
22
|
+
export type IconResolution = string | {
|
|
23
|
+
type: "url";
|
|
24
|
+
href: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: "svg";
|
|
27
|
+
content: string;
|
|
28
|
+
};
|
|
29
|
+
export interface IconLibrary {
|
|
30
|
+
/** Given an icon name, return where/what its SVG is. Return undefined if not found. */
|
|
31
|
+
resolver: (name: string) => IconResolution | undefined | Promise<IconResolution | undefined>;
|
|
32
|
+
/** Optional post-parse transform applied to the SVGElement. */
|
|
33
|
+
mutator?: (svg: SVGElement) => void;
|
|
34
|
+
}
|
|
35
|
+
export interface NysIconWatcher {
|
|
36
|
+
redraw(): void;
|
|
37
|
+
}
|
|
38
|
+
declare global {
|
|
39
|
+
var __nysIconRegistry: Map<string, IconLibrary> | undefined;
|
|
40
|
+
var __nysIconWatchers: Map<string, Set<NysIconWatcher>> | undefined;
|
|
41
|
+
var __nysIconDefaultRegistered: boolean | undefined;
|
|
42
|
+
}
|
|
43
|
+
/** Register or replace a named icon library. All watching icons using this library will redraw.
|
|
44
|
+
*
|
|
45
|
+
* Intended for custom/external libraries — the standard NYSDS set is
|
|
46
|
+
* built in and needs no registration. Registering under `"default"`
|
|
47
|
+
* replaces the built-in set (escape hatch).
|
|
48
|
+
*
|
|
49
|
+
* @example Register a Font Awesome library with a custom resolver:
|
|
50
|
+
* ```ts
|
|
51
|
+
* registerIconLibrary("fa", {
|
|
52
|
+
* resolver: (name) => `https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/svg/${name}.svg`
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function registerIconLibrary(name: string, library: IconLibrary): void;
|
|
57
|
+
/** Remove a registered icon library. All watching icons using this library will redraw. */
|
|
58
|
+
export declare function unregisterIconLibrary(name: string): void;
|
|
59
|
+
/** Get a registered icon library by name. */
|
|
60
|
+
export declare function getIconLibrary(name: string): IconLibrary | undefined;
|
|
61
|
+
/** Subscribe an icon instance to library change notifications. */
|
|
62
|
+
export declare function watchIconLibrary(name: string, watcher: NysIconWatcher): void;
|
|
63
|
+
/** Unsubscribe an icon instance from library change notifications. */
|
|
64
|
+
export declare function unwatchIconLibrary(name: string, watcher: NysIconWatcher): void;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { NysElement } from "@nysds/internals";
|
|
2
|
+
import { NysIconWatcher } from "./icon-library-registry";
|
|
3
|
+
/**
|
|
4
|
+
* Renders SVG icons from a registered icon library. The built-in NYSDS icon
|
|
5
|
+
* library (Material Symbols) is the default. Custom libraries (Font Awesome,
|
|
6
|
+
* Material Icons, etc.) can be registered via `registerIconLibrary()`.
|
|
7
|
+
*
|
|
8
|
+
* Pass `name` to select an icon. Use `library` to choose a registered library
|
|
9
|
+
* (defaults to `"default"` for NYSDS icons). Use `ariaLabel` to make the icon
|
|
10
|
+
* accessible (removes `aria-hidden`). Supports size presets, rotation,
|
|
11
|
+
* flipping, and custom colors.
|
|
12
|
+
*
|
|
13
|
+
* @summary SVG icon with swappable library support, size, rotation, and color options.
|
|
14
|
+
* @element nys-icon
|
|
15
|
+
*
|
|
16
|
+
* @example Basic
|
|
17
|
+
* ```html
|
|
18
|
+
* <nys-icon name="check_circle"></nys-icon>
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @example ARIA Label
|
|
22
|
+
* ```html
|
|
23
|
+
* <nys-icon
|
|
24
|
+
* name="edit_square"
|
|
25
|
+
* ariaLabel="Edit content"
|
|
26
|
+
* ></nys-icon>
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example Size relative
|
|
30
|
+
* ```html
|
|
31
|
+
* <nys-icon
|
|
32
|
+
* name="edit_square"
|
|
33
|
+
* size="4xl"
|
|
34
|
+
* ></nys-icon>
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example Size literal
|
|
38
|
+
* ```html
|
|
39
|
+
* <nys-icon
|
|
40
|
+
* name="edit_square"
|
|
41
|
+
* size="24"
|
|
42
|
+
* ></nys-icon>
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @example Color
|
|
46
|
+
* ```html
|
|
47
|
+
* <nys-icon
|
|
48
|
+
* name="edit_square"
|
|
49
|
+
* color="#db117d"
|
|
50
|
+
* ></nys-icon>
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example Rotate
|
|
54
|
+
* ```html
|
|
55
|
+
* <nys-icon
|
|
56
|
+
* name="edit_square"
|
|
57
|
+
* rotate="20"
|
|
58
|
+
* ></nys-icon>
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @example Flip
|
|
62
|
+
* ```html
|
|
63
|
+
* <nys-icon
|
|
64
|
+
* name="edit_square"
|
|
65
|
+
* flip="vertical"
|
|
66
|
+
* ></nys-icon>
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @example External Library - Font Awesome
|
|
70
|
+
* ```html
|
|
71
|
+
* <nys-icon name="heart" library="fa"></nys-icon>
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* @render External Library - Font Awesome
|
|
75
|
+
* ```html
|
|
76
|
+
* <nys-icon name="heart" library="fa"></nys-icon>
|
|
77
|
+
* <script data-scope="module">
|
|
78
|
+
* registerIconLibrary("fa", {
|
|
79
|
+
* resolver: (name) =>
|
|
80
|
+
* `https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/svgs/solid/${name}.svg`,
|
|
81
|
+
* mutator: (svg) => {
|
|
82
|
+
* svg.setAttribute("fill", "currentColor");
|
|
83
|
+
* },
|
|
84
|
+
* });
|
|
85
|
+
* </script>
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @example External Library - Material
|
|
89
|
+
* ```html
|
|
90
|
+
* <nys-icon name="favorite" library="material"></nys-icon>
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
*
|
|
94
|
+
* @render External Library - Material
|
|
95
|
+
* ```html
|
|
96
|
+
* <nys-icon name="favorite" library="material"></nys-icon>
|
|
97
|
+
* <script data-scope="module">
|
|
98
|
+
* registerIconLibrary("material", {
|
|
99
|
+
* resolver: (name) =>
|
|
100
|
+
* `https://fonts.gstatic.com/s/i/short-term/release/materialsymbolsoutlined/${name}/default/24px.svg`,
|
|
101
|
+
* mutator: (svg) => {
|
|
102
|
+
* svg.setAttribute("fill", "currentColor");
|
|
103
|
+
* },
|
|
104
|
+
* });
|
|
105
|
+
* </script>
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export declare class NysIcon extends NysElement implements NysIconWatcher {
|
|
109
|
+
static styles: import("lit").CSSResult;
|
|
110
|
+
/** Icon name to resolve from the selected library. Required. */
|
|
111
|
+
name: string;
|
|
112
|
+
/** Which registered icon library to use. Defaults to the built-in NYSDS library. */
|
|
113
|
+
library: string;
|
|
114
|
+
/** Accessible label. When set, removes `aria-hidden` and adds `aria-label` to the SVG. */
|
|
115
|
+
ariaLabel: string;
|
|
116
|
+
/** Rotation in degrees. Applied via CSS `rotate`. */
|
|
117
|
+
rotate: string;
|
|
118
|
+
/** Flip direction: `horizontal`, `vertical`, or empty for none. */
|
|
119
|
+
flip: string;
|
|
120
|
+
/** Icon color. Accepts any CSS color value. Defaults to `currentcolor`. */
|
|
121
|
+
color: string;
|
|
122
|
+
/**
|
|
123
|
+
* Icon size. Semantic sizes: `xs`-`5xl`. Pixel sizes: `12`-`50`.
|
|
124
|
+
* @default "md"
|
|
125
|
+
*/
|
|
126
|
+
size: "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl" | "4xl" | "5xl" | "12" | "14" | "16" | "18" | "20" | "24" | "32" | "40" | "50";
|
|
127
|
+
private _svg;
|
|
128
|
+
/** Monotonically increasing token so stale async fetches are discarded. */
|
|
129
|
+
private _loadSeq;
|
|
130
|
+
/** Promise for the in-flight icon load. */
|
|
131
|
+
private _loadPromise;
|
|
132
|
+
/** Resolves when the current icon load (if any) is complete. */
|
|
133
|
+
get updateComplete(): Promise<boolean>;
|
|
134
|
+
connectedCallback(): void;
|
|
135
|
+
disconnectedCallback(): void;
|
|
136
|
+
/** Called by the icon library registry when the current library changes. */
|
|
137
|
+
redraw(): void;
|
|
138
|
+
/**
|
|
139
|
+
* Lit calls firstUpdated after the first render, once all reactive
|
|
140
|
+
* properties (including those set from the template) are resolved.
|
|
141
|
+
* This guarantees the initial _loadIcon runs with the correct name
|
|
142
|
+
* and library values, avoiding the race where updated() might not
|
|
143
|
+
* fire for properties that equal their defaults.
|
|
144
|
+
*/
|
|
145
|
+
firstUpdated(): void;
|
|
146
|
+
private static _validName;
|
|
147
|
+
updated(changedProps: Map<string, unknown>): void;
|
|
148
|
+
private _loadIcon;
|
|
149
|
+
private _applyAttributes;
|
|
150
|
+
/**
|
|
151
|
+
* Reflects the icon's accessible state onto the host:
|
|
152
|
+
* - Labeled icon -> role="img" + aria-label, no aria-hidden.
|
|
153
|
+
* - Decorative -> aria-hidden="true", no role/label.
|
|
154
|
+
*
|
|
155
|
+
* These are reflected as host *attributes* (rather than via
|
|
156
|
+
* ElementInternals) on purpose: aria-hidden / role / aria-label in
|
|
157
|
+
* attribute form have universal, consistent assistive-technology support,
|
|
158
|
+
* whereas AT support for these states set through ElementInternals is still
|
|
159
|
+
* uneven across browser/screen-reader combinations. The host role/label
|
|
160
|
+
* therefore depend on runtime state (ariaLabel), so defaultRole stays null.
|
|
161
|
+
*/
|
|
162
|
+
private _reflectHostSemantics;
|
|
163
|
+
render(): import("lit-html").TemplateResult<1> | null;
|
|
164
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|