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

new extension vike-react-chakra #151

Merged
merged 26 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
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: 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/
97 changes: 97 additions & 0 deletions packages/vike-react-chakra/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<!-- WARNING: keep links absolute in this file so they work on NPM too -->
phonzammi marked this conversation as resolved.
Show resolved Hide resolved

[<img src="https://vike.dev/vike-readme.svg" align="right" height="90">](https://vike.dev)
[![npm version](https://img.shields.io/npm/v/vike-react-chakra)](https://www.npmjs.com/package/vike-react-chakra)

# `vike-react-chakra`

[Installation](#installation)
[Basic usage](#basic-usage)
[`+chakra` Setting](#chakra-setting)
[See also](#see-also)

<br/>

## Installation

1. npm install @chakra-ui/react @emotion/react vike-react-chakra
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
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]
}
```

> [!NOTE]
> The `vike-react-chakra` extension requires [`vike-react`](https://vike.dev/vike-react).

<br/>

## Basic usage
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
```jsx
import { HStack, Button } from '@chakra-ui/react'

const Demo = () => {
return (
<HStack>
<Button>Click me</Button>
<Button>Click me</Button>
</HStack>
)
}
```

> [!NOTE]
> Chakra UI provides a cli tool to add snippets.
> Snippets are pre-built components that you can use to build your UI faster.
> Using the `@chakra-ui/cli` you can add snippets to your project.

```
npx @chakra-ui/cli snippet add
```

<br/>

## `+chakra` Setting
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
`vike-react-chakra` provides a custom setting, `+chakra`, for configuring custom theme system, custom locale to adds `LocaleProvider` and can be used to disable Chakra UI for specific pages.
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
```js
// pages/+chakra.js
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
import React, { ReactNode } from 'react'
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
import { createSystem, defaultConfig, defineConfig } from '@chakra-ui/react'

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

const system = createSystem(defaultConfig, customConfig)

export const chakra = {
system,
locale: "ar-BH"
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
}
```

To disable Chakra UI for specific page:
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
```js
// pages/about/+chakra.js
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
export const chakra = null
```

<br/>

## See also
phonzammi marked this conversation as resolved.
Show resolved Hide resolved

- [Chakra UI Components](https://www.chakra-ui.com/docs/components/concepts/overview)
- [Chakra UI Styling](https://www.chakra-ui.com/docs/styling/overview)
- [Chakra UI Theming](https://www.chakra-ui.com/docs/theming/overview)
31 changes: 31 additions & 0 deletions packages/vike-react-chakra/integration/+config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export { config as default }

import { LocaleProviderProps, SystemContext } from '@chakra-ui/react'
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
import type { Config } from 'vike/types'

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

declare global {
namespace Vike {
interface Config {
chakra?: null | {
system?: SystemContext
locale?: LocaleProviderProps['locale']
}
}
}
}
27 changes: 27 additions & 0 deletions packages/vike-react-chakra/integration/Wrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export { Wrapper }

import React, { ReactNode } from 'react'
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
import { ChakraProvider, LocaleProvider, defaultSystem } from '@chakra-ui/react'
brillout marked this conversation as resolved.
Show resolved Hide resolved
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 }) {
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
if (locale) {
return <LocaleProvider locale={locale}>{children}</LocaleProvider>
}
return <>{children}</>
}
51 changes: 51 additions & 0 deletions packages/vike-react-chakra/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "vike-react-chakra",
"version": "0.0.1",
brillout marked this conversation as resolved.
Show resolved Hide resolved
"type": "module",
"exports": {
"./config": "./dist/integration/+config.js",
"./__internal/integration/Wrapper": "./dist/integration/Wrapper.js"
brillout marked this conversation as resolved.
Show resolved Hide resolved
},
"scripts": {
"dev": "tsc --watch",
"build": "rimraf dist/ && tsc",
"release": "release-me patch",
"release:minor": "release-me minor",
"release:commit": "release-me commit"
},
"peerDependencies": {
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
"@chakra-ui/react": ">=3",
"@emotion/react": ">=11",
"react": ">=18.0.0",
"vike-react": ">=0.4.13"
},
"devDependencies": {
"@brillout/release-me": "0.3.8",
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
"@chakra-ui/react": "^3.0.2",
"@emotion/react": "^11.13.3",
"@types/node": "^20.11.17",
"@types/react": "^18.2.55",
"jsdom": "^24.0.0",
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
"react": "^18.3.1",
"rimraf": "^5.0.5",
"typescript": "^5.5.3",
"vike": "^0.4.197",
"vike-react": "^0.5.7",
"vite": "^5.4.0"
},
"typesVersions": {
"*": {
"config": [
"dist/integration/+config.d.ts"
],
"__internal/integration/Wrapper": [
"dist/integration/Wrapper.d.ts"
]
}
},
"files": [
"dist"
],
"repository": "github:vikejs/vike-react",
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
"license": "MIT"
}
24 changes: 24 additions & 0 deletions packages/vike-react-chakra/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
brillout marked this conversation as resolved.
Show resolved Hide resolved
"compilerOptions": {
"outDir": "./dist/",
// Resolution
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Bundler",
// Libs
"lib": ["ES2021", "DOM", "DOM.Iterable"],
"types": ["vite/client", "vike-react"],
// Strictness
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitAny": true,
// Output
"declaration": true,
"noEmitOnError": false,
"rootDir": "./",
// Misc
"esModuleInterop": true,
"skipLibCheck": true,
"jsx": "react"
}
}
Loading