@soroush.design/cli 0.2.0-alpha.2 → 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.
Files changed (184) hide show
  1. package/README.md +9 -112
  2. package/dist/bin.mjs +1 -1
  3. package/dist/index.d.mts +14 -3
  4. package/dist/index.mjs +1 -1
  5. package/dist/{io-uDtjQuG6.mjs → io-C_8_JerZ.mjs} +579 -643
  6. package/package.json +26 -5
  7. package/templates/astro/js/base/README.md +93 -0
  8. package/templates/astro/js/base/_gitignore +5 -0
  9. package/templates/astro/js/base/astro.config.mjs +23 -0
  10. package/templates/astro/js/base/src/components/About.jsx +24 -0
  11. package/templates/astro/js/base/src/components/Counter.jsx +26 -0
  12. package/templates/astro/js/base/src/components/Greeting.jsx +60 -0
  13. package/templates/astro/js/base/src/components/Header.jsx +99 -0
  14. package/templates/astro/js/base/src/components/Home.jsx +32 -0
  15. package/templates/astro/js/base/src/components/Home.test.jsx +39 -0
  16. package/templates/astro/js/base/src/components/NotFound.jsx +17 -0
  17. package/templates/astro/js/base/src/data/api.js +84 -0
  18. package/templates/astro/js/base/src/layouts/Layout.astro +52 -0
  19. package/templates/astro/js/base/src/pages/404.astro +12 -0
  20. package/templates/astro/js/base/src/pages/about.astro +12 -0
  21. package/templates/astro/js/base/src/pages/index.astro +23 -0
  22. package/templates/astro/js/base/src/theme/Island.jsx +89 -0
  23. package/templates/astro/js/base/src/theme/scheme.js +32 -0
  24. package/templates/astro/js/base/vitest.config.js +12 -0
  25. package/templates/astro/js/sample-data/src/components/ItemDetails.jsx +64 -0
  26. package/templates/astro/js/sample-data/src/components/ItemList.jsx +100 -0
  27. package/templates/astro/js/sample-data/src/components/StatusChip.jsx +8 -0
  28. package/templates/astro/js/sample-data/src/data/items.js +65 -0
  29. package/templates/astro/js/sample-data/src/pages/items/[id].astro +37 -0
  30. package/templates/astro/js/sample-data/src/pages/items/index.astro +20 -0
  31. package/templates/astro/js/with/jotai/src/components/Counter.jsx +26 -0
  32. package/templates/astro/js/with/jotai/src/state/counter.js +4 -0
  33. package/templates/astro/js/with/msw/_env +3 -0
  34. package/templates/astro/js/with/msw/src/mocks/browser.js +5 -0
  35. package/templates/astro/js/with/msw/src/mocks/handlers.js +24 -0
  36. package/templates/astro/js/with/msw/src/mocks/server.js +5 -0
  37. package/templates/astro/js/with/msw/src/mocks/setupTests.js +7 -0
  38. package/templates/astro/js/with/msw/src/mocks/start.js +14 -0
  39. package/templates/astro/js/with/react-hook-form/src/components/Form.jsx +313 -0
  40. package/templates/astro/js/with/react-hook-form/src/pages/form.astro +8 -0
  41. package/templates/astro/js/with/redux-toolkit/src/components/Counter.jsx +27 -0
  42. package/templates/astro/js/with/redux-toolkit/src/state/store.js +18 -0
  43. package/templates/astro/js/with/tanstack-query/src/data/queryClient.js +13 -0
  44. package/templates/astro/js/with/zustand/src/components/Counter.jsx +24 -0
  45. package/templates/astro/js/with/zustand/src/state/counter.js +14 -0
  46. package/templates/astro/ts/base/README.md +93 -0
  47. package/templates/astro/ts/base/_gitignore +5 -0
  48. package/templates/astro/ts/base/astro.config.mjs +23 -0
  49. package/templates/astro/ts/base/src/components/About.tsx +24 -0
  50. package/templates/astro/ts/base/src/components/Counter.tsx +26 -0
  51. package/templates/astro/ts/base/src/components/Greeting.tsx +61 -0
  52. package/templates/astro/ts/base/src/components/Header.tsx +99 -0
  53. package/templates/astro/ts/base/src/components/Home.test.tsx +39 -0
  54. package/templates/astro/ts/base/src/components/Home.tsx +33 -0
  55. package/templates/astro/ts/base/src/components/NotFound.tsx +17 -0
  56. package/templates/astro/ts/base/src/data/api.ts +92 -0
  57. package/templates/astro/ts/base/src/layouts/Layout.astro +52 -0
  58. package/templates/astro/ts/base/src/pages/404.astro +12 -0
  59. package/templates/astro/ts/base/src/pages/about.astro +12 -0
  60. package/templates/astro/ts/base/src/pages/index.astro +23 -0
  61. package/templates/astro/ts/base/src/theme/Island.tsx +90 -0
  62. package/templates/astro/ts/base/src/theme/scheme.ts +34 -0
  63. package/templates/astro/ts/base/tsconfig.json +9 -0
  64. package/templates/astro/ts/base/vitest.config.ts +12 -0
  65. package/templates/astro/ts/sample-data/src/components/ItemDetails.tsx +65 -0
  66. package/templates/astro/ts/sample-data/src/components/ItemList.tsx +109 -0
  67. package/templates/astro/ts/sample-data/src/components/StatusChip.tsx +9 -0
  68. package/templates/astro/ts/sample-data/src/data/items.ts +71 -0
  69. package/templates/astro/ts/sample-data/src/pages/items/[id].astro +39 -0
  70. package/templates/astro/ts/sample-data/src/pages/items/index.astro +20 -0
  71. package/templates/astro/ts/with/jotai/src/components/Counter.tsx +26 -0
  72. package/templates/astro/ts/with/jotai/src/state/counter.ts +4 -0
  73. package/templates/astro/ts/with/msw/_env +3 -0
  74. package/templates/astro/ts/with/msw/src/mocks/browser.ts +5 -0
  75. package/templates/astro/ts/with/msw/src/mocks/handlers.ts +24 -0
  76. package/templates/astro/ts/with/msw/src/mocks/server.ts +5 -0
  77. package/templates/astro/ts/with/msw/src/mocks/setupTests.ts +7 -0
  78. package/templates/astro/ts/with/msw/src/mocks/start.ts +14 -0
  79. package/templates/astro/ts/with/react-hook-form/src/components/Form.tsx +317 -0
  80. package/templates/astro/ts/with/react-hook-form/src/pages/form.astro +8 -0
  81. package/templates/astro/ts/with/redux-toolkit/src/components/Counter.tsx +27 -0
  82. package/templates/astro/ts/with/redux-toolkit/src/state/store.ts +19 -0
  83. package/templates/astro/ts/with/tanstack-query/src/data/queryClient.ts +13 -0
  84. package/templates/astro/ts/with/zustand/src/components/Counter.tsx +24 -0
  85. package/templates/astro/ts/with/zustand/src/state/counter.ts +19 -0
  86. package/templates/templates.md +66 -0
  87. package/templates/vike/js/base/README.md +87 -0
  88. package/templates/vike/js/base/_gitignore +4 -0
  89. package/templates/vike/js/base/src/data/api.js +76 -0
  90. package/templates/vike/js/base/src/pages/+Layout.jsx +183 -0
  91. package/templates/vike/js/base/src/pages/+config.js +12 -0
  92. package/templates/vike/js/base/src/pages/+onRenderClient.jsx +39 -0
  93. package/templates/vike/js/base/src/pages/+onRenderHtml.jsx +27 -0
  94. package/templates/vike/js/base/src/pages/PageShell.jsx +47 -0
  95. package/templates/vike/js/base/src/pages/_error/+Page.jsx +14 -0
  96. package/templates/vike/js/base/src/pages/about/+Page.jsx +14 -0
  97. package/templates/vike/js/base/src/pages/index/+Page.jsx +33 -0
  98. package/templates/vike/js/base/src/pages/index/Counter.jsx +20 -0
  99. package/templates/vike/js/base/src/pages/index/Greeting.jsx +48 -0
  100. package/templates/vike/js/base/src/pages/index/Page.test.jsx +67 -0
  101. package/templates/vike/js/base/src/pages/usePageContext.jsx +20 -0
  102. package/templates/vike/js/base/vite.config.js +14 -0
  103. package/templates/vike/js/base/vitest.config.js +13 -0
  104. package/templates/vike/js/sample-data/src/data/items.js +58 -0
  105. package/templates/vike/js/sample-data/src/pages/items/+Page.jsx +49 -0
  106. package/templates/vike/js/sample-data/src/pages/items/+data.js +26 -0
  107. package/templates/vike/js/sample-data/src/pages/items/@id/+Page.jsx +40 -0
  108. package/templates/vike/js/sample-data/src/pages/items/@id/+data.js +33 -0
  109. package/templates/vike/js/sample-data/src/pages/items/@id/+onBeforePrerenderStart.js +8 -0
  110. package/templates/vike/js/sample-data/src/pages/items/StatusChip.jsx +8 -0
  111. package/templates/vike/js/spa/src/pages/+config.js +12 -0
  112. package/templates/vike/js/spa/src/pages/+onRenderClient.jsx +25 -0
  113. package/templates/vike/js/spa/src/pages/+onRenderHtml.jsx +16 -0
  114. package/templates/vike/js/spa/src/pages/about/+Page.jsx +14 -0
  115. package/templates/vike/js/ssg/src/pages/+config.js +11 -0
  116. package/templates/vike/js/ssg/src/pages/about/+Page.jsx +13 -0
  117. package/templates/vike/js/ssr/src/pages/+config.js +12 -0
  118. package/templates/vike/js/ssr/src/pages/+onRenderHtml.jsx +27 -0
  119. package/templates/vike/js/ssr/src/pages/about/+Page.jsx +14 -0
  120. package/templates/vike/js/with/jotai/src/pages/index/Counter.jsx +19 -0
  121. package/templates/vike/js/with/jotai/src/state/counter.js +4 -0
  122. package/templates/vike/js/with/jotai/src/state/scheme.js +4 -0
  123. package/templates/vike/js/with/msw/_env +3 -0
  124. package/templates/vike/js/with/msw/src/mocks/browser.js +5 -0
  125. package/templates/vike/js/with/msw/src/mocks/handlers.js +24 -0
  126. package/templates/vike/js/with/msw/src/mocks/server.js +5 -0
  127. package/templates/vike/js/with/msw/src/mocks/setupTests.js +7 -0
  128. package/templates/vike/js/with/msw/src/mocks/start.js +10 -0
  129. package/templates/vike/js/with/react-hook-form/src/pages/form/+Page.jsx +294 -0
  130. package/templates/vike/js/with/redux-toolkit/src/pages/index/Counter.jsx +20 -0
  131. package/templates/vike/js/with/redux-toolkit/src/state/store.js +27 -0
  132. package/templates/vike/js/with/zustand/src/pages/index/Counter.jsx +17 -0
  133. package/templates/vike/js/with/zustand/src/state/counter.js +14 -0
  134. package/templates/vike/js/with/zustand/src/state/scheme.js +14 -0
  135. package/templates/vike/ts/base/README.md +87 -0
  136. package/templates/vike/ts/base/_gitignore +4 -0
  137. package/templates/vike/ts/base/src/data/api.ts +84 -0
  138. package/templates/vike/ts/base/src/pages/+Layout.tsx +184 -0
  139. package/templates/vike/ts/base/src/pages/+config.ts +14 -0
  140. package/templates/vike/ts/base/src/pages/+onRenderClient.tsx +41 -0
  141. package/templates/vike/ts/base/src/pages/+onRenderHtml.tsx +28 -0
  142. package/templates/vike/ts/base/src/pages/PageShell.tsx +48 -0
  143. package/templates/vike/ts/base/src/pages/_error/+Page.tsx +14 -0
  144. package/templates/vike/ts/base/src/pages/about/+Page.tsx +14 -0
  145. package/templates/vike/ts/base/src/pages/index/+Page.tsx +33 -0
  146. package/templates/vike/ts/base/src/pages/index/Counter.tsx +20 -0
  147. package/templates/vike/ts/base/src/pages/index/Greeting.tsx +49 -0
  148. package/templates/vike/ts/base/src/pages/index/Page.test.tsx +67 -0
  149. package/templates/vike/ts/base/src/pages/usePageContext.tsx +27 -0
  150. package/templates/vike/ts/base/src/vike.d.ts +20 -0
  151. package/templates/vike/ts/base/tsconfig.json +18 -0
  152. package/templates/vike/ts/base/vite.config.ts +14 -0
  153. package/templates/vike/ts/base/vitest.config.ts +13 -0
  154. package/templates/vike/ts/sample-data/src/data/items.ts +64 -0
  155. package/templates/vike/ts/sample-data/src/pages/items/+Page.tsx +50 -0
  156. package/templates/vike/ts/sample-data/src/pages/items/+data.ts +28 -0
  157. package/templates/vike/ts/sample-data/src/pages/items/@id/+Page.tsx +41 -0
  158. package/templates/vike/ts/sample-data/src/pages/items/@id/+data.ts +37 -0
  159. package/templates/vike/ts/sample-data/src/pages/items/@id/+onBeforePrerenderStart.ts +8 -0
  160. package/templates/vike/ts/sample-data/src/pages/items/StatusChip.tsx +9 -0
  161. package/templates/vike/ts/spa/src/pages/+config.ts +14 -0
  162. package/templates/vike/ts/spa/src/pages/+onRenderClient.tsx +27 -0
  163. package/templates/vike/ts/spa/src/pages/+onRenderHtml.tsx +17 -0
  164. package/templates/vike/ts/spa/src/pages/about/+Page.tsx +14 -0
  165. package/templates/vike/ts/ssg/src/pages/+config.ts +13 -0
  166. package/templates/vike/ts/ssg/src/pages/about/+Page.tsx +13 -0
  167. package/templates/vike/ts/ssr/src/pages/+config.ts +14 -0
  168. package/templates/vike/ts/ssr/src/pages/+onRenderHtml.tsx +28 -0
  169. package/templates/vike/ts/ssr/src/pages/about/+Page.tsx +14 -0
  170. package/templates/vike/ts/with/jotai/src/pages/index/Counter.tsx +19 -0
  171. package/templates/vike/ts/with/jotai/src/state/counter.ts +4 -0
  172. package/templates/vike/ts/with/jotai/src/state/scheme.ts +4 -0
  173. package/templates/vike/ts/with/msw/_env +3 -0
  174. package/templates/vike/ts/with/msw/src/mocks/browser.ts +5 -0
  175. package/templates/vike/ts/with/msw/src/mocks/handlers.ts +24 -0
  176. package/templates/vike/ts/with/msw/src/mocks/server.ts +5 -0
  177. package/templates/vike/ts/with/msw/src/mocks/setupTests.ts +7 -0
  178. package/templates/vike/ts/with/msw/src/mocks/start.ts +10 -0
  179. package/templates/vike/ts/with/react-hook-form/src/pages/form/+Page.tsx +298 -0
  180. package/templates/vike/ts/with/redux-toolkit/src/pages/index/Counter.tsx +20 -0
  181. package/templates/vike/ts/with/redux-toolkit/src/state/store.ts +29 -0
  182. package/templates/vike/ts/with/zustand/src/pages/index/Counter.tsx +17 -0
  183. package/templates/vike/ts/with/zustand/src/state/counter.ts +19 -0
  184. package/templates/vike/ts/with/zustand/src/state/scheme.ts +19 -0
