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

feat: new extension vike-react-styled-jsx #158

Merged
merged 26 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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 @@ -30,7 +30,8 @@
"vike-react-apollo": "link:./packages/vike-react-apollo/",
"vike-react-chakra": "link:./packages/vike-react-chakra/",
"vike-react-antd": "link:./packages/vike-react-antd/",
"vike-react-styled-components": "link:./packages/vike-react-styled-components/"
"vike-react-styled-components": "link:./packages/vike-react-styled-components/",
"vike-react-styled-jsx": "link:./packages/vike-react-styled-jsx/"
}
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions packages/vike-react-styled-jsx/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/dist/
101 changes: 101 additions & 0 deletions packages/vike-react-styled-jsx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# `vike-react-styled-jsx`

[Installation](#installation)
[Settings](#settings)
[Version history](https://github.com/vikejs/vike-react/blob/main/packages/vike-react-styled-jsx/CHANGELOG.md)
[See also](#see-also)

<br/>

Integrates [styled-jsx](https://github.com/vercel/styled-jsx) to your [`vike-react`](https://vike.dev/vike-react) app.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be before the TOC. Intervention++


## Installation

1. `npm install vike-react-styled-jsx styled-jsx`
2. Extend `+config.js`:
```js
// pages/+config.js

import vikeReact from "vike-react/config"
import vikeReactStyledJsx from "vike-react-styled-jsx/config"

export default {
// ...
extends: [vikeReact, vikeReactStyledJsx]
}
```

3. Add `styled-jsx/babel` to plugins in your babel configuration:
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
```js
// vite.config.js
import { defineConfig } from "vite"
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
import react from "@vitejs/plugin-react"
import vike from "vike/plugin"

export default defineConfig({
plugins: [
vike(),
react({
babel: {
plugins: [["styled-jsx/babel"]],
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
},
}),
],
});
```

4. You can now use styled-jsx at any of your components.
brillout marked this conversation as resolved.
Show resolved Hide resolved
```jsx
function SomeComponent() {
return (
<div>
<p>only this paragraph will get the style :)</p>
phonzammi marked this conversation as resolved.
Show resolved Hide resolved

<style jsx>{`
p {
color: red;
}
`}</style>
</div>
)
}
```

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

<br/>

## Settings

`vike-react-styled-jsx` provides a configuration `+styledJsx` that adds a [CSP nonce](https://github.com/vercel/styled-jsx?tab=readme-ov-file#content-security-policy) meta tag and injects the nonce into the style tag.
phonzammi marked this conversation as resolved.
Show resolved Hide resolved

```ts
// pages/+styledJsx.js
export { styledJsx }

import nanoid from 'nanoid'

const styledJsx = {
nonce: Buffer.from(nanoid()).toString('base64') //ex: N2M0MDhkN2EtMmRkYi00MTExLWFhM2YtNDhkNTc4NGJhMjA3
}
```

You can remove the styled-jsx SSR integration from [some of your pages](https://vike.dev/config#inheritance):
phonzammi marked this conversation as resolved.
Show resolved Hide resolved

```js
// pages/about/+styledJsx.js

export const styledJsx = null
```

brillout marked this conversation as resolved.
Show resolved Hide resolved
For full customization consider [ejecting](https://vike.dev/eject).
phonzammi marked this conversation as resolved.
Show resolved Hide resolved

> [!NOTE]
> Consider making a [Pull Request before ejecting](https://vike.dev/eject#when-to-eject).

brillout marked this conversation as resolved.
Show resolved Hide resolved
<br/>

## See also

- [Vike Docs > styled-jsx](https://vike.dev/styled-jsx)
15 changes: 15 additions & 0 deletions packages/vike-react-styled-jsx/Wrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export { Wrapper }

import React, { type ReactNode } from 'react'
import { StyleRegistry, createStyleRegistry } from 'styled-jsx'
import { usePageContext } from 'vike-react/usePageContext'

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

if (pageContext.config.styledJsx === null) return <>{children}</>

pageContext.styledJsxRegistry = createStyleRegistry()

return <StyleRegistry registry={pageContext.styledJsxRegistry}>{children}</StyleRegistry>
}
35 changes: 35 additions & 0 deletions packages/vike-react-styled-jsx/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export { config as default }

import type { Config } from 'vike/types'
import type { StyledJsxStyleRegistry } from 'styled-jsx'

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

declare global {
namespace Vike {
interface PageContext {
styledJsxRegistry?: StyledJsxStyleRegistry
}
interface Config {
styledJsx?: null | {
nonce?: string
}
}
}
}
26 changes: 26 additions & 0 deletions packages/vike-react-styled-jsx/onAfterRenderHtml.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export { onAfterRenderHtml }

import React from 'react'
import { useConfig } from 'vike-react/useConfig'
import type { PageContext } from 'vike/types'

function onAfterRenderHtml(pageContext: PageContext) {
const config = useConfig()
const registry = pageContext.styledJsxRegistry

if (registry) {
const nonce = pageContext.config.styledJsx?.nonce
const styles = registry.styles({ nonce })

config({
Head: (
<>
{nonce ? <meta property="csp-nonce" content={nonce} /> : ''}
phonzammi marked this conversation as resolved.
Show resolved Hide resolved
{styles}
</>
),
})

registry.flush()
}
}
52 changes: 52 additions & 0 deletions packages/vike-react-styled-jsx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "vike-react-styled-jsx",
"version": "0.0.0",
"type": "module",
"exports": {
"./config": "./dist/config.js",
"./__internal/onAfterRenderHtml": "./dist/onAfterRenderHtml.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": {
"styled-jsx": ">=5",
"react": ">=18",
"vike-react": ">=0.4.13"
},
"devDependencies": {
"@brillout/release-me": "^0.4.2",
"@types/react": "^18.2.55",
"react": "^18.3.1",
"rimraf": "^5.0.5",
"styled-jsx": "^5.1.6",
"typescript": "^5.5.3",
"vike": "^0.4.203",
"vike-react": "^0.5.10",
"vite": "^5.4.0"
},
"typesVersions": {
"*": {
"config": [
"dist/config.d.ts"
],
"__internal/onAfterRenderHtml": [
"dist/onAfterRenderHtml.d.ts"
],
"__internal/Wrapper": [
"dist/Wrapper.d.ts"
]
}
},
"files": [
"dist"
],
"repository": "https://github.com/vikejs/vike-react/tree/main/packages/vike-react-styled-jsx",
"license": "MIT"
}
16 changes: 16 additions & 0 deletions packages/vike-react-styled-jsx/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
}
}
54 changes: 54 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading