@hamur/icon 0.0.0-stage → 0.0.2

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 (88) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +14 -2
  3. package/dist/chunks/bun-react-fhd05zxm.js +206 -0
  4. package/dist/chunks/bun-react-nygte91t.js +305 -0
  5. package/dist/src/adapters/angular.js +104 -0
  6. package/dist/src/adapters/html.js +47 -0
  7. package/dist/src/adapters/preact.js +56 -0
  8. package/dist/src/adapters/react.js +66 -0
  9. package/dist/src/adapters/solid.js +97 -0
  10. package/dist/src/adapters/svelte.js +6 -0
  11. package/dist/src/adapters/vue.js +63 -0
  12. package/dist/src/index.js +897 -0
  13. package/dist/src/testing.js +46 -0
  14. package/dist/types/src/adapters/angular-icon.d.ts +32 -0
  15. package/dist/types/src/adapters/angular.d.ts +2 -0
  16. package/dist/types/src/adapters/html.d.ts +8 -0
  17. package/dist/types/src/adapters/preact.d.ts +6 -0
  18. package/dist/types/src/adapters/react.d.ts +6 -0
  19. package/dist/types/src/adapters/solid-icon.d.ts +6 -0
  20. package/dist/types/src/adapters/solid.d.ts +4 -0
  21. package/dist/types/src/adapters/svelte.d.ts +2 -0
  22. package/dist/types/src/adapters/vue.d.ts +41 -0
  23. package/dist/types/src/build/client.d.ts +21 -0
  24. package/dist/types/src/build/collections.d.ts +13 -0
  25. package/dist/types/src/build/css.d.ts +10 -0
  26. package/dist/types/src/build/custom.d.ts +10 -0
  27. package/dist/types/src/build/endpoint.d.ts +4 -0
  28. package/dist/types/src/build/runtime.d.ts +3 -0
  29. package/dist/types/src/build/scan.d.ts +37 -0
  30. package/dist/types/src/clean.d.ts +3 -0
  31. package/dist/types/src/index.d.ts +8 -0
  32. package/dist/types/src/kit.d.ts +10 -0
  33. package/dist/types/src/locate.d.ts +4 -0
  34. package/dist/types/src/module.d.ts +3 -0
  35. package/dist/types/src/name.d.ts +13 -0
  36. package/dist/types/src/options.d.ts +46 -0
  37. package/dist/types/src/render.d.ts +68 -0
  38. package/dist/types/src/runtime/bundle.d.ts +2 -0
  39. package/dist/types/src/runtime/classes.d.ts +3 -0
  40. package/dist/types/src/runtime/icons.d.ts +27 -0
  41. package/dist/types/src/runtime/index.d.ts +2 -0
  42. package/dist/types/src/runtime/remote.d.ts +22 -0
  43. package/dist/types/src/runtime/server.d.ts +3 -0
  44. package/dist/types/src/server/door.d.ts +1 -0
  45. package/dist/types/src/server/endpoint.d.ts +28 -0
  46. package/dist/types/src/server/handler.d.ts +1 -0
  47. package/dist/types/src/testing.d.ts +2 -0
  48. package/dist/types/src/types.d.ts +102 -0
  49. package/package.json +141 -4
  50. package/src/adapters/Icon.svelte +60 -0
  51. package/src/adapters/angular-icon.ts +104 -0
  52. package/src/adapters/angular.ts +2 -0
  53. package/src/adapters/html.ts +44 -0
  54. package/src/adapters/preact.ts +63 -0
  55. package/src/adapters/react.ts +80 -0
  56. package/src/adapters/solid-icon.ts +99 -0
  57. package/src/adapters/solid.ts +8 -0
  58. package/src/adapters/svelte.ts +2 -0
  59. package/src/adapters/test/IconPage.svelte +5 -0
  60. package/src/adapters/test/support.ts +78 -0
  61. package/src/adapters/vue.ts +64 -0
  62. package/src/build/client.ts +113 -0
  63. package/src/build/collections.ts +86 -0
  64. package/src/build/css.ts +67 -0
  65. package/src/build/custom.ts +120 -0
  66. package/src/build/endpoint.ts +16 -0
  67. package/src/build/runtime.ts +24 -0
  68. package/src/build/scan.ts +148 -0
  69. package/src/clean.ts +66 -0
  70. package/src/env.d.ts +22 -0
  71. package/src/index.ts +10 -0
  72. package/src/kit.ts +29 -0
  73. package/src/locate.ts +20 -0
  74. package/src/module.ts +35 -0
  75. package/src/name.ts +38 -0
  76. package/src/options.ts +275 -0
  77. package/src/render.ts +247 -0
  78. package/src/runtime/bundle.ts +26 -0
  79. package/src/runtime/classes.ts +5 -0
  80. package/src/runtime/icons.ts +146 -0
  81. package/src/runtime/index.ts +17 -0
  82. package/src/runtime/remote.ts +147 -0
  83. package/src/runtime/server.ts +7 -0
  84. package/src/server/door.ts +1 -0
  85. package/src/server/endpoint.ts +251 -0
  86. package/src/server/handler.ts +30 -0
  87. package/src/testing.ts +23 -0
  88. package/src/types.ts +97 -0
