-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
5dd4832
feat: new extension `vike-react-styled-jsx`
phonzammi 6f2a2d8
improve code and support adding `csp nonce`
phonzammi b4deda1
polish
phonzammi 841e6cb
test if the `Wrapper` still loads on the client side
phonzammi caa08d1
temporary workaround
phonzammi d5390de
temporary workaround 2
phonzammi 008773b
format
phonzammi 2041820
temporary workaround 3
phonzammi c2ff3a4
test optimizeDeps
phonzammi c06d341
Revert "test if the `Wrapper` still loads on the client side"
phonzammi 77aa376
polish
phonzammi 57b06b5
add new section
phonzammi 146f90c
polish
phonzammi becd4f0
polish
phonzammi e619490
polish
phonzammi a3bab0c
polish README
phonzammi a3cde20
polish
phonzammi d6e15b5
polish
phonzammi 65e9166
Update packages/vike-react-styled-jsx/README.md
brillout 788aa43
Update packages/vike-react-styled-jsx/README.md
brillout 6809734
Update packages/vike-react-styled-jsx/README.md
brillout 239b596
Update packages/vike-react-styled-jsx/README.md
brillout 1ffa1e0
Update packages/vike-react-styled-jsx/README.md
brillout 279ede8
Update packages/vike-react-styled-jsx/README.md
brillout 4c4760e
polish
brillout 5ec476d
polish
brillout File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules/ | ||
/dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
## 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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++