Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add vike-react-zustand #57

Open
wants to merge 114 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
114 commits
Select commit Hold shift + click to select a range
6dd65e8
add vike-react-zustand
nitedani Dec 26, 2023
95e4199
refactor
nitedani Dec 26, 2023
a103404
minor
nitedani Dec 26, 2023
8f6db14
packagejson
nitedani Dec 26, 2023
7d7b398
readme
nitedani Dec 26, 2023
c68848b
packages
nitedani Dec 26, 2023
ca31852
newline
nitedani Dec 26, 2023
a4fbb02
refactor
nitedani Dec 26, 2023
37a87e8
refactor
nitedani Dec 27, 2023
91b1b50
pass server state to client
nitedani Dec 27, 2023
9b7eee7
newline
nitedani Dec 27, 2023
a7b071b
minor
nitedani Dec 27, 2023
d61f024
minor
nitedani Dec 27, 2023
e3b413b
pass everything to the client
nitedani Dec 27, 2023
247eb06
simplify zustand hydration
nitedani Dec 28, 2023
4076465
newline
nitedani Dec 28, 2023
8620dac
remove config
nitedani Dec 28, 2023
fcaad10
minor
brillout Dec 28, 2023
8de06c2
minor refactor
brillout Dec 28, 2023
46856c5
minor
brillout Dec 28, 2023
5d4c81e
minor refactor
brillout Dec 28, 2023
791f8bd
minor refactor
brillout Dec 28, 2023
d84e3ca
make example more interesting
brillout Dec 28, 2023
ecb036f
new design - wip
brillout Dec 28, 2023
4f493b0
add types
nitedani Dec 28, 2023
3828d27
fix withPageContext
nitedani Dec 29, 2023
bfaa7e9
remove types
nitedani Dec 29, 2023
e0cdea5
add proxy
nitedani Dec 29, 2023
dd4d3a4
comment
nitedani Dec 29, 2023
e0a4238
add devtools, update example
nitedani Dec 29, 2023
91b28a5
remove lodash dep, add jsdoc
nitedani Dec 29, 2023
08eba39
add assert
nitedani Dec 29, 2023
5b86477
update example
nitedani Dec 29, 2023
adb9b3a
update example
nitedani Dec 29, 2023
5e7218b
refactor
nitedani Dec 29, 2023
ca615a6
add TODOs
nitedani Dec 29, 2023
0a6a80d
minor
brillout Dec 30, 2023
a4c5a01
refactor types
nitedani Dec 30, 2023
ead978f
make state internal
brillout Jan 1, 2024
ad1866d
comment
brillout Jan 1, 2024
6ddf6f8
fix readme
brillout Jan 1, 2024
4050ed0
minor refactor
brillout Jan 1, 2024
79b862f
minor
brillout Jan 1, 2024
c0a9d0f
misc
brillout Jan 1, 2024
a163ee3
minor
brillout Jan 1, 2024
ea56910
refactor
brillout Jan 1, 2024
a9e5b42
update lock file
brillout Jan 2, 2024
a91a999
fix ts
brillout Jan 2, 2024
47ae91a
minor refactor
brillout Jan 2, 2024
ee22f11
minor refactor
brillout Jan 2, 2024
83874c0
minor refactor
brillout Jan 2, 2024
79d6aa6
fix removeFunctionsAndUndefined
nitedani Jan 2, 2024
c1d4b06
minor refactor
brillout Jan 2, 2024
deda358
remove proxy for now
nitedani Jan 2, 2024
d06d78b
fix proxy
nitedani Jan 2, 2024
f958767
useStoreApi
nitedani Jan 2, 2024
598905f
support multiple stores
nitedani Jan 2, 2024
4286464
minor
nitedani Jan 2, 2024
90d514d
add store.subscribe example
nitedani Jan 2, 2024
22eb527
typescript
nitedani Jan 2, 2024
5332370
document
nitedani Jan 2, 2024
c70430e
document
nitedani Jan 2, 2024
ddc5c2a
add StoreHookOnly and StoreApiOnly types
nitedani Jan 2, 2024
15bd3b7
fix types
nitedani Jan 2, 2024
60d995f
refactor
nitedani Jan 2, 2024
bcfafa9
multiple store trick
nitedani Jan 2, 2024
ee3e69c
multiple store trick
nitedani Jan 2, 2024
70b5649
minor
nitedani Jan 2, 2024
d743e3b
replace proxy with function
nitedani Jan 2, 2024
285f2d7
Revert "replace proxy with function"
nitedani Jan 2, 2024
691e758
vite transform for multiple stores
nitedani Jan 3, 2024
e3a253e
createWithPageContext
nitedani Jan 3, 2024
e9c96dc
withPageContext middleware
nitedani Jan 3, 2024
88cb4a6
remove unused code
nitedani Jan 3, 2024
affefc0
store id setter
nitedani Jan 3, 2024
ffd2e46
add docs, clean up example
nitedani Jan 3, 2024
2e3f7b2
docs for multiple stores
nitedani Jan 3, 2024
e59fb6b
minor
nitedani Jan 4, 2024
9dc7e1a
make trick clearer
brillout Jan 6, 2024
9a3c6fe
simple transformer
nitedani Jan 7, 2024
373a568
add regex playground
nitedani Jan 7, 2024
1398287
update regex playground
nitedani Jan 7, 2024
f85f6cb
regex playground
nitedani Jan 7, 2024
ddfa876
update regex
nitedani Jan 7, 2024
b319062
regex
nitedani Jan 7, 2024
d686953
remove transform (too ugly/leaks memory in dev)
nitedani Jan 7, 2024
b35d84e
transformer experiment
nitedani Jan 7, 2024
8d97920
regex playground
nitedani Jan 7, 2024
f4967b3
add comments and improve regex
nitedani Jan 7, 2024
1d95927
es-module-lexer, readme.md
nitedani Jan 7, 2024
6fad211
improve transformer plugin
nitedani Jan 7, 2024
9ea1fa8
simplify regex transform
nitedani Jan 8, 2024
560e833
export vite plugin
nitedani Jan 20, 2024
639f86d
set Vike extension name
brillout Feb 14, 2024
3880d24
fix rebase
nitedani Feb 16, 2024
55f7e21
update readme
nitedani Feb 16, 2024
05ef87e
fix: deep merge store on hydration
nitedani Feb 16, 2024
03bcd6d
fix: store hydration error
nitedani Feb 16, 2024
cc124e8
fix: store hydration error
nitedani Feb 16, 2024
141a8b8
improve removeFunctionsAndUndefined
nitedani Apr 4, 2024
897a288
sourcemap passthrough
nitedani Apr 4, 2024
ad24b71
chore: update release-me
brillout Apr 18, 2024
71ae3c3
fmt
brillout Apr 18, 2024
09eebf6
chore: update release-me
brillout Apr 22, 2024
d94a405
chore: update release-me usage
brillout Apr 22, 2024
38fefd3
chore: update release-me
brillout Apr 23, 2024
d62c4d1
use rimraf
brillout May 6, 2024
d924500
cumulative wrapper
nitedani Jun 27, 2024
a933c0d
fix rebase
nitedani Jun 28, 2024
ced6465
revert change
nitedani Jun 28, 2024
0d645de
update
nitedani Jul 7, 2024
5a47f10
format
nitedani Jul 7, 2024
b5c1615
fix hydration
nitedani Jul 7, 2024
0f94f41
refactor
nitedani Jul 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/zustand/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/dist/
36 changes: 36 additions & 0 deletions examples/zustand/assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions examples/zustand/layouts/HeadDefault.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default HeadDefault

