acrylblends 0.6.2 → 0.6.3
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/README.md +4 -1
- package/docs/framework/README.md +3 -2
- package/docs/framework/UPSTREAM.md +1 -1
- package/docs/framework/cordis/acryl-in-a-cordis-app.md +6 -0
- package/docs/framework/getting-started/basic.md +1 -0
- package/docs/framework/getting-started/desktop.md +41 -0
- package/docs/framework/guides/jsx-dom.md +0 -3
- package/docs/framework/guides/jsx.md +0 -3
- package/docs/framework/helpers/css.md +0 -3
- package/docs/framework/upstream.json +3 -3
- package/lib/accepts.d.ts +1 -1
- package/lib/acryl-base.d.ts +1 -1
- package/lib/adapter.d.ts +1 -1
- package/lib/{app-BdrVHBlJ.d.ts → app-wFwpg-bt.d.ts} +1 -1
- package/lib/base-BpQy2Rk7.d.ts +783 -0
- package/lib/base-ddiVmiE7.js +721 -0
- package/lib/basic-auth.d.ts +1 -1
- package/lib/bearer-auth.d.ts +1 -1
- package/lib/body-limit.d.ts +1 -1
- package/lib/cache.d.ts +1 -1
- package/lib/client.d.ts +2 -2
- package/lib/combine.d.ts +1 -1
- package/lib/common-D8Z4JRCw.d.ts +46 -0
- package/lib/components-Ce5FRyHU.js +268 -0
- package/lib/components-DYZdxJIJ.js +24 -0
- package/lib/compress.d.ts +1 -1
- package/lib/conninfo.d.ts +2 -2
- package/lib/constants-BR9jM9BC.js +9 -0
- package/lib/{context-B5Y1lLha.d.ts → context-DVQBjuns.d.ts} +1 -1
- package/lib/context-DxFdY_un.d.ts +250 -0
- package/lib/context-storage.d.ts +1 -1
- package/lib/{cookie-D9_nQbsM.d.ts → cookie-CCelEqky.d.ts} +1 -1
- package/lib/cookie.d.ts +3 -3
- package/lib/cors.d.ts +1 -1
- package/lib/csrf.d.ts +1 -1
- package/lib/css-D2jb7X8b.js +320 -0
- package/lib/css.d.ts +82 -0
- package/lib/css.js +134 -0
- package/lib/dev.d.ts +2 -2
- package/lib/etag.d.ts +1 -1
- package/lib/factory.d.ts +2 -2
- package/lib/hooks-Bq5q9IMl.js +762 -0
- package/lib/html-SllAe0YT.d.ts +46 -0
- package/lib/html.d.ts +1 -1
- package/lib/index-C850MeBA.d.ts +94 -0
- package/lib/{index-m12zcmph.d.ts → index-CUTfU69R.d.ts} +3 -3
- package/lib/{index-DtEoak1i.d.ts → index-Cqcdluyn.d.ts} +2 -2
- package/lib/{index-Kqou8Fjh.d.ts → index-CsvB-F9j.d.ts} +2 -2
- package/lib/index.d.ts +4 -4
- package/lib/ip-restriction.d.ts +2 -2
- package/lib/jsx/dom/client.d.ts +30 -0
- package/lib/jsx/dom/client.js +49 -0
- package/lib/jsx/dom/css.d.ts +84 -0
- package/lib/jsx/dom/css.js +2 -0
- package/lib/jsx/dom/jsx-dev-runtime.d.ts +3 -0
- package/lib/jsx/dom/jsx-dev-runtime.js +2 -0
- package/lib/jsx/dom/jsx-runtime.d.ts +3 -0
- package/lib/jsx/dom/jsx-runtime.js +2 -0
- package/lib/jsx/dom/server.d.ts +105 -0
- package/lib/jsx/dom/server.js +37 -0
- package/lib/jsx/dom.d.ts +5 -0
- package/lib/jsx/dom.js +74 -0
- package/lib/jsx/jsx-dev-runtime.d.ts +6 -0
- package/lib/jsx/jsx-dev-runtime.js +14 -0
- package/lib/jsx/jsx-runtime.d.ts +10 -0
- package/lib/jsx/jsx-runtime.js +30 -0
- package/lib/jsx/streaming.d.ts +2 -0
- package/lib/jsx/streaming.js +2 -0
- package/lib/jsx-dev-runtime-B22O-Npk.d.ts +7 -0
- package/lib/jsx-dev-runtime-kAhR_4C7.js +354 -0
- package/lib/jsx-runtime-BVVgDSdq.d.ts +1 -0
- package/lib/jsx.d.ts +3 -0
- package/lib/jsx.js +47 -0
- package/lib/jwk.d.ts +4 -4
- package/lib/{jwt-BA65QhnJ.d.ts → jwt-BARhotDz.d.ts} +1 -1
- package/lib/jwt.d.ts +4 -4
- package/lib/language.d.ts +1 -1
- package/lib/logger.d.ts +1 -1
- package/lib/loopback-only.d.ts +1 -1
- package/lib/method-not-allowed.d.ts +2 -2
- package/lib/method-override.d.ts +2 -2
- package/lib/mount.d.ts +1 -1
- package/lib/node-server/conninfo.d.ts +1 -1
- package/lib/node-server/serve-static.d.ts +1 -1
- package/lib/node-server/serve-static.js +1 -1
- package/lib/node-server.d.ts +1 -1
- package/lib/node-server.js +1 -1
- package/lib/powered-by.d.ts +1 -1
- package/lib/pretty-json.d.ts +1 -1
- package/lib/proxy.d.ts +1 -1
- package/lib/quick.d.ts +1 -1
- package/lib/request-id.d.ts +1 -1
- package/lib/request.d.ts +1 -1
- package/lib/route.d.ts +1 -1
- package/lib/schema-validator.d.ts +1 -1
- package/lib/secure-headers.d.ts +1 -1
- package/lib/{serve-static-Dw_m6KxR.js → serve-static-Cw23shXv.js} +1 -0
- package/lib/serve-static.d.ts +1 -1
- package/lib/streaming.d.ts +1 -1
- package/lib/testing.d.ts +4 -4
- package/lib/timeout.d.ts +1 -1
- package/lib/timing.d.ts +1 -1
- package/lib/tiny.d.ts +1 -1
- package/lib/trailing-slash.d.ts +1 -1
- package/lib/{types-Bd29gh2o.d.ts → types-CQiGmsWW.d.ts} +3 -3
- package/lib/{types-D9DEXMjE.d.ts → types-Lqx5BxQF.d.ts} +8 -1
- package/lib/{types-ZM9gIiKh.d.ts → types-nEL8VPDC.d.ts} +1 -1
- package/lib/types.d.ts +1 -1
- package/lib/utils/body.d.ts +1 -1
- package/lib/utils/cookie.d.ts +1 -1
- package/lib/utils/crypto.d.ts +1 -1
- package/lib/utils/html.d.ts +1 -45
- package/lib/utils/ipaddr.d.ts +1 -1
- package/lib/utils/jwt.d.ts +1 -1
- package/lib/utils/types.d.ts +2 -2
- package/lib/validator.d.ts +2 -2
- package/lib/ws.d.ts +1 -1
- package/package.json +45 -1
- package/plugins/acryl-inspect/upstream.json +1 -1
- package/plugins/acryl-node-server/upstream.json +1 -1
- package/plugins/acryl-router/src/core/helper/css/common.case.test.tsx +506 -0
- package/plugins/acryl-router/src/core/helper/css/common.ts +337 -0
- package/plugins/acryl-router/src/core/helper/css/index.test.tsx +678 -0
- package/plugins/acryl-router/src/core/helper/css/index.ts +262 -0
- package/plugins/acryl-router/src/core/helper/streaming/sse.test.tsx +557 -0
- package/plugins/acryl-router/src/core/jsx/base.test.tsx +97 -0
- package/plugins/acryl-router/src/core/jsx/base.ts +490 -0
- package/plugins/acryl-router/src/core/jsx/children.test.ts +52 -0
- package/plugins/acryl-router/src/core/jsx/children.ts +20 -0
- package/plugins/acryl-router/src/core/jsx/components.test.tsx +936 -0
- package/plugins/acryl-router/src/core/jsx/components.ts +257 -0
- package/plugins/acryl-router/src/core/jsx/constants.ts +6 -0
- package/plugins/acryl-router/src/core/jsx/context-isolation.test.ts +655 -0
- package/plugins/acryl-router/src/core/jsx/context.ts +266 -0
- package/plugins/acryl-router/src/core/jsx/dom/client.test.tsx +128 -0
- package/plugins/acryl-router/src/core/jsx/dom/client.ts +89 -0
- package/plugins/acryl-router/src/core/jsx/dom/components.test.tsx +330 -0
- package/plugins/acryl-router/src/core/jsx/dom/components.ts +39 -0
- package/plugins/acryl-router/src/core/jsx/dom/context.test.tsx +184 -0
- package/plugins/acryl-router/src/core/jsx/dom/context.ts +52 -0
- package/plugins/acryl-router/src/core/jsx/dom/css.test.tsx +149 -0
- package/plugins/acryl-router/src/core/jsx/dom/css.ts +263 -0
- package/plugins/acryl-router/src/core/jsx/dom/hooks/index.test.tsx +172 -0
- package/plugins/acryl-router/src/core/jsx/dom/hooks/index.ts +91 -0
- package/plugins/acryl-router/src/core/jsx/dom/index.test.tsx +3681 -0
- package/plugins/acryl-router/src/core/jsx/dom/index.ts +170 -0
- package/plugins/acryl-router/src/core/jsx/dom/intrinsic-element/components.test.tsx +1344 -0
- package/plugins/acryl-router/src/core/jsx/dom/intrinsic-element/components.ts +438 -0
- package/plugins/acryl-router/src/core/jsx/dom/jsx-dev-runtime.ts +23 -0
- package/plugins/acryl-router/src/core/jsx/dom/jsx-runtime.ts +8 -0
- package/plugins/acryl-router/src/core/jsx/dom/render.ts +879 -0
- package/plugins/acryl-router/src/core/jsx/dom/server.test.tsx +199 -0
- package/plugins/acryl-router/src/core/jsx/dom/server.ts +76 -0
- package/plugins/acryl-router/src/core/jsx/dom/utils.ts +7 -0
- package/plugins/acryl-router/src/core/jsx/hooks/dom.test.tsx +859 -0
- package/plugins/acryl-router/src/core/jsx/hooks/index.ts +437 -0
- package/plugins/acryl-router/src/core/jsx/hooks/string.test.tsx +40 -0
- package/plugins/acryl-router/src/core/jsx/index.test.tsx +1744 -0
- package/plugins/acryl-router/src/core/jsx/index.ts +115 -0
- package/plugins/acryl-router/src/core/jsx/intrinsic-element/common.ts +23 -0
- package/plugins/acryl-router/src/core/jsx/intrinsic-element/components.test.tsx +666 -0
- package/plugins/acryl-router/src/core/jsx/intrinsic-element/components.ts +210 -0
- package/plugins/acryl-router/src/core/jsx/intrinsic-elements.ts +938 -0
- package/plugins/acryl-router/src/core/jsx/jsx-dev-runtime.ts +26 -0
- package/plugins/acryl-router/src/core/jsx/jsx-runtime.test.tsx +85 -0
- package/plugins/acryl-router/src/core/jsx/jsx-runtime.ts +51 -0
- package/plugins/acryl-router/src/core/jsx/streaming.test.tsx +1281 -0
- package/plugins/acryl-router/src/core/jsx/streaming.ts +231 -0
- package/plugins/acryl-router/src/core/jsx/types.ts +41 -0
- package/plugins/acryl-router/src/core/jsx/utils.test.ts +366 -0
- package/plugins/acryl-router/src/core/jsx/utils.ts +202 -0
- package/plugins/acryl-router/src/core/utils/types.ts +6 -0
- package/plugins/acryl-router/src/helpers/index.ts +1 -0
- package/plugins/acryl-router/tests/jsx.spec.tsx +63 -0
- package/plugins/acryl-router/upstream.json +3 -3
- package/plugins/acryl-router-http/upstream.json +1 -1
- package/plugins/acryl-state/upstream.json +1 -1
- package/plugins/desktop/upstream.json +1 -1
- package/plugins/desktop-web/upstream.json +1 -1
- package/plugins/save/plugin.json +1 -1
- package/templates/todo/upstream.json +1 -1
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/** @jsxImportSource ../ */
|
|
2
|
+
import { JSDOM } from 'jsdom'
|
|
3
|
+
// run tests by old style jsx default
|
|
4
|
+
// acryl-router/jsx/jsx-runtime and acryl-router/jsx/dom/jsx-runtime are tested in their respective settings
|
|
5
|
+
import { render } from './index.ts'
|
|
6
|
+
import type { JSXNode } from '../index.ts'
|
|
7
|
+
import { Style, createCssContext, css, rawCssString } from '../../helper/css/index.ts'
|
|
8
|
+
import { minify } from '../../helper/css/common.ts'
|
|
9
|
+
import { renderTest } from '../../helper/css/common.case.test.tsx'
|
|
10
|
+
|
|
11
|
+
describe('Style and css for jsx/dom', () => {
|
|
12
|
+
beforeAll(() => {
|
|
13
|
+
global.requestAnimationFrame = (cb) => setTimeout(cb)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
let dom: JSDOM
|
|
17
|
+
let root: HTMLElement
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
dom = new JSDOM('<html><body><div id="root"></div></body></html>', {
|
|
20
|
+
runScripts: 'dangerously',
|
|
21
|
+
})
|
|
22
|
+
global.document = dom.window.document
|
|
23
|
+
global.HTMLElement = dom.window.HTMLElement
|
|
24
|
+
global.SVGElement = dom.window.SVGElement
|
|
25
|
+
global.Text = dom.window.Text
|
|
26
|
+
root = document.getElementById('root') as HTMLElement
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('<Style />', async () => {
|
|
30
|
+
const App = () => {
|
|
31
|
+
return (
|
|
32
|
+
<div>
|
|
33
|
+
<Style />
|
|
34
|
+
<div
|
|
35
|
+
class={css`
|
|
36
|
+
color: red;
|
|
37
|
+
`}
|
|
38
|
+
>
|
|
39
|
+
red
|
|
40
|
+
</div>
|
|
41
|
+
</div>
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
render(<App />, root)
|
|
45
|
+
expect(root.innerHTML).toBe(
|
|
46
|
+
'<div><style id="acryl-css"></style><div class="css-3142110215">red</div></div>'
|
|
47
|
+
)
|
|
48
|
+
await Promise.resolve()
|
|
49
|
+
expect(root.querySelector('style')?.sheet?.cssRules[0].cssText).toBe(
|
|
50
|
+
'.css-3142110215 {color: red;}'
|
|
51
|
+
)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('<Style nonce="1234" />', async () => {
|
|
55
|
+
const App = () => {
|
|
56
|
+
return (
|
|
57
|
+
<div>
|
|
58
|
+
<Style nonce='1234' />
|
|
59
|
+
</div>
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
render(<App />, root)
|
|
63
|
+
expect(root.innerHTML).toBe('<div><style id="acryl-css" nonce="1234"></style></div>')
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('<Style>{css`global`}</Style>', async () => {
|
|
67
|
+
const App = () => {
|
|
68
|
+
return (
|
|
69
|
+
<div>
|
|
70
|
+
<Style>{css`
|
|
71
|
+
color: red;
|
|
72
|
+
`}</Style>
|
|
73
|
+
<div
|
|
74
|
+
class={css`
|
|
75
|
+
color: red;
|
|
76
|
+
`}
|
|
77
|
+
>
|
|
78
|
+
red
|
|
79
|
+
</div>
|
|
80
|
+
</div>
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
render(<App />, root)
|
|
84
|
+
expect(root.innerHTML).toBe(
|
|
85
|
+
'<div><style id="acryl-css">color:red</style><div class="css-3142110215">red</div></div>'
|
|
86
|
+
)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('classNameSlug with createCssContext', async () => {
|
|
90
|
+
const { css: customCss, Style: CustomStyle } = createCssContext({
|
|
91
|
+
id: 'custom-dom',
|
|
92
|
+
classNameSlug: (hash, label) => (label ? `h-${label}` : hash),
|
|
93
|
+
})
|
|
94
|
+
const App = () => {
|
|
95
|
+
return (
|
|
96
|
+
<div>
|
|
97
|
+
<CustomStyle />
|
|
98
|
+
<div
|
|
99
|
+
class={customCss`
|
|
100
|
+
/* hero */
|
|
101
|
+
color: blue;
|
|
102
|
+
`}
|
|
103
|
+
>
|
|
104
|
+
blue
|
|
105
|
+
</div>
|
|
106
|
+
</div>
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
render(<App />, root)
|
|
110
|
+
expect(root.innerHTML).toBe(
|
|
111
|
+
'<div><style id="custom-dom"></style><div class="h-hero">blue</div></div>'
|
|
112
|
+
)
|
|
113
|
+
await Promise.resolve()
|
|
114
|
+
expect(root.querySelector('style')?.sheet?.cssRules[0].cssText).toBe('.h-hero {color: blue;}')
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
describe('render', () => {
|
|
119
|
+
renderTest(() => {
|
|
120
|
+
const cssContext = createCssContext({ id: 'acryl-css' })
|
|
121
|
+
|
|
122
|
+
const dom = new JSDOM('<html><body><div id="root"></div></body></html>', {
|
|
123
|
+
runScripts: 'dangerously',
|
|
124
|
+
})
|
|
125
|
+
global.document = dom.window.document
|
|
126
|
+
global.HTMLElement = dom.window.HTMLElement
|
|
127
|
+
global.Text = dom.window.Text
|
|
128
|
+
const root = document.getElementById('root') as HTMLElement
|
|
129
|
+
|
|
130
|
+
const toString = async (node: JSXNode) => {
|
|
131
|
+
render(node, root)
|
|
132
|
+
await Promise.resolve()
|
|
133
|
+
const style = root.querySelector('style')
|
|
134
|
+
if (style) {
|
|
135
|
+
style.textContent = minify(
|
|
136
|
+
[...(style.sheet?.cssRules || [])].map((r) => r.cssText).join('') || ''
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
return root.innerHTML
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
toString,
|
|
144
|
+
rawCssString,
|
|
145
|
+
...cssContext,
|
|
146
|
+
support: { nest: false },
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
})
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module
|
|
3
|
+
* This module provides APIs that enable `acryl-router/jsx/dom` to support.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { FC, PropsWithChildren } from '../index.ts'
|
|
7
|
+
import type {
|
|
8
|
+
ClassNameSlug,
|
|
9
|
+
CssClassName,
|
|
10
|
+
CssVariableType,
|
|
11
|
+
OnInvalidSlug,
|
|
12
|
+
} from '../../helper/css/common.ts'
|
|
13
|
+
import {
|
|
14
|
+
CLASS_NAME,
|
|
15
|
+
DEFAULT_STYLE_ID,
|
|
16
|
+
PSEUDO_GLOBAL_SELECTOR,
|
|
17
|
+
SELECTOR,
|
|
18
|
+
SELECTORS,
|
|
19
|
+
STYLE_STRING,
|
|
20
|
+
cssCommon,
|
|
21
|
+
cxCommon,
|
|
22
|
+
keyframesCommon,
|
|
23
|
+
viewTransitionCommon,
|
|
24
|
+
} from '../../helper/css/common.ts'
|
|
25
|
+
export { rawCssString } from '../../helper/css/common.ts'
|
|
26
|
+
|
|
27
|
+
const splitRule = (rule: string): string[] => {
|
|
28
|
+
const result: string[] = []
|
|
29
|
+
let startPos = 0
|
|
30
|
+
let depth = 0
|
|
31
|
+
for (let i = 0, len = rule.length; i < len; i++) {
|
|
32
|
+
const char = rule[i]
|
|
33
|
+
|
|
34
|
+
// consume quote
|
|
35
|
+
|
|
36
|
+
if (char === "'" || char === '"') {
|
|
37
|
+
const quote = char
|
|
38
|
+
i++
|
|
39
|
+
for (; i < len; i++) {
|
|
40
|
+
if (rule[i] === '\\') {
|
|
41
|
+
i++
|
|
42
|
+
continue
|
|
43
|
+
}
|
|
44
|
+
if (rule[i] === quote) {
|
|
45
|
+
break
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
continue
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// comments are removed from the rule in advance
|
|
52
|
+
if (char === '{') {
|
|
53
|
+
depth++
|
|
54
|
+
continue
|
|
55
|
+
}
|
|
56
|
+
if (char === '}') {
|
|
57
|
+
depth--
|
|
58
|
+
if (depth === 0) {
|
|
59
|
+
result.push(rule.slice(startPos, i + 1))
|
|
60
|
+
startPos = i + 1
|
|
61
|
+
}
|
|
62
|
+
continue
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return result
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
interface CreateCssJsxDomObjectsType {
|
|
69
|
+
(args: { id: Readonly<string> }): readonly [
|
|
70
|
+
{
|
|
71
|
+
toString(this: CssClassName): string
|
|
72
|
+
},
|
|
73
|
+
FC<PropsWithChildren<void>>,
|
|
74
|
+
]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const createCssJsxDomObjects: CreateCssJsxDomObjectsType = ({ id }) => {
|
|
78
|
+
let styleSheet: CSSStyleSheet | null | undefined = undefined
|
|
79
|
+
const findStyleSheet = (): [CSSStyleSheet, Set<string>] | [] => {
|
|
80
|
+
if (!styleSheet) {
|
|
81
|
+
styleSheet = document.querySelector<HTMLStyleElement>(`style#${id}`)
|
|
82
|
+
?.sheet as CSSStyleSheet | null
|
|
83
|
+
if (styleSheet) {
|
|
84
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
85
|
+
;(styleSheet as any).addedStyles = new Set<string>()
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
89
|
+
return styleSheet ? [styleSheet, (styleSheet as any).addedStyles] : []
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const insertRule = (className: string, styleString: string) => {
|
|
93
|
+
const [sheet, addedStyles] = findStyleSheet()
|
|
94
|
+
if (!sheet || !addedStyles) {
|
|
95
|
+
Promise.resolve().then(() => {
|
|
96
|
+
if (!findStyleSheet()[0]) {
|
|
97
|
+
throw new Error('style sheet not found')
|
|
98
|
+
}
|
|
99
|
+
insertRule(className, styleString)
|
|
100
|
+
})
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (!addedStyles.has(className)) {
|
|
105
|
+
addedStyles.add(className)
|
|
106
|
+
;(className.startsWith(PSEUDO_GLOBAL_SELECTOR)
|
|
107
|
+
? splitRule(styleString)
|
|
108
|
+
: [`${className[0] === '@' ? '' : '.'}${className}{${styleString}}`]
|
|
109
|
+
).forEach((rule) => {
|
|
110
|
+
sheet.insertRule(rule, sheet.cssRules.length)
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const cssObject = {
|
|
116
|
+
toString(this: CssClassName): string {
|
|
117
|
+
const selector = this[SELECTOR]
|
|
118
|
+
insertRule(selector, this[STYLE_STRING])
|
|
119
|
+
this[SELECTORS].forEach(({ [CLASS_NAME]: className, [STYLE_STRING]: styleString }) => {
|
|
120
|
+
insertRule(className, styleString)
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
return this[CLASS_NAME]
|
|
124
|
+
},
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const Style: FC<PropsWithChildren<{ nonce?: string }>> = ({ children, nonce }) =>
|
|
128
|
+
({
|
|
129
|
+
tag: 'style',
|
|
130
|
+
props: {
|
|
131
|
+
id,
|
|
132
|
+
nonce,
|
|
133
|
+
children:
|
|
134
|
+
children &&
|
|
135
|
+
(Array.isArray(children) ? children : [children]).map(
|
|
136
|
+
(c) => (c as unknown as CssClassName)[STYLE_STRING]
|
|
137
|
+
),
|
|
138
|
+
},
|
|
139
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
140
|
+
}) as any
|
|
141
|
+
|
|
142
|
+
return [cssObject, Style] as const
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
interface CssType {
|
|
146
|
+
(strings: TemplateStringsArray, ...values: CssVariableType[]): string
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
interface CxType {
|
|
150
|
+
(...args: (string | boolean | null | undefined)[]): string
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
interface KeyframesType {
|
|
154
|
+
(strings: TemplateStringsArray, ...values: CssVariableType[]): CssClassName
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
interface ViewTransitionType {
|
|
158
|
+
(strings: TemplateStringsArray, ...values: CssVariableType[]): string
|
|
159
|
+
(content: string): string
|
|
160
|
+
(): string
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
interface DefaultContextType {
|
|
164
|
+
css: CssType
|
|
165
|
+
cx: CxType
|
|
166
|
+
keyframes: KeyframesType
|
|
167
|
+
viewTransition: ViewTransitionType
|
|
168
|
+
Style: FC<PropsWithChildren<void>>
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* @experimental
|
|
173
|
+
* `createCssContext` is an experimental feature.
|
|
174
|
+
* The API might be changed.
|
|
175
|
+
*
|
|
176
|
+
* @param options.id - The ID for the style element
|
|
177
|
+
* @param options.classNameSlug - Optional function to customize generated CSS class names
|
|
178
|
+
* @param options.onInvalidSlug - Optional callback function called when an invalid slug is returned from ClassNameSlug
|
|
179
|
+
*/
|
|
180
|
+
export const createCssContext = ({
|
|
181
|
+
id,
|
|
182
|
+
classNameSlug,
|
|
183
|
+
onInvalidSlug,
|
|
184
|
+
}: {
|
|
185
|
+
id: Readonly<string>
|
|
186
|
+
classNameSlug?: ClassNameSlug
|
|
187
|
+
onInvalidSlug?: OnInvalidSlug
|
|
188
|
+
}): DefaultContextType => {
|
|
189
|
+
const [cssObject, Style] = createCssJsxDomObjects({ id })
|
|
190
|
+
|
|
191
|
+
const newCssClassNameObject = (cssClassName: CssClassName): string => {
|
|
192
|
+
cssClassName.toString = cssObject.toString
|
|
193
|
+
return cssClassName as unknown as string
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const css: CssType = (strings, ...values) => {
|
|
197
|
+
return newCssClassNameObject(cssCommon(strings, values, classNameSlug, onInvalidSlug))
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const cx: CxType = (...args) => {
|
|
201
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
202
|
+
args = cxCommon(args as any) as any
|
|
203
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
204
|
+
return css(Array(args.length).fill('') as any, ...args)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const keyframes: KeyframesType = (strings, ...values) =>
|
|
208
|
+
keyframesCommon(strings, values, classNameSlug, onInvalidSlug)
|
|
209
|
+
|
|
210
|
+
const viewTransition: ViewTransitionType = ((
|
|
211
|
+
strings: TemplateStringsArray | string | undefined,
|
|
212
|
+
...values: CssVariableType[]
|
|
213
|
+
) => {
|
|
214
|
+
return newCssClassNameObject(
|
|
215
|
+
viewTransitionCommon(strings as any, values, classNameSlug, onInvalidSlug) // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
216
|
+
)
|
|
217
|
+
}) as ViewTransitionType
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
css,
|
|
221
|
+
cx,
|
|
222
|
+
keyframes,
|
|
223
|
+
viewTransition,
|
|
224
|
+
Style,
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const defaultContext: DefaultContextType = createCssContext({ id: DEFAULT_STYLE_ID })
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* @experimental
|
|
232
|
+
* `css` is an experimental feature.
|
|
233
|
+
* The API might be changed.
|
|
234
|
+
*/
|
|
235
|
+
export const css = defaultContext.css
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* @experimental
|
|
239
|
+
* `cx` is an experimental feature.
|
|
240
|
+
* The API might be changed.
|
|
241
|
+
*/
|
|
242
|
+
export const cx = defaultContext.cx
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* @experimental
|
|
246
|
+
* `keyframes` is an experimental feature.
|
|
247
|
+
* The API might be changed.
|
|
248
|
+
*/
|
|
249
|
+
export const keyframes = defaultContext.keyframes
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* @experimental
|
|
253
|
+
* `viewTransition` is an experimental feature.
|
|
254
|
+
* The API might be changed.
|
|
255
|
+
*/
|
|
256
|
+
export const viewTransition = defaultContext.viewTransition
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* @experimental
|
|
260
|
+
* `Style` is an experimental feature.
|
|
261
|
+
* The API might be changed.
|
|
262
|
+
*/
|
|
263
|
+
export const Style = defaultContext.Style
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/** @jsxImportSource ../../ */
|
|
2
|
+
import { JSDOM } from 'jsdom'
|
|
3
|
+
import { useActionState, useFormStatus, useOptimistic } from './index.ts'
|
|
4
|
+
import { render, useCallback, useState } from '../index.ts'
|
|
5
|
+
|
|
6
|
+
describe('Hooks', () => {
|
|
7
|
+
beforeAll(() => {
|
|
8
|
+
global.requestAnimationFrame = (cb) => setTimeout(cb)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
let dom: JSDOM
|
|
12
|
+
let root: HTMLElement
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
dom = new JSDOM('<html><body><div id="root"></div></body></html>', {
|
|
15
|
+
runScripts: 'dangerously',
|
|
16
|
+
})
|
|
17
|
+
global.document = dom.window.document
|
|
18
|
+
global.HTMLElement = dom.window.HTMLElement
|
|
19
|
+
global.SVGElement = dom.window.SVGElement
|
|
20
|
+
global.Text = dom.window.Text
|
|
21
|
+
global.FormData = dom.window.FormData
|
|
22
|
+
root = document.getElementById('root') as HTMLElement
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
describe('useActionState', () => {
|
|
26
|
+
it('should return initial state', () => {
|
|
27
|
+
const [state] = useActionState(() => {}, 'initial')
|
|
28
|
+
expect(state).toBe('initial')
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('should return updated state', async () => {
|
|
32
|
+
const action = vi.fn().mockReturnValue('updated')
|
|
33
|
+
|
|
34
|
+
const App = () => {
|
|
35
|
+
const [state, formAction] = useActionState(action, 'initial')
|
|
36
|
+
return (
|
|
37
|
+
<>
|
|
38
|
+
<div>{state}</div>
|
|
39
|
+
<form action={formAction}>
|
|
40
|
+
<input type='text' name='name' value='updated' />
|
|
41
|
+
<button>Submit</button>
|
|
42
|
+
</form>
|
|
43
|
+
</>
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
render(<App />, root)
|
|
48
|
+
expect(root.innerHTML).toBe(
|
|
49
|
+
'<div>initial</div><form><input type="text" name="name" value="updated"><button>Submit</button></form>'
|
|
50
|
+
)
|
|
51
|
+
root.querySelector('button')?.click()
|
|
52
|
+
await Promise.resolve()
|
|
53
|
+
await Promise.resolve()
|
|
54
|
+
expect(root.innerHTML).toBe(
|
|
55
|
+
'<div>updated</div><form><input type="text" name="name" value="updated"><button>Submit</button></form>'
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
expect(action).toHaveBeenCalledOnce()
|
|
59
|
+
const [initialState, formData] = action.mock.calls[0]
|
|
60
|
+
expect(initialState).toBe('initial')
|
|
61
|
+
expect(formData).toBeInstanceOf(FormData)
|
|
62
|
+
expect(formData.get('name')).toBe('updated')
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
describe('useFormStatus', () => {
|
|
67
|
+
it('should return initial state', () => {
|
|
68
|
+
const status = useFormStatus()
|
|
69
|
+
expect(status).toEqual({
|
|
70
|
+
pending: false,
|
|
71
|
+
data: null,
|
|
72
|
+
method: null,
|
|
73
|
+
action: null,
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('should return updated state', async () => {
|
|
78
|
+
let formResolve: () => void = () => {}
|
|
79
|
+
const formPromise = new Promise<void>((r) => (formResolve = r))
|
|
80
|
+
let status: ReturnType<typeof useFormStatus> | undefined
|
|
81
|
+
const Status = () => {
|
|
82
|
+
status = useFormStatus()
|
|
83
|
+
return null
|
|
84
|
+
}
|
|
85
|
+
const App = () => {
|
|
86
|
+
const [, setCount] = useState(0)
|
|
87
|
+
const action = useCallback(() => {
|
|
88
|
+
setCount((count) => count + 1)
|
|
89
|
+
return formPromise
|
|
90
|
+
}, [])
|
|
91
|
+
return (
|
|
92
|
+
<>
|
|
93
|
+
<form action={action}>
|
|
94
|
+
<Status />
|
|
95
|
+
<input type='text' name='name' value='updated' />
|
|
96
|
+
<button>Submit</button>
|
|
97
|
+
</form>
|
|
98
|
+
</>
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
render(<App />, root)
|
|
103
|
+
expect(root.innerHTML).toBe(
|
|
104
|
+
'<form><input type="text" name="name" value="updated"><button>Submit</button></form>'
|
|
105
|
+
)
|
|
106
|
+
root.querySelector('button')?.click()
|
|
107
|
+
await Promise.resolve()
|
|
108
|
+
await Promise.resolve()
|
|
109
|
+
await Promise.resolve()
|
|
110
|
+
await Promise.resolve()
|
|
111
|
+
expect(status).toEqual({
|
|
112
|
+
pending: true,
|
|
113
|
+
data: expect.any(FormData),
|
|
114
|
+
method: 'post',
|
|
115
|
+
action: expect.any(Function),
|
|
116
|
+
})
|
|
117
|
+
formResolve?.()
|
|
118
|
+
await Promise.resolve()
|
|
119
|
+
await Promise.resolve()
|
|
120
|
+
expect(status).toEqual({
|
|
121
|
+
pending: false,
|
|
122
|
+
data: null,
|
|
123
|
+
method: null,
|
|
124
|
+
action: null,
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
describe('useOptimistic', () => {
|
|
130
|
+
it('should return updated state', async () => {
|
|
131
|
+
let formResolve: () => void = () => {}
|
|
132
|
+
const formPromise = new Promise<void>((r) => (formResolve = r))
|
|
133
|
+
const App = () => {
|
|
134
|
+
const [count, setCount] = useState(0)
|
|
135
|
+
const [optimisticCount, setOptimisticCount] = useOptimistic(count, (_c, n: number) => n)
|
|
136
|
+
const action = useCallback(async () => {
|
|
137
|
+
setOptimisticCount(count + 1)
|
|
138
|
+
await formPromise
|
|
139
|
+
setCount((count) => count + 2)
|
|
140
|
+
}, [])
|
|
141
|
+
|
|
142
|
+
return (
|
|
143
|
+
<>
|
|
144
|
+
<form action={action}>
|
|
145
|
+
<div>{optimisticCount}</div>
|
|
146
|
+
<input type='text' name='name' value='updated' />
|
|
147
|
+
<button>Submit</button>
|
|
148
|
+
</form>
|
|
149
|
+
</>
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
render(<App />, root)
|
|
154
|
+
expect(root.innerHTML).toBe(
|
|
155
|
+
'<form><div>0</div><input type="text" name="name" value="updated"><button>Submit</button></form>'
|
|
156
|
+
)
|
|
157
|
+
root.querySelector('button')?.click()
|
|
158
|
+
await Promise.resolve()
|
|
159
|
+
expect(root.innerHTML).toBe(
|
|
160
|
+
'<form><div>1</div><input type="text" name="name" value="updated"><button>Submit</button></form>'
|
|
161
|
+
)
|
|
162
|
+
formResolve?.()
|
|
163
|
+
await Promise.resolve()
|
|
164
|
+
await Promise.resolve()
|
|
165
|
+
await Promise.resolve()
|
|
166
|
+
await Promise.resolve()
|
|
167
|
+
expect(root.innerHTML).toBe(
|
|
168
|
+
'<form><div>2</div><input type="text" name="name" value="updated"><button>Submit</button></form>'
|
|
169
|
+
)
|
|
170
|
+
})
|
|
171
|
+
})
|
|
172
|
+
})
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provide hooks used only in jsx/dom
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { PERMALINK } from '../../constants.ts'
|
|
6
|
+
import type { Context } from '../../context.ts'
|
|
7
|
+
import { useContext } from '../../context.ts'
|
|
8
|
+
import { useCallback, useState } from '../../hooks/index.ts'
|
|
9
|
+
import { createContext } from '../context.ts'
|
|
10
|
+
|
|
11
|
+
type FormStatus =
|
|
12
|
+
| {
|
|
13
|
+
pending: false
|
|
14
|
+
data: null
|
|
15
|
+
method: null
|
|
16
|
+
action: null
|
|
17
|
+
}
|
|
18
|
+
| {
|
|
19
|
+
pending: true
|
|
20
|
+
data: FormData
|
|
21
|
+
method: 'get' | 'post'
|
|
22
|
+
action: string | ((formData: FormData) => void | Promise<void>)
|
|
23
|
+
}
|
|
24
|
+
export const FormContext: Context<FormStatus> = createContext<FormStatus>({
|
|
25
|
+
pending: false,
|
|
26
|
+
data: null,
|
|
27
|
+
method: null,
|
|
28
|
+
action: null,
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
const actions: Set<Promise<unknown>> = new Set()
|
|
32
|
+
export const registerAction = (action: Promise<unknown>) => {
|
|
33
|
+
actions.add(action)
|
|
34
|
+
action.finally(() => actions.delete(action))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* This hook returns the current form status
|
|
39
|
+
* @returns FormStatus
|
|
40
|
+
*/
|
|
41
|
+
export const useFormStatus = (): FormStatus => {
|
|
42
|
+
return useContext(FormContext)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* This hook returns the current state and a function to update the state optimistically
|
|
47
|
+
* The current state is updated optimistically and then reverted to the original state when all actions are resolved
|
|
48
|
+
* @param state
|
|
49
|
+
* @param updateState
|
|
50
|
+
* @returns [T, (action: N) => void]
|
|
51
|
+
*/
|
|
52
|
+
export const useOptimistic = <T, N>(
|
|
53
|
+
state: T,
|
|
54
|
+
updateState: (currentState: T, action: N) => T
|
|
55
|
+
): [T, (action: N) => void] => {
|
|
56
|
+
const [optimisticState, setOptimisticState] = useState(state)
|
|
57
|
+
if (actions.size > 0) {
|
|
58
|
+
Promise.all(actions).finally(() => {
|
|
59
|
+
setOptimisticState(state)
|
|
60
|
+
})
|
|
61
|
+
} else {
|
|
62
|
+
setOptimisticState(state)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const cb = useCallback((newData: N) => {
|
|
66
|
+
setOptimisticState((currentState) => updateState(currentState, newData))
|
|
67
|
+
}, [])
|
|
68
|
+
|
|
69
|
+
return [optimisticState, cb]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* This hook returns the current state and a function to update the state by form action
|
|
74
|
+
* @param fn
|
|
75
|
+
* @param initialState
|
|
76
|
+
* @param permalink
|
|
77
|
+
* @returns [T, (data: FormData) => void]
|
|
78
|
+
*/
|
|
79
|
+
export const useActionState = <T>(
|
|
80
|
+
fn: Function,
|
|
81
|
+
initialState: T,
|
|
82
|
+
permalink?: string
|
|
83
|
+
): [T, Function] => {
|
|
84
|
+
const [state, setState] = useState(initialState)
|
|
85
|
+
const actionState = async (data: FormData) => {
|
|
86
|
+
setState(await fn(state, data))
|
|
87
|
+
}
|
|
88
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
89
|
+
;(actionState as any)[PERMALINK] = permalink
|
|
90
|
+
return [state, actionState]
|
|
91
|
+
}
|