kiru 0.44.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.
Files changed (70) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +5 -0
  3. package/package.json +81 -0
  4. package/src/appContext.ts +186 -0
  5. package/src/cloneVNode.ts +14 -0
  6. package/src/constants.ts +146 -0
  7. package/src/context.ts +56 -0
  8. package/src/dom.ts +712 -0
  9. package/src/element.ts +54 -0
  10. package/src/env.ts +6 -0
  11. package/src/error.ts +85 -0
  12. package/src/flags.ts +15 -0
  13. package/src/form/index.ts +662 -0
  14. package/src/form/types.ts +261 -0
  15. package/src/form/utils.ts +19 -0
  16. package/src/generateId.ts +19 -0
  17. package/src/globalContext.ts +161 -0
  18. package/src/globals.ts +21 -0
  19. package/src/hmr.ts +178 -0
  20. package/src/hooks/index.ts +14 -0
  21. package/src/hooks/useAsync.ts +136 -0
  22. package/src/hooks/useCallback.ts +31 -0
  23. package/src/hooks/useContext.ts +79 -0
  24. package/src/hooks/useEffect.ts +44 -0
  25. package/src/hooks/useEffectEvent.ts +24 -0
  26. package/src/hooks/useId.ts +42 -0
  27. package/src/hooks/useLayoutEffect.ts +47 -0
  28. package/src/hooks/useMemo.ts +33 -0
  29. package/src/hooks/useReducer.ts +50 -0
  30. package/src/hooks/useRef.ts +40 -0
  31. package/src/hooks/useState.ts +62 -0
  32. package/src/hooks/useSyncExternalStore.ts +59 -0
  33. package/src/hooks/useViewTransition.ts +26 -0
  34. package/src/hooks/utils.ts +259 -0
  35. package/src/hydration.ts +67 -0
  36. package/src/index.ts +61 -0
  37. package/src/jsx.ts +11 -0
  38. package/src/lazy.ts +238 -0
  39. package/src/memo.ts +48 -0
  40. package/src/portal.ts +43 -0
  41. package/src/profiling.ts +105 -0
  42. package/src/props.ts +36 -0
  43. package/src/reconciler.ts +531 -0
  44. package/src/renderToString.ts +91 -0
  45. package/src/router/index.ts +2 -0
  46. package/src/router/route.ts +51 -0
  47. package/src/router/router.ts +275 -0
  48. package/src/router/routerUtils.ts +49 -0
  49. package/src/scheduler.ts +522 -0
  50. package/src/signals/base.ts +237 -0
  51. package/src/signals/computed.ts +139 -0
  52. package/src/signals/effect.ts +60 -0
  53. package/src/signals/globals.ts +11 -0
  54. package/src/signals/index.ts +12 -0
  55. package/src/signals/jsx.ts +45 -0
  56. package/src/signals/types.ts +10 -0
  57. package/src/signals/utils.ts +12 -0
  58. package/src/signals/watch.ts +151 -0
  59. package/src/ssr/client.ts +29 -0
  60. package/src/ssr/hydrationBoundary.ts +63 -0
  61. package/src/ssr/index.ts +1 -0
  62. package/src/ssr/server.ts +124 -0
  63. package/src/store.ts +241 -0
  64. package/src/swr.ts +360 -0
  65. package/src/transition.ts +80 -0
  66. package/src/types.dom.ts +1250 -0
  67. package/src/types.ts +209 -0
  68. package/src/types.utils.ts +39 -0
  69. package/src/utils.ts +581 -0
  70. package/src/warning.ts +9 -0
