@fluxus-ui/icons 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +40 -2
- package/bin/icons-png.mjs +51 -0
- package/dist/index.cjs +969 -0
- package/dist/index.d.cts +260 -0
- package/dist/index.d.ts +260 -0
- package/dist/index.js +881 -0
- package/dist/react.cjs +260 -0
- package/dist/react.d.cts +65 -0
- package/dist/react.d.ts +65 -0
- package/dist/react.js +236 -0
- package/package.json +77 -3
package/dist/react.cjs
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
"use strict";
|
|
3
|
+
"use client";
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
+
|
|
22
|
+
// src/react.tsx
|
|
23
|
+
var react_exports = {};
|
|
24
|
+
__export(react_exports, {
|
|
25
|
+
Icon: () => Icon
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(react_exports);
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
|
|
30
|
+
// src/attach.ts
|
|
31
|
+
var import_base = require("@fluxus-ui/core/base");
|
|
32
|
+
|
|
33
|
+
// src/pose.ts
|
|
34
|
+
var REST = "rest";
|
|
35
|
+
var ICON_SPRINGS = {
|
|
36
|
+
state: { stiffness: 320, damping: 26 },
|
|
37
|
+
push: { stiffness: 180, damping: 12 },
|
|
38
|
+
turn: { stiffness: 120, damping: 20 }
|
|
39
|
+
};
|
|
40
|
+
var poseOf = (part, state, progress) => {
|
|
41
|
+
const p = { ...part.rest, ...state ? part.on?.[state] : void 0 };
|
|
42
|
+
return { d: p.d ?? part.d, x: p.x ?? 0, y: p.y ?? 0, r: p.r ?? 0, s: p.s ?? 1, draw: p.draw === "p" ? progress : p.draw ?? 1, o: p.o ?? 1 };
|
|
43
|
+
};
|
|
44
|
+
var draws = (part) => [part.rest, ...Object.values(part.on ?? {})].some((p) => p?.draw !== void 0);
|
|
45
|
+
var n = (v) => String(+v.toFixed(2));
|
|
46
|
+
var attrsOf = (part, pose) => {
|
|
47
|
+
const [cx, cy] = part.c ?? [8, 8];
|
|
48
|
+
const t = (pose.x || pose.y ? `translate(${n(pose.x)} ${n(pose.y)})` : "") + (pose.r ? `rotate(${n(pose.r)} ${cx} ${cy})` : "") + (pose.s !== 1 ? `translate(${cx} ${cy})scale(${n(pose.s)})translate(${-cx} ${-cy})` : "");
|
|
49
|
+
const hidden = pose.draw < 1e-3 || pose.o < 1e-3;
|
|
50
|
+
return {
|
|
51
|
+
d: pose.d,
|
|
52
|
+
transform: t || null,
|
|
53
|
+
// With round caps a dash of 0 still paints a dot: a part not drawn at all is hidden instead.
|
|
54
|
+
"stroke-dasharray": pose.draw > 0.999 || hidden ? null : `${n(pose.draw)} 2`,
|
|
55
|
+
visibility: hidden ? "hidden" : null,
|
|
56
|
+
opacity: pose.o > 0.999 || hidden ? null : n(pose.o)
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
var partsMarkup = (def, state, progress = 0) => def.parts.map((part) => {
|
|
60
|
+
const attrs = attrsOf(part, poseOf(part, state, progress));
|
|
61
|
+
let out = `<path data-p="${part.id}"`;
|
|
62
|
+
if (draws(part)) out += ' pathLength="1"';
|
|
63
|
+
if (part.fill) out += ' fill="currentColor"';
|
|
64
|
+
for (const [k, v] of Object.entries(attrs)) if (v !== null) out += ` ${k}="${v}"`;
|
|
65
|
+
return `${out}/>`;
|
|
66
|
+
}).join("");
|
|
67
|
+
var parse = (d) => (d.match(/[A-Z]|-?[\d.]+/g) ?? []).map((t) => /[A-Z]/.test(t) ? t : Number(t));
|
|
68
|
+
var mix = (a, b, t) => a.map((v, i) => typeof v === "number" ? n(v + (b[i] - v) * t) : v).join(" ");
|
|
69
|
+
|
|
70
|
+
// src/attach.ts
|
|
71
|
+
var NUMS = ["x", "y", "r", "s", "draw", "o"];
|
|
72
|
+
var CONTROL = "[aria-pressed],[aria-expanded],[aria-checked]";
|
|
73
|
+
var HOST = 'a,button,summary,label,[role="button"],[role="menuitem"],[role="tab"],[role="switch"],[role="checkbox"]';
|
|
74
|
+
var WATCHED = ["aria-pressed", "aria-expanded", "aria-checked"];
|
|
75
|
+
function attachIcon(svg, def, options = {}) {
|
|
76
|
+
const reduced = () => (options.respectReducedMotion ?? true) && (0, import_base.prefersReducedMotion)();
|
|
77
|
+
const control = svg.closest(CONTROL);
|
|
78
|
+
const target = svg.closest(HOST) ?? control ?? svg;
|
|
79
|
+
const stateOf = () => svg.dataset.state ?? (control && WATCHED.some((a) => control.getAttribute(a) === "true") ? "on" : REST);
|
|
80
|
+
const progressOf = () => Math.min(1, Math.max(0, Number(svg.dataset.progress) || 0));
|
|
81
|
+
let state = stateOf();
|
|
82
|
+
let ends = [state, state];
|
|
83
|
+
let aim = 1;
|
|
84
|
+
let g = 1;
|
|
85
|
+
let motion = null;
|
|
86
|
+
const timers = /* @__PURE__ */ new Set();
|
|
87
|
+
const lives = [];
|
|
88
|
+
for (const part of def.parts) {
|
|
89
|
+
const el = svg.querySelector(`[data-p="${part.id}"]`);
|
|
90
|
+
if (!el) continue;
|
|
91
|
+
const pose = poseOf(part, state, progressOf());
|
|
92
|
+
lives.push({ part, el, from: pose, to: pose, off: { x: 0, y: 0, r: 0, s: 0 }, pushes: {} });
|
|
93
|
+
}
|
|
94
|
+
const now = (live) => {
|
|
95
|
+
const lag = live.part.lag ?? 0;
|
|
96
|
+
const t = lag ? Math.max(0, (g - lag) / (1 - lag)) : g;
|
|
97
|
+
const { from, to } = live;
|
|
98
|
+
const pose = { ...to, d: from.d === to.d ? to.d : t >= 1 ? to.d : t <= 0 ? from.d : mix(parse(from.d), parse(to.d), t) };
|
|
99
|
+
for (const k of NUMS) pose[k] = from[k] + (to[k] - from[k]) * t;
|
|
100
|
+
pose.draw = Math.min(1, Math.max(0, pose.draw));
|
|
101
|
+
pose.o = Math.min(1, Math.max(0, pose.o));
|
|
102
|
+
return pose;
|
|
103
|
+
};
|
|
104
|
+
const paint = () => {
|
|
105
|
+
for (const live of lives) {
|
|
106
|
+
const pose = now(live);
|
|
107
|
+
const { off } = live;
|
|
108
|
+
const attrs = attrsOf(live.part, { ...pose, x: pose.x + off.x, y: pose.y + off.y, r: pose.r + off.r, s: pose.s + off.s });
|
|
109
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
110
|
+
if (v === null) live.el.removeAttribute(k);
|
|
111
|
+
else if (live.el.getAttribute(k) !== v) live.el.setAttribute(k, v);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const settle = () => {
|
|
116
|
+
for (const live of lives) live.from = live.to = aim ? live.to : live.from;
|
|
117
|
+
ends = [state, state];
|
|
118
|
+
aim = 1;
|
|
119
|
+
g = 1;
|
|
120
|
+
paint();
|
|
121
|
+
};
|
|
122
|
+
const go = (next, force = false) => {
|
|
123
|
+
if (next === state && !force) return;
|
|
124
|
+
const running = motion !== null && !motion.done;
|
|
125
|
+
if (reduced()) {
|
|
126
|
+
motion?.stop();
|
|
127
|
+
motion = null;
|
|
128
|
+
state = next;
|
|
129
|
+
aim = 1;
|
|
130
|
+
for (const live of lives) live.to = poseOf(live.part, next, progressOf());
|
|
131
|
+
settle();
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (running && !force && next === ends[1 - aim]) {
|
|
135
|
+
aim = aim ? 0 : 1;
|
|
136
|
+
state = next;
|
|
137
|
+
motion.retarget(aim);
|
|
138
|
+
} else {
|
|
139
|
+
for (const live of lives) {
|
|
140
|
+
live.from = now(live);
|
|
141
|
+
live.to = poseOf(live.part, next, progressOf());
|
|
142
|
+
}
|
|
143
|
+
motion?.stop();
|
|
144
|
+
ends = [running || force ? null : state, next];
|
|
145
|
+
state = next;
|
|
146
|
+
aim = 1;
|
|
147
|
+
g = 0;
|
|
148
|
+
motion = (0, import_base.spring)({
|
|
149
|
+
from: 0,
|
|
150
|
+
to: 1,
|
|
151
|
+
...ICON_SPRINGS.state,
|
|
152
|
+
restDelta: 2e-3,
|
|
153
|
+
onUpdate: (v) => {
|
|
154
|
+
g = v;
|
|
155
|
+
paint();
|
|
156
|
+
},
|
|
157
|
+
onRest: settle
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
if (!force) kick(next);
|
|
161
|
+
};
|
|
162
|
+
const kick = (name) => {
|
|
163
|
+
if (reduced()) return;
|
|
164
|
+
for (const k of def.kicks?.[name] ?? []) {
|
|
165
|
+
const start = () => {
|
|
166
|
+
for (const live of lives) {
|
|
167
|
+
if (k.p !== "*" && k.p !== live.part.id) continue;
|
|
168
|
+
const old = live.pushes[k.k];
|
|
169
|
+
const carried = old && !old.done ? old.velocity : 0;
|
|
170
|
+
old?.stop();
|
|
171
|
+
const to = k.to ?? 0;
|
|
172
|
+
live.pushes[k.k] = (0, import_base.spring)({
|
|
173
|
+
from: live.off[k.k],
|
|
174
|
+
to,
|
|
175
|
+
velocity: carried + (k.v ?? 0),
|
|
176
|
+
...to ? ICON_SPRINGS.turn : ICON_SPRINGS.push,
|
|
177
|
+
restDelta: k.k === "s" ? 2e-3 : 0.05,
|
|
178
|
+
restSpeed: 0.05,
|
|
179
|
+
onUpdate: (v) => {
|
|
180
|
+
live.off[k.k] = v;
|
|
181
|
+
if (!motion || motion.done) paint();
|
|
182
|
+
},
|
|
183
|
+
onRest: () => {
|
|
184
|
+
live.off[k.k] = 0;
|
|
185
|
+
paint();
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
if (k.wait) {
|
|
191
|
+
const timer = window.setTimeout(() => {
|
|
192
|
+
timers.delete(timer);
|
|
193
|
+
start();
|
|
194
|
+
}, k.wait);
|
|
195
|
+
timers.add(timer);
|
|
196
|
+
} else start();
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
paint();
|
|
200
|
+
const watcher = new MutationObserver((records) => {
|
|
201
|
+
go(stateOf(), records.some((r) => r.attributeName === "data-progress"));
|
|
202
|
+
});
|
|
203
|
+
watcher.observe(svg, { attributes: true, attributeFilter: ["data-state", "data-progress"] });
|
|
204
|
+
if (control) watcher.observe(control, { attributes: true, attributeFilter: WATCHED });
|
|
205
|
+
const onEnter = (e) => {
|
|
206
|
+
if (e.pointerType === "mouse") kick("hover");
|
|
207
|
+
};
|
|
208
|
+
const onClick = () => kick("press");
|
|
209
|
+
target.addEventListener("pointerenter", onEnter);
|
|
210
|
+
target.addEventListener("click", onClick);
|
|
211
|
+
return {
|
|
212
|
+
kick,
|
|
213
|
+
destroy() {
|
|
214
|
+
watcher.disconnect();
|
|
215
|
+
target.removeEventListener("pointerenter", onEnter);
|
|
216
|
+
target.removeEventListener("click", onClick);
|
|
217
|
+
motion?.stop();
|
|
218
|
+
for (const timer of timers) clearTimeout(timer);
|
|
219
|
+
for (const live of lives) {
|
|
220
|
+
for (const push of Object.values(live.pushes)) push.stop();
|
|
221
|
+
live.off = { x: 0, y: 0, r: 0, s: 0 };
|
|
222
|
+
}
|
|
223
|
+
settle();
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// src/react.tsx
|
|
229
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
230
|
+
function Icon({ icon, state, progress, label, className, ...rest }) {
|
|
231
|
+
const ref = (0, import_react.useRef)(null);
|
|
232
|
+
const first = (0, import_react.useRef)({ state, progress }).current;
|
|
233
|
+
const parts = (0, import_react.useMemo)(() => ({ __html: partsMarkup(icon, first.state, first.progress) }), [icon]);
|
|
234
|
+
(0, import_react.useEffect)(() => attachIcon(ref.current, icon).destroy, [icon]);
|
|
235
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
236
|
+
"svg",
|
|
237
|
+
{
|
|
238
|
+
ref,
|
|
239
|
+
className: className ? `fx-i ${className}` : "fx-i",
|
|
240
|
+
"data-fx-icon": icon.name,
|
|
241
|
+
"data-state": state,
|
|
242
|
+
"data-progress": progress,
|
|
243
|
+
viewBox: "0 0 16 16",
|
|
244
|
+
width: "1em",
|
|
245
|
+
height: "1em",
|
|
246
|
+
fill: "none",
|
|
247
|
+
stroke: "currentColor",
|
|
248
|
+
strokeWidth: "1.5",
|
|
249
|
+
strokeLinecap: "round",
|
|
250
|
+
strokeLinejoin: "round",
|
|
251
|
+
...label ? { role: "img", "aria-label": label } : { "aria-hidden": true },
|
|
252
|
+
...rest,
|
|
253
|
+
dangerouslySetInnerHTML: parts
|
|
254
|
+
}
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
258
|
+
0 && (module.exports = {
|
|
259
|
+
Icon
|
|
260
|
+
});
|
package/dist/react.d.cts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ComponentPropsWithoutRef } from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What an icon is, and what it looks like in a state. No DOM here: the server,
|
|
6
|
+
* the build and the engine all read an icon through these. (SPEC §14.36)
|
|
7
|
+
*/
|
|
8
|
+
interface Pose {
|
|
9
|
+
/** Another outline with the same commands: the part morphs into it. */
|
|
10
|
+
d?: string;
|
|
11
|
+
x?: number;
|
|
12
|
+
y?: number;
|
|
13
|
+
/** Degrees, around the pivot of the part. */
|
|
14
|
+
r?: number;
|
|
15
|
+
s?: number;
|
|
16
|
+
/** How much of the stroke is drawn, 0–1; 'p' follows the progress of the icon. */
|
|
17
|
+
draw?: number | 'p';
|
|
18
|
+
o?: number;
|
|
19
|
+
}
|
|
20
|
+
interface IconPart {
|
|
21
|
+
id: string;
|
|
22
|
+
/** Its outline at rest, on a 16 × 16 grid: absolute commands, numbers apart by single spaces (`M3 4.5 L13 4.5`). */
|
|
23
|
+
d: string;
|
|
24
|
+
/** The pivot of `r` and `s`. Default: the centre of the grid. */
|
|
25
|
+
c?: readonly [number, number];
|
|
26
|
+
/** It sets off this much later than the rest, 0–0.5 of the way. */
|
|
27
|
+
lag?: number;
|
|
28
|
+
/** Filled as well as stroked. */
|
|
29
|
+
fill?: boolean;
|
|
30
|
+
/** Its pose at rest, when it is not the plain one. */
|
|
31
|
+
rest?: Pose;
|
|
32
|
+
/** Its pose in each other state, over the one at rest. */
|
|
33
|
+
on?: Readonly<Record<string, Pose>>;
|
|
34
|
+
}
|
|
35
|
+
interface Kick {
|
|
36
|
+
/** The part it pushes; '*' is all of them. */
|
|
37
|
+
p: string;
|
|
38
|
+
k: 'x' | 'y' | 'r' | 's';
|
|
39
|
+
/** Speed it starts with, units per second. */
|
|
40
|
+
v?: number;
|
|
41
|
+
/** Where it ends; it must look like 0 (a full turn). Default: 0. */
|
|
42
|
+
to?: number;
|
|
43
|
+
/** It waits this long, ms. */
|
|
44
|
+
wait?: number;
|
|
45
|
+
}
|
|
46
|
+
interface IconDef {
|
|
47
|
+
name: string;
|
|
48
|
+
parts: readonly IconPart[];
|
|
49
|
+
/** Pushes by what sets them off: 'hover', 'press' or the name of a state it enters. */
|
|
50
|
+
kicks?: Readonly<Record<string, readonly Kick[]>>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface IconProps extends Omit<ComponentPropsWithoutRef<'svg'>, 'children'> {
|
|
54
|
+
icon: IconDef;
|
|
55
|
+
/** Its state; without it, the control around it says (aria-pressed, aria-expanded, aria-checked). */
|
|
56
|
+
state?: string;
|
|
57
|
+
/** 0–1, for the parts that follow it. */
|
|
58
|
+
progress?: number;
|
|
59
|
+
/** Its name for a screen reader; without it the icon is decoration. */
|
|
60
|
+
label?: string;
|
|
61
|
+
}
|
|
62
|
+
/** A live icon (SPEC §14.36). React paints it once, standing still; the engine moves it from then on. */
|
|
63
|
+
declare function Icon({ icon, state, progress, label, className, ...rest }: IconProps): react.JSX.Element;
|
|
64
|
+
|
|
65
|
+
export { Icon, type IconProps };
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ComponentPropsWithoutRef } from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What an icon is, and what it looks like in a state. No DOM here: the server,
|
|
6
|
+
* the build and the engine all read an icon through these. (SPEC §14.36)
|
|
7
|
+
*/
|
|
8
|
+
interface Pose {
|
|
9
|
+
/** Another outline with the same commands: the part morphs into it. */
|
|
10
|
+
d?: string;
|
|
11
|
+
x?: number;
|
|
12
|
+
y?: number;
|
|
13
|
+
/** Degrees, around the pivot of the part. */
|
|
14
|
+
r?: number;
|
|
15
|
+
s?: number;
|
|
16
|
+
/** How much of the stroke is drawn, 0–1; 'p' follows the progress of the icon. */
|
|
17
|
+
draw?: number | 'p';
|
|
18
|
+
o?: number;
|
|
19
|
+
}
|
|
20
|
+
interface IconPart {
|
|
21
|
+
id: string;
|
|
22
|
+
/** Its outline at rest, on a 16 × 16 grid: absolute commands, numbers apart by single spaces (`M3 4.5 L13 4.5`). */
|
|
23
|
+
d: string;
|
|
24
|
+
/** The pivot of `r` and `s`. Default: the centre of the grid. */
|
|
25
|
+
c?: readonly [number, number];
|
|
26
|
+
/** It sets off this much later than the rest, 0–0.5 of the way. */
|
|
27
|
+
lag?: number;
|
|
28
|
+
/** Filled as well as stroked. */
|
|
29
|
+
fill?: boolean;
|
|
30
|
+
/** Its pose at rest, when it is not the plain one. */
|
|
31
|
+
rest?: Pose;
|
|
32
|
+
/** Its pose in each other state, over the one at rest. */
|
|
33
|
+
on?: Readonly<Record<string, Pose>>;
|
|
34
|
+
}
|
|
35
|
+
interface Kick {
|
|
36
|
+
/** The part it pushes; '*' is all of them. */
|
|
37
|
+
p: string;
|
|
38
|
+
k: 'x' | 'y' | 'r' | 's';
|
|
39
|
+
/** Speed it starts with, units per second. */
|
|
40
|
+
v?: number;
|
|
41
|
+
/** Where it ends; it must look like 0 (a full turn). Default: 0. */
|
|
42
|
+
to?: number;
|
|
43
|
+
/** It waits this long, ms. */
|
|
44
|
+
wait?: number;
|
|
45
|
+
}
|
|
46
|
+
interface IconDef {
|
|
47
|
+
name: string;
|
|
48
|
+
parts: readonly IconPart[];
|
|
49
|
+
/** Pushes by what sets them off: 'hover', 'press' or the name of a state it enters. */
|
|
50
|
+
kicks?: Readonly<Record<string, readonly Kick[]>>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface IconProps extends Omit<ComponentPropsWithoutRef<'svg'>, 'children'> {
|
|
54
|
+
icon: IconDef;
|
|
55
|
+
/** Its state; without it, the control around it says (aria-pressed, aria-expanded, aria-checked). */
|
|
56
|
+
state?: string;
|
|
57
|
+
/** 0–1, for the parts that follow it. */
|
|
58
|
+
progress?: number;
|
|
59
|
+
/** Its name for a screen reader; without it the icon is decoration. */
|
|
60
|
+
label?: string;
|
|
61
|
+
}
|
|
62
|
+
/** A live icon (SPEC §14.36). React paints it once, standing still; the engine moves it from then on. */
|
|
63
|
+
declare function Icon({ icon, state, progress, label, className, ...rest }: IconProps): react.JSX.Element;
|
|
64
|
+
|
|
65
|
+
export { Icon, type IconProps };
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
"use client";
|
|
3
|
+
|
|
4
|
+
// src/react.tsx
|
|
5
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
6
|
+
|
|
7
|
+
// src/attach.ts
|
|
8
|
+
import { prefersReducedMotion, spring } from "@fluxus-ui/core/base";
|
|
9
|
+
|
|
10
|
+
// src/pose.ts
|
|
11
|
+
var REST = "rest";
|
|
12
|
+
var ICON_SPRINGS = {
|
|
13
|
+
state: { stiffness: 320, damping: 26 },
|
|
14
|
+
push: { stiffness: 180, damping: 12 },
|
|
15
|
+
turn: { stiffness: 120, damping: 20 }
|
|
16
|
+
};
|
|
17
|
+
var poseOf = (part, state, progress) => {
|
|
18
|
+
const p = { ...part.rest, ...state ? part.on?.[state] : void 0 };
|
|
19
|
+
return { d: p.d ?? part.d, x: p.x ?? 0, y: p.y ?? 0, r: p.r ?? 0, s: p.s ?? 1, draw: p.draw === "p" ? progress : p.draw ?? 1, o: p.o ?? 1 };
|
|
20
|
+
};
|
|
21
|
+
var draws = (part) => [part.rest, ...Object.values(part.on ?? {})].some((p) => p?.draw !== void 0);
|
|
22
|
+
var n = (v) => String(+v.toFixed(2));
|
|
23
|
+
var attrsOf = (part, pose) => {
|
|
24
|
+
const [cx, cy] = part.c ?? [8, 8];
|
|
25
|
+
const t = (pose.x || pose.y ? `translate(${n(pose.x)} ${n(pose.y)})` : "") + (pose.r ? `rotate(${n(pose.r)} ${cx} ${cy})` : "") + (pose.s !== 1 ? `translate(${cx} ${cy})scale(${n(pose.s)})translate(${-cx} ${-cy})` : "");
|
|
26
|
+
const hidden = pose.draw < 1e-3 || pose.o < 1e-3;
|
|
27
|
+
return {
|
|
28
|
+
d: pose.d,
|
|
29
|
+
transform: t || null,
|
|
30
|
+
// With round caps a dash of 0 still paints a dot: a part not drawn at all is hidden instead.
|
|
31
|
+
"stroke-dasharray": pose.draw > 0.999 || hidden ? null : `${n(pose.draw)} 2`,
|
|
32
|
+
visibility: hidden ? "hidden" : null,
|
|
33
|
+
opacity: pose.o > 0.999 || hidden ? null : n(pose.o)
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
var partsMarkup = (def, state, progress = 0) => def.parts.map((part) => {
|
|
37
|
+
const attrs = attrsOf(part, poseOf(part, state, progress));
|
|
38
|
+
let out = `<path data-p="${part.id}"`;
|
|
39
|
+
if (draws(part)) out += ' pathLength="1"';
|
|
40
|
+
if (part.fill) out += ' fill="currentColor"';
|
|
41
|
+
for (const [k, v] of Object.entries(attrs)) if (v !== null) out += ` ${k}="${v}"`;
|
|
42
|
+
return `${out}/>`;
|
|
43
|
+
}).join("");
|
|
44
|
+
var parse = (d) => (d.match(/[A-Z]|-?[\d.]+/g) ?? []).map((t) => /[A-Z]/.test(t) ? t : Number(t));
|
|
45
|
+
var mix = (a, b, t) => a.map((v, i) => typeof v === "number" ? n(v + (b[i] - v) * t) : v).join(" ");
|
|
46
|
+
|
|
47
|
+
// src/attach.ts
|
|
48
|
+
var NUMS = ["x", "y", "r", "s", "draw", "o"];
|
|
49
|
+
var CONTROL = "[aria-pressed],[aria-expanded],[aria-checked]";
|
|
50
|
+
var HOST = 'a,button,summary,label,[role="button"],[role="menuitem"],[role="tab"],[role="switch"],[role="checkbox"]';
|
|
51
|
+
var WATCHED = ["aria-pressed", "aria-expanded", "aria-checked"];
|
|
52
|
+
function attachIcon(svg, def, options = {}) {
|
|
53
|
+
const reduced = () => (options.respectReducedMotion ?? true) && prefersReducedMotion();
|
|
54
|
+
const control = svg.closest(CONTROL);
|
|
55
|
+
const target = svg.closest(HOST) ?? control ?? svg;
|
|
56
|
+
const stateOf = () => svg.dataset.state ?? (control && WATCHED.some((a) => control.getAttribute(a) === "true") ? "on" : REST);
|
|
57
|
+
const progressOf = () => Math.min(1, Math.max(0, Number(svg.dataset.progress) || 0));
|
|
58
|
+
let state = stateOf();
|
|
59
|
+
let ends = [state, state];
|
|
60
|
+
let aim = 1;
|
|
61
|
+
let g = 1;
|
|
62
|
+
let motion = null;
|
|
63
|
+
const timers = /* @__PURE__ */ new Set();
|
|
64
|
+
const lives = [];
|
|
65
|
+
for (const part of def.parts) {
|
|
66
|
+
const el = svg.querySelector(`[data-p="${part.id}"]`);
|
|
67
|
+
if (!el) continue;
|
|
68
|
+
const pose = poseOf(part, state, progressOf());
|
|
69
|
+
lives.push({ part, el, from: pose, to: pose, off: { x: 0, y: 0, r: 0, s: 0 }, pushes: {} });
|
|
70
|
+
}
|
|
71
|
+
const now = (live) => {
|
|
72
|
+
const lag = live.part.lag ?? 0;
|
|
73
|
+
const t = lag ? Math.max(0, (g - lag) / (1 - lag)) : g;
|
|
74
|
+
const { from, to } = live;
|
|
75
|
+
const pose = { ...to, d: from.d === to.d ? to.d : t >= 1 ? to.d : t <= 0 ? from.d : mix(parse(from.d), parse(to.d), t) };
|
|
76
|
+
for (const k of NUMS) pose[k] = from[k] + (to[k] - from[k]) * t;
|
|
77
|
+
pose.draw = Math.min(1, Math.max(0, pose.draw));
|
|
78
|
+
pose.o = Math.min(1, Math.max(0, pose.o));
|
|
79
|
+
return pose;
|
|
80
|
+
};
|
|
81
|
+
const paint = () => {
|
|
82
|
+
for (const live of lives) {
|
|
83
|
+
const pose = now(live);
|
|
84
|
+
const { off } = live;
|
|
85
|
+
const attrs = attrsOf(live.part, { ...pose, x: pose.x + off.x, y: pose.y + off.y, r: pose.r + off.r, s: pose.s + off.s });
|
|
86
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
87
|
+
if (v === null) live.el.removeAttribute(k);
|
|
88
|
+
else if (live.el.getAttribute(k) !== v) live.el.setAttribute(k, v);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const settle = () => {
|
|
93
|
+
for (const live of lives) live.from = live.to = aim ? live.to : live.from;
|
|
94
|
+
ends = [state, state];
|
|
95
|
+
aim = 1;
|
|
96
|
+
g = 1;
|
|
97
|
+
paint();
|
|
98
|
+
};
|
|
99
|
+
const go = (next, force = false) => {
|
|
100
|
+
if (next === state && !force) return;
|
|
101
|
+
const running = motion !== null && !motion.done;
|
|
102
|
+
if (reduced()) {
|
|
103
|
+
motion?.stop();
|
|
104
|
+
motion = null;
|
|
105
|
+
state = next;
|
|
106
|
+
aim = 1;
|
|
107
|
+
for (const live of lives) live.to = poseOf(live.part, next, progressOf());
|
|
108
|
+
settle();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (running && !force && next === ends[1 - aim]) {
|
|
112
|
+
aim = aim ? 0 : 1;
|
|
113
|
+
state = next;
|
|
114
|
+
motion.retarget(aim);
|
|
115
|
+
} else {
|
|
116
|
+
for (const live of lives) {
|
|
117
|
+
live.from = now(live);
|
|
118
|
+
live.to = poseOf(live.part, next, progressOf());
|
|
119
|
+
}
|
|
120
|
+
motion?.stop();
|
|
121
|
+
ends = [running || force ? null : state, next];
|
|
122
|
+
state = next;
|
|
123
|
+
aim = 1;
|
|
124
|
+
g = 0;
|
|
125
|
+
motion = spring({
|
|
126
|
+
from: 0,
|
|
127
|
+
to: 1,
|
|
128
|
+
...ICON_SPRINGS.state,
|
|
129
|
+
restDelta: 2e-3,
|
|
130
|
+
onUpdate: (v) => {
|
|
131
|
+
g = v;
|
|
132
|
+
paint();
|
|
133
|
+
},
|
|
134
|
+
onRest: settle
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
if (!force) kick(next);
|
|
138
|
+
};
|
|
139
|
+
const kick = (name) => {
|
|
140
|
+
if (reduced()) return;
|
|
141
|
+
for (const k of def.kicks?.[name] ?? []) {
|
|
142
|
+
const start = () => {
|
|
143
|
+
for (const live of lives) {
|
|
144
|
+
if (k.p !== "*" && k.p !== live.part.id) continue;
|
|
145
|
+
const old = live.pushes[k.k];
|
|
146
|
+
const carried = old && !old.done ? old.velocity : 0;
|
|
147
|
+
old?.stop();
|
|
148
|
+
const to = k.to ?? 0;
|
|
149
|
+
live.pushes[k.k] = spring({
|
|
150
|
+
from: live.off[k.k],
|
|
151
|
+
to,
|
|
152
|
+
velocity: carried + (k.v ?? 0),
|
|
153
|
+
...to ? ICON_SPRINGS.turn : ICON_SPRINGS.push,
|
|
154
|
+
restDelta: k.k === "s" ? 2e-3 : 0.05,
|
|
155
|
+
restSpeed: 0.05,
|
|
156
|
+
onUpdate: (v) => {
|
|
157
|
+
live.off[k.k] = v;
|
|
158
|
+
if (!motion || motion.done) paint();
|
|
159
|
+
},
|
|
160
|
+
onRest: () => {
|
|
161
|
+
live.off[k.k] = 0;
|
|
162
|
+
paint();
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
if (k.wait) {
|
|
168
|
+
const timer = window.setTimeout(() => {
|
|
169
|
+
timers.delete(timer);
|
|
170
|
+
start();
|
|
171
|
+
}, k.wait);
|
|
172
|
+
timers.add(timer);
|
|
173
|
+
} else start();
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
paint();
|
|
177
|
+
const watcher = new MutationObserver((records) => {
|
|
178
|
+
go(stateOf(), records.some((r) => r.attributeName === "data-progress"));
|
|
179
|
+
});
|
|
180
|
+
watcher.observe(svg, { attributes: true, attributeFilter: ["data-state", "data-progress"] });
|
|
181
|
+
if (control) watcher.observe(control, { attributes: true, attributeFilter: WATCHED });
|
|
182
|
+
const onEnter = (e) => {
|
|
183
|
+
if (e.pointerType === "mouse") kick("hover");
|
|
184
|
+
};
|
|
185
|
+
const onClick = () => kick("press");
|
|
186
|
+
target.addEventListener("pointerenter", onEnter);
|
|
187
|
+
target.addEventListener("click", onClick);
|
|
188
|
+
return {
|
|
189
|
+
kick,
|
|
190
|
+
destroy() {
|
|
191
|
+
watcher.disconnect();
|
|
192
|
+
target.removeEventListener("pointerenter", onEnter);
|
|
193
|
+
target.removeEventListener("click", onClick);
|
|
194
|
+
motion?.stop();
|
|
195
|
+
for (const timer of timers) clearTimeout(timer);
|
|
196
|
+
for (const live of lives) {
|
|
197
|
+
for (const push of Object.values(live.pushes)) push.stop();
|
|
198
|
+
live.off = { x: 0, y: 0, r: 0, s: 0 };
|
|
199
|
+
}
|
|
200
|
+
settle();
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// src/react.tsx
|
|
206
|
+
import { jsx } from "react/jsx-runtime";
|
|
207
|
+
function Icon({ icon, state, progress, label, className, ...rest }) {
|
|
208
|
+
const ref = useRef(null);
|
|
209
|
+
const first = useRef({ state, progress }).current;
|
|
210
|
+
const parts = useMemo(() => ({ __html: partsMarkup(icon, first.state, first.progress) }), [icon]);
|
|
211
|
+
useEffect(() => attachIcon(ref.current, icon).destroy, [icon]);
|
|
212
|
+
return /* @__PURE__ */ jsx(
|
|
213
|
+
"svg",
|
|
214
|
+
{
|
|
215
|
+
ref,
|
|
216
|
+
className: className ? `fx-i ${className}` : "fx-i",
|
|
217
|
+
"data-fx-icon": icon.name,
|
|
218
|
+
"data-state": state,
|
|
219
|
+
"data-progress": progress,
|
|
220
|
+
viewBox: "0 0 16 16",
|
|
221
|
+
width: "1em",
|
|
222
|
+
height: "1em",
|
|
223
|
+
fill: "none",
|
|
224
|
+
stroke: "currentColor",
|
|
225
|
+
strokeWidth: "1.5",
|
|
226
|
+
strokeLinecap: "round",
|
|
227
|
+
strokeLinejoin: "round",
|
|
228
|
+
...label ? { role: "img", "aria-label": label } : { "aria-hidden": true },
|
|
229
|
+
...rest,
|
|
230
|
+
dangerouslySetInnerHTML: parts
|
|
231
|
+
}
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
export {
|
|
235
|
+
Icon
|
|
236
|
+
};
|