未验证 提交 974cae68 编写于 作者: C Christian Alfoni 提交者: GitHub

Update example: update to new stitches version and api (#14876)

上级 75b25901
import { createConfig } from '@stitches/css' import { createCss } from '@stitches/css'
import { createStyled } from '@stitches/styled' import { createStyled } from '@stitches/styled'
const config = createConfig({ export const css = createCss({
tokens: { tokens: {
colors: { colors: {
RED: 'tomato', RED: 'tomato',
}, },
}, },
}) })
/*
With Typescript:
const { Provider, styled, useCss } = createStyled<typeof config>()
*/
const { Provider, styled, useCss } = createStyled()
export { config, Provider, styled, useCss } export const styled = createStyled(css)
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"@stitches/css": "2.0.6", "@stitches/css": "3.0.0",
"@stitches/styled": "2.0.6", "@stitches/styled": "3.0.0",
"next": "9.3.5", "next": "9.3.5",
"react": "16.13.1", "react": "16.13.1",
"react-dom": "16.13.1" "react-dom": "16.13.1"
......
import App from 'next/app'
import { createCss } from '@stitches/css'
import { Provider, config } from '../css'
/*
With Typescript:
export default class MyApp extends App<{ serverCss: TCss<typeof config> }> {
*/
export default class MyApp extends App {
render() {
const { Component, pageProps, serverCss } = this.props
return (
<Provider css={serverCss || createCss(config)}>
<Component {...pageProps} />
</Provider>
)
}
}
import Document from 'next/document' import Document from 'next/document'
import { createCss } from '@stitches/css' import { css } from '../css'
import { config } from '../css'
export default class MyDocument extends Document { export default class MyDocument extends Document {
static async getInitialProps(ctx) { static async getInitialProps(ctx) {
const css = createCss(config)
const originalRenderPage = ctx.renderPage const originalRenderPage = ctx.renderPage
try { try {
ctx.renderPage = () => let extractedStyles
originalRenderPage({ ctx.renderPage = () => {
enhanceApp: (App) => (props) => <App {...props} serverCss={css} />, const { styles, result } = css.getStyles(originalRenderPage)
}) extractedStyles = styles
return result
}
const initialProps = await Document.getInitialProps(ctx) const initialProps = await Document.getInitialProps(ctx)
return { return {
...initialProps, ...initialProps,
styles: ( styles: (
<> <>
{initialProps.styles} {initialProps.styles}
<style>{css.getStyles()}</style> {extractedStyles.map((content) => (
<style>{content}</style>
))}
</> </>
), ),
} }
......
import { styled } from '../css' import { styled } from '../css'
const Header = styled.h1((css) => css.color('RED')) const Header = styled.h1({
color: 'RED',
})
export default function Home() { export default function Home() {
return <Header>Hello world</Header> return <Header>Hello world</Header>
......
import { createConfig } from '@stitches/css' import { createCss } from '@stitches/css'
import * as React from 'react'
const config = createConfig({ export const css = createCss({
tokens: { tokens: {
colors: { colors: {
RED: 'tomato', RED: 'tomato',
}, },
}, },
}) })
/*
With Typescript:
const context = React.createContext<TCss<typeof config>>(null)
*/
const context = React.createContext(null)
/*
With Typescript:
const Provider = ({ css, children }: { css: TCss<typeof config>, children?: React.ReactNode }) => {
return <context.Provider value={css}>{children}</context.Provider>
}
*/
const Provider = ({ css, children }) => {
return <context.Provider value={css}>{children}</context.Provider>
}
const useCss = () => React.useContext(context)
export { config, Provider, useCss }
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"@stitches/css": "2.0.6", "@stitches/css": "3.0.0",
"next": "9.3.5", "next": "9.3.5",
"react": "16.13.1", "react": "16.13.1",
"react-dom": "16.13.1" "react-dom": "16.13.1"
......
import { createCss } from '@stitches/css'
import App from 'next/app'
import { config, Provider } from '../css'
/*
With Typescript:
export default class MyApp extends App<{ serverCss: TCss<typeof config> }> {
*/
export default class MyApp extends App {
render() {
const { Component, pageProps, serverCss } = this.props
return (
<Provider css={serverCss || createCss(config)}>
<Component {...pageProps} />
</Provider>
)
}
}
import { createCss } from '@stitches/css'
import Document from 'next/document' import Document from 'next/document'
import { config } from '../css' import { css } from '../css'
export default class MyDocument extends Document { export default class MyDocument extends Document {
static async getInitialProps(ctx) { static async getInitialProps(ctx) {
const css = createCss(config)
const originalRenderPage = ctx.renderPage const originalRenderPage = ctx.renderPage
try { try {
ctx.renderPage = () => let extractedStyles
originalRenderPage({ ctx.renderPage = () => {
enhanceApp: (App) => (props) => <App {...props} serverCss={css} />, const { styles, result } = css.getStyles(originalRenderPage)
}) extractedStyles = styles
return result
}
const initialProps = await Document.getInitialProps(ctx) const initialProps = await Document.getInitialProps(ctx)
return { return {
...initialProps, ...initialProps,
styles: ( styles: (
<> <>
{initialProps.styles} {initialProps.styles}
{css.getStyles().map((css, index) => ( {extractedStyles.map((content) => (
<style key={index}>{css}</style> <style>{content}</style>
))} ))}
</> </>
), ),
......
import { useCss } from '../css' import { css } from '../css'
export default function Home() { export default function Home() {
const css = useCss() return (
return <h1 className={css.color('RED')}>Hello world</h1> <h1
className={css({
color: 'RED',
})}
>
Hello world
</h1>
)
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册