@soroush.design/cli 0.2.0-alpha.3 → 0.2.0-alpha.5

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 (250) hide show
  1. package/dist/bin.mjs +2 -15
  2. package/dist/index.d.mts +4 -4
  3. package/dist/index.mjs +1 -1
  4. package/dist/{io-Bybgct-f.mjs → io-CqI2BTaP.mjs} +430 -1032
  5. package/package.json +14 -5
  6. package/templates/astro/js/base/README.md +93 -0
  7. package/templates/astro/js/base/_gitignore +5 -0
  8. package/templates/astro/js/base/astro.config.mjs +23 -0
  9. package/templates/astro/js/base/src/components/About.jsx +24 -0
  10. package/templates/astro/js/base/src/components/Counter.jsx +26 -0
  11. package/templates/astro/js/base/src/components/Greeting.jsx +60 -0
  12. package/templates/astro/js/base/src/components/Header.jsx +99 -0
  13. package/templates/astro/js/base/src/components/Home.jsx +32 -0
  14. package/templates/astro/js/base/src/components/Home.test.jsx +39 -0
  15. package/templates/astro/js/base/src/components/NotFound.jsx +17 -0
  16. package/templates/astro/js/base/src/data/api.js +84 -0
  17. package/templates/astro/js/base/src/layouts/Layout.astro +52 -0
  18. package/templates/astro/js/base/src/pages/404.astro +12 -0
  19. package/templates/astro/js/base/src/pages/about.astro +12 -0
  20. package/templates/astro/js/base/src/pages/index.astro +23 -0
  21. package/templates/astro/js/base/src/theme/Island.jsx +89 -0
  22. package/templates/astro/js/base/src/theme/scheme.js +32 -0
  23. package/templates/astro/js/base/vitest.config.js +12 -0
  24. package/templates/astro/js/sample-data/src/components/ItemDetails.jsx +64 -0
  25. package/templates/astro/js/sample-data/src/components/ItemList.jsx +100 -0
  26. package/templates/astro/js/sample-data/src/components/StatusChip.jsx +8 -0
  27. package/templates/astro/js/sample-data/src/data/items.js +65 -0
  28. package/templates/astro/js/sample-data/src/pages/items/[id].astro +37 -0
  29. package/templates/astro/js/sample-data/src/pages/items/index.astro +20 -0
  30. package/templates/astro/js/with/jotai/src/components/Counter.jsx +26 -0
  31. package/templates/astro/js/with/jotai/src/state/counter.js +4 -0
  32. package/templates/astro/js/with/msw/_env +3 -0
  33. package/templates/astro/js/with/msw/src/mocks/browser.js +5 -0
  34. package/templates/astro/js/with/msw/src/mocks/handlers.js +24 -0
  35. package/templates/astro/js/with/msw/src/mocks/server.js +5 -0
  36. package/templates/astro/js/with/msw/src/mocks/setupTests.js +7 -0
  37. package/templates/astro/js/with/msw/src/mocks/start.js +14 -0
  38. package/templates/astro/js/with/react-hook-form/src/components/Form.jsx +313 -0
  39. package/templates/astro/js/with/react-hook-form/src/pages/form.astro +8 -0
  40. package/templates/astro/js/with/redux-toolkit/src/components/Counter.jsx +27 -0
  41. package/templates/astro/js/with/redux-toolkit/src/state/store.js +18 -0
  42. package/templates/astro/js/with/tanstack-query/src/data/queryClient.js +13 -0
  43. package/templates/astro/js/with/zustand/src/components/Counter.jsx +24 -0
  44. package/templates/astro/js/with/zustand/src/state/counter.js +14 -0
  45. package/templates/astro/ts/base/README.md +93 -0
  46. package/templates/astro/ts/base/_gitignore +5 -0
  47. package/templates/astro/ts/base/astro.config.mjs +23 -0
  48. package/templates/astro/ts/base/src/components/About.tsx +24 -0
  49. package/templates/astro/ts/base/src/components/Counter.tsx +26 -0
  50. package/templates/astro/ts/base/src/components/Greeting.tsx +61 -0
  51. package/templates/astro/ts/base/src/components/Header.tsx +99 -0
  52. package/templates/astro/ts/base/src/components/Home.test.tsx +39 -0
  53. package/templates/astro/ts/base/src/components/Home.tsx +33 -0
  54. package/templates/astro/ts/base/src/components/NotFound.tsx +17 -0
  55. package/templates/astro/ts/base/src/data/api.ts +92 -0
  56. package/templates/astro/ts/base/src/layouts/Layout.astro +52 -0
  57. package/templates/astro/ts/base/src/pages/404.astro +12 -0
  58. package/templates/astro/ts/base/src/pages/about.astro +12 -0
  59. package/templates/astro/ts/base/src/pages/index.astro +23 -0
  60. package/templates/astro/ts/base/src/theme/Island.tsx +90 -0
  61. package/templates/astro/ts/base/src/theme/scheme.ts +34 -0
  62. package/templates/astro/ts/base/tsconfig.json +9 -0
  63. package/templates/astro/ts/base/vitest.config.ts +12 -0
  64. package/templates/astro/ts/sample-data/src/components/ItemDetails.tsx +65 -0
  65. package/templates/astro/ts/sample-data/src/components/ItemList.tsx +109 -0
  66. package/templates/astro/ts/sample-data/src/components/StatusChip.tsx +9 -0
  67. package/templates/astro/ts/sample-data/src/data/items.ts +71 -0
  68. package/templates/astro/ts/sample-data/src/pages/items/[id].astro +39 -0
  69. package/templates/astro/ts/sample-data/src/pages/items/index.astro +20 -0
  70. package/templates/astro/ts/with/jotai/src/components/Counter.tsx +26 -0
  71. package/templates/astro/ts/with/jotai/src/state/counter.ts +4 -0
  72. package/templates/astro/ts/with/msw/_env +3 -0
  73. package/templates/astro/ts/with/msw/src/mocks/browser.ts +5 -0
  74. package/templates/astro/ts/with/msw/src/mocks/handlers.ts +24 -0
  75. package/templates/astro/ts/with/msw/src/mocks/server.ts +5 -0
  76. package/templates/astro/ts/with/msw/src/mocks/setupTests.ts +7 -0
  77. package/templates/astro/ts/with/msw/src/mocks/start.ts +14 -0
  78. package/templates/astro/ts/with/react-hook-form/src/components/Form.tsx +317 -0
  79. package/templates/astro/ts/with/react-hook-form/src/pages/form.astro +8 -0
  80. package/templates/astro/ts/with/redux-toolkit/src/components/Counter.tsx +27 -0
  81. package/templates/astro/ts/with/redux-toolkit/src/state/store.ts +19 -0
  82. package/templates/astro/ts/with/tanstack-query/src/data/queryClient.ts +13 -0
  83. package/templates/astro/ts/with/zustand/src/components/Counter.tsx +24 -0
  84. package/templates/astro/ts/with/zustand/src/state/counter.ts +19 -0
  85. package/templates/react-router/js/base/README.md +67 -0
  86. package/templates/react-router/js/base/_gitignore +4 -0
  87. package/templates/react-router/js/base/index.html +12 -0
  88. package/templates/react-router/js/base/src/components/RouterLink.jsx +12 -0
  89. package/templates/react-router/js/base/src/data/api.js +76 -0
  90. package/templates/react-router/js/base/src/main.jsx +38 -0
  91. package/templates/react-router/js/base/src/pages/ErrorPage.jsx +14 -0
  92. package/templates/react-router/js/base/src/pages/Layout.jsx +141 -0
  93. package/templates/react-router/js/base/src/pages/about/Page.jsx +14 -0
  94. package/templates/react-router/js/base/src/pages/index/Counter.jsx +18 -0
  95. package/templates/react-router/js/base/src/pages/index/Greeting.jsx +48 -0
  96. package/templates/react-router/js/base/src/pages/index/Page.jsx +29 -0
  97. package/templates/react-router/js/base/src/pages/index/Page.test.jsx +61 -0
  98. package/templates/react-router/js/base/src/routes.jsx +46 -0
  99. package/templates/react-router/js/base/vite.config.js +13 -0
  100. package/templates/react-router/js/base/vitest.config.js +13 -0
  101. package/templates/react-router/js/sample-data/src/data/items.js +55 -0
  102. package/templates/react-router/js/sample-data/src/pages/item/Page.jsx +49 -0
  103. package/templates/react-router/js/sample-data/src/pages/item/loader.js +14 -0
  104. package/templates/react-router/js/sample-data/src/pages/items/Page.jsx +48 -0
  105. package/templates/react-router/js/sample-data/src/pages/items/StatusChip.jsx +8 -0
  106. package/templates/react-router/js/sample-data/src/pages/items/loader.js +11 -0
  107. package/templates/react-router/js/with/jotai/src/pages/index/Counter.jsx +19 -0
  108. package/templates/react-router/js/with/jotai/src/state/counter.js +4 -0
  109. package/templates/react-router/js/with/jotai/src/state/scheme.js +4 -0
  110. package/templates/react-router/js/with/msw/_env +3 -0
  111. package/templates/react-router/js/with/msw/src/mocks/browser.js +5 -0
  112. package/templates/react-router/js/with/msw/src/mocks/handlers.js +24 -0
  113. package/templates/react-router/js/with/msw/src/mocks/server.js +5 -0
  114. package/templates/react-router/js/with/msw/src/mocks/setupTests.js +7 -0
  115. package/templates/react-router/js/with/msw/src/mocks/start.js +10 -0
  116. package/templates/react-router/js/with/react-hook-form/src/pages/form/Page.jsx +294 -0
  117. package/templates/react-router/js/with/redux-toolkit/src/pages/index/Counter.jsx +20 -0
  118. package/templates/react-router/js/with/redux-toolkit/src/state/store.js +27 -0
  119. package/templates/react-router/js/with/zustand/src/pages/index/Counter.jsx +17 -0
  120. package/templates/react-router/js/with/zustand/src/state/counter.js +14 -0
  121. package/templates/react-router/js/with/zustand/src/state/scheme.js +14 -0
  122. package/templates/react-router/ts/base/README.md +67 -0
  123. package/templates/react-router/ts/base/_gitignore +4 -0
  124. package/templates/react-router/ts/base/index.html +12 -0
  125. package/templates/react-router/ts/base/src/components/RouterLink.tsx +12 -0
  126. package/templates/react-router/ts/base/src/data/api.ts +84 -0
  127. package/templates/react-router/ts/base/src/main.tsx +38 -0
  128. package/templates/react-router/ts/base/src/pages/ErrorPage.tsx +14 -0
  129. package/templates/react-router/ts/base/src/pages/Layout.tsx +141 -0
  130. package/templates/react-router/ts/base/src/pages/about/Page.tsx +14 -0
  131. package/templates/react-router/ts/base/src/pages/index/Counter.tsx +18 -0
  132. package/templates/react-router/ts/base/src/pages/index/Greeting.tsx +49 -0
  133. package/templates/react-router/ts/base/src/pages/index/Page.test.tsx +61 -0
  134. package/templates/react-router/ts/base/src/pages/index/Page.tsx +29 -0
  135. package/templates/react-router/ts/base/src/routes.tsx +47 -0
  136. package/templates/react-router/ts/base/tsconfig.json +18 -0
  137. package/templates/react-router/ts/base/vite.config.ts +13 -0
  138. package/templates/react-router/ts/base/vitest.config.ts +13 -0
  139. package/templates/react-router/ts/sample-data/src/data/items.ts +61 -0
  140. package/templates/react-router/ts/sample-data/src/pages/item/Page.tsx +50 -0
  141. package/templates/react-router/ts/sample-data/src/pages/item/loader.ts +14 -0
  142. package/templates/react-router/ts/sample-data/src/pages/items/Page.tsx +49 -0
  143. package/templates/react-router/ts/sample-data/src/pages/items/StatusChip.tsx +9 -0
  144. package/templates/react-router/ts/sample-data/src/pages/items/loader.ts +11 -0
  145. package/templates/react-router/ts/with/jotai/src/pages/index/Counter.tsx +19 -0
  146. package/templates/react-router/ts/with/jotai/src/state/counter.ts +4 -0
  147. package/templates/react-router/ts/with/jotai/src/state/scheme.ts +4 -0
  148. package/templates/react-router/ts/with/msw/_env +3 -0
  149. package/templates/react-router/ts/with/msw/src/mocks/browser.ts +5 -0
  150. package/templates/react-router/ts/with/msw/src/mocks/handlers.ts +24 -0
  151. package/templates/react-router/ts/with/msw/src/mocks/server.ts +5 -0
  152. package/templates/react-router/ts/with/msw/src/mocks/setupTests.ts +7 -0
  153. package/templates/react-router/ts/with/msw/src/mocks/start.ts +10 -0
  154. package/templates/react-router/ts/with/react-hook-form/src/pages/form/Page.tsx +298 -0
  155. package/templates/react-router/ts/with/redux-toolkit/src/pages/index/Counter.tsx +20 -0
  156. package/templates/react-router/ts/with/redux-toolkit/src/state/store.ts +29 -0
  157. package/templates/react-router/ts/with/zustand/src/pages/index/Counter.tsx +17 -0
  158. package/templates/react-router/ts/with/zustand/src/state/counter.ts +19 -0
  159. package/templates/react-router/ts/with/zustand/src/state/scheme.ts +19 -0
  160. package/templates/templates.md +67 -0
  161. package/templates/vike/js/base/README.md +83 -0
  162. package/templates/vike/js/base/_gitignore +4 -0
  163. package/templates/vike/js/base/src/data/api.js +76 -0
  164. package/templates/vike/js/base/src/pages/+Layout.jsx +183 -0
  165. package/templates/vike/js/base/src/pages/+config.js +12 -0
  166. package/templates/vike/js/base/src/pages/+onRenderClient.jsx +39 -0
  167. package/templates/vike/js/base/src/pages/+onRenderHtml.jsx +27 -0
  168. package/templates/vike/js/base/src/pages/PageShell.jsx +47 -0
  169. package/templates/vike/js/base/src/pages/_error/+Page.jsx +14 -0
  170. package/templates/vike/js/base/src/pages/about/+Page.jsx +14 -0
  171. package/templates/vike/js/base/src/pages/index/+Page.jsx +33 -0
  172. package/templates/vike/js/base/src/pages/index/Counter.jsx +20 -0
  173. package/templates/vike/js/base/src/pages/index/Greeting.jsx +48 -0
  174. package/templates/vike/js/base/src/pages/index/Page.test.jsx +67 -0
  175. package/templates/vike/js/base/src/pages/usePageContext.jsx +20 -0
  176. package/templates/vike/js/base/vite.config.js +14 -0
  177. package/templates/vike/js/base/vitest.config.js +13 -0
  178. package/templates/vike/js/sample-data/src/data/items.js +58 -0
  179. package/templates/vike/js/sample-data/src/pages/items/+Page.jsx +49 -0
  180. package/templates/vike/js/sample-data/src/pages/items/+data.js +26 -0
  181. package/templates/vike/js/sample-data/src/pages/items/@id/+Page.jsx +40 -0
  182. package/templates/vike/js/sample-data/src/pages/items/@id/+data.js +33 -0
  183. package/templates/vike/js/sample-data/src/pages/items/@id/+onBeforePrerenderStart.js +8 -0
  184. package/templates/vike/js/sample-data/src/pages/items/StatusChip.jsx +8 -0
  185. package/templates/vike/js/ssg/src/pages/+config.js +11 -0
  186. package/templates/vike/js/ssg/src/pages/about/+Page.jsx +13 -0
  187. package/templates/vike/js/ssr/src/pages/+config.js +12 -0
  188. package/templates/vike/js/ssr/src/pages/+onRenderHtml.jsx +27 -0
  189. package/templates/vike/js/ssr/src/pages/about/+Page.jsx +14 -0
  190. package/templates/vike/js/with/jotai/src/pages/index/Counter.jsx +19 -0
  191. package/templates/vike/js/with/jotai/src/state/counter.js +4 -0
  192. package/templates/vike/js/with/jotai/src/state/scheme.js +4 -0
  193. package/templates/vike/js/with/msw/_env +3 -0
  194. package/templates/vike/js/with/msw/src/mocks/browser.js +5 -0
  195. package/templates/vike/js/with/msw/src/mocks/handlers.js +24 -0
  196. package/templates/vike/js/with/msw/src/mocks/server.js +5 -0
  197. package/templates/vike/js/with/msw/src/mocks/setupTests.js +7 -0
  198. package/templates/vike/js/with/msw/src/mocks/start.js +10 -0
  199. package/templates/vike/js/with/react-hook-form/src/pages/form/+Page.jsx +294 -0
  200. package/templates/vike/js/with/redux-toolkit/src/pages/index/Counter.jsx +20 -0
  201. package/templates/vike/js/with/redux-toolkit/src/state/store.js +27 -0
  202. package/templates/vike/js/with/zustand/src/pages/index/Counter.jsx +17 -0
  203. package/templates/vike/js/with/zustand/src/state/counter.js +14 -0
  204. package/templates/vike/js/with/zustand/src/state/scheme.js +14 -0
  205. package/templates/vike/ts/base/README.md +83 -0
  206. package/templates/vike/ts/base/_gitignore +4 -0
  207. package/templates/vike/ts/base/src/data/api.ts +84 -0
  208. package/templates/vike/ts/base/src/pages/+Layout.tsx +184 -0
  209. package/templates/vike/ts/base/src/pages/+config.ts +14 -0
  210. package/templates/vike/ts/base/src/pages/+onRenderClient.tsx +41 -0
  211. package/templates/vike/ts/base/src/pages/+onRenderHtml.tsx +28 -0
  212. package/templates/vike/ts/base/src/pages/PageShell.tsx +48 -0
  213. package/templates/vike/ts/base/src/pages/_error/+Page.tsx +14 -0
  214. package/templates/vike/ts/base/src/pages/about/+Page.tsx +14 -0
  215. package/templates/vike/ts/base/src/pages/index/+Page.tsx +33 -0
  216. package/templates/vike/ts/base/src/pages/index/Counter.tsx +20 -0
  217. package/templates/vike/ts/base/src/pages/index/Greeting.tsx +49 -0
  218. package/templates/vike/ts/base/src/pages/index/Page.test.tsx +67 -0
  219. package/templates/vike/ts/base/src/pages/usePageContext.tsx +27 -0
  220. package/templates/vike/ts/base/src/vike.d.ts +20 -0
  221. package/templates/vike/ts/base/tsconfig.json +18 -0
  222. package/templates/vike/ts/base/vite.config.ts +14 -0
  223. package/templates/vike/ts/base/vitest.config.ts +13 -0
  224. package/templates/vike/ts/sample-data/src/data/items.ts +64 -0
  225. package/templates/vike/ts/sample-data/src/pages/items/+Page.tsx +50 -0
  226. package/templates/vike/ts/sample-data/src/pages/items/+data.ts +28 -0
  227. package/templates/vike/ts/sample-data/src/pages/items/@id/+Page.tsx +41 -0
  228. package/templates/vike/ts/sample-data/src/pages/items/@id/+data.ts +37 -0
  229. package/templates/vike/ts/sample-data/src/pages/items/@id/+onBeforePrerenderStart.ts +8 -0
  230. package/templates/vike/ts/sample-data/src/pages/items/StatusChip.tsx +9 -0
  231. package/templates/vike/ts/ssg/src/pages/+config.ts +13 -0
  232. package/templates/vike/ts/ssg/src/pages/about/+Page.tsx +13 -0
  233. package/templates/vike/ts/ssr/src/pages/+config.ts +14 -0
  234. package/templates/vike/ts/ssr/src/pages/+onRenderHtml.tsx +28 -0
  235. package/templates/vike/ts/ssr/src/pages/about/+Page.tsx +14 -0
  236. package/templates/vike/ts/with/jotai/src/pages/index/Counter.tsx +19 -0
  237. package/templates/vike/ts/with/jotai/src/state/counter.ts +4 -0
  238. package/templates/vike/ts/with/jotai/src/state/scheme.ts +4 -0
  239. package/templates/vike/ts/with/msw/_env +3 -0
  240. package/templates/vike/ts/with/msw/src/mocks/browser.ts +5 -0
  241. package/templates/vike/ts/with/msw/src/mocks/handlers.ts +24 -0
  242. package/templates/vike/ts/with/msw/src/mocks/server.ts +5 -0
  243. package/templates/vike/ts/with/msw/src/mocks/setupTests.ts +7 -0
  244. package/templates/vike/ts/with/msw/src/mocks/start.ts +10 -0
  245. package/templates/vike/ts/with/react-hook-form/src/pages/form/+Page.tsx +298 -0
  246. package/templates/vike/ts/with/redux-toolkit/src/pages/index/Counter.tsx +20 -0
  247. package/templates/vike/ts/with/redux-toolkit/src/state/store.ts +29 -0
  248. package/templates/vike/ts/with/zustand/src/pages/index/Counter.tsx +17 -0
  249. package/templates/vike/ts/with/zustand/src/state/counter.ts +19 -0
  250. package/templates/vike/ts/with/zustand/src/state/scheme.ts +19 -0