@@ -0,0 +1,102 @@
1
+ /** One icon of the Iconify JSON format; `width` and `height` default to the collection's, then 16. */
2
+ export interface IconifyIcon {
3
+ body: string;
4
+ width?: number;
5
+ height?: number;
6
+ left?: number;
7
+ top?: number;
8
+ /** Quarter turns clockwise. */
9
+ rotate?: number;
10
+ hFlip?: boolean;
11
+ vFlip?: boolean;
12
+ hidden?: boolean;
13
+ }
14
+ /** An alias: another icon of the collection by name, with its own size or transformation over the parent's. */
15
+ export interface IconifyAlias extends Omit<Partial<IconifyIcon>, "body"> {
16
+ parent: string;
17
+ }
18
+ /** The Iconify JSON format of `@iconify-json/<prefix>/icons.json`, read with no Iconify package. */
19
+ export interface IconifyJSON {
20
+ prefix: string;
21
+ icons: Record<string, IconifyIcon>;
22
+ aliases?: Record<string, IconifyAlias>;
23
+ width?: number;
24
+ height?: number;
25
+ left?: number;
26
+ top?: number;
27
+ not_found?: string[];
28
+ }
29
+ export type IconMode = "svg" | "css";
30
+ export type IconCustomize = (content: string, name: string, prefix: string) => string;
31
+ /** The props every adapter's `Icon` takes; every other attribute goes on the `<svg>` or the `<span>`. */
32
+ export interface IconProps {
33
+ /** `"mdi:home"`, `"i-mdi-home"` or `"i-mdi:home"`. */
34
+ name: string;
35
+ /** Default `"1em"`; a number is CSS pixels. */
36
+ size?: string | number;
37
+ /** Default `"svg"`. */
38
+ mode?: IconMode;
39
+ /** The icon's accessible name; without it the icon is decorative. */
40
+ title?: string;
41
+ /** Receives the icon's body and returns the one drawn. */
42
+ customize?: IconCustomize;
43
+ }
44
+ /** The keys of `icon` that `Icon` reads at render, sent as `runtimeConfig.public.icon`. */
45
+ export interface IconDefaults {
46
+ size?: string | number;
47
+ class?: string;
48
+ mode?: IconMode;
49
+ aliases?: Record<string, string>;
50
+ }
51
+ /** `icon` in `hamur.config.ts`. */
52
+ export interface IconOptions extends IconDefaults {
53
+ provider?: "server" | "iconify" | "none";
54
+ iconifyApiEndpoint?: string;
55
+ fallbackToApi?: boolean;
56
+ fetchTimeout?: number;
57
+ serverBundle?: "local" | false | {
58
+ collections: string[];
59
+ };
60
+ clientBundle?: {
61
+ icons?: string[];
62
+ scan?: boolean | {
63
+ include?: string[];
64
+ exclude?: string[];
65
+ };
66
+ sizeLimitKb?: number;
67
+ };
68
+ customCollections?: Array<{
69
+ prefix: string;
70
+ dir: string;
71
+ recursive?: boolean;
72
+ }>;
73
+ cssLayer?: string;
74
+ cssSelectorPrefix?: string;
75
+ cssWherePseudo?: boolean;
76
+ cssClasses?: boolean;
77
+ }
78
+ /** `#hamur/icon/options`: what the render reads. */
79
+ export interface IconRuntimeOptions {
80
+ /** Every prefix the build knows, longest first, so the `i-` form finds the longest that leaves a name. */
81
+ prefixes: string[];
82
+ /** The prefixes of the icon server bundle, each `<prefix>.json` of `useStorage("assets:icons")`. */
83
+ serverBundle: string[];
84
+ /** Where the browser asks for an icon the payload and the client bundle lack. */
85
+ provider: "server" | "iconify" | "none";
86
+ iconifyApiEndpoint: string;
87
+ fetchTimeout: number;
88
+ cssLayer?: string;
89
+ cssSelectorPrefix: string;
90
+ cssWherePseudo: boolean;
91
+ }
92
+ /** `app.modulePayload.icon`, the icons a server render used, by prefix, in the Iconify JSON shape. */
93
+ export type IconPayload = Record<string, {
94
+ icons: Record<string, IconifyIcon>;
95
+ }>;
96
+ /** `#hamur/icon/endpoint`: what the endpoint reads. */
97
+ export interface IconEndpointOptions {
98
+ dev: boolean;
99
+ fallbackToApi: boolean;
100
+ iconifyApiEndpoint: string;
101
+ fetchTimeout: number;
102
+ }
package/package.json CHANGED
@@ -1,6 +1,143 @@
1
1
  {
2
2
  "name": "@hamur/icon",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.2",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/productdevbook/hamur.git",
9
+ "directory": "packages/icon"
10
+ },
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/types/src/index.d.ts",
14
+ "bun": "./src/index.ts",
15
+ "hamur-source": "./src/index.ts",
16
+ "node": "./dist/src/index.js",
17
+ "default": "./src/index.ts"
18
+ },
19
+ "./react": {
20
+ "types": "./dist/types/src/adapters/react.d.ts",
21
+ "bun": "./src/adapters/react.ts",
22
+ "hamur-source": "./src/adapters/react.ts",
23
+ "node": "./dist/src/adapters/react.js",
24
+ "default": "./src/adapters/react.ts"
25
+ },
26
+ "./vue": {
27
+ "types": "./dist/types/src/adapters/vue.d.ts",
28
+ "bun": "./src/adapters/vue.ts",
29
+ "hamur-source": "./src/adapters/vue.ts",
30
+ "node": "./dist/src/adapters/vue.js",
31
+ "default": "./src/adapters/vue.ts"
32
+ },
33
+ "./preact": {
34
+ "types": "./dist/types/src/adapters/preact.d.ts",
35
+ "bun": "./src/adapters/preact.ts",
36
+ "hamur-source": "./src/adapters/preact.ts",
37
+ "node": "./dist/src/adapters/preact.js",
38
+ "default": "./src/adapters/preact.ts"
39
+ },
40
+ "./svelte": {
41
+ "types": "./dist/types/src/adapters/svelte.d.ts",
42
+ "bun": "./src/adapters/svelte.ts",
43
+ "hamur-source": "./src/adapters/svelte.ts",
44
+ "node": "./dist/src/adapters/svelte.js",
45
+ "default": "./src/adapters/svelte.ts"
46
+ },
47
+ "./solid": {
48
+ "types": "./dist/types/src/adapters/solid.d.ts",
49
+ "bun": "./src/adapters/solid.ts",
50
+ "hamur-source": "./src/adapters/solid.ts",
51
+ "node": "./dist/src/adapters/solid.js",
52
+ "default": "./src/adapters/solid.ts"
53
+ },
54
+ "./html": {
55
+ "types": "./dist/types/src/adapters/html.d.ts",
56
+ "bun": "./src/adapters/html.ts",
57
+ "hamur-source": "./src/adapters/html.ts",
58
+ "node": "./dist/src/adapters/html.js",
59
+ "default": "./src/adapters/html.ts"
60
+ },
61
+ "./angular": {
62
+ "types": "./dist/types/src/adapters/angular.d.ts",
63
+ "bun": "./src/adapters/angular.ts",
64
+ "hamur-source": "./src/adapters/angular.ts",
65
+ "node": "./dist/src/adapters/angular.js",
66
+ "default": "./src/adapters/angular.ts"
67
+ },
68
+ "./testing": {
69
+ "types": "./dist/types/src/testing.d.ts",
70
+ "bun": "./src/testing.ts",
71
+ "hamur-source": "./src/testing.ts",
72
+ "node": "./dist/src/testing.js",
73
+ "default": "./src/testing.ts"
74
+ },
75
+ "./package.json": "./package.json"
76
+ },
77
+ "files": [
78
+ "dist",
79
+ "src"
80
+ ],
81
+ "dependencies": {},
82
+ "peerDependencies": {
83
+ "hamur": "^0.0.2",
84
+ "@hamur/adapter-react": "^0.0.2",
85
+ "@hamur/adapter-vue": "^0.0.2",
86
+ "@hamur/adapter-preact": "^0.0.2",
87
+ "@hamur/adapter-svelte": "^0.0.2",
88
+ "@hamur/adapter-solid": "^0.0.2",
89
+ "@hamur/adapter-html": "^0.0.2",
90
+ "@hamur/adapter-angular": "^0.0.2",
91
+ "react": "^19.3.1",
92
+ "preact": "^11.0.2",
93
+ "vue": "^3.5.44",
94
+ "svelte": "^5.57.2",
95
+ "solid-js": "^2.0.4",
96
+ "@solidjs/web": "^2.0.4",
97
+ "@angular/core": "^22.3.0"
98
+ },
99
+ "peerDependenciesMeta": {
100
+ "@hamur/adapter-react": {
101
+ "optional": true
102
+ },
103
+ "@hamur/adapter-vue": {
104
+ "optional": true
105
+ },
106
+ "@hamur/adapter-preact": {
107
+ "optional": true
108
+ },
109
+ "@hamur/adapter-svelte": {
110
+ "optional": true
111
+ },
112
+ "@hamur/adapter-solid": {
113
+ "optional": true
114
+ },
115
+ "@hamur/adapter-html": {
116
+ "optional": true
117
+ },
118
+ "@hamur/adapter-angular": {
119
+ "optional": true
120
+ },
121
+ "react": {
122
+ "optional": true
123
+ },
124
+ "preact": {
125
+ "optional": true
126
+ },
127
+ "vue": {
128
+ "optional": true
129
+ },
130
+ "svelte": {
131
+ "optional": true
132
+ },
133
+ "solid-js": {
134
+ "optional": true
135
+ },
136
+ "@solidjs/web": {
137
+ "optional": true
138
+ },
139
+ "@angular/core": {
140
+ "optional": true
141
+ }
142
+ }
143
+ }
@@ -0,0 +1,60 @@
1
+ <script lang="ts">
2
+ import { untrack } from "svelte"
3
+ import type { SvgNode } from "hamur/adapter"
4
+ import { useApp, useRuntimeConfig, useHead } from "@hamur/adapter-svelte"
5
+ import { svgAttributes, svgValue } from "../render.ts"
6
+ import { iconHead, icons } from "../runtime/index.ts"
7
+ import type { IconDefaults, IconCustomize, IconMode } from "../types.ts"
8
+
9
+ type Props = {
10
+ name: string
11
+ size?: string | number
12
+ mode?: IconMode
13
+ title?: string
14
+ customize?: IconCustomize
15
+ class?: string
16
+ [attribute: string]: unknown
17
+ }
18
+
19
+ let { name, size, mode, title, customize, class: className, ...rest }: Props = $props()
20
+ const id = $props.id()
21
+ const uid = id.replace(/[^\w-]/g, "_")
22
+ const app = useApp()
23
+ const config = useRuntimeConfig().public as { icon?: IconDefaults }
24
+ const seen = icons.version()
25
+ let answers = $state(0)
26
+ $effect(() => {
27
+ if (icons.version() !== seen) untrack(() => answers++)
28
+ return icons.subscribe(() => answers++)
29
+ })
30
+ const view = $derived(
31
+ (answers,
32
+ icons.view(
33
+ app,
34
+ {
35
+ name,
36
+ size,
37
+ mode,
38
+ title,
39
+ customize,
40
+ class: className,
41
+ labelled: rest["aria-label"] != null || rest["aria-labelledby"] != null,
42
+ },
43
+ config.icon,
44
+ )),
45
+ )
46
+ useHead(() => iconHead(view))
47
+ </script>
48
+
49
+ {#snippet draw(node: SvgNode)}{#if typeof node === "string" || Array.isArray(node)}{svgValue(
50
+ node,
51
+ uid,
52
+ )}{:else}<svelte:element this={node.tag} xmlns="http://www.w3.org/2000/svg" {...svgAttributes(node, uid)}
53
+ >{#each node.children as child}{@render draw(child)}{/each}</svelte:element
54
+ >{/if}{/snippet}
55
+
56
+ {#if view.mode === "css"}<span {...view.attrs} {...rest}></span>{:else}<svg {...view.attrs} {...rest}
57
+ >{#if view.title !== undefined}<title>{view.title}</title>{/if}{#each view.nodes as node}{@render draw(
58
+ node,
59
+ )}{/each}</svg
60
+ >{/if}
@@ -0,0 +1,104 @@
1
+ import { Component, DestroyRef, ElementRef, HostAttributeToken, Injector, Renderer2, computed, effect, inject, input, runInInjectionContext, signal, untracked, } from "@angular/core";
2
+ import { injectApp, injectHead, injectId, injectRuntimeConfig } from "@hamur/adapter-angular";
3
+ import { svgAttributes, svgValue } from "../render.ts";
4
+ import { iconHead, icons } from "../runtime/index.ts";
5
+ import * as i0 from "@angular/core";
6
+ /**
7
+ * `<hamur-icon name="mdi:home" mode="svg" />`, an icon of a collection by name, drawn as `<svg>` from the sanitised
8
+ * body, or as a `<span>` and one CSS rule, inside the `<hamur-icon>` element. The nodes are written with the renderer, as no template
9
+ * can name an element the body decides; the props are taken off the host, and `aria-label` and `aria-labelledby` are
10
+ * also written on the drawn element.
11
+ */
12
+ export class Icon {
13
+ name = input.required(/* @ts-ignore */
14
+ ...(ngDevMode ? [{ debugName: "name" }] : /* istanbul ignore next */ []));
15
+ size = input(/* @ts-ignore */
16
+ ...(ngDevMode ? [undefined, { debugName: "size" }] : /* istanbul ignore next */ []));
17
+ mode = input(/* @ts-ignore */
18
+ ...(ngDevMode ? [undefined, { debugName: "mode" }] : /* istanbul ignore next */ []));
19
+ title = input(/* @ts-ignore */
20
+ ...(ngDevMode ? [undefined, { debugName: "title" }] : /* istanbul ignore next */ []));
21
+ customize = input(/* @ts-ignore */
22
+ ...(ngDevMode ? [undefined, { debugName: "customize" }] : /* istanbul ignore next */ []));
23
+ injector = inject(Injector);
24
+ app = injectApp();
25
+ config = injectRuntimeConfig().public;
26
+ host = inject(ElementRef).nativeElement;
27
+ renderer = inject(Renderer2);
28
+ label = inject(new HostAttributeToken("aria-label"), { optional: true });
29
+ labelledby = inject(new HostAttributeToken("aria-labelledby"), { optional: true });
30
+ uid = injectId().replace(/[^\w-]/g, "_");
31
+ answers = signal(icons.version(), /* @ts-ignore */
32
+ ...(ngDevMode ? [{ debugName: "answers" }] : /* istanbul ignore next */ []));
33
+ view = computed(() => {
34
+ void this.answers();
35
+ return icons.view(this.app, {
36
+ name: this.name(),
37
+ size: this.size(),
38
+ mode: this.mode(),
39
+ title: this.title(),
40
+ customize: this.customize(),
41
+ labelled: this.label !== null || this.labelledby !== null,
42
+ }, this.config.icon);
43
+ }, /* @ts-ignore */
44
+ ...(ngDevMode ? [{ debugName: "view" }] : /* istanbul ignore next */ []));
45
+ constructor() {
46
+ inject(DestroyRef).onDestroy(icons.subscribe(() => this.answers.set(icons.version())));
47
+ effect(() => {
48
+ const view = this.view();
49
+ untracked(() => this.draw(view));
50
+ });
51
+ }
52
+ ngOnInit() {
53
+ runInInjectionContext(this.injector, () => injectHead(() => iconHead(this.view())));
54
+ }
55
+ draw(view) {
56
+ const { renderer, host } = this;
57
+ while (host.firstChild)
58
+ renderer.removeChild(host, host.firstChild);
59
+ const root = view.mode === "css" ? renderer.createElement("span") : renderer.createElement("svg", "svg");
60
+ const attrs = { ...view.attrs };
61
+ if (this.label !== null)
62
+ attrs["aria-label"] = this.label;
63
+ if (this.labelledby !== null)
64
+ attrs["aria-labelledby"] = this.labelledby;
65
+ for (const [name, value] of Object.entries(attrs))
66
+ renderer.setAttribute(root, name, value);
67
+ if (view.mode === "svg") {
68
+ if (view.title !== undefined) {
69
+ const title = renderer.createElement("title", "svg");
70
+ renderer.appendChild(title, renderer.createText(view.title));
71
+ renderer.appendChild(root, title);
72
+ }
73
+ for (const node of view.nodes)
74
+ renderer.appendChild(root, this.element(node));
75
+ }
76
+ renderer.appendChild(host, root);
77
+ }
78
+ element(node) {
79
+ if (typeof node === "string" || Array.isArray(node))
80
+ return this.renderer.createText(svgValue(node, this.uid));
81
+ const element = this.renderer.createElement(node.tag, "svg");
82
+ for (const [name, value] of Object.entries(svgAttributes(node, this.uid)))
83
+ this.renderer.setAttribute(element, name, value);
84
+ for (const child of node.children)
85
+ this.renderer.appendChild(element, this.element(child));
86
+ return element;
87
+ }
88
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Icon, deps: [], target: i0.ɵɵFactoryTarget.Component });
89
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.1.0", version: "22.3.2", type: Icon, isStandalone: true, selector: "hamur-icon", inputs: { name: { classPropertyName: "name", publicName: "name", isSignal: true, isRequired: true, transformFunction: null }, size: { classPropertyName: "size", publicName: "size", isSignal: true, isRequired: false, transformFunction: null }, mode: { classPropertyName: "mode", publicName: "mode", isSignal: true, isRequired: false, transformFunction: null }, title: { classPropertyName: "title", publicName: "title", isSignal: true, isRequired: false, transformFunction: null }, customize: { classPropertyName: "customize", publicName: "customize", isSignal: true, isRequired: false, transformFunction: null } }, host: { properties: { "attr.name": "null", "attr.size": "null", "attr.mode": "null", "attr.title": "null", "attr.customize": "null" } }, ngImport: i0, template: "", isInline: true });
90
+ }
91
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Icon, decorators: [{
92
+ type: Component,
93
+ args: [{
94
+ selector: "hamur-icon",
95
+ template: "",
96
+ host: {
97
+ "[attr.name]": "null",
98
+ "[attr.size]": "null",
99
+ "[attr.mode]": "null",
100
+ "[attr.title]": "null",
101
+ "[attr.customize]": "null",
102
+ },
103
+ }]
104
+ }], ctorParameters: () => [], propDecorators: { name: [{ type: i0.Input, args: [{ isSignal: true, alias: "name", required: true }] }], size: [{ type: i0.Input, args: [{ isSignal: true, alias: "size", required: false }] }], mode: [{ type: i0.Input, args: [{ isSignal: true, alias: "mode", required: false }] }], title: [{ type: i0.Input, args: [{ isSignal: true, alias: "title", required: false }] }], customize: [{ type: i0.Input, args: [{ isSignal: true, alias: "customize", required: false }] }] } });
@@ -0,0 +1,2 @@
1
+ export { Icon } from "./angular-icon.ts"
2
+ export type { IconProps } from "../types.ts"
@@ -0,0 +1,44 @@
1
+ import { Await, useApp, useRuntimeConfig, useHead, useId } from "@hamur/adapter-html"
2
+ import { attributesMarkup, escapeHtml, svgMarkup } from "../render.ts"
3
+ import { iconHead, icons } from "../runtime/index.ts"
4
+ import type { IconDefaults, IconProps } from "../types.ts"
5
+
6
+ export type { IconProps }
7
+
8
+ export type HtmlIconProps = IconProps & { class?: string; [attribute: string]: unknown }
9
+
10
+ function attributes(values: Record<string, unknown>): Record<string, string> {
11
+ const out: Record<string, string> = {}
12
+ for (const [name, value] of Object.entries(values)) {
13
+ if (value === undefined || value === null || value === false) continue
14
+ out[name] = value === true ? "" : String(value)
15
+ }
16
+ return out
17
+ }
18
+
19
+ /** An icon of a collection by name as markup, `<svg>` from the sanitised body or a `<span>` and one CSS rule; other props are attributes. */
20
+ export function Icon(props: HtmlIconProps): string {
21
+ const { name, size, mode, title, customize, class: className, ...rest } = props
22
+ const view = icons.view(
23
+ useApp(),
24
+ {
25
+ name,
26
+ size,
27
+ mode,
28
+ title,
29
+ customize,
30
+ class: className,
31
+ labelled: rest["aria-label"] != null || rest["aria-labelledby"] != null,
32
+ },
33
+ (useRuntimeConfig().public as { icon?: IconDefaults }).icon,
34
+ )
35
+ let markup: string
36
+ if (view.mode === "css") {
37
+ if (view.head !== undefined) useHead(iconHead(view))
38
+ markup = `<span${attributesMarkup({ ...view.attrs, ...attributes(rest) })}></span>`
39
+ } else {
40
+ const uid = useId().replace(/[^\w-]/g, "_")
41
+ markup = `<svg${attributesMarkup({ ...view.attrs, ...attributes(rest) })}>${view.title === undefined ? "" : `<title>${escapeHtml(view.title)}</title>`}${svgMarkup(view.nodes, uid)}</svg>`
42
+ }
43
+ return view.pending ? Await(view.pending, { fallback: markup, then: () => Icon(props) }) : markup
44
+ }
@@ -0,0 +1,63 @@
1
+ import { h, type ComponentChildren, type SVGAttributes } from "preact"
2
+ import { useEffect, useState } from "preact/hooks"
3
+ import { useApp, useRuntimeConfig, useHead, useId } from "@hamur/adapter-preact"
4
+ import type { SvgNode } from "hamur/adapter"
5
+ import { svgAttributes, svgValue } from "../render.ts"
6
+ import { iconHead, icons } from "../runtime/index.ts"
7
+ import type { IconDefaults, IconProps } from "../types.ts"
8
+
9
+ export type { IconProps }
10
+
11
+ export type PreactIconProps = IconProps & Omit<SVGAttributes<SVGSVGElement>, keyof IconProps | "ref" | "children">
12
+
13
+ function element(node: SvgNode, uid: string): ComponentChildren {
14
+ if (typeof node === "string" || Array.isArray(node)) return svgValue(node, uid)
15
+ return h(
16
+ node.tag,
17
+ svgAttributes(node, uid) as Record<string, any>,
18
+ ...node.children.map((child) => element(child, uid)),
19
+ )
20
+ }
21
+
22
+ /** An icon of a collection by name, drawn as `<svg>` from the sanitised body, or as a `<span>` and one CSS rule. */
23
+ export function Icon({
24
+ name,
25
+ size,
26
+ mode,
27
+ title,
28
+ customize,
29
+ class: className,
30
+ className: alias,
31
+ ...rest
32
+ }: PreactIconProps): ComponentChildren {
33
+ const uid = useId().replace(/[^\w-]/g, "_")
34
+ const seen = icons.version()
35
+ const [, update] = useState(0)
36
+ useEffect(() => {
37
+ if (icons.version() !== seen) update((count) => count + 1)
38
+ return icons.subscribe(() => update((count) => count + 1))
39
+ }, [])
40
+ const config = (useRuntimeConfig().public as { icon?: IconDefaults }).icon
41
+ const view = icons.view(
42
+ useApp(),
43
+ {
44
+ name,
45
+ size,
46
+ mode,
47
+ title,
48
+ customize,
49
+ class: [className, alias].filter((value) => typeof value === "string" && value !== "").join(" ") || undefined,
50
+ labelled: rest["aria-label"] != null || rest["aria-labelledby"] != null,
51
+ },
52
+ config,
53
+ )
54
+ useHead(iconHead(view))
55
+ const props: Record<string, any> = { ...view.attrs, ...rest }
56
+ if (view.mode === "css") return h("span", props)
57
+ return h(
58
+ "svg",
59
+ props,
60
+ view.title === undefined ? null : h("title", null, view.title),
61
+ ...view.nodes.map((node) => element(node, uid)),
62
+ )
63
+ }
@@ -0,0 +1,80 @@
1
+ import { createElement, useSyncExternalStore, type ReactNode, type SVGProps } from "react"
2
+ import { useApp, useRuntimeConfig, useHead, useId } from "@hamur/adapter-react"
3
+ import type { SvgNode } from "hamur/adapter"
4
+ import { svgValue } from "../render.ts"
5
+ import { iconHead, icons } from "../runtime/index.ts"
6
+ import type { IconDefaults, IconProps } from "../types.ts"
7
+
8
+ export type { IconProps }
9
+
10
+ export type ReactIconProps = IconProps & Omit<SVGProps<SVGSVGElement>, keyof IconProps | "ref" | "children">
11
+
12
+ function propName(name: string): string {
13
+ if (name === "class") return "className"
14
+ if (name === "tabindex") return "tabIndex"
15
+ if (name.startsWith("aria-") || name.startsWith("data-")) return name
16
+ return name.replace(/[-:]([a-z])/g, (_, letter: string) => letter.toUpperCase())
17
+ }
18
+
19
+ const styleKey = (name: string): string => name.replace(/-([a-z])/g, (_, letter: string) => letter.toUpperCase())
20
+
21
+ const h = (tag: string, props: Record<string, unknown> | null, ...children: ReactNode[]): ReactNode =>
22
+ createElement(tag, props, ...children)
23
+
24
+ function styleObject(text: string): Record<string, string> {
25
+ return Object.fromEntries(
26
+ text
27
+ .split(";")
28
+ .filter(Boolean)
29
+ .map((declaration) => [
30
+ styleKey(declaration.slice(0, declaration.indexOf(":"))),
31
+ declaration.slice(declaration.indexOf(":") + 1),
32
+ ]),
33
+ )
34
+ }
35
+
36
+ function rootProps(attrs: Record<string, string>): Record<string, unknown> {
37
+ const props: Record<string, unknown> = {}
38
+ for (const [name, value] of Object.entries(attrs))
39
+ props[name === "style" ? "style" : propName(name)] = name === "style" ? styleObject(value) : value
40
+ return props
41
+ }
42
+
43
+ function element(node: SvgNode, uid: string): ReactNode {
44
+ if (typeof node === "string" || Array.isArray(node)) return svgValue(node, uid)
45
+ const props: Record<string, unknown> = {}
46
+ for (const [name, value] of Object.entries(node.attrs)) props[propName(name)] = svgValue(value, uid)
47
+ if (node.style)
48
+ props.style = Object.fromEntries(
49
+ node.style.map((declaration) => [styleKey(declaration.name), svgValue(declaration.value, uid)]),
50
+ )
51
+ return h(node.tag, props, ...node.children.map((child) => element(child, uid)))
52
+ }
53
+
54
+ /** An icon of a collection by name, drawn as `<svg>` from the sanitised body, or as a `<span>` and one CSS rule. */
55
+ export function Icon({ name, size, mode, title, customize, className, ...rest }: ReactIconProps): ReactNode {
56
+ const uid = useId().replace(/[^\w-]/g, "_")
57
+ useSyncExternalStore(icons.subscribe, icons.version, icons.version)
58
+ const view = icons.view(
59
+ useApp(),
60
+ {
61
+ name,
62
+ size,
63
+ mode,
64
+ title,
65
+ customize,
66
+ class: className,
67
+ labelled: rest["aria-label"] != null || rest["aria-labelledby"] != null,
68
+ },
69
+ (useRuntimeConfig().public as { icon?: IconDefaults }).icon,
70
+ )
71
+ useHead(iconHead(view))
72
+ const props = { ...rootProps(view.attrs), ...rest }
73
+ if (view.mode === "css") return h("span", props)
74
+ return h(
75
+ "svg",
76
+ props,
77
+ view.title === undefined ? null : h("title", null, view.title),
78
+ ...view.nodes.map((node) => element(node, uid)),
79
+ )
80
+ }