@kayjs/icons 0.0.0-stage → 2.0.0-alpha.4

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/package.json CHANGED
@@ -1,6 +1,26 @@
1
1
  {
2
2
  "name": "@kayjs/icons",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "2.0.0-alpha.4",
4
+ "description": "Compile-time Iconify SVG icon plugin (~icons/<set>/<name>) for Kay v2",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "files": [
9
+ "src"
10
+ ],
11
+ "exports": {
12
+ ".": "./src/index.ts",
13
+ "./ambient": "./src/ambient.d.ts"
14
+ },
15
+ "engines": {
16
+ "bun": ">=1.4.0"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/Kayno0o/kayjs",
21
+ "directory": "packages/plugin-icons"
22
+ },
23
+ "peerDependencies": {
24
+ "kay": "^2.0.0-alpha.4"
25
+ }
26
+ }
package/src/Icon.tsx ADDED
@@ -0,0 +1,25 @@
1
+ import type { IconData } from './types'
2
+
3
+ export interface IconComponentProps {
4
+ icon: IconData
5
+ class?: string | undefined
6
+ className?: string | undefined
7
+ style?: string | Record<string, string | number | undefined> | undefined
8
+ 'aria-label'?: string | undefined
9
+ [attr: string]: unknown
10
+ }
11
+
12
+ export function Icon({ icon, ...rest }: IconComponentProps) {
13
+ const hasLabel = rest['aria-label'] !== undefined
14
+ return (
15
+ <svg
16
+ viewBox={icon.viewBox}
17
+ width={icon.width}
18
+ height={icon.height}
19
+ aria-hidden={hasLabel ? undefined : 'true'}
20
+ role={hasLabel ? 'img' : undefined}
21
+ {...rest}
22
+ innerHTML={{ __html: icon.body }}
23
+ />
24
+ )
25
+ }
@@ -0,0 +1,5 @@
1
+ declare module '~icons/*' {
2
+ import type { IconData } from '@kayjs/icons'
3
+ const icon: IconData
4
+ export default icon
5
+ }
package/src/index.ts ADDED
@@ -0,0 +1,117 @@
1
+ // @kayjs/icons — Zero-runtime inline SVG icons from @iconify-json/* via `import arrow from '~icons/tabler/arrow-right'`.
2
+ // Built strictly on the public KayPlugin API (bunPlugins).
3
+
4
+ import type { BunPlugin } from 'bun'
5
+ import type { KayPlugin } from 'kay/server'
6
+ import type { IconData, IconifyIcon, IconSet } from './types'
7
+
8
+ export { Icon } from './Icon'
9
+ export type { IconComponentProps } from './Icon'
10
+ export type { IconData, IconifyIcon, IconSet } from './types'
11
+
12
+ const DEFAULT_SIZE = 16
13
+
14
+ function resolveIcon(set: IconSet, name: string, depth: number): IconifyIcon | undefined {
15
+ const own = set.icons[name]
16
+ if (own !== undefined)
17
+ return own
18
+
19
+ const alias = set.aliases?.[name]
20
+ if (alias?.parent === undefined || alias.parent === '' || depth > 8)
21
+ return undefined
22
+
23
+ const parent = resolveIcon(set, alias.parent, depth + 1)
24
+ if (parent === undefined)
25
+ return undefined
26
+
27
+ return {
28
+ ...parent,
29
+ ...alias,
30
+ body: parent.body,
31
+ hFlip: Boolean(parent.hFlip) !== Boolean(alias.hFlip),
32
+ vFlip: Boolean(parent.vFlip) !== Boolean(alias.vFlip),
33
+ rotate: (parent.rotate ?? 0) + (alias.rotate ?? 0),
34
+ }
35
+ }
36
+
37
+ export function iconData(set: IconSet, name: string): IconData | undefined {
38
+ const ownPrefix = `${set.prefix}:`
39
+ const icon = resolveIcon(set, name.startsWith(ownPrefix) ? name.slice(ownPrefix.length) : name, 0)
40
+ if (icon?.body === undefined || icon.body === '')
41
+ return undefined
42
+
43
+ const width = icon.width ?? set.width ?? DEFAULT_SIZE
44
+ const height = icon.height ?? set.height ?? DEFAULT_SIZE
45
+ const left = icon.left ?? 0
46
+ const top = icon.top ?? 0
47
+ const transforms: string[] = []
48
+
49
+ if (icon.hFlip)
50
+ transforms.push(`translate(${left * 2 + width} 0) scale(-1 1)`)
51
+ if (icon.vFlip)
52
+ transforms.push(`translate(0 ${top * 2 + height}) scale(1 -1)`)
53
+ if (icon.rotate !== undefined && icon.rotate !== 0)
54
+ transforms.push(`rotate(${(icon.rotate % 4) * 90} ${left + width / 2} ${top + height / 2})`)
55
+
56
+ const body = transforms.length > 0 ? `<g transform="${transforms.join(' ')}">${icon.body}</g>` : icon.body
57
+
58
+ return {
59
+ viewBox: `${left} ${top} ${width} ${height}`,
60
+ width: `${Math.round((width / height) * 1000) / 1000}em`,
61
+ height: '1em',
62
+ body,
63
+ }
64
+ }
65
+
66
+ export interface IconsPluginOptions {
67
+ /** Optional preloaded custom icon sets keyed by prefix (e.g. `{ custom: mySet }`). */
68
+ sets?: Record<string, IconSet> | undefined
69
+ }
70
+
71
+ export function icons(options: IconsPluginOptions = {}): KayPlugin {
72
+ const cache = new Map<string, IconSet>(Object.entries(options.sets ?? {}))
73
+
74
+ return {
75
+ name: '@kayjs/icons',
76
+ bunPlugins: ({ root }): BunPlugin[] => [{
77
+ name: 'kayjs-icons',
78
+ setup(b) {
79
+ b.onResolve({ filter: /^~icons\/([^/]+)\/(.+)$/ }, (args) => {
80
+ return { path: args.path, namespace: 'kay-icon' }
81
+ })
82
+
83
+ b.onLoad({ filter: /.*/, namespace: 'kay-icon' }, async (args) => {
84
+ const match = /^~icons\/([^/]+)\/(.+)$/.exec(args.path)
85
+ if (match === null)
86
+ throw new Error(`@kayjs/icons: invalid specifier "${args.path}" (expected ~icons/<prefix>/<name>)`)
87
+
88
+ const prefix = match[1]!
89
+ const name = match[2]!
90
+ let set = cache.get(prefix)
91
+
92
+ if (set === undefined) {
93
+ const pkgPath = `@iconify-json/${prefix}/icons.json`
94
+ let resolved: string
95
+ try {
96
+ resolved = Bun.resolveSync(pkgPath, root)
97
+ }
98
+ catch {
99
+ throw new Error(`@kayjs/icons: could not resolve "${pkgPath}". Install @iconify-json/${prefix}.`)
100
+ }
101
+ set = await Bun.file(resolved).json() as IconSet
102
+ cache.set(prefix, set)
103
+ }
104
+
105
+ const data = iconData(set, name)
106
+ if (data === undefined)
107
+ throw new Error(`@kayjs/icons: icon "${name}" not found in set "${prefix}"`)
108
+
109
+ return {
110
+ contents: `export default ${JSON.stringify(data)};\n`,
111
+ loader: 'js',
112
+ }
113
+ })
114
+ },
115
+ }],
116
+ }
117
+ }
package/src/types.ts ADDED
@@ -0,0 +1,26 @@
1
+ export interface IconData {
2
+ viewBox: string
3
+ width: string
4
+ height: string
5
+ body: string
6
+ }
7
+
8
+ export interface IconifyIcon {
9
+ body?: string | undefined
10
+ parent?: string | undefined
11
+ width?: number | undefined
12
+ height?: number | undefined
13
+ left?: number | undefined
14
+ top?: number | undefined
15
+ rotate?: number | undefined
16
+ hFlip?: boolean | undefined
17
+ vFlip?: boolean | undefined
18
+ }
19
+
20
+ export interface IconSet {
21
+ prefix: string
22
+ icons: Record<string, IconifyIcon>
23
+ aliases?: Record<string, IconifyIcon> | undefined
24
+ width?: number | undefined
25
+ height?: number | undefined
26
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.