@@ -0,0 +1,23 @@
1
+ ---
2
+ // @if !ssg
3
+ import Counter from '../components/Counter'
4
+ // @endif
5
+ // @if tanstack-query || swr || msw
6
+ import Greeting from '../components/Greeting'
7
+ // @endif
8
+ import Home from '../components/Home'
9
+ import Layout from '../layouts/Layout.astro'
10
+ ---
11
+
12
+ <Layout>
13
+ {/* @if ssg */}
14
+ <Home />
15
+ {/* @else */}
16
+ <Home client:load>
17
+ {/* @if tanstack-query || swr || msw */}
18
+ <Greeting client:only="react" />
19
+ {/* @endif */}
20
+ <Counter client:only="react" />
21
+ </Home>
22
+ {/* @endif */}
23
+ </Layout>
@@ -0,0 +1,89 @@
1
+ // @if tanstack-query
2
+ import { useState } from 'react'
3
+ import { QueryClientProvider } from '@tanstack/react-query'
4
+ // @else
5
+ // @endif
6
+ // @if redux-toolkit
7
+ import { Provider as StoreProvider } from 'react-redux'
8
+ // @endif
9
+ import { ThemeProvider } from '@soroush.tech/design-system/theme'
10
+ // @if tanstack-query
11
+ import { getQueryClient } from '../data/queryClient'
12
+ // @endif
13
+ // @if redux-toolkit
14
+ import { store } from '../state/store'
15
+ // @endif
16
+ // @if !ssg
17
+ import { useScheme } from './scheme'
18
+ // @endif
19
+ import themes from './themes'
20
+
21
+ /**
22
+ // @if tanstack-query && redux-toolkit
23
+ * What every island renders inside: its theme, the query client and the Redux store. Astro
24
+ * renders each island as a React root of its own, so each one renders inside Island, and in the
25
+ * browser they share one client and one store; the header's Providers draw the base styles.
26
+ // @elif tanstack-query
27
+ * What every island renders inside: its theme and the query client. Astro renders each island as
28
+ * a React root of its own, so each one renders inside Island, and in the browser they share one
29
+ * client; the header's Providers draw the base styles.
30
+ // @elif redux-toolkit
31
+ * What every island renders inside: its theme and the Redux store. Astro renders each island as a
32
+ * React root of its own, so each one renders inside Island, and they share one store; the
33
+ * header's Providers draw the base styles.
34
+ // @else
35
+ * What every island renders inside: its theme. Astro renders each island as a React root of its
36
+ * own, so each one renders inside Island; the header's Providers draw the base styles, once a page.
37
+ // @endif
38
+ */
39
+ // @if ssg
40
+ function Island({ children }) {
41
+ return <ThemeProvider theme={themes.__SCHEME__}>{children}</ThemeProvider>
42
+ }
43
+ // @elif tanstack-query && redux-toolkit
44
+ function Island({ children }) {
45
+ const [queryClient] = useState(getQueryClient)
46
+ return (
47
+ <QueryClientProvider client={queryClient}>
48
+ <StoreProvider store={store}>
49
+ <ThemeProvider theme={themes[useScheme()]}>{children}</ThemeProvider>
50
+ </StoreProvider>
51
+ </QueryClientProvider>
52
+ )
53
+ }
54
+ // @elif tanstack-query
55
+ function Island({ children }) {
56
+ const [queryClient] = useState(getQueryClient)
57
+ return (
58
+ <QueryClientProvider client={queryClient}>
59
+ <ThemeProvider theme={themes[useScheme()]}>{children}</ThemeProvider>
60
+ </QueryClientProvider>
61
+ )
62
+ }
63
+ // @elif redux-toolkit
64
+ function Island({ children }) {
65
+ return (
66
+ <StoreProvider store={store}>
67
+ <ThemeProvider theme={themes[useScheme()]}>{children}</ThemeProvider>
68
+ </StoreProvider>
69
+ )
70
+ }
71
+ // @else
72
+ function Island({ children }) {
73
+ return <ThemeProvider theme={themes[useScheme()]}>{children}</ThemeProvider>
74
+ }
75
+ // @endif
76
+
77
+ /**
78
+ * `Component` as an island: rendered inside Island, so its hooks find what Island provides. Every
79
+ * island of the app is exported this way.
80
+ */
81
+ export function withIsland(Component) {
82
+ return function InIsland(props) {
83
+ return (
84
+ <Island>
85
+ <Component {...props} />
86
+ </Island>
87
+ )
88
+ }
89
+ }
@@ -0,0 +1,32 @@
1
+ // @if !ssg
2
+ import { useSyncExternalStore } from 'react'
3
+
4
+ let current = '__SCHEME__'
5
+ const listeners = new Set()
6
+
7
+ /** Switches the color scheme of every island, on this page and the pages the app moves to. */
8
+ export function setScheme(scheme) {
9
+ current = scheme
10
+ for (const listener of listeners) listener()
11
+ }
12
+
13
+ function subscribe(listener) {
14
+ listeners.add(listener)
15
+ return () => {
16
+ listeners.delete(listener)
17
+ }
18
+ }
19
+
20
+ /**
21
+ * The color scheme every island draws in. Each island is a React root of its own, so they share
22
+ * it here, not through context. An island first draws the default, as the server did, so it
23
+ * hydrates on the HTML it was given, then the scheme picked.
24
+ */
25
+ export function useScheme() {
26
+ return useSyncExternalStore(
27
+ subscribe,
28
+ () => current,
29
+ () => '__SCHEME__'
30
+ )
31
+ }
32
+ // @endif
@@ -0,0 +1,12 @@
1
+ /// <reference types="vitest/config" />
2
+ import { getViteConfig } from 'astro/config'
3
+
4
+ // The tests render components in jsdom, through the app's own Vite settings, as Astro builds them.
5
+ export default getViteConfig({
6
+ // @if msw
7
+ // The mock API answers the tests' requests, from Node, as it answers the app's in the browser.
8
+ test: { environment: 'jsdom', setupFiles: ['./src/mocks/setupTests.js'] },
9
+ // @else
10
+ test: { environment: 'jsdom' },
11
+ // @endif
12
+ })
@@ -0,0 +1,64 @@
1
+ import { Flex } from '@soroush.tech/design-system/Flex'
2
+ import { Link } from '@soroush.tech/design-system/Link'
3
+ import { Typography } from '@soroush.tech/design-system/Typography'
4
+ // @if tanstack-query
5
+ import { useQuery } from '@tanstack/react-query'
6
+ import { fetchItem } from '../data/api'
7
+ // @elif swr
8
+ import useSWR from 'swr'
9
+ import { fetchItem } from '../data/api'
10
+ // @endif
11
+ import { withIsland } from '../theme/Island'
12
+ import { StatusChip } from './StatusChip'
13
+
14
+ // @if ssr
15
+ /** One item, as src/pages/items/[id].astro reads it on each request. */
16
+ // @else
17
+ /** One item, as src/pages/items/[id].astro reads it at build time. */
18
+ // @endif
19
+ // @if tanstack-query
20
+ function ItemDetails({ item: initialItem }) {
21
+ const { data: item } = useQuery({
22
+ queryKey: ['items', initialItem.id],
23
+ queryFn: () => fetchItem(initialItem.id),
24
+ initialData: initialItem,
25
+ })
26
+ return (
27
+ <Flex gap={2} alignItems="flex-start" px={3} py={4}>
28
+ <Typography variant="h2">{item.title}</Typography>
29
+ <StatusChip status={item.status} />
30
+ <Typography color="secondary">{item.summary}</Typography>
31
+ <Link href="/items">All items</Link>
32
+ </Flex>
33
+ )
34
+ }
35
+ // @elif swr
36
+ function ItemDetails({ item: initialItem }) {
37
+ const { data: item = initialItem } = useSWR(
38
+ ['items', initialItem.id],
39
+ () => fetchItem(initialItem.id),
40
+ { fallbackData: initialItem }
41
+ )
42
+ return (
43
+ <Flex gap={2} alignItems="flex-start" px={3} py={4}>
44
+ <Typography variant="h2">{item.title}</Typography>
45
+ <StatusChip status={item.status} />
46
+ <Typography color="secondary">{item.summary}</Typography>
47
+ <Link href="/items">All items</Link>
48
+ </Flex>
49
+ )
50
+ }
51
+ // @else
52
+ function ItemDetails({ item }) {
53
+ return (
54
+ <Flex gap={2} alignItems="flex-start" px={3} py={4}>
55
+ <Typography variant="h2">{item.title}</Typography>
56
+ <StatusChip status={item.status} />
57
+ <Typography color="secondary">{item.summary}</Typography>
58
+ <Link href="/items">All items</Link>
59
+ </Flex>
60
+ )
61
+ }
62
+ // @endif
63
+
64
+ export default withIsland(ItemDetails)
@@ -0,0 +1,100 @@
1
+ import { Flex } from '@soroush.tech/design-system/Flex'
2
+ import { Link } from '@soroush.tech/design-system/Link'
3
+ import { Typography } from '@soroush.tech/design-system/Typography'
4
+ // @if tanstack-query
5
+ import { useQuery } from '@tanstack/react-query'
6
+ import { fetchItems } from '../data/api'
7
+ // @elif swr
8
+ import useSWR from 'swr'
9
+ import { fetchItems } from '../data/api'
10
+ // @endif
11
+ import { withIsland } from '../theme/Island'
12
+ import { StatusChip } from './StatusChip'
13
+
14
+ // @if ssr
15
+ /** The list of items, as src/pages/items/index.astro reads it on each request. */
16
+ // @else
17
+ /** The list of items, as src/pages/items/index.astro reads it at build time. */
18
+ // @endif
19
+ // @if tanstack-query
20
+ function ItemList({ items: initialItems }) {
21
+ // It starts on the items read with the page, then queries them again in the browser.
22
+ const { data: items } = useQuery({
23
+ queryKey: ['items'],
24
+ queryFn: fetchItems,
25
+ initialData: initialItems,
26
+ })
27
+ return (
28
+ <Flex gap={2} px={3} py={4}>
29
+ <Typography variant="h2">Items</Typography>
30
+ <Typography color="secondary">
31
+ {/* @if ssr */}
32
+ Sample data from src/data/items.js, which this page reads on each request.
33
+ {/* @else */}
34
+ Sample data from src/data/items.js, which this page reads at build time.
35
+ {/* @endif */}
36
+ </Typography>
37
+ <Flex gap={1}>
38
+ {items.map((item) => (
39
+ <Flex key={item.id} flexDirection="row" alignItems="center" gap={2}>
40
+ <Link href={`/items/${item.id}`}>{item.title}</Link>
41
+ <StatusChip status={item.status} />
42
+ </Flex>
43
+ ))}
44
+ </Flex>
45
+ </Flex>
46
+ )
47
+ }
48
+ // @elif swr
49
+ function ItemList({ items: initialItems }) {
50
+ // It starts on the items read with the page, then fetches them again in the browser.
51
+ const { data: items = initialItems } = useSWR('items', fetchItems, {
52
+ fallbackData: initialItems,
53
+ })
54
+ return (
55
+ <Flex gap={2} px={3} py={4}>
56
+ <Typography variant="h2">Items</Typography>
57
+ <Typography color="secondary">
58
+ {/* @if ssr */}
59
+ Sample data from src/data/items.js, which this page reads on each request.
60
+ {/* @else */}
61
+ Sample data from src/data/items.js, which this page reads at build time.
62
+ {/* @endif */}
63
+ </Typography>
64
+ <Flex gap={1}>
65
+ {items.map((item) => (
66
+ <Flex key={item.id} flexDirection="row" alignItems="center" gap={2}>
67
+ <Link href={`/items/${item.id}`}>{item.title}</Link>
68
+ <StatusChip status={item.status} />
69
+ </Flex>
70
+ ))}
71
+ </Flex>
72
+ </Flex>
73
+ )
74
+ }
75
+ // @else
76
+ function ItemList({ items }) {
77
+ return (
78
+ <Flex gap={2} px={3} py={4}>
79
+ <Typography variant="h2">Items</Typography>
80
+ <Typography color="secondary">
81
+ {/* @if ssr */}
82
+ Sample data from src/data/items.js, which this page reads on each request.
83
+ {/* @else */}
84
+ Sample data from src/data/items.js, which this page reads at build time.
85
+ {/* @endif */}
86
+ </Typography>
87
+ <Flex gap={1}>
88
+ {items.map((item) => (
89
+ <Flex key={item.id} flexDirection="row" alignItems="center" gap={2}>
90
+ <Link href={`/items/${item.id}`}>{item.title}</Link>
91
+ <StatusChip status={item.status} />
92
+ </Flex>
93
+ ))}
94
+ </Flex>
95
+ </Flex>
96
+ )
97
+ }
98
+ // @endif
99
+
100
+ export default withIsland(ItemList)
@@ -0,0 +1,8 @@
1
+ import { Chip } from '@soroush.tech/design-system/Chip'
2
+
3
+ const COLORS = { todo: 'default', doing: 'primary', done: 'success' }
4
+
5
+ /** An item's status, in its palette color. */
6
+ export function StatusChip({ status }) {
7
+ return <Chip label={status} size="sm" variant="outlined" color={COLORS[status]} />
8
+ }
@@ -0,0 +1,65 @@
1
+ // @if ts
2
+ /** One sample item. */
3
+
4
+ // @endif
5
+ // @if ssr
6
+ /**
7
+ * Sample data, so the items pages have something to show: the pages in src/pages/items read it
8
+ * on each request. Make them fetch your own data instead, and remove this file.
9
+ */
10
+ // @else
11
+ /**
12
+ * Sample data, so the items pages have something to show: the pages in src/pages/items read it
13
+ * at build time. Make them fetch your own data instead, and remove this file.
14
+ */
15
+ // @endif
16
+ export const items = [
17
+ {
18
+ id: 'theme',
19
+ title: 'Make the theme your own',
20
+ summary: 'Change the colors and fonts of both themes in src/theme/themes.js.',
21
+ status: 'doing',
22
+ },
23
+ {
24
+ id: 'fonts',
25
+ title: 'Load the fonts',
26
+ summary: 'The theme names Space Grotesk and JetBrains Mono, and does not load them.',
27
+ status: 'todo',
28
+ },
29
+ {
30
+ id: 'pages',
31
+ title: 'Add a page',
32
+ summary: 'An .astro file in src/pages is a new route: it renders your components.',
33
+ status: 'todo',
34
+ },
35
+ {
36
+ id: 'data',
37
+ title: 'Read your own data',
38
+ // @if ssr
39
+ summary: 'The items pages read this file on each request. Make them fetch from your API.',
40
+ // @else
41
+ summary: 'The items pages read this file at build time. Make them fetch from your API.',
42
+ // @endif
43
+ status: 'todo',
44
+ },
45
+ {
46
+ id: 'build',
47
+ title: 'Build it',
48
+ // @if ssr
49
+ summary: 'The server renders every page, these items among them, as HTML with its styles.',
50
+ // @else
51
+ summary: 'The build writes every page, these items among them, as HTML with its styles.',
52
+ // @endif
53
+ status: 'done',
54
+ },
55
+ {
56
+ id: 'deploy',
57
+ title: 'Deploy it',
58
+ // @if ssr
59
+ summary: 'npm start runs the server the build wrote, on any Node host.',
60
+ // @else
61
+ summary: 'The build is static files in dist, which any static host serves.',
62
+ // @endif
63
+ status: 'todo',
64
+ },
65
+ ]
@@ -0,0 +1,37 @@
1
+ ---
2
+ import ItemDetails from '../../components/ItemDetails'
3
+ import Layout from '../../layouts/Layout.astro'
4
+ // @if ssr
5
+ import { items } from '../../data/items'
6
+
7
+ // The item of the id in the URL, read on each request, or the 404 page for an id with no item.
8
+ const item = items.find(({ id }) => id === Astro.params.id)
9
+ if (!item) return Astro.rewrite('/404')
10
+ // @elif ts
11
+ import { items } from '../../data/items'
12
+
13
+ /** The pages this route writes at build time: one per item. */
14
+ export const getStaticPaths = (() =>
15
+ items.map((item) => ({ params: { id: item.id }, props: { item } })));
16
+
17
+
18
+ const { item } = Astro.props
19
+ // @else
20
+ import { items } from '../../data/items'
21
+
22
+ /** The pages this route writes at build time: one per item. */
23
+ export function getStaticPaths() {
24
+ return items.map((item) => ({ params: { id: item.id }, props: { item } }))
25
+ }
26
+
27
+ const { item } = Astro.props
28
+ // @endif
29
+ ---
30
+
31
+ <Layout>
32
+ {/* @if ssg */}
33
+ <ItemDetails item={item} />
34
+ {/* @else */}
35
+ <ItemDetails item={item} client:load />
36
+ {/* @endif */}
37
+ </Layout>
@@ -0,0 +1,20 @@
1
+ ---
2
+ import ItemList from '../../components/ItemList'
3
+ import Layout from '../../layouts/Layout.astro'
4
+ import { items } from '../../data/items'
5
+
6
+ // @if ssr
7
+ // What the list shows of each item, read on each request.
8
+ // @else
9
+ // What the list shows of each item, read at build time.
10
+ // @endif
11
+ const summaries = items.map(({ id, title, status }) => ({ id, title, status }))
12
+ ---
13
+
14
+ <Layout>
15
+ {/* @if ssg */}
16
+ <ItemList items={summaries} />
17
+ {/* @else */}
18
+ <ItemList items={summaries} client:load />
19
+ {/* @endif */}
20
+ </Layout>
@@ -0,0 +1,26 @@
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 { useAtom } from 'jotai'
6
+ import { countAtom } from '../state/counter'
7
+
8
+ /**
9
+ * A count, held in a Jotai atom, src/state/counter, 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, setCount] = useAtom(countAtom)
15
+ const increment = () => setCount(count + 1)
16
+ return (
17
+ <Flex flexDirection="row" alignItems="center" gap={2}>
18
+ <Button size="sm" variant="outlined" onClick={increment}>
19
+ Count
20
+ </Button>
21
+ <Typography>Count: {count}</Typography>
22
+ </Flex>
23
+ )
24
+ }
25
+
26
+ export default withIsland(Counter)
@@ -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,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. Astro hands the browser only the variables named PUBLIC_.
3
+ PUBLIC_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,14 @@
1
+ /**
2
+ * The mock API in the browser, started once, while PUBLIC_MSW, in .env, is true: src/data/api waits
3
+ * for it before each request. On the server, and in the tests, where msw's Node server answers,
4
+ * nothing starts. Set PUBLIC_MSW to false once the app's own API answers.
5
+ */
6
+ export const mocks = startMocks()
7
+
8
+ async function startMocks() {
9
+ const isBrowser = typeof window !== 'undefined' && import.meta.env.MODE !== 'test'
10
+ if (!isBrowser || import.meta.env.PUBLIC_MSW !== 'true') return
11
+ const { worker } = await import('./browser')
12
+ // A request the mock API has no handler for - a page the router fetches - goes to the network.
13
+ await worker.start({ onUnhandledFrame: 'bypass' })
14
+ }