@@ -0,0 +1,4 @@
1
+ import { atom } from 'jotai'
2
+
3
+ /** The home page's count: a Jotai atom, which any component can read. */
4
+ export const countAtom = atom(0)
@@ -0,0 +1,4 @@
1
+ import { atom } from 'jotai'
2
+
3
+ /** The color scheme the app is drawn in: a Jotai atom, which any component can read. */
4
+ export const schemeAtom = atom('__SCHEME__')
@@ -0,0 +1,3 @@
1
+ # The mock API in src/mocks answers in the browser while this is true. Set it to false once
2
+ # the app's own API answers.
3
+ VITE_MSW=true
@@ -0,0 +1,5 @@
1
+ import { setupWorker } from 'msw/browser'
2
+ import { handlers } from './handlers'
3
+
4
+ /** The mock API in the browser: a service worker answers the requests the handlers match. */
5
+ export const worker = setupWorker(...handlers)
@@ -0,0 +1,24 @@
1
+ import { http, HttpResponse } from 'msw'
2
+ // @if sample-data
3
+ import { items } from '../data/items'
4
+ // @endif
5
+
6
+ /** The mock API: what it answers, by method and path. Add a handler for each call the app makes. */
7
+ export const handlers = [
8
+ http.get('/api/greeting', () => HttpResponse.json({ message: 'Hello from the mock API' })),
9
+ // @if react-hook-form
10
+ http.post('/api/messages', async ({ request }) => {
11
+ const { name } = await request.json()
12
+ return HttpResponse.json({ message: `Thanks, ${name}: the mock API took your message.` })
13
+ }),
14
+ // @endif
15
+ // @if sample-data
16
+ http.get('/api/items', () =>
17
+ HttpResponse.json(items.map(({ id, title, status }) => ({ id, title, status })))
18
+ ),
19
+ http.get('/api/items/:id', ({ params }) => {
20
+ const item = items.find(({ id }) => id === params.id)
21
+ return item ? HttpResponse.json(item) : new HttpResponse(null, { status: 404 })
22
+ }),
23
+ // @endif
24
+ ]
@@ -0,0 +1,5 @@
1
+ import { setupServer } from 'msw/node'
2
+ import { handlers } from './handlers'
3
+
4
+ /** The mock API in Node, for the tests: it answers the requests the handlers match. */
5
+ export const server = setupServer(...handlers)
@@ -0,0 +1,7 @@
1
+ import { afterAll, afterEach, beforeAll } from 'vitest'
2
+ import { server } from './server'
3
+
4
+ // The mock API answers every request a test makes; one it has no handler for fails the test.
5
+ beforeAll(() => server.listen({ onUnhandledFrame: 'error' }))
6
+ afterEach(() => server.resetHandlers())
7
+ afterAll(() => server.close())
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Starts the mock API in the browser while VITE_MSW, in .env, is true. Set it to false once the
3
+ * app's own API answers.
4
+ */
5
+ export async function startMocks() {
6
+ if (import.meta.env.VITE_MSW !== 'true') return
7
+ const { worker } = await import('./browser')
8
+ // A request the mock API has no handler for - a page the router fetches - goes to the network.
9
+ await worker.start({ onUnhandledFrame: 'bypass' })
10
+ }
@@ -0,0 +1,294 @@
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
+ // @if tanstack-query || swr || msw
18
+ import { sendMessage } from '../../data/api'
19
+ // @endif
20
+
21
+ /** What the form takes, and what it says when a value will not do. */
22
+ const schema = z.object({
23
+ name: z.string().trim().min(1, 'Your name, please.'),
24
+ email: z.email('An email address, please.'),
25
+ })
26
+
27
+ // @if tanstack-query
28
+ /** A form whose values a Zod schema checks; once they pass, it sends them with a mutation of TanStack Query. */
29
+ export default function Page() {
30
+ const mutation = useMutation({ mutationFn: sendMessage })
31
+ const { control, handleSubmit } = useForm({
32
+ resolver: zodResolver(schema),
33
+ defaultValues: { name: '', email: '' },
34
+ })
35
+ return (
36
+ <Flex gap={2} alignItems="flex-start">
37
+ <Typography variant="h2">Form</Typography>
38
+ <Typography color="secondary">
39
+ React Hook Form holds the values, and a Zod schema checks them when the form is sent.
40
+ </Typography>
41
+ <form noValidate onSubmit={handleSubmit((values) => mutation.mutate(values))}>
42
+ <Flex gap={2} alignItems="flex-start">
43
+ <Controller
44
+ name="name"
45
+ control={control}
46
+ render={({ field, fieldState }) => (
47
+ <Flex gap={1}>
48
+ <TextInput
49
+ name={field.name}
50
+ value={field.value}
51
+ onChange={field.onChange}
52
+ placeholder="Name"
53
+ error={fieldState.invalid}
54
+ inputProps={{ 'aria-label': 'Name', autoComplete: 'name', onBlur: field.onBlur }}
55
+ />
56
+ <Typography variant="caption" color="error">
57
+ {fieldState.error?.message}
58
+ </Typography>
59
+ </Flex>
60
+ )}
61
+ />
62
+ <Controller
63
+ name="email"
64
+ control={control}
65
+ render={({ field, fieldState }) => (
66
+ <Flex gap={1}>
67
+ <TextInput
68
+ name={field.name}
69
+ value={field.value}
70
+ onChange={field.onChange}
71
+ type="email"
72
+ placeholder="Email"
73
+ error={fieldState.invalid}
74
+ inputProps={{
75
+ 'aria-label': 'Email',
76
+ autoComplete: 'email',
77
+ onBlur: field.onBlur,
78
+ }}
79
+ />
80
+ <Typography variant="caption" color="error">
81
+ {fieldState.error?.message}
82
+ </Typography>
83
+ </Flex>
84
+ )}
85
+ />
86
+ <Button type="submit">Send</Button>
87
+ </Flex>
88
+ </form>
89
+ {mutation.data ? <Typography>{mutation.data.message}</Typography> : null}
90
+ </Flex>
91
+ )
92
+ }
93
+ // @elif swr
94
+ /** A form whose values a Zod schema checks; once they pass, it sends them with SWR's mutation. */
95
+ export default function Page() {
96
+ const { trigger, data } = useSWRMutation('messages', (_key, { arg }) => sendMessage(arg))
97
+ const { control, handleSubmit } = useForm({
98
+ resolver: zodResolver(schema),
99
+ defaultValues: { name: '', email: '' },
100
+ })
101
+ return (
102
+ <Flex gap={2} alignItems="flex-start">
103
+ <Typography variant="h2">Form</Typography>
104
+ <Typography color="secondary">
105
+ React Hook Form holds the values, and a Zod schema checks them when the form is sent.
106
+ </Typography>
107
+ <form noValidate onSubmit={handleSubmit((values) => trigger(values))}>
108
+ <Flex gap={2} alignItems="flex-start">
109
+ <Controller
110
+ name="name"
111
+ control={control}
112
+ render={({ field, fieldState }) => (
113
+ <Flex gap={1}>
114
+ <TextInput
115
+ name={field.name}
116
+ value={field.value}
117
+ onChange={field.onChange}
118
+ placeholder="Name"
119
+ error={fieldState.invalid}
120
+ inputProps={{ 'aria-label': 'Name', autoComplete: 'name', onBlur: field.onBlur }}
121
+ />
122
+ <Typography variant="caption" color="error">
123
+ {fieldState.error?.message}
124
+ </Typography>
125
+ </Flex>
126
+ )}
127
+ />
128
+ <Controller
129
+ name="email"
130
+ control={control}
131
+ render={({ field, fieldState }) => (
132
+ <Flex gap={1}>
133
+ <TextInput
134
+ name={field.name}
135
+ value={field.value}
136
+ onChange={field.onChange}
137
+ type="email"
138
+ placeholder="Email"
139
+ error={fieldState.invalid}
140
+ inputProps={{
141
+ 'aria-label': 'Email',
142
+ autoComplete: 'email',
143
+ onBlur: field.onBlur,
144
+ }}
145
+ />
146
+ <Typography variant="caption" color="error">
147
+ {fieldState.error?.message}
148
+ </Typography>
149
+ </Flex>
150
+ )}
151
+ />
152
+ <Button type="submit">Send</Button>
153
+ </Flex>
154
+ </form>
155
+ {data ? <Typography>{data.message}</Typography> : null}
156
+ </Flex>
157
+ )
158
+ }
159
+ // @elif msw
160
+ /** A form whose values a Zod schema checks; once they pass, it sends them to the API. */
161
+ export default function Page() {
162
+ const [answer, setAnswer] = useState(null)
163
+ const { control, handleSubmit } = useForm({
164
+ resolver: zodResolver(schema),
165
+ defaultValues: { name: '', email: '' },
166
+ })
167
+ return (
168
+ <Flex gap={2} alignItems="flex-start">
169
+ <Typography variant="h2">Form</Typography>
170
+ <Typography color="secondary">
171
+ React Hook Form holds the values, and a Zod schema checks them when the form is sent.
172
+ </Typography>
173
+ <form
174
+ noValidate
175
+ onSubmit={handleSubmit(async (values) => setAnswer((await sendMessage(values)).message))}
176
+ >
177
+ <Flex gap={2} alignItems="flex-start">
178
+ <Controller
179
+ name="name"
180
+ control={control}
181
+ render={({ field, fieldState }) => (
182
+ <Flex gap={1}>
183
+ <TextInput
184
+ name={field.name}
185
+ value={field.value}
186
+ onChange={field.onChange}
187
+ placeholder="Name"
188
+ error={fieldState.invalid}
189
+ inputProps={{ 'aria-label': 'Name', autoComplete: 'name', onBlur: field.onBlur }}
190
+ />
191
+ <Typography variant="caption" color="error">
192
+ {fieldState.error?.message}
193
+ </Typography>
194
+ </Flex>
195
+ )}
196
+ />
197
+ <Controller
198
+ name="email"
199
+ control={control}
200
+ render={({ field, fieldState }) => (
201
+ <Flex gap={1}>
202
+ <TextInput
203
+ name={field.name}
204
+ value={field.value}
205
+ onChange={field.onChange}
206
+ type="email"
207
+ placeholder="Email"
208
+ error={fieldState.invalid}
209
+ inputProps={{
210
+ 'aria-label': 'Email',
211
+ autoComplete: 'email',
212
+ onBlur: field.onBlur,
213
+ }}
214
+ />
215
+ <Typography variant="caption" color="error">
216
+ {fieldState.error?.message}
217
+ </Typography>
218
+ </Flex>
219
+ )}
220
+ />
221
+ <Button type="submit">Send</Button>
222
+ </Flex>
223
+ </form>
224
+ {answer ? <Typography>{answer}</Typography> : null}
225
+ </Flex>
226
+ )
227
+ }
228
+ // @else
229
+ /** A form whose values a Zod schema checks; once they pass, it shows them. */
230
+ export default function Page() {
231
+ const [sent, setSent] = useState(null)
232
+ const { control, handleSubmit } = useForm({
233
+ resolver: zodResolver(schema),
234
+ defaultValues: { name: '', email: '' },
235
+ })
236
+ return (
237
+ <Flex gap={2} alignItems="flex-start">
238
+ <Typography variant="h2">Form</Typography>
239
+ <Typography color="secondary">
240
+ React Hook Form holds the values, and a Zod schema checks them when the form is sent.
241
+ </Typography>
242
+ <form noValidate onSubmit={handleSubmit(setSent)}>
243
+ <Flex gap={2} alignItems="flex-start">
244
+ <Controller
245
+ name="name"
246
+ control={control}
247
+ render={({ field, fieldState }) => (
248
+ <Flex gap={1}>
249
+ <TextInput
250
+ name={field.name}
251
+ value={field.value}
252
+ onChange={field.onChange}
253
+ placeholder="Name"
254
+ error={fieldState.invalid}
255
+ inputProps={{ 'aria-label': 'Name', autoComplete: 'name', onBlur: field.onBlur }}
256
+ />
257
+ <Typography variant="caption" color="error">
258
+ {fieldState.error?.message}
259
+ </Typography>
260
+ </Flex>
261
+ )}
262
+ />
263
+ <Controller
264
+ name="email"
265
+ control={control}
266
+ render={({ field, fieldState }) => (
267
+ <Flex gap={1}>
268
+ <TextInput
269
+ name={field.name}
270
+ value={field.value}
271
+ onChange={field.onChange}
272
+ type="email"
273
+ placeholder="Email"
274
+ error={fieldState.invalid}
275
+ inputProps={{
276
+ 'aria-label': 'Email',
277
+ autoComplete: 'email',
278
+ onBlur: field.onBlur,
279
+ }}
280
+ />
281
+ <Typography variant="caption" color="error">
282
+ {fieldState.error?.message}
283
+ </Typography>
284
+ </Flex>
285
+ )}
286
+ />
287
+ <Button type="submit">Send</Button>
288
+ </Flex>
289
+ </form>
290
+ {sent ? <Typography>Thanks, {sent.name}: the form was valid.</Typography> : null}
291
+ </Flex>
292
+ )
293
+ }
294
+ // @endif
@@ -0,0 +1,20 @@
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 { useDispatch, useSelector } from 'react-redux'
5
+ import { incremented } from '../../state/store'
6
+
7
+ /** A count, held in the Redux store, src/state/store, and the button that adds to it. */
8
+ export function Counter() {
9
+ const count = useSelector((state) => state.count)
10
+ const dispatch = useDispatch()
11
+ const increment = () => dispatch(incremented())
12
+ return (
13
+ <Flex flexDirection="row" alignItems="center" gap={2}>
14
+ <Button size="sm" variant="outlined" onClick={increment}>
15
+ Count
16
+ </Button>
17
+ <Typography>Count: {count}</Typography>
18
+ </Flex>
19
+ )
20
+ }
@@ -0,0 +1,27 @@
1
+ import { configureStore, createSlice } from '@reduxjs/toolkit'
2
+
3
+ /** The color scheme the app is drawn in: a slice of the store. */
4
+ const schemeSlice = createSlice({
5
+ name: 'scheme',
6
+ initialState: '__SCHEME__',
7
+ reducers: {
8
+ schemeChanged: (_scheme, action) => action.payload,
9
+ },
10
+ })
11
+
12
+ /** The home page's count: another slice. */
13
+ const countSlice = createSlice({
14
+ name: 'count',
15
+ initialState: 0,
16
+ reducers: {
17
+ incremented: (count) => count + 1,
18
+ },
19
+ })
20
+
21
+ export const { schemeChanged } = schemeSlice.actions
22
+ export const { incremented } = countSlice.actions
23
+
24
+ /** The app's Redux store, which PageShell provides to every page. */
25
+ export const store = configureStore({
26
+ reducer: { scheme: schemeSlice.reducer, count: countSlice.reducer },
27
+ })
@@ -0,0 +1,17 @@
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 { useCounter } from '../../state/counter'
5
+
6
+ /** A count, held in a zustand store, src/state/counter, and the button that adds to it. */
7
+ export function Counter() {
8
+ const { count, increment } = useCounter()
9
+ return (
10
+ <Flex flexDirection="row" alignItems="center" gap={2}>
11
+ <Button size="sm" variant="outlined" onClick={increment}>
12
+ Count
13
+ </Button>
14
+ <Typography>Count: {count}</Typography>
15
+ </Flex>
16
+ )
17
+ }
@@ -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,14 @@
1
+ import { create } from 'zustand'
2
+
3
+ /** The color scheme the app is drawn in: a zustand store, which any component can read. */
4
+ // @if ts
5
+ export const useScheme = create()((set) => ({
6
+ scheme: '__SCHEME__',
7
+ setScheme: (scheme) => set({ scheme }),
8
+ }))
9
+ // @else
10
+ export const useScheme = create((set) => ({
11
+ scheme: '__SCHEME__',
12
+ setScheme: (scheme) => set({ scheme }),
13
+ }))
14
+ // @endif
@@ -0,0 +1,87 @@
1
+ # __TITLE__
2
+
3
+ A [Vike](https://vike.dev) app on the [@soroush.tech design system](https://docs.soroush.tech):
4
+ <!-- @if isomorphic -->
5
+ prerendered at build time with its styles in the HTML, then moving between pages with no reload.
6
+ <!-- @elif ssr -->
7
+ every page rendered on the server, on each request, with its styles in the HTML, then moving between pages with no reload.
8
+ <!-- @elif spa -->
9
+ a single-page app: each page is an HTML shell from the build, rendered in the browser, which moves between pages with no reload.
10
+ <!-- @else -->
11
+ every page prerendered to HTML at build time, with its styles, and no JavaScript in the browser.
12
+ <!-- @endif -->
13
+
14
+ - `npm run dev` starts it, `npm run build` builds it, and `npm run preview` serves the build.
15
+ <!-- @if ssr -->
16
+ - `npm start` runs the server the build wrote, on `PORT` (3000 by default), on any Node host.
17
+ <!-- @endif -->
18
+ - `src/pages/` holds the pages: a folder per route, each with its `+Page`.
19
+ - `src/pages/+Layout.tsx` is the frame every page renders in, inside the app's providers.
20
+ - `src/theme/themes.ts` holds the light and the dark theme: make them your own.
21
+ <!-- @if isomorphic -->
22
+ - In `src/pages/`, `+onRenderHtml` and `+onRenderClient` render the pages, with the styles each page uses in its HTML.
23
+ <!-- @elif ssr -->
24
+ - In `src/pages/`, `+onRenderHtml` renders each page on the server and `+onRenderClient` takes it over in the browser, with the styles each page uses in its HTML.
25
+ <!-- @elif spa -->
26
+ - In `src/pages/`, `+onRenderHtml` writes the HTML shell of each page, and `+onRenderClient` renders the page in it.
27
+ <!-- @else -->
28
+ - In `src/pages/`, `+onRenderHtml` renders the pages to HTML, with the styles each page uses.
29
+ <!-- @endif -->
30
+ <!-- @if sample-data -->
31
+ - `src/pages/items/` lists the sample items of `src/data/items.ts`, with a page for each.
32
+ <!-- @if ssr -->
33
+ Their `+data` hooks read the data on each request: make them fetch your own.
34
+ <!-- @else -->
35
+ Their `+data` hooks read the data at build time: make them fetch your own.
36
+ <!-- @endif -->
37
+ <!-- @endif -->
38
+ <!-- @if !ssg && !zustand && !jotai && !redux-toolkit -->
39
+ - `src/pages/index/Counter.tsx` counts the presses of its button, in its own state.
40
+ <!-- @endif -->
41
+ <!-- @if tanstack-query && react-hook-form && sample-data -->
42
+ - [TanStack Query](https://tanstack.com/query): the query client lives in `+Layout`. The home page's `Greeting` reads `src/data/api.ts` with a query, and the form sends its values with a mutation. The items pages start on their build-time data, then query it in the browser.
43
+ <!-- @elif tanstack-query && react-hook-form -->
44
+ - [TanStack Query](https://tanstack.com/query): the query client lives in `+Layout`. The home page's `Greeting` reads `src/data/api.ts` with a query, and the form sends its values with a mutation.
45
+ <!-- @elif tanstack-query && sample-data -->
46
+ - [TanStack Query](https://tanstack.com/query): the query client lives in `+Layout`. The home page's `Greeting` reads `src/data/api.ts` with a query. The items pages start on their build-time data, then query it in the browser.
47
+ <!-- @elif tanstack-query -->
48
+ - [TanStack Query](https://tanstack.com/query): the query client lives in `+Layout`. The home page's `Greeting` reads `src/data/api.ts` with a query.
49
+ <!-- @endif -->
50
+ <!-- @if swr && react-hook-form && sample-data -->
51
+ - [SWR](https://swr.vercel.app): the home page's `Greeting` reads `src/data/api.ts` with SWR, and the form sends its values with a mutation. The items pages start on their build-time data, then fetch it in the browser.
52
+ <!-- @elif swr && react-hook-form -->
53
+ - [SWR](https://swr.vercel.app): the home page's `Greeting` reads `src/data/api.ts` with SWR, and the form sends its values with a mutation.
54
+ <!-- @elif swr && sample-data -->
55
+ - [SWR](https://swr.vercel.app): the home page's `Greeting` reads `src/data/api.ts` with SWR. The items pages start on their build-time data, then fetch it in the browser.
56
+ <!-- @elif swr -->
57
+ - [SWR](https://swr.vercel.app): the home page's `Greeting` reads `src/data/api.ts` with SWR.
58
+ <!-- @endif -->
59
+ <!-- @if msw && react-hook-form && sample-data -->
60
+ - [msw](https://mswjs.io): `src/mocks/handlers.ts` is a mock API, which answers the greeting, the form, the sample items in the browser while `VITE_MSW` in `.env` is true.
61
+ <!-- @elif msw && react-hook-form -->
62
+ - [msw](https://mswjs.io): `src/mocks/handlers.ts` is a mock API, which answers the greeting, the form in the browser while `VITE_MSW` in `.env` is true.
63
+ <!-- @elif msw && sample-data -->
64
+ - [msw](https://mswjs.io): `src/mocks/handlers.ts` is a mock API, which answers the greeting, the sample items in the browser while `VITE_MSW` in `.env` is true.
65
+ <!-- @elif msw -->
66
+ - [msw](https://mswjs.io): `src/mocks/handlers.ts` is a mock API, which answers the greeting in the browser while `VITE_MSW` in `.env` is true.
67
+ <!-- @endif -->
68
+ <!-- @if zustand -->
69
+ - [zustand](https://zustand.docs.pmnd.rs): `src/state/scheme.ts` holds the color scheme and `src/state/counter.ts` the home page's count, in stores.
70
+ <!-- @elif jotai -->
71
+ - [Jotai](https://jotai.org): `src/state/scheme.ts` holds the color scheme and `src/state/counter.ts` the home page's count, in atoms.
72
+ <!-- @elif redux-toolkit -->
73
+ - [Redux Toolkit](https://redux-toolkit.js.org): `src/state/store.ts` holds the color scheme and the home page's count, in slices of the store `PageShell` provides.
74
+ <!-- @endif -->
75
+ <!-- @if react-hook-form -->
76
+ - [React Hook Form](https://react-hook-form.com) and [Zod](https://zod.dev): `/form` is a form whose values a Zod schema checks.
77
+ <!-- @endif -->
78
+ <!-- @if ssg -->
79
+ - [Vitest](https://vitest.dev) and [Testing Library](https://testing-library.com): `npm run test` runs `src/pages/index/Page.test.tsx`, which checks the title.
80
+ <!-- @elif msw -->
81
+ - [Vitest](https://vitest.dev) and [Testing Library](https://testing-library.com): `npm run test` runs `src/pages/index/Page.test.tsx`, which checks the title, the counter and the greeting, which msw's Node server, `src/mocks/server.ts`, answers.
82
+ <!-- @else -->
83
+ - [Vitest](https://vitest.dev) and [Testing Library](https://testing-library.com): `npm run test` runs `src/pages/index/Page.test.tsx`, which checks the title and the counter.
84
+ <!-- @endif -->
85
+
86
+ Fonts: the theme names Space Grotesk and JetBrains Mono, and does not load them. See
87
+ https://docs.soroush.tech/design-system/getting-started/installation/#fonts
@@ -0,0 +1,4 @@
1
+ node_modules/
2
+ dist/
3
+ *.log
4
+ .DS_Store
@@ -0,0 +1,84 @@
1
+ // @if tanstack-query || swr || msw
2
+ // @if ts && sample-data && msw && (tanstack-query || swr)
3
+ import type { Item } from './items'
4
+
5
+ // @elif sample-data && !msw && (tanstack-query || swr)
6
+ import { items } from './items'
7
+
8
+ // @endif
9
+ // @if ts
10
+ /** What the API says to the app. */
11
+ export interface Greeting {
12
+ message: string
13
+ }
14
+ // @if react-hook-form
15
+
16
+ /** What the form sends. */
17
+ export interface Message {
18
+ name: string
19
+ email: string
20
+ }
21
+ // @endif
22
+
23
+ // @endif
24
+ // @if msw
25
+ /** A greeting, from the API: while VITE_MSW is true, the mock API of src/mocks answers. */
26
+ export async function fetchGreeting(): Promise<Greeting> {
27
+ const response = await fetch('/api/greeting')
28
+ if (!response.ok) throw new Error(`GET /api/greeting answered ${response.status}`)
29
+ return response.json()
30
+ }
31
+ // @else
32
+ /** A greeting, as an API would answer: make it fetch from yours. */
33
+ export function fetchGreeting(): Promise<Greeting> {
34
+ return Promise.resolve({ message: 'Hello from src/data/api: make it fetch from your API.' })
35
+ }
36
+ // @endif
37
+ // @if react-hook-form && msw
38
+
39
+ /** Sends the form's values to the API, which answers what it made of them. */
40
+ export async function sendMessage(message: Message): Promise<Greeting> {
41
+ const response = await fetch('/api/messages', {
42
+ method: 'POST',
43
+ headers: { 'Content-Type': 'application/json' },
44
+ body: JSON.stringify(message),
45
+ })
46
+ if (!response.ok) throw new Error(`POST /api/messages answered ${response.status}`)
47
+ return response.json()
48
+ }
49
+ // @elif react-hook-form
50
+
51
+ /** Sends the form's values, as an API would take them: make it post to yours. */
52
+ export function sendMessage(message: Message): Promise<Greeting> {
53
+ return Promise.resolve({ message: `Thanks, ${message.name}: your message was sent.` })
54
+ }
55
+ // @endif
56
+ // @if sample-data && msw && (tanstack-query || swr)
57
+
58
+ /** The items, from the API: while VITE_MSW is true, the mock API of src/mocks answers. */
59
+ export async function fetchItems(): Promise<Pick<Item, 'id' | 'title' | 'status'>[]> {
60
+ const response = await fetch('/api/items')
61
+ if (!response.ok) throw new Error(`GET /api/items answered ${response.status}`)
62
+ return response.json()
63
+ }
64
+
65
+ /** One item, from the API. */
66
+ export async function fetchItem(id: string): Promise<Item> {
67
+ const response = await fetch(`/api/items/${id}`)
68
+ if (!response.ok) throw new Error(`GET /api/items/${id} answered ${response.status}`)
69
+ return response.json()
70
+ }
71
+ // @elif sample-data && (tanstack-query || swr)
72
+
73
+ /** The items, as an API would answer: make it fetch from yours. */
74
+ export function fetchItems() {
75
+ return Promise.resolve(items.map(({ id, title, status }) => ({ id, title, status })))
76
+ }
77
+
78
+ /** One item, as an API would answer: make it fetch from yours. */
79
+ export function fetchItem(id: string) {
80
+ const item = items.find((candidate) => candidate.id === id)
81
+ return item ? Promise.resolve(item) : Promise.reject(new Error(`There is no item ${id}.`))
82
+ }
83
+ // @endif
84
+ // @endif