Skip to content

Commit 8eb82b3

Browse files
authored
Convert more jsx/styled-components examples to TypeScript (vercel#43117)
Converted 3 more examples to TypeScript. Individual contributions pushed as separate commits. ## 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)
1 parent 3f30e26 commit 8eb82b3

File tree

15 files changed

+139
-49
lines changed

15 files changed

+139
-49
lines changed

examples/with-styled-components-rtl/.babelrc

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {
3+
compiler: {
4+
styledComponents: true,
5+
},
6+
}
7+
8+
module.exports = nextConfig

examples/with-styled-components-rtl/package.json

+9-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
99
"next": "latest",
1010
"react": "^18.2.0",
1111
"react-dom": "^18.2.0",
12-
"react-is": "^17.0.2",
13-
"styled-components": "^5.2.3",
14-
"stylis-plugin-rtl": "1.0.0"
12+
"react-is": "^18.2.0",
13+
"styled-components": "^5.3.6",
14+
"stylis-plugin-rtl": "^2.1.1"
1515
},
1616
"devDependencies": {
17-
"babel-plugin-styled-components": "^1.12.0"
17+
"@types/node": "^18.11.9",
18+
"@types/react": "^18.0.25",
19+
"@types/react-dom": "^18.0.9",
20+
"@types/styled-components": "^5.1.26",
21+
"babel-plugin-styled-components": "^2.0.7",
22+
"typescript": "^4.9.3"
1823
}
1924
}

examples/with-styled-components-rtl/pages/_app.js examples/with-styled-components-rtl/pages/_app.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import type { AppProps } from 'next/app'
2+
import type { DefaultTheme } from 'styled-components'
13
import { ThemeProvider } from 'styled-components'
24

3-
const theme = {
5+
const theme: DefaultTheme = {
46
colors: {
57
primary: '#0070f3',
68
},
79
}
810

9-
export default function App({ Component, pageProps }) {
11+
export default function App({ Component, pageProps }: AppProps) {
1012
return (
1113
<ThemeProvider theme={theme}>
1214
<Component {...pageProps} />

examples/with-styled-components-rtl/pages/_document.js

-33
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { DocumentContext, DocumentInitialProps } from 'next/document'
2+
import Document from 'next/document'
3+
import { ServerStyleSheet, StyleSheetManager } from 'styled-components'
4+
import stylisRTLPlugin from 'stylis-plugin-rtl'
5+
6+
export default class MyDocument extends Document {
7+
static async getInitialProps(
8+
ctx: DocumentContext
9+
): Promise<DocumentInitialProps> {
10+
const sheet = new ServerStyleSheet()
11+
const originalRenderPage = ctx.renderPage
12+
13+
try {
14+
ctx.renderPage = () =>
15+
originalRenderPage({
16+
enhanceApp: (App) => (props) =>
17+
sheet.collectStyles(
18+
<StyleSheetManager stylisPlugins={[stylisRTLPlugin]}>
19+
<App {...props} />
20+
</StyleSheetManager>
21+
),
22+
})
23+
24+
const initialProps = await Document.getInitialProps(ctx)
25+
return {
26+
...initialProps,
27+
styles: [initialProps.styles, sheet.getStyleElement()],
28+
}
29+
} finally {
30+
sheet.seal()
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'styled-components'
2+
3+
declare module 'styled-components' {
4+
export interface DefaultTheme {
5+
colors: {
6+
primary: string
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"strict": true,
8+
"forceConsistentCasingInFileNames": true,
9+
"noEmit": true,
10+
"esModuleInterop": true,
11+
"module": "esnext",
12+
"moduleResolution": "node",
13+
"resolveJsonModule": true,
14+
"isolatedModules": true,
15+
"jsx": "preserve",
16+
"incremental": true
17+
},
18+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "styled.d.ts"],
19+
"exclude": ["node_modules"]
20+
}

examples/with-styled-jsx-plugins/package.json

+10-4
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@
66
"start": "next start"
77
},
88
"dependencies": {
9+
"lost": "^9.0.1",
910
"next": "latest",
10-
"lost": "8.2.0",
11-
"postcss-nested": "5.0.5",
12-
"postcss": "8.2.9",
11+
"postcss": "^8.4.19",
12+
"postcss-nested": "^6.0.0",
1313
"react": "^18.2.0",
1414
"react-dom": "^18.2.0",
15-
"styled-jsx-plugin-postcss": "4.0.1"
15+
"styled-jsx-plugin-postcss": "^4.0.1"
1616
},
1717
"postcss": {
1818
"plugins": {
1919
"lost": {},
2020
"postcss-nested": {}
2121
}
22+
},
23+
"devDependencies": {
24+
"@types/node": "^18.11.9",
25+
"@types/react": "^18.0.25",
26+
"@types/react-dom": "^18.0.9",
27+
"typescript": "^4.9.3"
2228
}
2329
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"strict": false,
8+
"forceConsistentCasingInFileNames": true,
9+
"noEmit": true,
10+
"esModuleInterop": true,
11+
"module": "esnext",
12+
"moduleResolution": "node",
13+
"resolveJsonModule": true,
14+
"isolatedModules": true,
15+
"jsx": "preserve",
16+
"incremental": true
17+
},
18+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
19+
"exclude": ["node_modules"]
20+
}

examples/with-styled-jsx-scss/package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
"react-dom": "^18.2.0"
1212
},
1313
"devDependencies": {
14-
"sass": "^1.32.8",
15-
"@styled-jsx/plugin-sass": "^3.0.0"
14+
"@styled-jsx/plugin-sass": "^4.0.2",
15+
"@types/node": "^18.11.9",
16+
"@types/react": "^18.0.25",
17+
"@types/react-dom": "^18.0.9",
18+
"sass": "^1.56.1",
19+
"typescript": "^4.9.3"
1620
}
1721
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"strict": false,
8+
"forceConsistentCasingInFileNames": true,
9+
"noEmit": true,
10+
"esModuleInterop": true,
11+
"module": "esnext",
12+
"moduleResolution": "node",
13+
"resolveJsonModule": true,
14+
"isolatedModules": true,
15+
"jsx": "preserve",
16+
"incremental": true
17+
},
18+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
19+
"exclude": ["node_modules"]
20+
}

0 commit comments

Comments
 (0)