morphico 0.3.0 → 0.5.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.
Files changed (73) hide show
  1. package/README.md +98 -41
  2. package/dist/esm/all.js +50 -0
  3. package/dist/esm/clip.js +283 -0
  4. package/dist/esm/engine.js +25 -0
  5. package/dist/esm/face.js +153 -0
  6. package/dist/esm/geometry.js +459 -0
  7. package/dist/esm/icon.js +41 -0
  8. package/dist/esm/index.js +50 -0
  9. package/dist/esm/morph.js +107 -0
  10. package/dist/esm/roundness.js +37 -0
  11. package/dist/esm/sets/battery.js +87 -0
  12. package/dist/esm/sets/bell.js +101 -0
  13. package/dist/esm/sets/characters.js +43 -0
  14. package/dist/esm/sets/check-mark.js +74 -0
  15. package/dist/esm/sets/checkbox.js +91 -0
  16. package/dist/esm/sets/chevron.js +70 -0
  17. package/dist/esm/sets/condensed.js +137 -0
  18. package/dist/esm/sets/copy.js +87 -0
  19. package/dist/esm/sets/eye.js +105 -0
  20. package/dist/esm/sets/faces.js +482 -0
  21. package/dist/esm/sets/folder.js +112 -0
  22. package/dist/esm/sets/handwritten.js +136 -0
  23. package/dist/esm/sets/lock.js +75 -0
  24. package/dist/esm/sets/lucide.js +66 -0
  25. package/dist/esm/sets/media.js +109 -0
  26. package/dist/esm/sets/menu.js +84 -0
  27. package/dist/esm/sets/mic.js +102 -0
  28. package/dist/esm/sets/radio.js +84 -0
  29. package/dist/esm/sets/round.js +244 -0
  30. package/dist/esm/sets/shapes.js +455 -0
  31. package/dist/esm/sets/slim.js +148 -0
  32. package/dist/esm/sets/square.js +149 -0
  33. package/dist/esm/sets/theme.js +107 -0
  34. package/dist/esm/sets/toggle.js +73 -0
  35. package/dist/esm/sets/volume.js +89 -0
  36. package/dist/esm/sets/weather.js +691 -0
  37. package/dist/esm/sets/wifi.js +68 -0
  38. package/dist/esm/svg.js +31 -0
  39. package/dist/esm/text.js +88 -0
  40. package/dist/esm/types.js +3 -0
  41. package/dist/morphico.js +4033 -3380
  42. package/dist/types/{iconSets.d.ts → all.d.ts} +0 -2
  43. package/dist/types/clip.d.ts +6 -11
  44. package/dist/types/face.d.ts +27 -0
  45. package/dist/types/icon.d.ts +4 -9
  46. package/dist/types/index.d.ts +5 -3
  47. package/dist/types/roundness.d.ts +8 -0
  48. package/dist/types/sets/battery.d.ts +1 -1
  49. package/dist/types/sets/bell.d.ts +1 -1
  50. package/dist/types/sets/checkbox.d.ts +1 -1
  51. package/dist/types/sets/chevron.d.ts +1 -1
  52. package/dist/types/sets/condensed.d.ts +1 -1
  53. package/dist/types/sets/copy.d.ts +1 -1
  54. package/dist/types/sets/eye.d.ts +1 -1
  55. package/dist/types/sets/faces.d.ts +25 -0
  56. package/dist/types/sets/folder.d.ts +1 -1
  57. package/dist/types/sets/handwritten.d.ts +1 -1
  58. package/dist/types/sets/lock.d.ts +1 -1
  59. package/dist/types/sets/media.d.ts +1 -1
  60. package/dist/types/sets/menu.d.ts +1 -1
  61. package/dist/types/sets/mic.d.ts +1 -1
  62. package/dist/types/sets/radio.d.ts +1 -1
  63. package/dist/types/sets/round.d.ts +1 -1
  64. package/dist/types/sets/slim.d.ts +1 -1
  65. package/dist/types/sets/square.d.ts +1 -1
  66. package/dist/types/sets/theme.d.ts +1 -1
  67. package/dist/types/sets/toggle.d.ts +1 -1
  68. package/dist/types/sets/volume.d.ts +1 -1
  69. package/dist/types/sets/weather.d.ts +1 -1
  70. package/dist/types/sets/wifi.d.ts +1 -1
  71. package/dist/types/text.d.ts +8 -13
  72. package/package.json +95 -3
  73. package/dist/morphico.mjs +0 -4003
@@ -0,0 +1,68 @@
1
+ /*! morphico v0.5.0 | code: MIT License, Copyright (c) 2026 Raycho Ivanov (see LICENSE)
2
+ * Icons based on Lucide (https://lucide.dev), some of them from Feather (https://feathericons.com):
3
+ *
4
+ * ISC License
5
+ *
6
+ * Copyright (c) 2026 Lucide Icons and Contributors
7
+ *
8
+ * Permission to use, copy, modify, and/or distribute this software for any
9
+ * purpose with or without fee is hereby granted, provided that the above
10
+ * copyright notice and this permission notice appear in all copies.
11
+ *
12
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
+ *
20
+ * The MIT License (MIT) (for the Lucide icons derived from Feather, listed in LICENSE-lucide)
21
+ *
22
+ * Copyright (c) 2013-present Cole Bemis
23
+ *
24
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
25
+ * of this software and associated documentation files (the "Software"), to deal
26
+ * in the Software without restriction, including without limitation the rights
27
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28
+ * copies of the Software, and to permit persons to whom the Software is
29
+ * furnished to do so, subject to the following conditions:
30
+ *
31
+ * The above copyright notice and this permission notice shall be included in all
32
+ * copies or substantial portions of the Software.
33
+ *
34
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40
+ * SOFTWARE.
41
+ */
42
+ import { LUCIDE_STROKE_WIDTH, lucideArc, lucideLine, lucidePoint } from './lucide.js';
43
+ const STATES = ['none', 'low', 'medium', 'high'];
44
+ const DOT = [lucideLine(12, 20, 12.01, 20)];
45
+ /** The arcs, small to big, each clockwise from its left end. */
46
+ const ARCS = [
47
+ [lucideArc(12, 19.9997, 5, -134.427, -45.573)],
48
+ [lucideArc(12, 20.0004, 10, -134.427, -45.573)],
49
+ [lucideArc(12, 20.0003, 15, -131.8103, -48.1897)],
50
+ ];
51
+ const NO_ARC = lucidePoint(12, 20);
52
+ /** Arc `arcIndex` in each state: itself, else the outermost arc that state has (or a point on the dot). */
53
+ const arcPiece = (arcIndex) => ({
54
+ strokes: Object.fromEntries(STATES.map((state, level) => [state, level > arcIndex ? ARCS[arcIndex] : level > 0 ? ARCS[level - 1] : NO_ARC])),
55
+ alternativeStrokes: {},
56
+ pairLayouts: {},
57
+ });
58
+ export const iconsWifi = {
59
+ segmentCount: 4,
60
+ strokeWidth: LUCIDE_STROKE_WIDTH,
61
+ lineCap: 'round',
62
+ lineJoin: 'round',
63
+ strokes: Object.fromEntries(STATES.map((state) => [state, DOT])),
64
+ alternativeStrokes: {},
65
+ pairLayouts: {},
66
+ morePieces: [0, 1, 2].map(arcPiece),
67
+ spins: {},
68
+ };
@@ -0,0 +1,31 @@
1
+ /*! morphico v0.5.0 | MIT License, Copyright (c) 2026 Raycho Ivanov (see LICENSE) */
2
+ const SVG_NAMESPACE = 'http://www.w3.org/2000/svg';
3
+ /** A CSS length as stroke width: drawn at that width on screen instead of scaling with the icon. */
4
+ export const hasFixedStrokeWidth = (style) => typeof style.strokeWidth === 'string';
5
+ export const createSvgElement = (tagName, attributes = {}) => {
6
+ const element = document.createElementNS(SVG_NAMESPACE, tagName);
7
+ for (const [name, value] of Object.entries(attributes))
8
+ element.setAttribute(name, String(value));
9
+ return element;
10
+ };
11
+ /** An <svg> drawn in the set's stroke style (or the given style), colored by the CSS `color` (currentColor). */
12
+ export const createIconSvg = (iconSet, viewBox, style = {}) => {
13
+ const svg = createSvgElement('svg', {
14
+ viewBox,
15
+ fill: style.fill === true ? 'currentColor' : style.fill || 'none',
16
+ stroke: 'currentColor',
17
+ 'stroke-linecap': style.lineCap ?? iconSet.lineCap,
18
+ 'stroke-linejoin': style.lineJoin ?? iconSet.lineJoin,
19
+ });
20
+ // a CSS length goes into the style (attributes can't hold rem / em / calc reliably); children inherit it
21
+ if (hasFixedStrokeWidth(style))
22
+ svg.style.strokeWidth = style.strokeWidth;
23
+ else
24
+ svg.setAttribute('stroke-width', String(style.strokeWidth ?? iconSet.strokeWidth));
25
+ return svg;
26
+ };
27
+ /** A stroked child element (path, circle); with a fixed (CSS length) stroke width its line doesn't scale. */
28
+ export const createStrokedElement = (tagName, style, attributes = {}) => createSvgElement(tagName, {
29
+ ...attributes,
30
+ ...(hasFixedStrokeWidth(style) && { 'vector-effect': 'non-scaling-stroke' }),
31
+ });
@@ -0,0 +1,88 @@
1
+ /*! morphico v0.5.0 | MIT License, Copyright (c) 2026 Raycho Ivanov (see LICENSE) */
2
+ // Layer 2: a line of characters (clock, counter, date) in narrow cells; changed digits morph.
3
+ // Every character reuses the 24×24 icon drawing; only the viewBox is narrowed to its cell.
4
+ import { createMorphingPath, hasState } from './morph.js';
5
+ import { checkedIconSet, checkedInit } from './roundness.js';
6
+ import { createIconSvg, createStrokedElement } from './svg.js';
7
+ /** Characters `text()` can show: digits, space, `-` `:` `.` `,` `/` — all of them morph. Anything else is removed. */
8
+ const UNSUPPORTED_CHARACTERS = /[^0-9 :.,\/-]/g;
9
+ /** Vertical part of the 24×24 grid shown in a cell: y 3…21 holds every digit including its stroke. */
10
+ const CELL_TOP = 3;
11
+ const CELL_HEIGHT = 18;
12
+ const formatNumber = (value) => String(+value.toFixed(4));
13
+ /** Adds a line of morphing characters (a <span> of <svg> cells) to `element`, showing `init` first. */
14
+ export const text = (element, init, options) => {
15
+ const firstValue = checkedInit(init, 'text');
16
+ const { set, size = '1em', cellWidth, spacing, strokeWidth, lineCap, lineJoin, fill, ...defaultMorphOptions } = options;
17
+ const style = { strokeWidth, lineCap, lineJoin, fill };
18
+ const iconSet = checkedIconSet(set, 'text');
19
+ const { textLayout } = iconSet;
20
+ if (!textLayout)
21
+ throw new Error('morphico: text() needs a digit set (this set has no text layout)');
22
+ const digitCellWidth = cellWidth ?? textLayout.cellWidth;
23
+ const cellSpacing = spacing ?? textLayout.spacing;
24
+ const unitWidth = typeof strokeWidth === 'number' ? strokeWidth : iconSet.strokeWidth; // a CSS length: about the set's own
25
+ const inkBottom = textLayout.baselineY + unitWidth / 2;
26
+ const line = document.createElement('span');
27
+ line.setAttribute('role', 'img');
28
+ line.style.whiteSpace = 'nowrap';
29
+ element.appendChild(line);
30
+ let cells = [];
31
+ let value = '';
32
+ const widthOf = (character) => {
33
+ if (character === '/')
34
+ return digitCellWidth * 0.7;
35
+ return ':.,'.includes(character) ? digitCellWidth / 2 : digitCellWidth;
36
+ };
37
+ /** CSS length: `units` grid units at the line's size. */
38
+ const scaled = (units) => `calc(${size} * ${formatNumber(units / CELL_HEIGHT)})`;
39
+ const createCell = (character, index) => {
40
+ const width = widthOf(character);
41
+ const svg = createIconSvg(iconSet, `${formatNumber(12 - width / 2)} ${CELL_TOP} ${formatNumber(width)} ${CELL_HEIGHT}`, style);
42
+ svg.setAttribute('aria-hidden', 'true');
43
+ Object.assign(svg.style, {
44
+ width: scaled(width),
45
+ height: size,
46
+ overflow: 'visible',
47
+ verticalAlign: scaled(inkBottom - CELL_TOP - CELL_HEIGHT), // digit bottom on the text baseline
48
+ marginLeft: index ? `var(--morphico-spacing, ${scaled(cellSpacing)})` : '0',
49
+ });
50
+ const morphingPath = createMorphingPath(svg.appendChild(createStrokedElement('path', style)), iconSet, character);
51
+ return { svg, morphingPath, width };
52
+ };
53
+ const rebuild = (characters) => {
54
+ for (const cell of cells)
55
+ cell.morphingPath.stop();
56
+ cells = characters.map(createCell);
57
+ line.replaceChildren(...cells.map((cell) => cell.svg));
58
+ };
59
+ const show = (nextValue, { onEnd, ...morphOptions } = {}) => {
60
+ value = [...nextValue.replace(UNSUPPORTED_CHARACTERS, '')].filter((character) => hasState(iconSet, character)).join('');
61
+ line.setAttribute('aria-label', value.trim());
62
+ const characters = [...value];
63
+ const sameLayout = characters.length === cells.length
64
+ && characters.every((character, index) => widthOf(character) === cells[index].width);
65
+ let change;
66
+ if (sameLayout) {
67
+ const morphs = cells.map((cell, index) => cell.morphingPath.morphTo(characters[index], { ...defaultMorphOptions, ...morphOptions }));
68
+ const finishedValue = value;
69
+ change = Promise.all(morphs).then((results) => ({ value: finishedValue, interrupted: results.some((result) => result.interrupted) }));
70
+ }
71
+ else {
72
+ rebuild(characters);
73
+ change = Promise.resolve({ value, interrupted: false });
74
+ }
75
+ if (onEnd)
76
+ change.then(onEnd);
77
+ return change;
78
+ };
79
+ show(firstValue);
80
+ return {
81
+ to: show,
82
+ destroy() {
83
+ for (const cell of cells)
84
+ cell.morphingPath.stop();
85
+ line.remove();
86
+ },
87
+ };
88
+ };
@@ -0,0 +1,3 @@
1
+ /*! morphico v0.5.0 | MIT License, Copyright (c) 2026 Raycho Ivanov (see LICENSE) */
2
+ // Shared types. Coordinates live in the 24×24 icon grid; y points down.
3
+ export {};