import React from 'react'

function HeadDefault() {
return (
<>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</>
)
}
72 changes: 72 additions & 0 deletions examples/zustand/layouts/LayoutDefault.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
export default LayoutDefault

import './style.css'
import React from 'react'
import logoUrl from '../assets/logo.svg'

function LayoutDefault({ children }: { children: React.ReactNode }) {
return (
<div
style={{
display: 'flex',
maxWidth: 900,
margin: 'auto'
}}
>
<Sidebar>
<Logo />
</Sidebar>
<Content>{children}</Content>
</div>
)
}

function Sidebar({ children }: { children: React.ReactNode }) {
return (
<div
id="sidebar"
style={{
padding: 20,
flexShrink: 0,
display: 'flex',
flexDirection: 'column',
lineHeight: '1.8em',
borderRight: '2px solid #eee'
}}
>
{children}
</div>
)
}

function Content({ children }: { children: React.ReactNode }) {
return (
<div id="page-container">
<div
id="page-content"
style={{
padding: 20,
paddingBottom: 50,
minHeight: '100vh'
}}
>
{children}
</div>
</div>
)
}

function Logo() {
return (
<div
style={{
marginTop: 20,
marginBottom: 10
}}
>
<a href="/">
<img src={logoUrl} height={64} width={64} />
</a>
</div>
)
}
29 changes: 29 additions & 0 deletions examples/zustand/layouts/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Links */
a {
text-decoration: none;
}
#sidebar a {
padding: 2px 10px;
margin-left: -10px;
}
#sidebar a.is-active {
background-color: #eee;
}

/* Reset */
body {
margin: 0;
font-family: sans-serif;
}
* {
box-sizing: border-box;
}

/* Page Transition Anmiation */
#page-content {
opacity: 1;
transition: opacity 0.3s ease-in-out;
}
body.page-is-transitioning #page-content {
opacity: 0;
}
23 changes: 23 additions & 0 deletions examples/zustand/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"test": "tsc --noEmit"
},
"dependencies": {
"@types/react": "^18.2.45",
"@types/react-dom": "^18.2.18",
"@vitejs/plugin-react": "^4.2.1",
"immer": "^10.0.3",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "^5.3.3",
"vike": "^0.4.168",
"vike-react": "^0.4.15",
"vike-react-zustand": "^0.0.1",
"vite": "^5.0.10",
"zustand": "^4.4.7"
},
"type": "module"
}
17 changes: 17 additions & 0 deletions examples/zustand/pages/+config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export { config }

import type { Config } from 'vike/types'
import vikeReact from 'vike-react/config'
import vikeReactZustand from 'vike-react-zustand/config'

// Default configs (can be overridden by pages)
const config = {
// <title>
title: 'My Vike + React App',
// https://vike.dev/stream
stream: true,
// https://vike.dev/ssr - this line can be removed since `true` is the default
ssr: true,
// https://vike.dev/extends
extends: [vikeReact, vikeReactZustand]
} satisfies Config
22 changes: 22 additions & 0 deletions examples/zustand/pages/_error/+Page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default Page

import React from 'react'

function Page({ is404, errorInfo }: { is404: boolean; errorInfo?: string }) {
if (is404) {
return (
<>
<h1>404 Page Not Found</h1>
<p>This page could not be found.</p>
<p>{errorInfo}</p>
</>
)
} else {
return (
<>
<h1>500 Internal Server Error</h1>
<p>Something went wrong.</p>
</>
)
}
}
32 changes: 32 additions & 0 deletions examples/zustand/pages/index/+Page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export default Page

import React, { useEffect } from 'react'
import { Counter } from './Counter'
import { useStore } from '../../store'
import { useStoreApi } from 'vike-react-zustand'

function Page() {
const nodeVersion = useStore((s) => s.nodeVersion)
const storeApi = useStoreApi(useStore)
useEffect(
() =>
storeApi.subscribe((state) => {
console.log(state)
}),
[]
)

return (
<>
<h1>My Vike + React app</h1>
This page is:
<ul>
<li>Rendered to HTML.</li>
<li>
Interactive while loading. <Counter />
</li>
</ul>
<div>Node version from server: {nodeVersion}</div>
</>
)
}
10 changes: 10 additions & 0 deletions examples/zustand/pages/index/Counter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export { Counter }

import React from 'react'
import { useStore } from '../../store'

function Counter() {
const { counter, setCounter } = useStore()

return <button onClick={() => setCounter(counter + 1)}>Counter {counter}</button>
}
8 changes: 8 additions & 0 deletions examples/zustand/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Example of using `vike-react-zustand`.

```bash
git clone git@github.com:vikejs/vike-react
cd vike-react/examples/zustand/
npm install
npm run dev
```
30 changes: 30 additions & 0 deletions examples/zustand/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export { useStore }

import { serverOnly, create, withPageContext } from 'vike-react-zustand'
import { immer } from 'zustand/middleware/immer'

interface Store {
counter: number
setCounter: (value: number) => void
nodeVersion: string
}

const useStore = create<Store>()(
// the devtools middleware is included by default
withPageContext((pageContext) =>
immer((set, get) => ({
counter: Math.floor(10000 * Math.random()),
setCounter(value) {
set((state) => {
state.counter = value
})
},

// the function passed to serverOnly only runs on the server
// the return value is available on client/server
...serverOnly(() => ({
nodeVersion: process.version
}))
}))
)
)
13 changes: 13 additions & 0 deletions examples/zustand/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"strict": true,
"module": "ES2020",
"moduleResolution": "Node",
"target": "ES2020",
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"types": ["vite/client"],
"jsx": "react",
"skipLibCheck": true,
"esModuleInterop": true
}
}
8 changes: 8 additions & 0 deletions examples/zustand/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import react from '@vitejs/plugin-react'
import vike from 'vike/plugin'
import type { UserConfig } from 'vite'
import { vikeReactZustand } from 'vike-react-zustand/plugin'

export default {
plugins: [react(), vike(), vikeReactZustand()]
} satisfies UserConfig
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"pnpm": {
"overrides": {
"vike-react": "link:./packages/vike-react/",
"vike-react-query": "link:./packages/vike-react-query/"
"vike-react-query": "link:./packages/vike-react-query/",
"vike-react-zustand": "link:./packages/vike-react-zustand/"
}
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions packages/vike-react-zustand/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/dist/
Empty file.
Loading