package/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2024-present, Rob (LankyMoose) Austen and Kaioken contributors
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # **Kaioken**
2
+
3
+ #### A powerful, easy-to-use rendering library with a tiny footprint
4
+
5
+ ### <a href="https://kaioken.dev">kaioken.dev</a>
package/package.json ADDED
@@ -0,0 +1,81 @@
1
+ {
2
+ "name": "kiru",
3
+ "version": "0.44.4",
4
+ "description": "A batteries-included, easy-to-use rendering library with a tiny footprint",
5
+ "main": "dist/index.js",
6
+ "type": "module",
7
+ "homepage": "https://kiru.dev",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "./form": {
14
+ "types": "./dist/form/index.d.ts",
15
+ "default": "./dist/form/index.js"
16
+ },
17
+ "./router": {
18
+ "types": "./dist/router/index.d.ts",
19
+ "default": "./dist/router/index.js"
20
+ },
21
+ "./swr": {
22
+ "types": "./dist/swr.d.ts",
23
+ "default": "./dist/swr.js"
24
+ },
25
+ "./utils": {
26
+ "types": "./dist/utils.d.ts",
27
+ "default": "./dist/utils.js"
28
+ },
29
+ "./ssr": {
30
+ "types": "./dist/ssr/index.d.ts",
31
+ "default": "./dist/ssr/index.js"
32
+ },
33
+ "./ssr/client": {
34
+ "types": "./dist/ssr/client.d.ts",
35
+ "default": "./dist/ssr/client.js"
36
+ },
37
+ "./ssr/server": {
38
+ "types": "./dist/ssr/server.d.ts",
39
+ "default": "./dist/ssr/server.js"
40
+ },
41
+ "./jsx-dev-runtime": {
42
+ "types": "./dist/jsx.d.ts",
43
+ "default": "./dist/jsx.js"
44
+ },
45
+ "./jsx-runtime": {
46
+ "types": "./dist/jsx.d.ts",
47
+ "default": "./dist/jsx.js"
48
+ }
49
+ },
50
+ "repository": {
51
+ "type": "git",
52
+ "url": "https://github.com/kirujs/kiru"
53
+ },
54
+ "types": "index.d.ts",
55
+ "keywords": [
56
+ "kiru",
57
+ "kirujs",
58
+ "kiru.js",
59
+ "dom",
60
+ "jsx",
61
+ "html",
62
+ "virtual-dom",
63
+ "reactive",
64
+ "signal"
65
+ ],
66
+ "author": "LankyMoose",
67
+ "license": "ISC",
68
+ "devDependencies": {
69
+ "@types/chai": "^5.2.2",
70
+ "@types/jsdom": "^21.1.7",
71
+ "@types/mocha": "^10.0.10",
72
+ "chai": "^5.2.1",
73
+ "ts-mocha": "^10.1.0",
74
+ "typescript": "^5.9.2"
75
+ },
76
+ "scripts": {
77
+ "build": "rm -rf dist && pnpm i && tsc",
78
+ "test": "node --test",
79
+ "dev": "tsc --watch"
80
+ }
81
+ }
@@ -0,0 +1,186 @@
1
+ import { flags } from "./flags.js"
2
+ import { FLAG } from "./constants.js"
3
+ import { createElement } from "./element.js"
4
+ import { __DEV__ } from "./env.js"
5
+ import { KaiokenError } from "./error.js"
6
+ import { renderMode } from "./globals.js"
7
+ import { hydrationStack } from "./hydration.js"
8
+ import { createScheduler, Scheduler } from "./scheduler.js"
9
+ import { generateRandomID } from "./generateId.js"
10
+
11
+ type VNode = Kaioken.VNode
12
+
13
+ export interface AppContextOptions {
14
+ root?: HTMLElement
15
+ /**
16
+ * @internal
17
+ * Sets the root node type for the app. Used for SSR & rendering without the DOM.
18
+ */
19
+ rootType?: ({ children }: { children: JSX.Children }) => JSX.Element
20
+ /**
21
+ * Sets the maximum render refresh time.
22
+ * @default 50
23
+ */
24
+ maxFrameMs?: number
25
+ name?: string
26
+ debug?: {
27
+ onRequestUpdate?: (vNode: VNode) => void
28
+ }
29
+ }
30
+
31
+ export interface AppContext<T extends Record<string, unknown> = {}> {
32
+ id: string
33
+ name: string
34
+ scheduler?: Scheduler
35
+ rootNode?: VNode
36
+ root?: HTMLElement
37
+ mounted: boolean
38
+ mount(): Promise<AppContext<T>>
39
+ unmount(): Promise<AppContext<T>>
40
+ setProps(fn: (oldProps: T) => T): Promise<AppContext<T>>
41
+ flushSync(): void
42
+ requestUpdate(vNode?: VNode): void
43
+ requestDelete(vNode: VNode): void
44
+ }
45
+
46
+ export function createAppContext<T extends Record<string, unknown> = {}>(
47
+ appFunc: (props: T) => JSX.Element,
48
+ appProps = {} as T,
49
+ options?: AppContextOptions
50
+ ): AppContext<T> {
51
+ const id = generateRandomID()
52
+ const name = options?.name ?? "App-" + id
53
+ let root = options?.root
54
+ let scheduler: Scheduler | undefined
55
+ let mounted = false
56
+ let appContext: AppContext<T>
57
+
58
+ const appNode = createElement(appFunc, appProps as T)
59
+ appNode.depth = 1
60
+ const rootNode = options?.rootType
61
+ ? createElement(options.rootType, {}, appNode)
62
+ : root
63
+ ? createElement(root!.nodeName.toLowerCase(), {}, appNode)
64
+ : void 0
65
+ if (rootNode) {
66
+ rootNode.depth = 0
67
+ }
68
+
69
+ function mount(): Promise<AppContext<T>> {
70
+ return new Promise<AppContext<T>>((resolve, reject) => {
71
+ if (mounted) return resolve(appContext)
72
+ if (!rootNode) return reject(new KaiokenError("Root node not configured"))
73
+ root ??= document.body
74
+ rootNode.dom = root
75
+ if (__DEV__) {
76
+ root.__kaiokenNode = rootNode
77
+ }
78
+ scheduler = createScheduler(appContext, options?.maxFrameMs ?? 50)
79
+ if (renderMode.current === "hydrate") {
80
+ hydrationStack.captureEvents(root!)
81
+ }
82
+ scheduler.nextIdle(() => {
83
+ if (renderMode.current === "hydrate") {
84
+ hydrationStack.releaseEvents(root!)
85
+ }
86
+ mounted = true
87
+ window.__kaioken?.emit("mount", appContext as AppContext<any>)
88
+ resolve(appContext)
89
+ }, false)
90
+ scheduler.queueUpdate(rootNode)
91
+ scheduler.flushSync()
92
+ })
93
+ }
94
+
95
+ function unmount(): Promise<AppContext<T>> {
96
+ return new Promise<AppContext<T>>((resolve) => {
97
+ if (!mounted) return resolve(appContext)
98
+ if (!rootNode?.child) return resolve(appContext)
99
+ requestDelete(rootNode.child)
100
+
101
+ scheduler?.nextIdle(() => {
102
+ scheduler = undefined
103
+ rootNode && (rootNode.child = null)
104
+ mounted = false
105
+ window.__kaioken?.emit("unmount", appContext as AppContext<any>)
106
+ resolve(appContext)
107
+ })
108
+ })
109
+ }
110
+
111
+ function setProps(fn: (oldProps: T) => T): Promise<AppContext<T>> {
112
+ const rootChild = rootNode?.child
113
+ if (!mounted || !rootChild || !scheduler)
114
+ throw new KaiokenError(
115
+ "Failed to apply new props - ensure the app is mounted"
116
+ )
117
+ return new Promise<AppContext<T>>((resolve) => {
118
+ scheduler!.clear()
119
+ const { children, ref, key, ...rest } = rootChild.props
120
+ rootChild.props = {
121
+ ...Object.assign(rest, fn(rest as T)),
122
+ children,
123
+ ref,
124
+ key,
125
+ }
126
+ scheduler!.queueUpdate(rootChild)
127
+ scheduler!.nextIdle(() => resolve(appContext))
128
+ })
129
+ }
130
+
131
+ function flushSync(): void {
132
+ scheduler?.flushSync()
133
+ }
134
+
135
+ function requestUpdate(vNode?: VNode): void {
136
+ if (!vNode) {
137
+ if (!mounted || !rootNode) return
138
+ vNode = rootNode
139
+ }
140
+ if (flags.get(vNode.flags, FLAG.DELETION)) return
141
+ if (__DEV__) {
142
+ if (options?.debug?.onRequestUpdate) {
143
+ options.debug.onRequestUpdate(vNode)
144
+ }
145
+ }
146
+ if (renderMode.current === "hydrate") {
147
+ return scheduler?.nextIdle((s) => {
148
+ !flags.get(vNode.flags, FLAG.DELETION) && s.queueUpdate(vNode)
149
+ })
150
+ }
151
+ scheduler?.queueUpdate(vNode)
152
+ }
153
+
154
+ function requestDelete(vNode: VNode): void {
155
+ if (flags.get(vNode.flags, FLAG.DELETION)) return
156
+ if (renderMode.current === "hydrate") {
157
+ return scheduler?.nextIdle((s) => {
158
+ !flags.get(vNode.flags, FLAG.DELETION) && s.queueDelete(vNode)
159
+ })
160
+ }
161
+ scheduler?.queueDelete(vNode)
162
+ }
163
+
164
+ return (appContext = {
165
+ id,
166
+ name,
167
+ get scheduler() {
168
+ return scheduler
169
+ },
170
+ get rootNode() {
171
+ return rootNode
172
+ },
173
+ get root() {
174
+ return root
175
+ },
176
+ get mounted() {
177
+ return mounted
178
+ },
179
+ mount,
180
+ unmount,
181
+ setProps,
182
+ flushSync,
183
+ requestUpdate,
184
+ requestDelete,
185
+ })
186
+ }
@@ -0,0 +1,14 @@
1
+ import { createElement } from "./element.js"
2
+ import { isVNode } from "./utils.js"
3
+
4
+ export function cloneVNode(vNode: Kaioken.VNode): Kaioken.VNode {
5
+ const children = vNode.props.children
6
+ let clonedChildren: unknown
7
+ if (isVNode(children)) {
8
+ clonedChildren = cloneVNode(children)
9
+ } else if (Array.isArray(children)) {
10
+ clonedChildren = children.map((c) => (isVNode(c) ? cloneVNode(c) : c))
11
+ }
12
+
13
+ return createElement(vNode.type, { ...vNode.props, children: clonedChildren })
14
+ }
@@ -0,0 +1,146 @@
1
+ export {
2
+ $SIGNAL,
3
+ $CONTEXT,
4
+ $CONTEXT_PROVIDER,
5
+ $FRAGMENT,
6
+ $KAIOKEN_ERROR,
7
+ $HMR_ACCEPT,
8
+ $MEMO,
9
+ $HYDRATION_BOUNDARY,
10
+ CONSECUTIVE_DIRTY_LIMIT,
11
+ FLAG,
12
+ REGEX_UNIT,
13
+ }
14
+
15
+ export { voidElements, svgTags, booleanAttributes }
16
+
17
+ const $SIGNAL = Symbol.for("kaioken.signal")
18
+ const $CONTEXT = Symbol.for("kaioken.context")
19
+ const $CONTEXT_PROVIDER = Symbol.for("kaioken.contextProvider")
20
+ const $FRAGMENT = Symbol.for("kaioken.fragment")
21
+ const $KAIOKEN_ERROR = Symbol.for("kaioken.error")
22
+ const $HMR_ACCEPT = Symbol.for("kaioken.hmrAccept")
23
+ const $MEMO = Symbol.for("kaioken.memo")
24
+ const $HYDRATION_BOUNDARY = Symbol.for("kaioken.hydrationBoundary")
25
+
26
+ const CONSECUTIVE_DIRTY_LIMIT = 50
27
+
28
+ const FLAG = {
29
+ UPDATE: 1,
30
+ PLACEMENT: 2,
31
+ DELETION: 3,
32
+ HAS_MEMO_ANCESTOR: 4,
33
+ } as const
34
+
35
+ const REGEX_UNIT = {
36
+ AMP_G: /&/g,
37
+ LT_G: /</g,
38
+ GT_G: />/g,
39
+ SQT_G: /'/g,
40
+ DBLQT_G: /"/g,
41
+ SLASH_G: /\//g,
42
+ SLASHN_SLASHR_G: /[\n\r]+/g,
43
+ ALPHA_UPPER_G: /[A-Z]/g,
44
+ } as const
45
+
46
+ const voidElements = new Set([
47
+ "area",
48
+ "base",
49
+ "br",
50
+ "col",
51
+ "embed",
52
+ "hr",
53
+ "img",
54
+ "input",
55
+ "link",
56
+ "meta",
57
+ "source",
58
+ "track",
59
+ "wbr",
60
+ ])
61
+
62
+ const svgTags = new Set([
63
+ "animateTransform",
64
+ "circle",
65
+ "clipPath",
66
+ "defs",
67
+ "desc",
68
+ "ellipse",
69
+ "feBlend",
70
+ "feColorMatrix",
71
+ "feComponentTransfer",
72
+ "feComposite",
73
+ "feConvolveMatrix",
74
+ "feDiffuseLighting",
75
+ "feDisplacementMap",
76
+ "feFlood",
77
+ "feFuncA",
78
+ "feFuncB",
79
+ "feFuncG",
80
+ "feFuncR",
81
+ "feGaussianBlur",
82
+ "feMerge",
83
+ "feMergeNode",
84
+ "feMorphology",
85
+ "feOffset",
86
+ "feSpecularLighting",
87
+ "feTile",
88
+ "feTurbulence",
89
+ "filter",
90
+ "foreignObject",
91
+ "g",
92
+ "image",
93
+ "line",
94
+ "linearGradient",
95
+ "marker",
96
+ "path",
97
+ "polygon",
98
+ "polyline",
99
+ "radialGradient",
100
+ "rect",
101
+ "stop",
102
+ "svg",
103
+ "switch",
104
+ "symbol",
105
+ "text",
106
+ "textPath",
107
+ "title",
108
+ "tspan",
109
+ "use",
110
+ ])
111
+
112
+ const booleanAttributes = new Set([
113
+ "allowfullscreen",
114
+ "autofocus",
115
+ "autoplay",
116
+ "async",
117
+ "checked",
118
+ "compact",
119
+ "controls",
120
+ "contenteditable",
121
+ "declare",
122
+ "default",
123
+ "defer",
124
+ "disabled",
125
+ "download",
126
+ "hidden",
127
+ "inert",
128
+ "ismap",
129
+ "multiple",
130
+ "nohref",
131
+ "noresize",
132
+ "noshade",
133
+ "novalidate",
134
+ "nowrap",
135
+ "open",
136
+ "popover",
137
+ "readonly",
138
+ "required",
139
+ "sandbox",
140
+ "scoped",
141
+ "selected",
142
+ "sortable",
143
+ "spellcheck",
144
+ "translate",
145
+ "wrap",
146
+ ])
package/src/context.ts ADDED
@@ -0,0 +1,56 @@
1
+ import { $CONTEXT, $CONTEXT_PROVIDER, $HMR_ACCEPT } from "./constants.js"
2
+ import { createElement } from "./element.js"
3
+ import { __DEV__ } from "./env.js"
4
+ import { GenericHMRAcceptor } from "./hmr.js"
5
+ import { useState } from "./hooks/useState.js"
6
+ import { traverseApply } from "./utils.js"
7
+
8
+ export function createContext<T>(defaultValue: T): Kaioken.Context<T> {
9
+ const ctx: Kaioken.Context<T> = {
10
+ [$CONTEXT]: true,
11
+ Provider: ({ value, children }: Kaioken.ProviderProps<T>) => {
12
+ const [dependents] = useState(() => new Set<Kaioken.VNode>())
13
+ return createElement(
14
+ $CONTEXT_PROVIDER,
15
+ { value, ctx, dependents },
16
+ typeof children === "function" ? children(value) : children
17
+ )
18
+ },
19
+ default: () => defaultValue,
20
+ set displayName(name: string) {
21
+ this.Provider.displayName = name
22
+ },
23
+ get displayName() {
24
+ return this.Provider.displayName || "Anonymous Context"
25
+ },
26
+ }
27
+ if (__DEV__) {
28
+ const asHmrAcceptor = ctx as any as GenericHMRAcceptor<Kaioken.Context<T>>
29
+ asHmrAcceptor[$HMR_ACCEPT] = {
30
+ inject: (prev) => {
31
+ const newProvider = ctx.Provider
32
+ window.__kaioken!.apps.forEach((ctx) => {
33
+ if (!ctx.mounted || !ctx.rootNode) return
34
+ traverseApply(ctx.rootNode, (vNode) => {
35
+ if (vNode.type === prev.Provider) {
36
+ vNode.type = newProvider
37
+ vNode.hmrUpdated = true
38
+ if (vNode.prev) {
39
+ vNode.prev.type = newProvider
40
+ }
41
+ ctx.requestUpdate(vNode)
42
+ }
43
+ })
44
+ })
45
+ },
46
+ destroy: () => {},
47
+ provide: () => ctx,
48
+ }
49
+ }
50
+
51
+ return ctx
52
+ }
53
+
54
+ export function isContext<T>(thing: unknown): thing is Kaioken.Context<T> {
55
+ return typeof thing === "object" && !!thing && $CONTEXT in thing
56
+ }