Unverified Commit 0e843e60 authored by Pedro Duarte's avatar Pedro Duarte Committed by GitHub

Update `with-stitches` example (#16827)

Hi 👋 

[Stitches](https://stitches.dev) `v0.0.1` beta was recently released. This PR updates the existing examples to the latest packages and API.

Notes:
- Remove `with-stitches-styled` example, that's no longer needed
- Update `with-stitches` example
  - Update dependency
  - Example done with Typescript
- Updated README

Thanks ✌️ 

![CleanShot 2020-09-03 at 18 23 09@2x](https://user-images.githubusercontent.com/372831/92141867-52d98d80-ee13-11ea-91ed-001cd46989f1.jpg)
parent 4048d298
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local
# vercel
.vercel
# Stitches (Styled) Example
This example shows how to use the [Stitches CSS-in-JS Library](https://github.com/christianalfoni/stitches).
## Deploy your own
Deploy the example using [Vercel](https://vercel.com):
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/vercel/next.js/tree/canary/examples/with-stitches-styled)
## How to use
Execute [Create Next App](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
```bash
npx create-next-app --example with-stitches-styled
# or
yarn create next-app --example with-stitches-styled
```
Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
import { createCss } from '@stitches/css'
import { createStyled } from '@stitches/styled'
export const css = createCss({
tokens: {
colors: {
RED: 'tomato',
},
},
})
export const styled = createStyled(css)
{
"name": "with-stitches-styled",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"@stitches/css": "3.0.3",
"@stitches/styled": "3.0.3",
"next": "9.3.5",
"react": "16.13.1",
"react-dom": "16.13.1"
},
"devDependencies": {
"@types/react": "^16.9.34",
"typescript": "^3.8.3"
},
"license": "MIT"
}
import { styled } from '../css'
const Header = styled.h1({
color: 'RED',
})
export default function Home() {
return <Header>Hello world</Header>
}
<svg width="82" height="16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill="url(#prefix__paint0_linear)" d="M9.018 0l9.019 16H0L9.018 0z"/>
<path fill="#333" fill-rule="evenodd" d="M51.634 12.028h-6.492V2.052h6.492v1.256H46.61v3.007h4.37V7.57h-4.37v3.202h5.024v1.255zm-14.063 0h-7.235v-1.096l5.342-7.624h-5.253V2.052h7.058v1.097l-5.342 7.623h5.43v1.256zm21.88 0h6.333v-1.256h-2.423V3.308h2.423V2.052h-6.332v1.256h2.441v7.465h-2.441v1.255zm18.22 0h-1.468v-8.72h-3.36V2.052h8.225v1.256H77.67v8.72z" clip-rule="evenodd"/>
<defs>
<linearGradient id="prefix__paint0_linear" x1="28.022" x2="16.189" y1="22.991" y2="8.569" gradientUnits="userSpaceOnUse">
<stop stop-color="#fff"/>
<stop offset="1"/>
</linearGradient>
</defs>
</svg>
# Stitches Example # Stitches Example
This example shows how to use the [Stitches CSS-in-JS Library](https://github.com/christianalfoni/stitches). This example shows how to use the [Stitches CSS-in-JS Library](https://github.com/modulz/stitches).
## Deploy your own ## Deploy your own
......
const StitchesLogo = () => (
<svg
xmlns="http://www.w3.org/2000/svg"
width="35"
height="35"
viewBox="0 0 35 35"
fill="none"
>
<circle
cx="17.5"
cy="17.5"
r="14.5"
stroke="currentColor"
strokeWidth="2"
/>
<path d="M12.8184 31.3218L31.8709 20.3218" stroke="currentColor" />
<path d="M3.31836 14.8674L22.3709 3.86743" stroke="currentColor" />
<path
d="M8.65332 29.1077L25.9738 19.1077"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
/>
<path
d="M9.21582 16.0815L26.5363 6.08154"
stroke="currentColor"
strokeLinecap="round"
/>
<path
d="M13.2334 14.2297L22.5099 21.1077"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
/>
<path
d="M16.6973 12.2302L25.9736 19.1078"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
/>
<path
d="M9.21582 16.0815L19.0459 23.1078"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
)
export default StitchesLogo
import { createCss } from '@stitches/css'
export const css = createCss({
tokens: {
colors: {
RED: 'tomato',
},
},
})
/// <reference types="next" />
/// <reference types="next/types/global" />
{ {
"name": "stitches", "name": "with-stitches",
"version": "0.1.0", "version": "0.1.0",
"private": true,
"scripts": { "scripts": {
"dev": "next dev", "dev": "next dev",
"build": "next build", "build": "next build",
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"@stitches/css": "3.0.2", "@stitches/react": "0.0.1",
"next": "9.3.5", "next": "latest",
"react": "16.13.1", "react": "^16.13.1",
"react-dom": "16.13.1" "react-dom": "^16.13.1"
}, },
"devDependencies": { "devDependencies": {
"@types/react": "^16.9.34", "@types/react": "^16.9.43",
"typescript": "^3.8.3" "typescript": "^3.9.7"
}, },
"license": "MIT" "license": "MIT"
} }
import Document from 'next/document'
import { css } from '../css'
export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const originalRenderPage = ctx.renderPage
try {
let extractedStyles
ctx.renderPage = () => {
const { styles, result } = css.getStyles(originalRenderPage)
extractedStyles = styles
return result
}
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{extractedStyles.map((content, index) => (
<style key={index}>{content}</style>
))}
</>
),
}
} finally {
}
}
}
import Document from 'next/document' import React from 'react'
import { css } from '../css' import NextDocument, { DocumentContext } from 'next/document'
import { css } from '../stitches.config'
export default class MyDocument extends Document { export default class Document extends NextDocument {
static async getInitialProps(ctx) { static async getInitialProps(ctx: DocumentContext) {
const originalRenderPage = ctx.renderPage const originalRenderPage = ctx.renderPage
try { try {
...@@ -13,15 +14,19 @@ export default class MyDocument extends Document { ...@@ -13,15 +14,19 @@ export default class MyDocument extends Document {
return result return result
} }
const initialProps = await Document.getInitialProps(ctx) const initialProps = await NextDocument.getInitialProps(ctx)
return { return {
...initialProps, ...initialProps,
styles: ( styles: (
<> <>
{initialProps.styles} {initialProps.styles}
{extractedStyles.map((content, index) => ( {extractedStyles.map((content, index) => (
<style key={index}>{content}</style> <style
key={index}
dangerouslySetInnerHTML={{ __html: content }}
/>
))} ))}
</> </>
), ),
......
import { css } from '../css'
export default function Home() {
return (
<h1
className={css({
color: 'RED',
})}
>
Hello world
</h1>
)
}
import Head from 'next/head'
import { styled } from '../stitches.config'
import StitchesLogo from '../components/StitchesLogo'
const Box = styled('div', {})
const Text = styled('p', {
fontFamily: '$system',
color: '$hiContrast',
})
const Link = styled('a', {
fontFamily: '$system',
textDecoration: 'none',
color: '$purple600',
})
const Container = styled('div', {
marginX: 'auto',
paddingX: '$3',
variants: {
size: {
'1': {
maxWidth: '300px',
},
'2': {
maxWidth: '585px',
},
'3': {
maxWidth: '865px',
},
},
},
})
export default function Home() {
return (
<Box css={{ paddingY: '$6' }}>
<Head>
<title>Use Stitches with Next.js</title>
</Head>
<Container size={{ initial: '1', bp1: '2' }}>
<StitchesLogo />
<Text as="h1">Hello, from Stitches.</Text>
<Text>
For full documentation, visit{' '}
<Link href="https://stitches.dev">stitches.dev</Link>.
</Text>
</Container>
</Box>
)
}
<svg width="82" height="16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill="url(#prefix__paint0_linear)" d="M9.018 0l9.019 16H0L9.018 0z"/>
<path fill="#333" fill-rule="evenodd" d="M51.634 12.028h-6.492V2.052h6.492v1.256H46.61v3.007h4.37V7.57h-4.37v3.202h5.024v1.255zm-14.063 0h-7.235v-1.096l5.342-7.624h-5.253V2.052h7.058v1.097l-5.342 7.623h5.43v1.256zm21.88 0h6.333v-1.256h-2.423V3.308h2.423V2.052h-6.332v1.256h2.441v7.465h-2.441v1.255zm18.22 0h-1.468v-8.72h-3.36V2.052h8.225v1.256H77.67v8.72z" clip-rule="evenodd"/>
<defs>
<linearGradient id="prefix__paint0_linear" x1="28.022" x2="16.189" y1="22.991" y2="8.569" gradientUnits="userSpaceOnUse">
<stop stop-color="#fff"/>
<stop offset="1"/>
</linearGradient>
</defs>
</svg>
import { createStyled } from '@stitches/react'
const theme = {
colors: {
$hiContrast: 'hsl(206,10%,5%)',
$loContrast: 'white',
$gray100: 'hsl(206,22%,99%)',
$gray200: 'hsl(206,12%,97%)',
$gray300: 'hsl(206,11%,92%)',
$gray400: 'hsl(206,10%,84%)',
$gray500: 'hsl(206,10%,76%)',
$gray600: 'hsl(206,10%,44%)',
$purple100: 'hsl(252,100%,99%)',
$purple200: 'hsl(252,100%,98%)',
$purple300: 'hsl(252,100%,94%)',
$purple400: 'hsl(252,75%,84%)',
$purple500: 'hsl(252,78%,60%)',
$purple600: 'hsl(252,80%,53%)',
},
space: {
$1: '5px',
$2: '10px',
$3: '15px',
$4: '20px',
$5: '25px',
$6: '35px',
},
sizes: {
$1: '5px',
$2: '10px',
$3: '15px',
$4: '20px',
$5: '25px',
$6: '35px',
},
fontSizes: {
$1: '12px',
$2: '13px',
$3: '15px',
$4: '17px',
$5: '19px',
$6: '21px',
},
fonts: {
$system: 'system-ui',
},
}
export const { styled, css } = createStyled({
tokens: theme,
utils: {
marginX: (config) => (
value: keyof typeof theme['space'] | (string & {})
) => ({
marginLeft: value,
marginRight: value,
}),
marginY: (config) => (
value: keyof typeof theme['space'] | (string & {})
) => ({
marginTop: value,
marginBottom: value,
}),
paddingX: (config) => (
value: keyof typeof theme['space'] | (string & {})
) => ({
paddingLeft: value,
paddingRight: value,
}),
paddingY: (config) => (
value: keyof typeof theme['space'] | (string & {})
) => ({
paddingTop: value,
paddingBottom: value,
}),
},
breakpoints: {
bp1: (rule) => `@media (min-width: 520px) { ${rule} }`,
bp2: (rule) => `@media (min-width: 900px) { ${rule} }`,
},
})
{
"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"
},
"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