Skip to content

Commit

Permalink
new extension vike-react-chakra (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
phonzammi authored Nov 17, 2024
1 parent d74324b commit f3b5400
Show file tree
Hide file tree
Showing 15 changed files with 2,114 additions and 89 deletions.
2 changes: 1 addition & 1 deletion examples/apollo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"react": "^18.3.1",
"react-dom": "^18.3.1",
"typescript": "^5.3.3",
"vike": "^0.4.197",
"vike": "^0.4.203",
"vike-react": "^0.5.7",
"vike-react-apollo": "^0.1.1",
"@apollo/client": "^3.10.8",
Expand Down
2 changes: 1 addition & 1 deletion examples/full/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"react-dom": "^18.3.1",
"react-streaming": "^0.3.43",
"typescript": "^5.5.4",
"vike": "^0.4.197",
"vike": "^0.4.203",
"vike-react": "^0.5.7",
"vite": "^5.4.0"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@vitejs/plugin-react": "^4.2.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"vike": "^0.4.197",
"vike": "^0.4.203",
"vike-react": "^0.5.7",
"vite": "^5.4.0"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/react-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"react": "^18.3.1",
"react-dom": "^18.3.1",
"typescript": "^5.5.3",
"vike": "^0.4.197",
"vike": "^0.4.203",
"vike-react": "^0.5.7",
"vike-react-query": "^0.1.1",
"@tanstack/react-query": "^5.20.1",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"overrides": {
"vike-react": "link:./packages/vike-react/",
"vike-react-query": "link:./packages/vike-react-query/",
"vike-react-apollo": "link:./packages/vike-react-apollo/"
"vike-react-apollo": "link:./packages/vike-react-apollo/",
"vike-react-chakra": "link:./packages/vike-react-chakra/"
}
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/vike-react-apollo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"react-streaming": "^0.3.43",
"rimraf": "^5.0.5",
"typescript": "^5.3.3",
"vike": "^0.4.197",
"vike": "^0.4.203",
"vike-react": "^0.5.7",
"vite": "^5.4.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/vike-react-chakra/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/dist/
78 changes: 78 additions & 0 deletions packages/vike-react-chakra/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# `vike-react-chakra`

[Installation](#installation)
[Settings](#settings)

<br/>

## Installation

1. `npm install vike-react-chakra @chakra-ui/react @emotion/react`
2. Extend `+config.js`:
```js
// pages/+config.js

import vikeReact from 'vike-react/config'
import vikeReactChakra from 'vike-react-chakra/config'

export default {
// ...
extends: [vikeReact, vikeReactChakra]
}
```
3. You can now use Chakra at any of your components.
```jsx
import { HStack, Button } from '@chakra-ui/react'

function SomeComponent() {
return (
<HStack>
<Button>Click me</Button>
<Button>Click me</Button>
</HStack>
)
}
```

> [!NOTE]
> The `vike-react-chakra` extension requires [`vike-react`](https://vike.dev/vike-react).
> [!NOTE]
> The extension does only one thing: it adds [a Wrapper](https://github.com/vikejs/vike-react/blob/main/packages/vike-react-chakra/Wrapper.tsx) to your pages.
<br/>


## Settings

`vike-react-chakra` provides a configuration `+chakra` for setting the theme system and locale.
```js
// pages/+chakra.js

export { chakra }

import { createSystem, defaultConfig, defineConfig } from '@chakra-ui/react'

const customConfig = defineConfig({
globalCss: {
"html, body": {
margin: 0,
padding: 0
}
}
})

const system = createSystem(defaultConfig, customConfig)

const chakra = {
system,
locale: "fr-FR"
}
```

You can remove Chakra from [some of your pages](https://vike.dev/config#inheritance):
```js
// pages/about/+chakra.js

export const chakra = null
```
27 changes: 27 additions & 0 deletions packages/vike-react-chakra/Wrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export { Wrapper }

import React, { type ReactNode } from 'react'
import { ChakraProvider, LocaleProvider, defaultSystem } from '@chakra-ui/react'
import { usePageContext } from 'vike-react/usePageContext'

function Wrapper({ children }: { children: ReactNode }) {
const pageContext = usePageContext()
const { chakra } = pageContext.config

if (chakra === null) {
return <>{children}</>
}

return (
<ChakraProvider value={chakra?.system ?? defaultSystem}>
<ChakraLocaleProvider locale={chakra?.locale}>{children}</ChakraLocaleProvider>
</ChakraProvider>
)
}

function ChakraLocaleProvider({ locale, children }: { locale?: string; children: ReactNode }) {
if (locale) {
return <LocaleProvider locale={locale}>{children}</LocaleProvider>
}
return <>{children}</>
}
32 changes: 32 additions & 0 deletions packages/vike-react-chakra/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export { config as default }

import type { LocaleProviderProps, SystemContext } from '@chakra-ui/react'
import type { Config } from 'vike/types'

const config = {
name: 'vike-react-chakra',
require: {
vike: '>=0.4.203',
'vike-react': '>=0.4.13',
},
Wrapper: 'import:vike-react-chakra/__internal/Wrapper:Wrapper',
meta: {
chakra: {
env: {
server: true,
client: true,
},
},
},
} satisfies Config

declare global {
namespace Vike {
interface Config {
chakra?: null | {
system?: SystemContext
locale?: LocaleProviderProps['locale']
}
}
}
}
46 changes: 46 additions & 0 deletions packages/vike-react-chakra/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "vike-react-chakra",
"version": "0.0.0",
"type": "module",
"exports": {
"./config": "./dist/config.js",
"./__internal/Wrapper": "./dist/Wrapper.js"
},
"scripts": {
"dev": "tsc --watch",
"build": "rimraf dist/ && tsc",
"release": "release-me patch",
"release:minor": "release-me minor",
"release:major": "release-me major",
"release:commit": "release-me commit"
},
"peerDependencies": {
"@chakra-ui/react": ">=3",
"@emotion/react": ">=11",
"react": ">=18",
"vike-react": ">=0.4.13"
},
"devDependencies": {
"@brillout/release-me": "^0.3.8",
"@chakra-ui/react": "^3.0.2",
"@emotion/react": "^11.13.3",
"@types/react": "^18.2.55",
"react": "^18.3.1",
"rimraf": "^5.0.5",
"typescript": "^5.5.3",
"vike": "^0.4.203",
"vike-react": "^0.5.7",
"vite": "^5.4.0"
},
"typesVersions": {
"*": {
"config": ["dist/config.d.ts"],
"__internal/Wrapper": ["dist/Wrapper.d.ts"]
}
},
"files": [
"dist"
],
"repository": "https://github.com/vikejs/vike-react/tree/main/packages/vike-react-chakra",
"license": "MIT"
}
16 changes: 16 additions & 0 deletions packages/vike-react-chakra/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"declaration": true,
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Bundler",
"jsx": "react",
"outDir": "./dist/",
"skipLibCheck": true,
"types": ["vike-react"],
// Strictness
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitAny": true
}
}
2 changes: 1 addition & 1 deletion packages/vike-react-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"react-streaming": "^0.3.43",
"rimraf": "^5.0.5",
"typescript": "^5.5.3",
"vike": "^0.4.197",
"vike": "^0.4.203",
"vike-react": "^0.5.7",
"vite": "^5.4.0",
"vitest": "^1.2.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/vike-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"react-streaming": "^0.3.43",
"rimraf": "^5.0.5",
"typescript": "^5.5.4",
"vike": "^0.4.197",
"vike": "^0.4.203",
"vite": "^5.4.0"
},
"typesVersions": {
Expand Down
Loading

0 comments on commit f3b5400

Please sign in to comment.