@hamur/icon 0.0.0-stage → 0.0.1
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 +22 -0
- package/README.md +14 -2
- package/dist/chunks/bun-react-fhd05zxm.js +206 -0
- package/dist/chunks/bun-react-nygte91t.js +305 -0
- package/dist/src/adapters/angular.js +104 -0
- package/dist/src/adapters/html.js +47 -0
- package/dist/src/adapters/preact.js +56 -0
- package/dist/src/adapters/react.js +66 -0
- package/dist/src/adapters/solid.js +97 -0
- package/dist/src/adapters/svelte.js +6 -0
- package/dist/src/adapters/vue.js +63 -0
- package/dist/src/index.js +897 -0
- package/dist/src/testing.js +46 -0
- package/dist/types/src/adapters/angular-icon.d.ts +29 -0
- package/dist/types/src/adapters/angular.d.ts +2 -0
- package/dist/types/src/adapters/html.d.ts +8 -0
- package/dist/types/src/adapters/preact.d.ts +6 -0
- package/dist/types/src/adapters/react.d.ts +6 -0
- package/dist/types/src/adapters/solid-icon.d.ts +6 -0
- package/dist/types/src/adapters/solid.d.ts +4 -0
- package/dist/types/src/adapters/svelte.d.ts +2 -0
- package/dist/types/src/adapters/vue.d.ts +41 -0
- package/dist/types/src/build/client.d.ts +21 -0
- package/dist/types/src/build/collections.d.ts +13 -0
- package/dist/types/src/build/css.d.ts +10 -0
- package/dist/types/src/build/custom.d.ts +10 -0
- package/dist/types/src/build/endpoint.d.ts +4 -0
- package/dist/types/src/build/runtime.d.ts +3 -0
- package/dist/types/src/build/scan.d.ts +37 -0
- package/dist/types/src/clean.d.ts +3 -0
- package/dist/types/src/index.d.ts +8 -0
- package/dist/types/src/kit.d.ts +10 -0
- package/dist/types/src/locate.d.ts +4 -0
- package/dist/types/src/module.d.ts +3 -0
- package/dist/types/src/name.d.ts +13 -0
- package/dist/types/src/options.d.ts +46 -0
- package/dist/types/src/render.d.ts +68 -0
- package/dist/types/src/runtime/bundle.d.ts +2 -0
- package/dist/types/src/runtime/classes.d.ts +3 -0
- package/dist/types/src/runtime/icons.d.ts +27 -0
- package/dist/types/src/runtime/index.d.ts +2 -0
- package/dist/types/src/runtime/remote.d.ts +22 -0
- package/dist/types/src/runtime/server.d.ts +3 -0
- package/dist/types/src/server/door.d.ts +1 -0
- package/dist/types/src/server/endpoint.d.ts +28 -0
- package/dist/types/src/server/handler.d.ts +1 -0
- package/dist/types/src/testing.d.ts +2 -0
- package/dist/types/src/types.d.ts +102 -0
- package/package.json +114 -4
- package/src/adapters/Icon.svelte +60 -0
- package/src/adapters/angular-icon.ts +112 -0
- package/src/adapters/angular.ts +2 -0
- package/src/adapters/html.ts +44 -0
- package/src/adapters/preact.ts +63 -0
- package/src/adapters/react.ts +80 -0
- package/src/adapters/solid-icon.ts +99 -0
- package/src/adapters/solid.ts +8 -0
- package/src/adapters/svelte.ts +2 -0
- package/src/adapters/test/IconPage.svelte +5 -0
- package/src/adapters/test/support.ts +78 -0
- package/src/adapters/vue.ts +64 -0
- package/src/build/client.ts +113 -0
- package/src/build/collections.ts +86 -0
- package/src/build/css.ts +67 -0
- package/src/build/custom.ts +120 -0
- package/src/build/endpoint.ts +16 -0
- package/src/build/runtime.ts +24 -0
- package/src/build/scan.ts +148 -0
- package/src/clean.ts +66 -0
- package/src/env.d.ts +22 -0
- package/src/index.ts +10 -0
- package/src/kit.ts +29 -0
- package/src/locate.ts +20 -0
- package/src/module.ts +35 -0
- package/src/name.ts +38 -0
- package/src/options.ts +275 -0
- package/src/render.ts +247 -0
- package/src/runtime/bundle.ts +26 -0
- package/src/runtime/classes.ts +5 -0
- package/src/runtime/icons.ts +146 -0
- package/src/runtime/index.ts +17 -0
- package/src/runtime/remote.ts +147 -0
- package/src/runtime/server.ts +7 -0
- package/src/server/door.ts +1 -0
- package/src/server/endpoint.ts +251 -0
- package/src/server/handler.ts +30 -0
- package/src/testing.ts +23 -0
- 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,116 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hamur/icon",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
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
|
+
"default": "./dist/src/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./react": {
|
|
17
|
+
"types": "./dist/types/src/adapters/react.d.ts",
|
|
18
|
+
"default": "./dist/src/adapters/react.js"
|
|
19
|
+
},
|
|
20
|
+
"./vue": {
|
|
21
|
+
"types": "./dist/types/src/adapters/vue.d.ts",
|
|
22
|
+
"default": "./dist/src/adapters/vue.js"
|
|
23
|
+
},
|
|
24
|
+
"./preact": {
|
|
25
|
+
"types": "./dist/types/src/adapters/preact.d.ts",
|
|
26
|
+
"default": "./dist/src/adapters/preact.js"
|
|
27
|
+
},
|
|
28
|
+
"./svelte": {
|
|
29
|
+
"types": "./dist/types/src/adapters/svelte.d.ts",
|
|
30
|
+
"default": "./dist/src/adapters/svelte.js"
|
|
31
|
+
},
|
|
32
|
+
"./solid": {
|
|
33
|
+
"types": "./dist/types/src/adapters/solid.d.ts",
|
|
34
|
+
"default": "./dist/src/adapters/solid.js"
|
|
35
|
+
},
|
|
36
|
+
"./html": {
|
|
37
|
+
"types": "./dist/types/src/adapters/html.d.ts",
|
|
38
|
+
"default": "./dist/src/adapters/html.js"
|
|
39
|
+
},
|
|
40
|
+
"./angular": {
|
|
41
|
+
"types": "./dist/types/src/adapters/angular.d.ts",
|
|
42
|
+
"default": "./dist/src/adapters/angular.js"
|
|
43
|
+
},
|
|
44
|
+
"./testing": {
|
|
45
|
+
"types": "./dist/types/src/testing.d.ts",
|
|
46
|
+
"default": "./dist/src/testing.js"
|
|
47
|
+
},
|
|
48
|
+
"./package.json": "./package.json"
|
|
49
|
+
},
|
|
50
|
+
"files": [
|
|
51
|
+
"dist",
|
|
52
|
+
"src"
|
|
53
|
+
],
|
|
54
|
+
"dependencies": {},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"hamur": "^0.0.1",
|
|
57
|
+
"@hamur/adapter-react": "^0.0.1",
|
|
58
|
+
"@hamur/adapter-vue": "^0.0.1",
|
|
59
|
+
"@hamur/adapter-preact": "^0.0.1",
|
|
60
|
+
"@hamur/adapter-svelte": "^0.0.1",
|
|
61
|
+
"@hamur/adapter-solid": "^0.0.1",
|
|
62
|
+
"@hamur/adapter-html": "^0.0.1",
|
|
63
|
+
"@hamur/adapter-angular": "^0.0.1",
|
|
64
|
+
"react": "^19.3.1",
|
|
65
|
+
"preact": "^11.0.2",
|
|
66
|
+
"vue": "^3.5.44",
|
|
67
|
+
"svelte": "^5.57.2",
|
|
68
|
+
"solid-js": "^2.0.4",
|
|
69
|
+
"@solidjs/web": "^2.0.4",
|
|
70
|
+
"@angular/core": "^22.3.0"
|
|
71
|
+
},
|
|
72
|
+
"peerDependenciesMeta": {
|
|
73
|
+
"@hamur/adapter-react": {
|
|
74
|
+
"optional": true
|
|
75
|
+
},
|
|
76
|
+
"@hamur/adapter-vue": {
|
|
77
|
+
"optional": true
|
|
78
|
+
},
|
|
79
|
+
"@hamur/adapter-preact": {
|
|
80
|
+
"optional": true
|
|
81
|
+
},
|
|
82
|
+
"@hamur/adapter-svelte": {
|
|
83
|
+
"optional": true
|
|
84
|
+
},
|
|
85
|
+
"@hamur/adapter-solid": {
|
|
86
|
+
"optional": true
|
|
87
|
+
},
|
|
88
|
+
"@hamur/adapter-html": {
|
|
89
|
+
"optional": true
|
|
90
|
+
},
|
|
91
|
+
"@hamur/adapter-angular": {
|
|
92
|
+
"optional": true
|
|
93
|
+
},
|
|
94
|
+
"react": {
|
|
95
|
+
"optional": true
|
|
96
|
+
},
|
|
97
|
+
"preact": {
|
|
98
|
+
"optional": true
|
|
99
|
+
},
|
|
100
|
+
"vue": {
|
|
101
|
+
"optional": true
|
|
102
|
+
},
|
|
103
|
+
"svelte": {
|
|
104
|
+
"optional": true
|
|
105
|
+
},
|
|
106
|
+
"solid-js": {
|
|
107
|
+
"optional": true
|
|
108
|
+
},
|
|
109
|
+
"@solidjs/web": {
|
|
110
|
+
"optional": true
|
|
111
|
+
},
|
|
112
|
+
"@angular/core": {
|
|
113
|
+
"optional": true
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -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,112 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Component,
|
|
3
|
+
DestroyRef,
|
|
4
|
+
ElementRef,
|
|
5
|
+
HostAttributeToken,
|
|
6
|
+
Injector,
|
|
7
|
+
Renderer2,
|
|
8
|
+
computed,
|
|
9
|
+
effect,
|
|
10
|
+
inject,
|
|
11
|
+
input,
|
|
12
|
+
runInInjectionContext,
|
|
13
|
+
signal,
|
|
14
|
+
untracked,
|
|
15
|
+
type OnInit,
|
|
16
|
+
} from "@angular/core"
|
|
17
|
+
import { injectApp, injectHead, injectId, injectRuntimeConfig } from "@hamur/adapter-angular"
|
|
18
|
+
import type { SvgNode } from "hamur/adapter"
|
|
19
|
+
import { svgAttributes, svgValue } from "../render.ts"
|
|
20
|
+
import { iconHead, icons, type IconRenderView } from "../runtime/index.ts"
|
|
21
|
+
import type { IconDefaults, IconCustomize, IconMode } from "../types.ts"
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* `<hamur-icon name="mdi:home" mode="svg" />`, an icon of a collection by name, drawn as `<svg>` from the sanitised
|
|
25
|
+
* body, or as a `<span>` and one CSS rule, inside the `<hamur-icon>` element. The nodes are written with the renderer, as no template
|
|
26
|
+
* can name an element the body decides; the props are taken off the host, and `aria-label` and `aria-labelledby` are
|
|
27
|
+
* also written on the drawn element.
|
|
28
|
+
*/
|
|
29
|
+
@Component({
|
|
30
|
+
selector: "hamur-icon",
|
|
31
|
+
template: "",
|
|
32
|
+
host: {
|
|
33
|
+
"[attr.name]": "null",
|
|
34
|
+
"[attr.size]": "null",
|
|
35
|
+
"[attr.mode]": "null",
|
|
36
|
+
"[attr.title]": "null",
|
|
37
|
+
"[attr.customize]": "null",
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
export class Icon implements OnInit {
|
|
41
|
+
readonly name = input.required<string>()
|
|
42
|
+
readonly size = input<string | number>()
|
|
43
|
+
readonly mode = input<IconMode>()
|
|
44
|
+
readonly title = input<string>()
|
|
45
|
+
readonly customize = input<IconCustomize>()
|
|
46
|
+
|
|
47
|
+
private readonly injector = inject(Injector)
|
|
48
|
+
private readonly app = injectApp()
|
|
49
|
+
private readonly config = injectRuntimeConfig().public as { icon?: IconDefaults }
|
|
50
|
+
private readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement
|
|
51
|
+
private readonly renderer = inject(Renderer2)
|
|
52
|
+
private readonly label = inject(new HostAttributeToken("aria-label"), { optional: true })
|
|
53
|
+
private readonly labelledby = inject(new HostAttributeToken("aria-labelledby"), { optional: true })
|
|
54
|
+
private readonly uid = injectId().replace(/[^\w-]/g, "_")
|
|
55
|
+
private readonly answers = signal(icons.version())
|
|
56
|
+
|
|
57
|
+
private readonly view = computed(() => {
|
|
58
|
+
void this.answers()
|
|
59
|
+
return icons.view(
|
|
60
|
+
this.app,
|
|
61
|
+
{
|
|
62
|
+
name: this.name(),
|
|
63
|
+
size: this.size(),
|
|
64
|
+
mode: this.mode(),
|
|
65
|
+
title: this.title(),
|
|
66
|
+
customize: this.customize(),
|
|
67
|
+
labelled: this.label !== null || this.labelledby !== null,
|
|
68
|
+
},
|
|
69
|
+
this.config.icon,
|
|
70
|
+
)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
constructor() {
|
|
74
|
+
inject(DestroyRef).onDestroy(icons.subscribe(() => this.answers.set(icons.version())))
|
|
75
|
+
effect(() => {
|
|
76
|
+
const view = this.view()
|
|
77
|
+
untracked(() => this.draw(view))
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
ngOnInit(): void {
|
|
82
|
+
runInInjectionContext(this.injector, () => injectHead(() => iconHead(this.view())))
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private draw(view: IconRenderView): void {
|
|
86
|
+
const { renderer, host } = this
|
|
87
|
+
while (host.firstChild) renderer.removeChild(host, host.firstChild)
|
|
88
|
+
const root = view.mode === "css" ? renderer.createElement("span") : renderer.createElement("svg", "svg")
|
|
89
|
+
const attrs = { ...view.attrs }
|
|
90
|
+
if (this.label !== null) attrs["aria-label"] = this.label
|
|
91
|
+
if (this.labelledby !== null) attrs["aria-labelledby"] = this.labelledby
|
|
92
|
+
for (const [name, value] of Object.entries(attrs)) renderer.setAttribute(root, name, value)
|
|
93
|
+
if (view.mode === "svg") {
|
|
94
|
+
if (view.title !== undefined) {
|
|
95
|
+
const title = renderer.createElement("title", "svg")
|
|
96
|
+
renderer.appendChild(title, renderer.createText(view.title))
|
|
97
|
+
renderer.appendChild(root, title)
|
|
98
|
+
}
|
|
99
|
+
for (const node of view.nodes) renderer.appendChild(root, this.element(node))
|
|
100
|
+
}
|
|
101
|
+
renderer.appendChild(host, root)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private element(node: SvgNode): Node {
|
|
105
|
+
if (typeof node === "string" || Array.isArray(node)) return this.renderer.createText(svgValue(node, this.uid))
|
|
106
|
+
const element = this.renderer.createElement(node.tag, "svg")
|
|
107
|
+
for (const [name, value] of Object.entries(svgAttributes(node, this.uid)))
|
|
108
|
+
this.renderer.setAttribute(element, name, value)
|
|
109
|
+
for (const child of node.children) this.renderer.appendChild(element, this.element(child))
|
|
110
|
+
return element
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createComponent,
|
|
3
|
+
createMemo,
|
|
4
|
+
createSignal,
|
|
5
|
+
isHydrating,
|
|
6
|
+
merge,
|
|
7
|
+
omit,
|
|
8
|
+
onCleanup,
|
|
9
|
+
type Component,
|
|
10
|
+
} from "solid-js"
|
|
11
|
+
import { dynamic, isServer, spread, type JSX } from "@solidjs/web"
|
|
12
|
+
import { useApp, useRuntimeConfig, useHead, useId } from "@hamur/adapter-solid"
|
|
13
|
+
import type { SvgNode } from "hamur/adapter"
|
|
14
|
+
import { SVG_NAMESPACE, svgAttributes, svgValue } from "../render.ts"
|
|
15
|
+
import { iconHead, type Icons } from "../runtime/icons.ts"
|
|
16
|
+
import type { IconDefaults, IconProps } from "../types.ts"
|
|
17
|
+
|
|
18
|
+
export type SolidIconProps = IconProps & Omit<JSX.SvgSVGAttributes<SVGSVGElement>, keyof IconProps | "ref" | "children">
|
|
19
|
+
|
|
20
|
+
const OWN = ["name", "size", "mode", "title", "customize", "class"] as const
|
|
21
|
+
|
|
22
|
+
const tags = new Map<string, Component<Record<string, unknown>>>()
|
|
23
|
+
|
|
24
|
+
function tagged(tag: string): Component<Record<string, unknown>> {
|
|
25
|
+
let made = tags.get(tag)
|
|
26
|
+
if (!made) tags.set(tag, (made = dynamic(() => tag, { static: true }) as Component<Record<string, unknown>>))
|
|
27
|
+
return made
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Solid makes a tag outside its SVG list (`title`, `a`) as an HTML element, so a client render creates each element in the SVG namespace itself. */
|
|
31
|
+
function svgElement(tag: string, props: Record<string, unknown>): JSX.Element {
|
|
32
|
+
if (isServer || isHydrating()) return createComponent(tagged(tag), props)
|
|
33
|
+
const element = document.createElementNS(SVG_NAMESPACE, tag)
|
|
34
|
+
spread(element, props)
|
|
35
|
+
return element as unknown as JSX.Element
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Solid's client writes a style prop through cssText, which rewrites the server's text, so a ref sets it as written.
|
|
39
|
+
function withStyle(attrs: Record<string, string>): Record<string, unknown> {
|
|
40
|
+
const { style, ...rest } = attrs
|
|
41
|
+
const out: Record<string, unknown> = { ...rest }
|
|
42
|
+
if (style === undefined) return out
|
|
43
|
+
return isServer ? { ...out, style } : { ...out, ref: (element: Element) => element.setAttribute("style", style) }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function element(node: SvgNode, uid: string): JSX.Element {
|
|
47
|
+
if (typeof node === "string" || Array.isArray(node)) return svgValue(node, uid)
|
|
48
|
+
return svgElement(node.tag, {
|
|
49
|
+
...withStyle(svgAttributes(node, uid)),
|
|
50
|
+
get children() {
|
|
51
|
+
return node.children.map((child) => element(child, uid))
|
|
52
|
+
},
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** `Icon` over `icons`, so a test renders it without the build's options module. */
|
|
57
|
+
export const createIcon = (icons: Icons) =>
|
|
58
|
+
function Icon(props: SolidIconProps): JSX.Element {
|
|
59
|
+
const own = props
|
|
60
|
+
const rest = omit(props, ...OWN)
|
|
61
|
+
const app = useApp()
|
|
62
|
+
const config = useRuntimeConfig().public as { icon?: IconDefaults }
|
|
63
|
+
const uid = useId().replace(/[^\w-]/g, "_")
|
|
64
|
+
const [answers, answered] = createSignal(icons.version())
|
|
65
|
+
onCleanup(icons.subscribe(() => answered(icons.version())))
|
|
66
|
+
const view = createMemo(() => {
|
|
67
|
+
answers()
|
|
68
|
+
return icons.view(
|
|
69
|
+
app,
|
|
70
|
+
{
|
|
71
|
+
name: own.name,
|
|
72
|
+
size: own.size,
|
|
73
|
+
mode: own.mode,
|
|
74
|
+
title: own.title,
|
|
75
|
+
customize: own.customize,
|
|
76
|
+
class: typeof own.class === "string" && own.class !== "" ? own.class : undefined,
|
|
77
|
+
labelled: rest["aria-label"] != null || rest["aria-labelledby"] != null,
|
|
78
|
+
},
|
|
79
|
+
config.icon,
|
|
80
|
+
)
|
|
81
|
+
})
|
|
82
|
+
useHead(() => iconHead(view()))
|
|
83
|
+
return (() => {
|
|
84
|
+
const current = view()
|
|
85
|
+
if (current.mode === "css") return createComponent(tagged("span"), merge(withStyle(current.attrs), rest))
|
|
86
|
+
const children = () => [
|
|
87
|
+
...(current.title === undefined ? [] : [svgElement("title", { children: current.title })]),
|
|
88
|
+
...current.nodes.map((node) => element(node, uid)),
|
|
89
|
+
]
|
|
90
|
+
return svgElement(
|
|
91
|
+
"svg",
|
|
92
|
+
merge(withStyle(current.attrs), rest, {
|
|
93
|
+
get children() {
|
|
94
|
+
return children()
|
|
95
|
+
},
|
|
96
|
+
}),
|
|
97
|
+
)
|
|
98
|
+
}) as unknown as JSX.Element
|
|
99
|
+
}
|