Unverified Commit 07d3da10 authored by Max Proske's avatar Max Proske Committed by GitHub

Convert `with-cssed`, `with-csx`, `with-styled-jsx` examples to TypeScript (#43018)

Updated 3 more examples to TypeScript. Changes to individual examples
pushed as separate commits.

- Swapped `cxs/lite` for `cxs`, as it's the only mode supported by
`@types/cxs`.

## Documentation / Examples

- [X] Make sure the linting passes by running `pnpm build && pnpm lint`
- [X] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 0a2ae766
...@@ -34,3 +34,6 @@ yarn-error.log* ...@@ -34,3 +34,6 @@ yarn-error.log*
# typescript # typescript
*.tsbuildinfo *.tsbuildinfo
next-env.d.ts next-env.d.ts
# cssed compilation artifacts
.*.module.css
...@@ -6,11 +6,16 @@ ...@@ -6,11 +6,16 @@
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"@types/react": "^16.9.48", "babel-plugin-macros": "^3.1.0",
"babel-plugin-macros": "^2.8.0", "cssed": "^1.1.5",
"cssed": "^1.1.2",
"next": "latest", "next": "latest",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0" "react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "^18.11.9",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.9",
"typescript": "^4.9.3"
} }
} }
...@@ -31,13 +31,14 @@ const styles = css` ...@@ -31,13 +31,14 @@ const styles = css`
export default function Home() { export default function Home() {
const [isDark, setDark] = useState(false) const [isDark, setDark] = useState(false)
return ( return (
<> <>
<Head> <Head>
<title>With cssed</title> <title>With cssed</title>
</Head> </Head>
<div <div
onClick={() => setDark(!isDark)} onClick={() => setDark((prevState) => !prevState)}
className={styles.box + ' ' + (isDark ? styles.dark : styles.light)} className={styles.box + ' ' + (isDark ? styles.dark : styles.light)}
> >
Cssed demo Cssed demo
......
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
...@@ -6,9 +6,16 @@ ...@@ -6,9 +6,16 @@
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"cxs": "^3.0.0", "cxs": "^6.2.0",
"next": "latest", "next": "latest",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0" "react-dom": "^18.2.0"
},
"devDependencies": {
"@types/cxs": "^6.2.1",
"@types/node": "^18.11.9",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.9",
"typescript": "^4.9.3"
} }
} }
import type { DocumentContext, DocumentInitialProps } from 'next/document'
import Document, { Html, Head, Main, NextScript } from 'next/document' import Document, { Html, Head, Main, NextScript } from 'next/document'
import cxs from 'cxs/lite' import cxs from 'cxs'
export default class MyDocument extends Document { export default class MyDocument extends Document {
static async getInitialProps({ renderPage }) { static async getInitialProps(
const page = await renderPage() ctx: DocumentContext
const style = cxs.getCss() ): Promise<DocumentInitialProps> {
return { ...page, style } const initialProps = await Document.getInitialProps(ctx)
const styles = cxs.css()
cxs.reset()
return {
...initialProps,
styles: (
<>
{initialProps.styles}
<style dangerouslySetInnerHTML={{ __html: styles }} />
</>
),
}
} }
render() { render() {
return ( return (
<Html> <Html>
<Head> <Head />
<style
id="cxs-style"
dangerouslySetInnerHTML={{ __html: this.props.style }}
/>
</Head>
<body> <body>
<Main /> <Main />
<NextScript /> <NextScript />
......
import cxs from 'cxs/lite' import cxs from 'cxs'
// Using cxs/lite on both the server and client,
// the styles will need to be rehydrated.
if (typeof window !== 'undefined') {
const styleTag = document.getElementById('cxs-style')
const serverCss = styleTag.innerHTML
cxs.rehydrate(serverCss)
}
const cx = { const cx = {
root: cxs({ root: cxs({
......
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
...@@ -9,5 +9,11 @@ ...@@ -9,5 +9,11 @@
"next": "latest", "next": "latest",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0" "react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "^18.11.9",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.9",
"typescript": "^4.9.3"
} }
} }
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment