Unverified Commit 0cb2037a authored by Rafo's avatar Rafo Committed by GitHub

[examples] Update styled-components with Babel - add types (#36125)

This PR is looking forward to improve documentation exemples about usage of Next.js with Typescript and Styled-components. 

I added Typescript types to the original exemple [Example app with styled-components using babel
](https://github.com/vercel/next.js/tree/canary/examples/with-styled-components-babel)

## Feature

- [x] Related issues linked using 
- [x] Documentation added

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`

fixes #36008
parent fca10719
/// <reference types="next" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
...@@ -13,6 +13,9 @@ ...@@ -13,6 +13,9 @@
"styled-components": "^5.2.3" "styled-components": "^5.2.3"
}, },
"devDependencies": { "devDependencies": {
"babel-plugin-styled-components": "^1.12.0" "@types/node": "17.0.24",
"@types/styled-components": "5.1.25",
"babel-plugin-styled-components": "^1.12.0",
"typescript": "4.6.3"
} }
} }
...@@ -8,7 +8,13 @@ const GlobalStyle = createGlobalStyle` ...@@ -8,7 +8,13 @@ const GlobalStyle = createGlobalStyle`
} }
` `
const theme = { interface ThemeInterface {
colors: {
primary: string
}
}
const theme: ThemeInterface = {
colors: { colors: {
primary: '#0070f3', primary: '#0070f3',
}, },
......
import Document, { Head, Html, Main, NextScript } from 'next/document' import Document, { DocumentContext, DocumentInitialProps } from 'next/document'
import { ServerStyleSheet } from 'styled-components' import { ServerStyleSheet } from 'styled-components'
export default class MyDocument extends Document { export default class MyDocument extends Document {
render() { static async getInitialProps(
return ( ctx: DocumentContext
<Html lang="en"> ): Promise<DocumentInitialProps> {
<Head></Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet() const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage const originalRenderPage = ctx.renderPage
...@@ -27,12 +18,12 @@ export default class MyDocument extends Document { ...@@ -27,12 +18,12 @@ export default class MyDocument extends Document {
const initialProps = await Document.getInitialProps(ctx) const initialProps = await Document.getInitialProps(ctx)
return { return {
...initialProps, ...initialProps,
styles: ( styles: [
<> <>
{initialProps.styles} {initialProps.styles}
{sheet.getStyleElement()} {sheet.getStyleElement()}
</> </>,
), ],
} }
} finally { } finally {
sheet.seal() sheet.seal()
......
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"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