@soroush.design/cli 0.2.0-alpha.3 → 0.2.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/dist/bin.mjs +1 -1
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +1 -1
- package/dist/{io-Bybgct-f.mjs → io-C_8_JerZ.mjs} +368 -1039
- package/package.json +13 -5
- package/templates/astro/js/base/README.md +93 -0
- package/templates/astro/js/base/_gitignore +5 -0
- package/templates/astro/js/base/astro.config.mjs +23 -0
- package/templates/astro/js/base/src/components/About.jsx +24 -0
- package/templates/astro/js/base/src/components/Counter.jsx +26 -0
- package/templates/astro/js/base/src/components/Greeting.jsx +60 -0
- package/templates/astro/js/base/src/components/Header.jsx +99 -0
- package/templates/astro/js/base/src/components/Home.jsx +32 -0
- package/templates/astro/js/base/src/components/Home.test.jsx +39 -0
- package/templates/astro/js/base/src/components/NotFound.jsx +17 -0
- package/templates/astro/js/base/src/data/api.js +84 -0
- package/templates/astro/js/base/src/layouts/Layout.astro +52 -0
- package/templates/astro/js/base/src/pages/404.astro +12 -0
- package/templates/astro/js/base/src/pages/about.astro +12 -0
- package/templates/astro/js/base/src/pages/index.astro +23 -0
- package/templates/astro/js/base/src/theme/Island.jsx +89 -0
- package/templates/astro/js/base/src/theme/scheme.js +32 -0
- package/templates/astro/js/base/vitest.config.js +12 -0
- package/templates/astro/js/sample-data/src/components/ItemDetails.jsx +64 -0
- package/templates/astro/js/sample-data/src/components/ItemList.jsx +100 -0
- package/templates/astro/js/sample-data/src/components/StatusChip.jsx +8 -0
- package/templates/astro/js/sample-data/src/data/items.js +65 -0
- package/templates/astro/js/sample-data/src/pages/items/[id].astro +37 -0
- package/templates/astro/js/sample-data/src/pages/items/index.astro +20 -0
- package/templates/astro/js/with/jotai/src/components/Counter.jsx +26 -0
- package/templates/astro/js/with/jotai/src/state/counter.js +4 -0
- package/templates/astro/js/with/msw/_env +3 -0
- package/templates/astro/js/with/msw/src/mocks/browser.js +5 -0
- package/templates/astro/js/with/msw/src/mocks/handlers.js +24 -0
- package/templates/astro/js/with/msw/src/mocks/server.js +5 -0
- package/templates/astro/js/with/msw/src/mocks/setupTests.js +7 -0
- package/templates/astro/js/with/msw/src/mocks/start.js +14 -0
- package/templates/astro/js/with/react-hook-form/src/components/Form.jsx +313 -0
- package/templates/astro/js/with/react-hook-form/src/pages/form.astro +8 -0
- package/templates/astro/js/with/redux-toolkit/src/components/Counter.jsx +27 -0
- package/templates/astro/js/with/redux-toolkit/src/state/store.js +18 -0
- package/templates/astro/js/with/tanstack-query/src/data/queryClient.js +13 -0
- package/templates/astro/js/with/zustand/src/components/Counter.jsx +24 -0
- package/templates/astro/js/with/zustand/src/state/counter.js +14 -0
- package/templates/astro/ts/base/README.md +93 -0
- package/templates/astro/ts/base/_gitignore +5 -0
- package/templates/astro/ts/base/astro.config.mjs +23 -0
- package/templates/astro/ts/base/src/components/About.tsx +24 -0
- package/templates/astro/ts/base/src/components/Counter.tsx +26 -0
- package/templates/astro/ts/base/src/components/Greeting.tsx +61 -0
- package/templates/astro/ts/base/src/components/Header.tsx +99 -0
- package/templates/astro/ts/base/src/components/Home.test.tsx +39 -0
- package/templates/astro/ts/base/src/components/Home.tsx +33 -0
- package/templates/astro/ts/base/src/components/NotFound.tsx +17 -0
- package/templates/astro/ts/base/src/data/api.ts +92 -0
- package/templates/astro/ts/base/src/layouts/Layout.astro +52 -0
- package/templates/astro/ts/base/src/pages/404.astro +12 -0
- package/templates/astro/ts/base/src/pages/about.astro +12 -0
- package/templates/astro/ts/base/src/pages/index.astro +23 -0
- package/templates/astro/ts/base/src/theme/Island.tsx +90 -0
- package/templates/astro/ts/base/src/theme/scheme.ts +34 -0
- package/templates/astro/ts/base/tsconfig.json +9 -0
- package/templates/astro/ts/base/vitest.config.ts +12 -0
- package/templates/astro/ts/sample-data/src/components/ItemDetails.tsx +65 -0
- package/templates/astro/ts/sample-data/src/components/ItemList.tsx +109 -0
- package/templates/astro/ts/sample-data/src/components/StatusChip.tsx +9 -0
- package/templates/astro/ts/sample-data/src/data/items.ts +71 -0
- package/templates/astro/ts/sample-data/src/pages/items/[id].astro +39 -0
- package/templates/astro/ts/sample-data/src/pages/items/index.astro +20 -0
- package/templates/astro/ts/with/jotai/src/components/Counter.tsx +26 -0
- package/templates/astro/ts/with/jotai/src/state/counter.ts +4 -0
- package/templates/astro/ts/with/msw/_env +3 -0
- package/templates/astro/ts/with/msw/src/mocks/browser.ts +5 -0
- package/templates/astro/ts/with/msw/src/mocks/handlers.ts +24 -0
- package/templates/astro/ts/with/msw/src/mocks/server.ts +5 -0
- package/templates/astro/ts/with/msw/src/mocks/setupTests.ts +7 -0
- package/templates/astro/ts/with/msw/src/mocks/start.ts +14 -0
- package/templates/astro/ts/with/react-hook-form/src/components/Form.tsx +317 -0
- package/templates/astro/ts/with/react-hook-form/src/pages/form.astro +8 -0
- package/templates/astro/ts/with/redux-toolkit/src/components/Counter.tsx +27 -0
- package/templates/astro/ts/with/redux-toolkit/src/state/store.ts +19 -0
- package/templates/astro/ts/with/tanstack-query/src/data/queryClient.ts +13 -0
- package/templates/astro/ts/with/zustand/src/components/Counter.tsx +24 -0
- package/templates/astro/ts/with/zustand/src/state/counter.ts +19 -0
- package/templates/templates.md +66 -0
- package/templates/vike/js/base/README.md +87 -0
- package/templates/vike/js/base/_gitignore +4 -0
- package/templates/vike/js/base/src/data/api.js +76 -0
- package/templates/vike/js/base/src/pages/+Layout.jsx +183 -0
- package/templates/vike/js/base/src/pages/+config.js +12 -0
- package/templates/vike/js/base/src/pages/+onRenderClient.jsx +39 -0
- package/templates/vike/js/base/src/pages/+onRenderHtml.jsx +27 -0
- package/templates/vike/js/base/src/pages/PageShell.jsx +47 -0
- package/templates/vike/js/base/src/pages/_error/+Page.jsx +14 -0
- package/templates/vike/js/base/src/pages/about/+Page.jsx +14 -0
- package/templates/vike/js/base/src/pages/index/+Page.jsx +33 -0
- package/templates/vike/js/base/src/pages/index/Counter.jsx +20 -0
- package/templates/vike/js/base/src/pages/index/Greeting.jsx +48 -0
- package/templates/vike/js/base/src/pages/index/Page.test.jsx +67 -0
- package/templates/vike/js/base/src/pages/usePageContext.jsx +20 -0
- package/templates/vike/js/base/vite.config.js +14 -0
- package/templates/vike/js/base/vitest.config.js +13 -0
- package/templates/vike/js/sample-data/src/data/items.js +58 -0
- package/templates/vike/js/sample-data/src/pages/items/+Page.jsx +49 -0
- package/templates/vike/js/sample-data/src/pages/items/+data.js +26 -0
- package/templates/vike/js/sample-data/src/pages/items/@id/+Page.jsx +40 -0
- package/templates/vike/js/sample-data/src/pages/items/@id/+data.js +33 -0
- package/templates/vike/js/sample-data/src/pages/items/@id/+onBeforePrerenderStart.js +8 -0
- package/templates/vike/js/sample-data/src/pages/items/StatusChip.jsx +8 -0
- package/templates/vike/js/spa/src/pages/+config.js +12 -0
- package/templates/vike/js/spa/src/pages/+onRenderClient.jsx +25 -0
- package/templates/vike/js/spa/src/pages/+onRenderHtml.jsx +16 -0
- package/templates/vike/js/spa/src/pages/about/+Page.jsx +14 -0
- package/templates/vike/js/ssg/src/pages/+config.js +11 -0
- package/templates/vike/js/ssg/src/pages/about/+Page.jsx +13 -0
- package/templates/vike/js/ssr/src/pages/+config.js +12 -0
- package/templates/vike/js/ssr/src/pages/+onRenderHtml.jsx +27 -0
- package/templates/vike/js/ssr/src/pages/about/+Page.jsx +14 -0
- package/templates/vike/js/with/jotai/src/pages/index/Counter.jsx +19 -0
- package/templates/vike/js/with/jotai/src/state/counter.js +4 -0
- package/templates/vike/js/with/jotai/src/state/scheme.js +4 -0
- package/templates/vike/js/with/msw/_env +3 -0
- package/templates/vike/js/with/msw/src/mocks/browser.js +5 -0
- package/templates/vike/js/with/msw/src/mocks/handlers.js +24 -0
- package/templates/vike/js/with/msw/src/mocks/server.js +5 -0
- package/templates/vike/js/with/msw/src/mocks/setupTests.js +7 -0
- package/templates/vike/js/with/msw/src/mocks/start.js +10 -0
- package/templates/vike/js/with/react-hook-form/src/pages/form/+Page.jsx +294 -0
- package/templates/vike/js/with/redux-toolkit/src/pages/index/Counter.jsx +20 -0
- package/templates/vike/js/with/redux-toolkit/src/state/store.js +27 -0
- package/templates/vike/js/with/zustand/src/pages/index/Counter.jsx +17 -0
- package/templates/vike/js/with/zustand/src/state/counter.js +14 -0
- package/templates/vike/js/with/zustand/src/state/scheme.js +14 -0
- package/templates/vike/ts/base/README.md +87 -0
- package/templates/vike/ts/base/_gitignore +4 -0
- package/templates/vike/ts/base/src/data/api.ts +84 -0
- package/templates/vike/ts/base/src/pages/+Layout.tsx +184 -0
- package/templates/vike/ts/base/src/pages/+config.ts +14 -0
- package/templates/vike/ts/base/src/pages/+onRenderClient.tsx +41 -0
- package/templates/vike/ts/base/src/pages/+onRenderHtml.tsx +28 -0
- package/templates/vike/ts/base/src/pages/PageShell.tsx +48 -0
- package/templates/vike/ts/base/src/pages/_error/+Page.tsx +14 -0
- package/templates/vike/ts/base/src/pages/about/+Page.tsx +14 -0
- package/templates/vike/ts/base/src/pages/index/+Page.tsx +33 -0
- package/templates/vike/ts/base/src/pages/index/Counter.tsx +20 -0
- package/templates/vike/ts/base/src/pages/index/Greeting.tsx +49 -0
- package/templates/vike/ts/base/src/pages/index/Page.test.tsx +67 -0
- package/templates/vike/ts/base/src/pages/usePageContext.tsx +27 -0
- package/templates/vike/ts/base/src/vike.d.ts +20 -0
- package/templates/vike/ts/base/tsconfig.json +18 -0
- package/templates/vike/ts/base/vite.config.ts +14 -0
- package/templates/vike/ts/base/vitest.config.ts +13 -0
- package/templates/vike/ts/sample-data/src/data/items.ts +64 -0
- package/templates/vike/ts/sample-data/src/pages/items/+Page.tsx +50 -0
- package/templates/vike/ts/sample-data/src/pages/items/+data.ts +28 -0
- package/templates/vike/ts/sample-data/src/pages/items/@id/+Page.tsx +41 -0
- package/templates/vike/ts/sample-data/src/pages/items/@id/+data.ts +37 -0
- package/templates/vike/ts/sample-data/src/pages/items/@id/+onBeforePrerenderStart.ts +8 -0
- package/templates/vike/ts/sample-data/src/pages/items/StatusChip.tsx +9 -0
- package/templates/vike/ts/spa/src/pages/+config.ts +14 -0
- package/templates/vike/ts/spa/src/pages/+onRenderClient.tsx +27 -0
- package/templates/vike/ts/spa/src/pages/+onRenderHtml.tsx +17 -0
- package/templates/vike/ts/spa/src/pages/about/+Page.tsx +14 -0
- package/templates/vike/ts/ssg/src/pages/+config.ts +13 -0
- package/templates/vike/ts/ssg/src/pages/about/+Page.tsx +13 -0
- package/templates/vike/ts/ssr/src/pages/+config.ts +14 -0
- package/templates/vike/ts/ssr/src/pages/+onRenderHtml.tsx +28 -0
- package/templates/vike/ts/ssr/src/pages/about/+Page.tsx +14 -0
- package/templates/vike/ts/with/jotai/src/pages/index/Counter.tsx +19 -0
- package/templates/vike/ts/with/jotai/src/state/counter.ts +4 -0
- package/templates/vike/ts/with/jotai/src/state/scheme.ts +4 -0
- package/templates/vike/ts/with/msw/_env +3 -0
- package/templates/vike/ts/with/msw/src/mocks/browser.ts +5 -0
- package/templates/vike/ts/with/msw/src/mocks/handlers.ts +24 -0
- package/templates/vike/ts/with/msw/src/mocks/server.ts +5 -0
- package/templates/vike/ts/with/msw/src/mocks/setupTests.ts +7 -0
- package/templates/vike/ts/with/msw/src/mocks/start.ts +10 -0
- package/templates/vike/ts/with/react-hook-form/src/pages/form/+Page.tsx +298 -0
- package/templates/vike/ts/with/redux-toolkit/src/pages/index/Counter.tsx +20 -0
- package/templates/vike/ts/with/redux-toolkit/src/state/store.ts +29 -0
- package/templates/vike/ts/with/zustand/src/pages/index/Counter.tsx +17 -0
- package/templates/vike/ts/with/zustand/src/state/counter.ts +19 -0
- package/templates/vike/ts/with/zustand/src/state/scheme.ts +19 -0
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
// @if !tanstack-query && !swr
|
|
2
|
+
import { useState } from 'react'
|
|
3
|
+
// @endif
|
|
4
|
+
import { zodResolver } from '@hookform/resolvers/zod'
|
|
5
|
+
// @if tanstack-query
|
|
6
|
+
import { useMutation } from '@tanstack/react-query'
|
|
7
|
+
// @endif
|
|
8
|
+
import { Controller, useForm } from 'react-hook-form'
|
|
9
|
+
// @if swr
|
|
10
|
+
import useSWRMutation from 'swr/mutation'
|
|
11
|
+
// @endif
|
|
12
|
+
import { z } from 'zod'
|
|
13
|
+
import { Button } from '@soroush.tech/design-system/Button'
|
|
14
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
15
|
+
import { TextInput } from '@soroush.tech/design-system/TextInput'
|
|
16
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
17
|
+
import { withIsland } from '../theme/Island'
|
|
18
|
+
// @if tanstack-query || swr || msw
|
|
19
|
+
import { sendMessage } from '../data/api'
|
|
20
|
+
// @endif
|
|
21
|
+
|
|
22
|
+
/** What the form takes, and what it says when a value will not do. */
|
|
23
|
+
const schema = z.object({
|
|
24
|
+
name: z.string().trim().min(1, 'Your name, please.'),
|
|
25
|
+
email: z.email('An email address, please.'),
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
// @if tanstack-query
|
|
29
|
+
/** A form whose values a Zod schema checks; once they pass, it sends them with a mutation of TanStack Query. */
|
|
30
|
+
function Form() {
|
|
31
|
+
const mutation = useMutation({ mutationFn: sendMessage })
|
|
32
|
+
const { control, handleSubmit } = useForm({
|
|
33
|
+
resolver: zodResolver(schema),
|
|
34
|
+
defaultValues: { name: '', email: '' },
|
|
35
|
+
})
|
|
36
|
+
return (
|
|
37
|
+
<Flex gap={2} alignItems="flex-start" px={3} py={4}>
|
|
38
|
+
<Typography variant="h2">Form</Typography>
|
|
39
|
+
<Typography color="secondary">
|
|
40
|
+
React Hook Form holds the values, and a Zod schema checks them when the form is sent.
|
|
41
|
+
</Typography>
|
|
42
|
+
<form noValidate onSubmit={handleSubmit((values) => mutation.mutate(values))}>
|
|
43
|
+
<Flex gap={2} alignItems="flex-start">
|
|
44
|
+
<Controller
|
|
45
|
+
name="name"
|
|
46
|
+
control={control}
|
|
47
|
+
render={({ field, fieldState }) => (
|
|
48
|
+
<Flex gap={1}>
|
|
49
|
+
<TextInput
|
|
50
|
+
name={field.name}
|
|
51
|
+
value={field.value}
|
|
52
|
+
onChange={field.onChange}
|
|
53
|
+
placeholder="Name"
|
|
54
|
+
error={fieldState.invalid}
|
|
55
|
+
inputProps={{
|
|
56
|
+
'aria-label': 'Name',
|
|
57
|
+
autoComplete: 'name',
|
|
58
|
+
onBlur: field.onBlur,
|
|
59
|
+
}}
|
|
60
|
+
/>
|
|
61
|
+
<Typography variant="caption" color="error">
|
|
62
|
+
{fieldState.error?.message}
|
|
63
|
+
</Typography>
|
|
64
|
+
</Flex>
|
|
65
|
+
)}
|
|
66
|
+
/>
|
|
67
|
+
<Controller
|
|
68
|
+
name="email"
|
|
69
|
+
control={control}
|
|
70
|
+
render={({ field, fieldState }) => (
|
|
71
|
+
<Flex gap={1}>
|
|
72
|
+
<TextInput
|
|
73
|
+
name={field.name}
|
|
74
|
+
value={field.value}
|
|
75
|
+
onChange={field.onChange}
|
|
76
|
+
type="email"
|
|
77
|
+
placeholder="Email"
|
|
78
|
+
error={fieldState.invalid}
|
|
79
|
+
inputProps={{
|
|
80
|
+
'aria-label': 'Email',
|
|
81
|
+
autoComplete: 'email',
|
|
82
|
+
onBlur: field.onBlur,
|
|
83
|
+
}}
|
|
84
|
+
/>
|
|
85
|
+
<Typography variant="caption" color="error">
|
|
86
|
+
{fieldState.error?.message}
|
|
87
|
+
</Typography>
|
|
88
|
+
</Flex>
|
|
89
|
+
)}
|
|
90
|
+
/>
|
|
91
|
+
<Button type="submit">Send</Button>
|
|
92
|
+
</Flex>
|
|
93
|
+
</form>
|
|
94
|
+
{mutation.data ? <Typography>{mutation.data.message}</Typography> : null}
|
|
95
|
+
</Flex>
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
// @elif swr
|
|
99
|
+
/** A form whose values a Zod schema checks; once they pass, it sends them with SWR's mutation. */
|
|
100
|
+
function Form() {
|
|
101
|
+
const { trigger, data } = useSWRMutation('messages', (_key, { arg }) => sendMessage(arg))
|
|
102
|
+
const { control, handleSubmit } = useForm({
|
|
103
|
+
resolver: zodResolver(schema),
|
|
104
|
+
defaultValues: { name: '', email: '' },
|
|
105
|
+
})
|
|
106
|
+
return (
|
|
107
|
+
<Flex gap={2} alignItems="flex-start" px={3} py={4}>
|
|
108
|
+
<Typography variant="h2">Form</Typography>
|
|
109
|
+
<Typography color="secondary">
|
|
110
|
+
React Hook Form holds the values, and a Zod schema checks them when the form is sent.
|
|
111
|
+
</Typography>
|
|
112
|
+
<form noValidate onSubmit={handleSubmit((values) => trigger(values))}>
|
|
113
|
+
<Flex gap={2} alignItems="flex-start">
|
|
114
|
+
<Controller
|
|
115
|
+
name="name"
|
|
116
|
+
control={control}
|
|
117
|
+
render={({ field, fieldState }) => (
|
|
118
|
+
<Flex gap={1}>
|
|
119
|
+
<TextInput
|
|
120
|
+
name={field.name}
|
|
121
|
+
value={field.value}
|
|
122
|
+
onChange={field.onChange}
|
|
123
|
+
placeholder="Name"
|
|
124
|
+
error={fieldState.invalid}
|
|
125
|
+
inputProps={{
|
|
126
|
+
'aria-label': 'Name',
|
|
127
|
+
autoComplete: 'name',
|
|
128
|
+
onBlur: field.onBlur,
|
|
129
|
+
}}
|
|
130
|
+
/>
|
|
131
|
+
<Typography variant="caption" color="error">
|
|
132
|
+
{fieldState.error?.message}
|
|
133
|
+
</Typography>
|
|
134
|
+
</Flex>
|
|
135
|
+
)}
|
|
136
|
+
/>
|
|
137
|
+
<Controller
|
|
138
|
+
name="email"
|
|
139
|
+
control={control}
|
|
140
|
+
render={({ field, fieldState }) => (
|
|
141
|
+
<Flex gap={1}>
|
|
142
|
+
<TextInput
|
|
143
|
+
name={field.name}
|
|
144
|
+
value={field.value}
|
|
145
|
+
onChange={field.onChange}
|
|
146
|
+
type="email"
|
|
147
|
+
placeholder="Email"
|
|
148
|
+
error={fieldState.invalid}
|
|
149
|
+
inputProps={{
|
|
150
|
+
'aria-label': 'Email',
|
|
151
|
+
autoComplete: 'email',
|
|
152
|
+
onBlur: field.onBlur,
|
|
153
|
+
}}
|
|
154
|
+
/>
|
|
155
|
+
<Typography variant="caption" color="error">
|
|
156
|
+
{fieldState.error?.message}
|
|
157
|
+
</Typography>
|
|
158
|
+
</Flex>
|
|
159
|
+
)}
|
|
160
|
+
/>
|
|
161
|
+
<Button type="submit">Send</Button>
|
|
162
|
+
</Flex>
|
|
163
|
+
</form>
|
|
164
|
+
{data ? <Typography>{data.message}</Typography> : null}
|
|
165
|
+
</Flex>
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
// @elif msw
|
|
169
|
+
/** A form whose values a Zod schema checks; once they pass, it sends them to the API. */
|
|
170
|
+
function Form() {
|
|
171
|
+
const [answer, setAnswer] = useState(null)
|
|
172
|
+
const { control, handleSubmit } = useForm({
|
|
173
|
+
resolver: zodResolver(schema),
|
|
174
|
+
defaultValues: { name: '', email: '' },
|
|
175
|
+
})
|
|
176
|
+
return (
|
|
177
|
+
<Flex gap={2} alignItems="flex-start" px={3} py={4}>
|
|
178
|
+
<Typography variant="h2">Form</Typography>
|
|
179
|
+
<Typography color="secondary">
|
|
180
|
+
React Hook Form holds the values, and a Zod schema checks them when the form is sent.
|
|
181
|
+
</Typography>
|
|
182
|
+
<form
|
|
183
|
+
noValidate
|
|
184
|
+
onSubmit={handleSubmit(async (values) => setAnswer((await sendMessage(values)).message))}
|
|
185
|
+
>
|
|
186
|
+
<Flex gap={2} alignItems="flex-start">
|
|
187
|
+
<Controller
|
|
188
|
+
name="name"
|
|
189
|
+
control={control}
|
|
190
|
+
render={({ field, fieldState }) => (
|
|
191
|
+
<Flex gap={1}>
|
|
192
|
+
<TextInput
|
|
193
|
+
name={field.name}
|
|
194
|
+
value={field.value}
|
|
195
|
+
onChange={field.onChange}
|
|
196
|
+
placeholder="Name"
|
|
197
|
+
error={fieldState.invalid}
|
|
198
|
+
inputProps={{
|
|
199
|
+
'aria-label': 'Name',
|
|
200
|
+
autoComplete: 'name',
|
|
201
|
+
onBlur: field.onBlur,
|
|
202
|
+
}}
|
|
203
|
+
/>
|
|
204
|
+
<Typography variant="caption" color="error">
|
|
205
|
+
{fieldState.error?.message}
|
|
206
|
+
</Typography>
|
|
207
|
+
</Flex>
|
|
208
|
+
)}
|
|
209
|
+
/>
|
|
210
|
+
<Controller
|
|
211
|
+
name="email"
|
|
212
|
+
control={control}
|
|
213
|
+
render={({ field, fieldState }) => (
|
|
214
|
+
<Flex gap={1}>
|
|
215
|
+
<TextInput
|
|
216
|
+
name={field.name}
|
|
217
|
+
value={field.value}
|
|
218
|
+
onChange={field.onChange}
|
|
219
|
+
type="email"
|
|
220
|
+
placeholder="Email"
|
|
221
|
+
error={fieldState.invalid}
|
|
222
|
+
inputProps={{
|
|
223
|
+
'aria-label': 'Email',
|
|
224
|
+
autoComplete: 'email',
|
|
225
|
+
onBlur: field.onBlur,
|
|
226
|
+
}}
|
|
227
|
+
/>
|
|
228
|
+
<Typography variant="caption" color="error">
|
|
229
|
+
{fieldState.error?.message}
|
|
230
|
+
</Typography>
|
|
231
|
+
</Flex>
|
|
232
|
+
)}
|
|
233
|
+
/>
|
|
234
|
+
<Button type="submit">Send</Button>
|
|
235
|
+
</Flex>
|
|
236
|
+
</form>
|
|
237
|
+
{answer ? <Typography>{answer}</Typography> : null}
|
|
238
|
+
</Flex>
|
|
239
|
+
)
|
|
240
|
+
}
|
|
241
|
+
// @else
|
|
242
|
+
/** A form whose values a Zod schema checks; once they pass, it shows them. */
|
|
243
|
+
function Form() {
|
|
244
|
+
const [sent, setSent] = useState(null)
|
|
245
|
+
const { control, handleSubmit } = useForm({
|
|
246
|
+
resolver: zodResolver(schema),
|
|
247
|
+
defaultValues: { name: '', email: '' },
|
|
248
|
+
})
|
|
249
|
+
return (
|
|
250
|
+
<Flex gap={2} alignItems="flex-start" px={3} py={4}>
|
|
251
|
+
<Typography variant="h2">Form</Typography>
|
|
252
|
+
<Typography color="secondary">
|
|
253
|
+
React Hook Form holds the values, and a Zod schema checks them when the form is sent.
|
|
254
|
+
</Typography>
|
|
255
|
+
<form noValidate onSubmit={handleSubmit(setSent)}>
|
|
256
|
+
<Flex gap={2} alignItems="flex-start">
|
|
257
|
+
<Controller
|
|
258
|
+
name="name"
|
|
259
|
+
control={control}
|
|
260
|
+
render={({ field, fieldState }) => (
|
|
261
|
+
<Flex gap={1}>
|
|
262
|
+
<TextInput
|
|
263
|
+
name={field.name}
|
|
264
|
+
value={field.value}
|
|
265
|
+
onChange={field.onChange}
|
|
266
|
+
placeholder="Name"
|
|
267
|
+
error={fieldState.invalid}
|
|
268
|
+
inputProps={{
|
|
269
|
+
'aria-label': 'Name',
|
|
270
|
+
autoComplete: 'name',
|
|
271
|
+
onBlur: field.onBlur,
|
|
272
|
+
}}
|
|
273
|
+
/>
|
|
274
|
+
<Typography variant="caption" color="error">
|
|
275
|
+
{fieldState.error?.message}
|
|
276
|
+
</Typography>
|
|
277
|
+
</Flex>
|
|
278
|
+
)}
|
|
279
|
+
/>
|
|
280
|
+
<Controller
|
|
281
|
+
name="email"
|
|
282
|
+
control={control}
|
|
283
|
+
render={({ field, fieldState }) => (
|
|
284
|
+
<Flex gap={1}>
|
|
285
|
+
<TextInput
|
|
286
|
+
name={field.name}
|
|
287
|
+
value={field.value}
|
|
288
|
+
onChange={field.onChange}
|
|
289
|
+
type="email"
|
|
290
|
+
placeholder="Email"
|
|
291
|
+
error={fieldState.invalid}
|
|
292
|
+
inputProps={{
|
|
293
|
+
'aria-label': 'Email',
|
|
294
|
+
autoComplete: 'email',
|
|
295
|
+
onBlur: field.onBlur,
|
|
296
|
+
}}
|
|
297
|
+
/>
|
|
298
|
+
<Typography variant="caption" color="error">
|
|
299
|
+
{fieldState.error?.message}
|
|
300
|
+
</Typography>
|
|
301
|
+
</Flex>
|
|
302
|
+
)}
|
|
303
|
+
/>
|
|
304
|
+
<Button type="submit">Send</Button>
|
|
305
|
+
</Flex>
|
|
306
|
+
</form>
|
|
307
|
+
{sent ? <Typography>Thanks, {sent.name}: the form was valid.</Typography> : null}
|
|
308
|
+
</Flex>
|
|
309
|
+
)
|
|
310
|
+
}
|
|
311
|
+
// @endif
|
|
312
|
+
|
|
313
|
+
export default withIsland(Form)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Button } from '@soroush.tech/design-system/Button'
|
|
2
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
3
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
4
|
+
import { withIsland } from '../theme/Island'
|
|
5
|
+
import { useDispatch, useSelector } from 'react-redux'
|
|
6
|
+
import { incremented } from '../state/store'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A count, held in the Redux store, src/state/store, and the button that adds to it.
|
|
10
|
+
* An island of its own, which renders in the browser only: the count it shows is the store's,
|
|
11
|
+
* not the HTML's.
|
|
12
|
+
*/
|
|
13
|
+
function Counter() {
|
|
14
|
+
const count = useSelector((state) => state.count)
|
|
15
|
+
const dispatch = useDispatch()
|
|
16
|
+
const increment = () => dispatch(incremented())
|
|
17
|
+
return (
|
|
18
|
+
<Flex flexDirection="row" alignItems="center" gap={2}>
|
|
19
|
+
<Button size="sm" variant="outlined" onClick={increment}>
|
|
20
|
+
Count
|
|
21
|
+
</Button>
|
|
22
|
+
<Typography>Count: {count}</Typography>
|
|
23
|
+
</Flex>
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default withIsland(Counter)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { configureStore, createSlice } from '@reduxjs/toolkit'
|
|
2
|
+
|
|
3
|
+
/** The home page's count: a slice of the store. */
|
|
4
|
+
const countSlice = createSlice({
|
|
5
|
+
name: 'count',
|
|
6
|
+
initialState: 0,
|
|
7
|
+
reducers: {
|
|
8
|
+
incremented: (count) => count + 1,
|
|
9
|
+
},
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
export const { incremented } = countSlice.actions
|
|
13
|
+
|
|
14
|
+
/** The app's Redux store, which every island's Island provides: one store, for all of them. */
|
|
15
|
+
export const store = configureStore({ reducer: { count: countSlice.reducer } })
|
|
16
|
+
// @if ts
|
|
17
|
+
|
|
18
|
+
// @endif
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { QueryClient } from '@tanstack/react-query'
|
|
2
|
+
|
|
3
|
+
let browserClient
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The query client of an island. In the browser every island gets the same one, so they share its
|
|
7
|
+
* cache; on the server each render gets its own, so no request sees another's data.
|
|
8
|
+
*/
|
|
9
|
+
export function getQueryClient() {
|
|
10
|
+
if (typeof window === 'undefined') return new QueryClient()
|
|
11
|
+
browserClient ??= new QueryClient()
|
|
12
|
+
return browserClient
|
|
13
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Button } from '@soroush.tech/design-system/Button'
|
|
2
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
3
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
4
|
+
import { withIsland } from '../theme/Island'
|
|
5
|
+
import { useCounter } from '../state/counter'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A count, held in a zustand store, src/state/counter, and the button that adds to it.
|
|
9
|
+
* An island of its own, which renders in the browser only: the count it shows is the store's,
|
|
10
|
+
* not the HTML's.
|
|
11
|
+
*/
|
|
12
|
+
function Counter() {
|
|
13
|
+
const { count, increment } = useCounter()
|
|
14
|
+
return (
|
|
15
|
+
<Flex flexDirection="row" alignItems="center" gap={2}>
|
|
16
|
+
<Button size="sm" variant="outlined" onClick={increment}>
|
|
17
|
+
Count
|
|
18
|
+
</Button>
|
|
19
|
+
<Typography>Count: {count}</Typography>
|
|
20
|
+
</Flex>
|
|
21
|
+
)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default withIsland(Counter)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { create } from 'zustand'
|
|
2
|
+
|
|
3
|
+
/** The home page's count: a zustand store, which any component can read. */
|
|
4
|
+
// @if ts
|
|
5
|
+
export const useCounter = create()((set) => ({
|
|
6
|
+
count: 0,
|
|
7
|
+
increment: () => set((state) => ({ count: state.count + 1 })),
|
|
8
|
+
}))
|
|
9
|
+
// @else
|
|
10
|
+
export const useCounter = create((set) => ({
|
|
11
|
+
count: 0,
|
|
12
|
+
increment: () => set((state) => ({ count: state.count + 1 })),
|
|
13
|
+
}))
|
|
14
|
+
// @endif
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# __TITLE__
|
|
2
|
+
|
|
3
|
+
An [Astro](https://astro.build) app on the [@soroush.tech design system](https://docs.soroush.tech):
|
|
4
|
+
<!-- @if isomorphic -->
|
|
5
|
+
every page is HTML from the build, with its styles, and its parts are React islands
|
|
6
|
+
that hydrate in the browser. Astro's client router moves between pages with no reload.
|
|
7
|
+
<!-- @elif ssr -->
|
|
8
|
+
every page is rendered on the server, on each request, with its styles, and its parts are
|
|
9
|
+
React islands that hydrate in the browser. Astro's client router moves between pages with no
|
|
10
|
+
reload.
|
|
11
|
+
<!-- @else -->
|
|
12
|
+
every page is HTML from the build, with its styles, and no JavaScript in the browser: its
|
|
13
|
+
React parts render at build time only.
|
|
14
|
+
<!-- @endif -->
|
|
15
|
+
|
|
16
|
+
- `npm run dev` starts it, `npm run build` builds it, and `npm run preview` serves the build.
|
|
17
|
+
<!-- @if ssr -->
|
|
18
|
+
- `npm start` runs the server the build wrote, on `HOST` and `PORT` (4321 by default), on any
|
|
19
|
+
Node host.
|
|
20
|
+
<!-- @endif -->
|
|
21
|
+
- `src/pages/` holds the pages: an `.astro` file per route, which renders a component of
|
|
22
|
+
`src/components/` in `src/layouts/Layout.astro`.
|
|
23
|
+
- `src/components/Header.tsx` draws the header, inside the app's providers, which draw the base
|
|
24
|
+
styles once a page.
|
|
25
|
+
<!-- @if ssg -->
|
|
26
|
+
- Each React part of a page renders inside `Island`, at build time.
|
|
27
|
+
<!-- @else -->
|
|
28
|
+
- Each React part of a page is an island, a React root of its own: it renders inside
|
|
29
|
+
`Island` (`src/theme/Island.tsx`), and the islands share the color scheme through
|
|
30
|
+
`src/theme/scheme.ts`, not context.
|
|
31
|
+
- The home page's counter is an island of its own, which renders in the browser only
|
|
32
|
+
(`client:only`), so the count it shows is the one in memory, not the build's.
|
|
33
|
+
- `src/layouts/Layout.astro` keeps the design system's styles as the client router moves
|
|
34
|
+
between pages.
|
|
35
|
+
<!-- @endif -->
|
|
36
|
+
- `src/theme/themes.ts` holds the light and the dark theme: make them your own.
|
|
37
|
+
<!-- @if sample-data && ssr -->
|
|
38
|
+
- `src/pages/items/` lists the sample items of `src/data/items.ts`, with a page for each,
|
|
39
|
+
read on each request: make them fetch your own.
|
|
40
|
+
<!-- @elif sample-data -->
|
|
41
|
+
- `src/pages/items/` lists the sample items of `src/data/items.ts`, with a page for each,
|
|
42
|
+
read at build time: make them fetch your own.
|
|
43
|
+
<!-- @endif -->
|
|
44
|
+
<!-- @if tanstack-query && react-hook-form -->
|
|
45
|
+
- [TanStack Query](https://tanstack.com/query): every island's `Island` provides one query client,
|
|
46
|
+
`src/data/queryClient.ts`. The home page's `Greeting` reads `src/data/api.ts` with a query,
|
|
47
|
+
and the form sends its values with a mutation.
|
|
48
|
+
<!-- @elif tanstack-query -->
|
|
49
|
+
- [TanStack Query](https://tanstack.com/query): every island's `Island` provides one query client,
|
|
50
|
+
`src/data/queryClient.ts`. The home page's `Greeting` reads `src/data/api.ts` with a query.
|
|
51
|
+
<!-- @endif -->
|
|
52
|
+
<!-- @if swr && react-hook-form -->
|
|
53
|
+
- [SWR](https://swr.vercel.app): the home page's `Greeting` reads `src/data/api.ts` with SWR,
|
|
54
|
+
and the form sends its values with a mutation.
|
|
55
|
+
<!-- @elif swr -->
|
|
56
|
+
- [SWR](https://swr.vercel.app): the home page's `Greeting` reads `src/data/api.ts` with SWR.
|
|
57
|
+
<!-- @endif -->
|
|
58
|
+
<!-- @if (tanstack-query || swr) && sample-data -->
|
|
59
|
+
The items pages start on the items they were rendered with, then fetch them again in the
|
|
60
|
+
browser.
|
|
61
|
+
<!-- @endif -->
|
|
62
|
+
<!-- @if msw -->
|
|
63
|
+
- [msw](https://mswjs.io): `src/mocks/handlers.ts` is a mock API, which answers the requests of
|
|
64
|
+
`src/data/api.ts` in the browser while `PUBLIC_MSW` in `.env` is true, and in the tests
|
|
65
|
+
through msw's Node server, `src/mocks/server.ts`.
|
|
66
|
+
<!-- @endif -->
|
|
67
|
+
<!-- @if zustand -->
|
|
68
|
+
- [zustand](https://zustand.docs.pmnd.rs): `src/state/counter.ts` holds the home page's count, in
|
|
69
|
+
a store every island shares.
|
|
70
|
+
<!-- @elif jotai -->
|
|
71
|
+
- [Jotai](https://jotai.org): `src/state/counter.ts` holds the home page's count, in an atom
|
|
72
|
+
every island shares.
|
|
73
|
+
<!-- @elif redux-toolkit -->
|
|
74
|
+
- [Redux Toolkit](https://redux-toolkit.js.org): `src/state/store.ts` holds the home page's
|
|
75
|
+
count, in a slice of the store every island's `Island` provides.
|
|
76
|
+
<!-- @endif -->
|
|
77
|
+
<!-- @if react-hook-form -->
|
|
78
|
+
- [React Hook Form](https://react-hook-form.com) and [Zod](https://zod.dev): `/form` is a form
|
|
79
|
+
whose values a Zod schema checks.
|
|
80
|
+
<!-- @endif -->
|
|
81
|
+
<!-- @if ssg -->
|
|
82
|
+
- [Vitest](https://vitest.dev) and [Testing Library](https://testing-library.com): `npm run test`
|
|
83
|
+
runs `src/components/Home.test.tsx`, which checks the title.
|
|
84
|
+
<!-- @elif msw -->
|
|
85
|
+
- [Vitest](https://vitest.dev) and [Testing Library](https://testing-library.com): `npm run test`
|
|
86
|
+
runs `src/components/Home.test.tsx`, which checks the title, the counter and the greeting.
|
|
87
|
+
<!-- @else -->
|
|
88
|
+
- [Vitest](https://vitest.dev) and [Testing Library](https://testing-library.com): `npm run test`
|
|
89
|
+
runs `src/components/Home.test.tsx`, which checks the title and the counter.
|
|
90
|
+
<!-- @endif -->
|
|
91
|
+
|
|
92
|
+
Fonts: the theme names Space Grotesk and JetBrains Mono, and does not load them. See
|
|
93
|
+
https://docs.soroush.tech/design-system/getting-started/installation/#fonts
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// @if ssr
|
|
2
|
+
import node from '@astrojs/node'
|
|
3
|
+
// @endif
|
|
4
|
+
import react from '@astrojs/react'
|
|
5
|
+
// @if msw
|
|
6
|
+
import { msw } from 'msw/vite'
|
|
7
|
+
// @endif
|
|
8
|
+
import { defineConfig } from 'astro/config'
|
|
9
|
+
|
|
10
|
+
// The settings of the app: https://docs.astro.build/en/reference/configuration-reference/
|
|
11
|
+
export default defineConfig({
|
|
12
|
+
// @if ssr
|
|
13
|
+
// SSR: every page is rendered on the server, on each request, by the Node server the build
|
|
14
|
+
// writes: npm start.
|
|
15
|
+
output: 'server',
|
|
16
|
+
adapter: node({ mode: 'standalone' }),
|
|
17
|
+
// @endif
|
|
18
|
+
integrations: [react()],
|
|
19
|
+
// @if msw
|
|
20
|
+
// msw's plugin serves the mock API's worker, in development and in the build.
|
|
21
|
+
vite: { plugins: [msw({ mode: 'worker-only' })] },
|
|
22
|
+
// @endif
|
|
23
|
+
})
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
2
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
3
|
+
import { withIsland } from '../theme/Island'
|
|
4
|
+
|
|
5
|
+
function About() {
|
|
6
|
+
return (
|
|
7
|
+
<Flex gap={2} px={3} py={4}>
|
|
8
|
+
<Typography variant="h2">About</Typography>
|
|
9
|
+
<Typography color="secondary">
|
|
10
|
+
{/* @if ssr */}
|
|
11
|
+
A second page: the server renders it, with its styles, and the app moves to it without a
|
|
12
|
+
reload.
|
|
13
|
+
{/* @elif ssg */}
|
|
14
|
+
A second page: it is HTML with its styles from the build, and a link loads it as a new page.
|
|
15
|
+
{/* @else */}
|
|
16
|
+
A second page: it is HTML with its styles from the build, and the app moves to it without a
|
|
17
|
+
reload.
|
|
18
|
+
{/* @endif */}
|
|
19
|
+
</Typography>
|
|
20
|
+
</Flex>
|
|
21
|
+
)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default withIsland(About)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// @if !ssg
|
|
2
|
+
import { useState } from 'react'
|
|
3
|
+
import { Button } from '@soroush.tech/design-system/Button'
|
|
4
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
5
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
6
|
+
import { withIsland } from '../theme/Island'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A count, held in the component's own state, and the button that adds to it. An island of its
|
|
10
|
+
* own, which renders in the browser only.
|
|
11
|
+
*/
|
|
12
|
+
function Counter() {
|
|
13
|
+
const [count, setCount] = useState(0)
|
|
14
|
+
const increment = () => setCount(count + 1)
|
|
15
|
+
return (
|
|
16
|
+
<Flex flexDirection="row" alignItems="center" gap={2}>
|
|
17
|
+
<Button size="sm" variant="outlined" onClick={increment}>
|
|
18
|
+
Count
|
|
19
|
+
</Button>
|
|
20
|
+
<Typography>Count: {count}</Typography>
|
|
21
|
+
</Flex>
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default withIsland(Counter)
|
|
26
|
+
// @endif
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// @if tanstack-query || swr || msw
|
|
2
|
+
// @if tanstack-query
|
|
3
|
+
import { useQuery } from '@tanstack/react-query'
|
|
4
|
+
// @elif swr
|
|
5
|
+
import useSWR from 'swr'
|
|
6
|
+
// @else
|
|
7
|
+
import { useEffect, useState } from 'react'
|
|
8
|
+
// @endif
|
|
9
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
10
|
+
// @if !tanstack-query && !swr
|
|
11
|
+
import type { Greeting as Reply } from '../data/api'
|
|
12
|
+
// @endif
|
|
13
|
+
import { fetchGreeting } from '../data/api'
|
|
14
|
+
import { withIsland } from '../theme/Island'
|
|
15
|
+
|
|
16
|
+
// @if tanstack-query
|
|
17
|
+
/**
|
|
18
|
+
* What the API says, read with a query of TanStack Query. An island of its own, which renders in
|
|
19
|
+
* the browser only, where the query runs.
|
|
20
|
+
*/
|
|
21
|
+
function Greeting() {
|
|
22
|
+
const { data } = useQuery({ queryKey: ['greeting'], queryFn: fetchGreeting })
|
|
23
|
+
return (
|
|
24
|
+
<Typography color="secondary">
|
|
25
|
+
{data ? data.message : 'Asking the API for a greeting...'}
|
|
26
|
+
</Typography>
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
// @elif swr
|
|
30
|
+
/**
|
|
31
|
+
* What the API says, read with SWR. An island of its own, which renders in the browser only,
|
|
32
|
+
* where SWR runs.
|
|
33
|
+
*/
|
|
34
|
+
function Greeting() {
|
|
35
|
+
const { data } = useSWR('greeting', fetchGreeting)
|
|
36
|
+
return (
|
|
37
|
+
<Typography color="secondary">
|
|
38
|
+
{data ? data.message : 'Asking the API for a greeting...'}
|
|
39
|
+
</Typography>
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
// @else
|
|
43
|
+
/**
|
|
44
|
+
* What the API says, read with fetch, as the island loads. An island of its own, which renders in
|
|
45
|
+
* the browser only.
|
|
46
|
+
*/
|
|
47
|
+
function Greeting() {
|
|
48
|
+
const [data, setData] = useState<Reply>()
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
fetchGreeting().then(setData, () => setData({ message: 'The API did not answer.' }))
|
|
51
|
+
}, [])
|
|
52
|
+
return (
|
|
53
|
+
<Typography color="secondary">
|
|
54
|
+
{data ? data.message : 'Asking the API for a greeting...'}
|
|
55
|
+
</Typography>
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
// @endif
|
|
59
|
+
|
|
60
|
+
export default withIsland(Greeting)
|
|
61
|
+
// @endif
|