@mal-icons/web 0.1.1 → 0.2.7

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/index.cjs ADDED
@@ -0,0 +1,187 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ function __accessProp(key) {
6
+ return this[key];
7
+ }
8
+ var __toCommonJS = (from) => {
9
+ var entry = (__moduleCache ??= new WeakMap).get(from), desc;
10
+ if (entry)
11
+ return entry;
12
+ entry = __defProp({}, "__esModule", { value: true });
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (var key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(entry, key))
16
+ __defProp(entry, key, {
17
+ get: __accessProp.bind(from, key),
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
+ });
20
+ }
21
+ __moduleCache.set(from, entry);
22
+ return entry;
23
+ };
24
+ var __moduleCache;
25
+ var __returnValue = (v) => v;
26
+ function __exportSetter(name, newValue) {
27
+ this[name] = __returnValue.bind(null, newValue);
28
+ }
29
+ var __export = (target, all) => {
30
+ for (var name in all)
31
+ __defProp(target, name, {
32
+ get: all[name],
33
+ enumerable: true,
34
+ configurable: true,
35
+ set: __exportSetter.bind(all, name)
36
+ });
37
+ };
38
+
39
+ // packages/web/src/index.ts
40
+ var exports_src = {};
41
+ __export(exports_src, {
42
+ renderIcon: () => renderIcon,
43
+ registerIcons: () => registerIcons,
44
+ isIconData: () => isIconData,
45
+ defineMalIcon: () => defineMalIcon,
46
+ clearRegistry: () => clearRegistry,
47
+ cdnLoader: () => cdnLoader,
48
+ animationClass: () => import_core2.animationClass,
49
+ WEIGHT_STROKE_WIDTH: () => import_core2.WEIGHT_STROKE_WIDTH,
50
+ ICON_ANIMATIONS_CSS: () => import_core2.ICON_ANIMATIONS_CSS
51
+ });
52
+ module.exports = __toCommonJS(exports_src);
53
+ var import_core = require("@mal-icons/core");
54
+ var import_core2 = require("@mal-icons/core");
55
+ var SVG_NS = "http://www.w3.org/2000/svg";
56
+ function renderIcon(data, props = {}) {
57
+ const { size, color, className } = import_core.resolveIconAttrs({ size: props.size, color: props.color, className: props.className }, {});
58
+ const svg = document.createElementNS(SVG_NS, "svg");
59
+ svg.setAttribute("viewBox", data.viewBox);
60
+ svg.setAttribute("stroke", "currentColor");
61
+ svg.setAttribute("fill", "currentColor");
62
+ svg.setAttribute("stroke-width", "0");
63
+ if (data.defaultAttr) {
64
+ for (const [k, v] of Object.entries(data.defaultAttr))
65
+ svg.setAttribute(k, String(v));
66
+ }
67
+ svg.setAttribute("width", String(size));
68
+ svg.setAttribute("height", String(size));
69
+ if (props.weight)
70
+ svg.setAttribute("stroke-width", String(import_core.WEIGHT_STROKE_WIDTH[props.weight]));
71
+ const fullClassName = [className, props.animate ? import_core.animationClass(props.animate) : undefined].filter(Boolean).join(" ");
72
+ if (fullClassName)
73
+ svg.setAttribute("class", fullClassName);
74
+ if (color)
75
+ svg.style.color = color;
76
+ if (props.secondaryColor)
77
+ svg.style.setProperty("--mal-icons-secondary", props.secondaryColor);
78
+ if (props.title) {
79
+ svg.setAttribute("role", "img");
80
+ const title = document.createElementNS(SVG_NS, "title");
81
+ title.textContent = props.title;
82
+ svg.appendChild(title);
83
+ } else {
84
+ svg.setAttribute("role", "presentation");
85
+ svg.setAttribute("aria-hidden", "true");
86
+ }
87
+ for (const [tag, attr] of data.nodes) {
88
+ const el = document.createElementNS(SVG_NS, tag);
89
+ for (const [k, v] of Object.entries(attr))
90
+ el.setAttribute(k, String(v));
91
+ svg.appendChild(el);
92
+ }
93
+ return svg;
94
+ }
95
+ function isIconData(value) {
96
+ return typeof value === "object" && value !== null && typeof value.viewBox === "string" && Array.isArray(value.nodes);
97
+ }
98
+ function cdnLoader(baseUrl, fetchImpl = fetch) {
99
+ const base = baseUrl.replace(/\/+$/, "");
100
+ return async (name) => {
101
+ const res = await fetchImpl(`${base}/${name}.json`);
102
+ if (!res.ok)
103
+ throw new Error(`mal-icons: failed to load "${name}" (${res.status})`);
104
+ const data = await res.json();
105
+ if (!isIconData(data))
106
+ throw new Error(`mal-icons: malformed icon payload for "${name}"`);
107
+ return data;
108
+ };
109
+ }
110
+ var registry = new Map;
111
+ function registerIcons(icons) {
112
+ for (const [name, data] of Object.entries(icons))
113
+ registry.set(name, data);
114
+ }
115
+ function clearRegistry() {
116
+ registry.clear();
117
+ }
118
+ var NAME_ATTRS = ["size", "color", "title", "class", "weight", "animate"];
119
+ function readProps(el) {
120
+ const props = {};
121
+ const size = el.getAttribute("size");
122
+ if (size !== null)
123
+ props.size = size;
124
+ const color = el.getAttribute("color");
125
+ if (color !== null)
126
+ props.color = color;
127
+ const title = el.getAttribute("title");
128
+ if (title !== null)
129
+ props.title = title;
130
+ const className = el.getAttribute("class");
131
+ if (className !== null)
132
+ props.className = className;
133
+ const weight = el.getAttribute("weight");
134
+ if (weight !== null && weight in import_core.WEIGHT_STROKE_WIDTH) {
135
+ props.weight = weight;
136
+ }
137
+ const animate = el.getAttribute("animate");
138
+ if (animate !== null)
139
+ props.animate = animate;
140
+ return props;
141
+ }
142
+ function defineMalIcon(tagName = "mal-icons", loader) {
143
+ if (typeof customElements === "undefined" || customElements.get(tagName))
144
+ return;
145
+
146
+ class MalIconElement extends HTMLElement {
147
+ static get observedAttributes() {
148
+ return ["name", "src", ...NAME_ATTRS];
149
+ }
150
+ attributeChangedCallback() {
151
+ this.render();
152
+ }
153
+ connectedCallback() {
154
+ this.render();
155
+ }
156
+ mount(data) {
157
+ this.replaceChildren(renderIcon(data, readProps(this)));
158
+ }
159
+ render() {
160
+ const name = this.getAttribute("name");
161
+ if (name && registry.has(name)) {
162
+ const data = registry.get(name);
163
+ if (data)
164
+ this.mount(data);
165
+ return;
166
+ }
167
+ const src = this.getAttribute("src");
168
+ let pending;
169
+ if (src) {
170
+ pending = fetch(src).then(async (res) => {
171
+ if (!res.ok)
172
+ throw new Error(`mal-icons: failed to load "${src}" (${res.status})`);
173
+ const data = await res.json();
174
+ if (!isIconData(data))
175
+ throw new Error(`mal-icons: malformed icon payload at "${src}"`);
176
+ return data;
177
+ });
178
+ } else if (name && loader) {
179
+ pending = loader(name);
180
+ }
181
+ if (!pending)
182
+ return;
183
+ pending.then((data) => this.mount(data)).catch(() => {});
184
+ }
185
+ }
186
+ customElements.define(tagName, MalIconElement);
187
+ }
package/dist/index.js ADDED
@@ -0,0 +1,155 @@
1
+ // packages/web/src/index.ts
2
+ import {
3
+ WEIGHT_STROKE_WIDTH,
4
+ animationClass,
5
+ resolveIconAttrs
6
+ } from "@mal-icons/core";
7
+ import {
8
+ ICON_ANIMATIONS_CSS,
9
+ WEIGHT_STROKE_WIDTH as WEIGHT_STROKE_WIDTH2,
10
+ animationClass as animationClass2
11
+ } from "@mal-icons/core";
12
+ var SVG_NS = "http://www.w3.org/2000/svg";
13
+ function renderIcon(data, props = {}) {
14
+ const { size, color, className } = resolveIconAttrs({ size: props.size, color: props.color, className: props.className }, {});
15
+ const svg = document.createElementNS(SVG_NS, "svg");
16
+ svg.setAttribute("viewBox", data.viewBox);
17
+ svg.setAttribute("stroke", "currentColor");
18
+ svg.setAttribute("fill", "currentColor");
19
+ svg.setAttribute("stroke-width", "0");
20
+ if (data.defaultAttr) {
21
+ for (const [k, v] of Object.entries(data.defaultAttr))
22
+ svg.setAttribute(k, String(v));
23
+ }
24
+ svg.setAttribute("width", String(size));
25
+ svg.setAttribute("height", String(size));
26
+ if (props.weight)
27
+ svg.setAttribute("stroke-width", String(WEIGHT_STROKE_WIDTH[props.weight]));
28
+ const fullClassName = [className, props.animate ? animationClass(props.animate) : undefined].filter(Boolean).join(" ");
29
+ if (fullClassName)
30
+ svg.setAttribute("class", fullClassName);
31
+ if (color)
32
+ svg.style.color = color;
33
+ if (props.secondaryColor)
34
+ svg.style.setProperty("--mal-icons-secondary", props.secondaryColor);
35
+ if (props.title) {
36
+ svg.setAttribute("role", "img");
37
+ const title = document.createElementNS(SVG_NS, "title");
38
+ title.textContent = props.title;
39
+ svg.appendChild(title);
40
+ } else {
41
+ svg.setAttribute("role", "presentation");
42
+ svg.setAttribute("aria-hidden", "true");
43
+ }
44
+ for (const [tag, attr] of data.nodes) {
45
+ const el = document.createElementNS(SVG_NS, tag);
46
+ for (const [k, v] of Object.entries(attr))
47
+ el.setAttribute(k, String(v));
48
+ svg.appendChild(el);
49
+ }
50
+ return svg;
51
+ }
52
+ function isIconData(value) {
53
+ return typeof value === "object" && value !== null && typeof value.viewBox === "string" && Array.isArray(value.nodes);
54
+ }
55
+ function cdnLoader(baseUrl, fetchImpl = fetch) {
56
+ const base = baseUrl.replace(/\/+$/, "");
57
+ return async (name) => {
58
+ const res = await fetchImpl(`${base}/${name}.json`);
59
+ if (!res.ok)
60
+ throw new Error(`mal-icons: failed to load "${name}" (${res.status})`);
61
+ const data = await res.json();
62
+ if (!isIconData(data))
63
+ throw new Error(`mal-icons: malformed icon payload for "${name}"`);
64
+ return data;
65
+ };
66
+ }
67
+ var registry = new Map;
68
+ function registerIcons(icons) {
69
+ for (const [name, data] of Object.entries(icons))
70
+ registry.set(name, data);
71
+ }
72
+ function clearRegistry() {
73
+ registry.clear();
74
+ }
75
+ var NAME_ATTRS = ["size", "color", "title", "class", "weight", "animate"];
76
+ function readProps(el) {
77
+ const props = {};
78
+ const size = el.getAttribute("size");
79
+ if (size !== null)
80
+ props.size = size;
81
+ const color = el.getAttribute("color");
82
+ if (color !== null)
83
+ props.color = color;
84
+ const title = el.getAttribute("title");
85
+ if (title !== null)
86
+ props.title = title;
87
+ const className = el.getAttribute("class");
88
+ if (className !== null)
89
+ props.className = className;
90
+ const weight = el.getAttribute("weight");
91
+ if (weight !== null && weight in WEIGHT_STROKE_WIDTH) {
92
+ props.weight = weight;
93
+ }
94
+ const animate = el.getAttribute("animate");
95
+ if (animate !== null)
96
+ props.animate = animate;
97
+ return props;
98
+ }
99
+ function defineMalIcon(tagName = "mal-icons", loader) {
100
+ if (typeof customElements === "undefined" || customElements.get(tagName))
101
+ return;
102
+
103
+ class MalIconElement extends HTMLElement {
104
+ static get observedAttributes() {
105
+ return ["name", "src", ...NAME_ATTRS];
106
+ }
107
+ attributeChangedCallback() {
108
+ this.render();
109
+ }
110
+ connectedCallback() {
111
+ this.render();
112
+ }
113
+ mount(data) {
114
+ this.replaceChildren(renderIcon(data, readProps(this)));
115
+ }
116
+ render() {
117
+ const name = this.getAttribute("name");
118
+ if (name && registry.has(name)) {
119
+ const data = registry.get(name);
120
+ if (data)
121
+ this.mount(data);
122
+ return;
123
+ }
124
+ const src = this.getAttribute("src");
125
+ let pending;
126
+ if (src) {
127
+ pending = fetch(src).then(async (res) => {
128
+ if (!res.ok)
129
+ throw new Error(`mal-icons: failed to load "${src}" (${res.status})`);
130
+ const data = await res.json();
131
+ if (!isIconData(data))
132
+ throw new Error(`mal-icons: malformed icon payload at "${src}"`);
133
+ return data;
134
+ });
135
+ } else if (name && loader) {
136
+ pending = loader(name);
137
+ }
138
+ if (!pending)
139
+ return;
140
+ pending.then((data) => this.mount(data)).catch(() => {});
141
+ }
142
+ }
143
+ customElements.define(tagName, MalIconElement);
144
+ }
145
+ export {
146
+ renderIcon,
147
+ registerIcons,
148
+ isIconData,
149
+ defineMalIcon,
150
+ clearRegistry,
151
+ cdnLoader,
152
+ animationClass2 as animationClass,
153
+ WEIGHT_STROKE_WIDTH2 as WEIGHT_STROKE_WIDTH,
154
+ ICON_ANIMATIONS_CSS
155
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mal-icons/web",
3
- "version": "0.1.1",
3
+ "version": "0.2.7",
4
4
  "description": "Zero-build web components and icon data for mal-icons.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -36,7 +36,7 @@
36
36
  "./package.json": "./package.json"
37
37
  },
38
38
  "dependencies": {
39
- "@mal-icons/core": "0.1.0"
39
+ "@mal-icons/core": "0.2.7"
40
40
  },
41
41
  "publishConfig": {
42
42
  "access": "public"