Unverified Commit cc35859f authored by Lazar Nikolov's avatar Lazar Nikolov Committed by GitHub

docs(examples): Updated Chakra UI examples (#36333)

## Overview

This PR updates both Chakra UI examples by updating the Chakra UI packages to the latest versions, and added better examples for Semantic Tokens (new feature in Chakra UI), and pseudo props.

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`
Co-authored-by: 's avatarBalázs Orbán <18369201+balazsorban44@users.noreply.github.com>
parent 38aa6d71
# 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
# Example app with [chakra-ui](https://github.com/chakra-ui/chakra-ui) and TypeScript ## Deprecated
This example features how to use [chakra-ui](https://github.com/chakra-ui/chakra-ui) as the component library within a Next.js app with TypeScript. The main [`with-chakra-ui`](../with-chakra-ui) example has been refactored to use TypeScript, so this example is deprecated.
Next.js and chakra-ui have built-in TypeScript declarations, so we'll get autocompletion for their modules straight away.
We are connecting the Next.js `_app.js` with `chakra-ui`'s Provider and theme so the pages can have app-wide dark/light mode. We are also creating some components which shows the usage of `chakra-ui`'s style props.
## Deploy your own
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-chakra-ui-typescript)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-chakra-ui-typescript&project-name=with-chakra-ui-typescript&repository-name=with-chakra-ui-typescript)
## How to use
### Using `create-next-app`
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-chakra-ui-typescript with-chakra-ui-typescript-app
# or
yarn create next-app --example with-chakra-ui-typescript with-chakra-ui-typescript-app
# or
pnpm create next-app -- --example with-chakra-ui-typescript with-chakra-ui-typescript-app
```
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
## Notes
Chakra has supported Gradients and RTL in `v1.1`. To utilize RTL, [add RTL direction and swap](https://chakra-ui.com/docs/features/rtl-support).
If you don't have multi-direction app, you should make `<Html lang="ar" dir="rtl">` inside `_document.ts`.
{
"private": true,
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"@chakra-ui/icons": "^1.0.5",
"@chakra-ui/react": "^1.4.2",
"@chakra-ui/theme-tools": "1.1.2",
"@emotion/react": "11.1.5",
"@emotion/styled": "11.1.5",
"framer-motion": "^4.0.3",
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"@types/node": "^14.6.0",
"@types/react": "^17.0.3",
"@types/react-dom": "^17.0.3",
"typescript": "4.3.2"
}
}
import { Flex, useColorMode, FlexProps } from '@chakra-ui/react'
export const Container = (props: FlexProps) => {
const { colorMode } = useColorMode()
const bgColor = { light: 'gray.50', dark: 'gray.900' }
const color = { light: 'black', dark: 'white' }
return (
<Flex
direction="column"
alignItems="center"
justifyContent="flex-start"
bg={bgColor[colorMode]}
color={color[colorMode]}
{...props}
/>
)
}
# Example app with [chakra-ui](https://github.com/chakra-ui/chakra-ui) # Example app with [chakra-ui](https://github.com/chakra-ui/chakra-ui) and TypeScript
This example features how to use [chakra-ui](https://github.com/chakra-ui/chakra-ui) as the component library within a Next.js app. This example features how to use [chakra-ui](https://github.com/chakra-ui/chakra-ui) as the component library within a Next.js app with TypeScript.
We are connecting the Next.js `_app.js` with `chakra-ui`'s Theme and ColorMode containers so the pages can have app-wide dark/light mode. We are also creating some components which shows the usage of `chakra-ui`'s style props. Next.js and chakra-ui have built-in TypeScript declarations, so we'll get autocompletion for their modules straight away.
We are connecting the Next.js `_app.js` with `chakra-ui`'s Provider and theme so the pages can have app-wide dark/light mode. We are also creating some components which shows the usage of `chakra-ui`'s style props.
## Deploy your own ## Deploy your own
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-chakra-ui) Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-chakra-ui)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-chakra-ui&project-name=with-chakra-ui&repository-name=with-chakra-ui) [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-chakra-ui-typescript&project-name=with-chakra-ui&repository-name=with-chakra-ui)
## How to use ## How to use
### Using `create-next-app`
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: 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 ```bash
...@@ -28,4 +32,4 @@ Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&ut ...@@ -28,4 +32,4 @@ Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&ut
Chakra has supported Gradients and RTL in `v1.1`. To utilize RTL, [add RTL direction and swap](https://chakra-ui.com/docs/features/rtl-support). Chakra has supported Gradients and RTL in `v1.1`. To utilize RTL, [add RTL direction and swap](https://chakra-ui.com/docs/features/rtl-support).
If you don't have multi-direction app, you should make `<Html lang="ar" dir="rtl">` inside `_document.js`. If you don't have multi-direction app, you should make `<Html lang="ar" dir="rtl">` inside `_document.ts`.
{ {
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "next dev", "dev": "next",
"build": "next build", "build": "next build",
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"@chakra-ui/icons": "^1.0.0", "@chakra-ui/icons": "^1.1.7",
"@chakra-ui/react": "^1.4.2", "@chakra-ui/react": "1.8.8",
"@emotion/react": "^11.0.0", "@emotion/react": "^11",
"@emotion/styled": "^11.0.0", "@emotion/styled": "^11",
"framer-motion": "^4.0.3", "framer-motion": "^6",
"next": "latest", "next": "latest",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2" "react-dom": "^17.0.2"
},
"devDependencies": {
"@types/node": "^14.6.0",
"@types/react": "^17.0.3",
"@types/react-dom": "^17.0.3",
"typescript": "4.3.2"
} }
} }
import { Link as ChakraLink, Button } from '@chakra-ui/react'
import { Container } from './Container'
export const CTA = () => (
<Container
flexDirection="row"
position="fixed"
bottom="0"
width="100%"
maxWidth="48rem"
py={2}
>
<ChakraLink isExternal href="https://chakra-ui.com" flexGrow={1} mx={2}>
<Button width="100%" bgGradient="linear(to-tr, teal.300,yellow.400)">
chakra-ui
</Button>
</ChakraLink>
<ChakraLink
isExternal
href="https://github.com/vercel/next.js/blob/canary/examples/with-chakra-ui"
flexGrow={3}
mx={2}
>
<Button width="100%" bgGradient="linear(to-tr, teal.300,yellow.400)">
View Repo
</Button>
</ChakraLink>
</Container>
)
...@@ -6,26 +6,36 @@ export const CTA = () => ( ...@@ -6,26 +6,36 @@ export const CTA = () => (
<Container <Container
flexDirection="row" flexDirection="row"
position="fixed" position="fixed"
bottom="0" bottom={0}
width="100%" width="full"
maxWidth="48rem" maxWidth="3xl"
py={3} py={3}
> >
<ChakraLink isExternal href="https://chakra-ui.com" flexGrow={1} mx={2}> <Button
<Button width="100%" variant="outline" colorScheme="green"> as={ChakraLink}
isExternal
href="https://chakra-ui.com"
variant="outline"
colorScheme="green"
rounded="button"
flexGrow={1}
mx={2}
width="full"
>
chakra-ui chakra-ui
</Button> </Button>
</ChakraLink> <Button
as={ChakraLink}
<ChakraLink
isExternal isExternal
href="https://github.com/vercel/next.js/blob/canary/examples/with-chakra-ui-typescript" href="https://github.com/vercel/next.js/blob/canary/examples/with-chakra-ui-typescript"
variant="solid"
colorScheme="green"
rounded="button"
flexGrow={3} flexGrow={3}
mx={2} mx={2}
width="full"
> >
<Button width="100%" variant="solid" colorScheme="green">
View Repo View Repo
</Button> </Button>
</ChakraLink>
</Container> </Container>
) )
import { Flex, useColorMode } from '@chakra-ui/react'
export const Container = (props) => {
const { colorMode } = useColorMode()
const bgColor = { light: 'gray.50', dark: 'gray.900' }
const color = { light: 'black', dark: 'white' }
return (
<Flex
direction="column"
alignItems="center"
justifyContent="flex-start"
bg={bgColor[colorMode]}
color={color[colorMode]}
{...props}
/>
)
}
import { Flex, FlexProps } from '@chakra-ui/react'
export const Container = (props: FlexProps) => (
<Flex
direction="column"
alignItems="center"
justifyContent="flex-start"
bg="gray.50"
color="black"
_dark={{
bg: 'gray.900',
color: 'white',
}}
transition="all 0.15s ease-out"
{...props}
/>
)
import { useColorMode, Switch } from '@chakra-ui/react'
export const DarkModeSwitch = () => {
const { colorMode, toggleColorMode } = useColorMode()
const isDark = colorMode === 'dark'
return (
<Switch
position="fixed"
top="1rem"
right="1rem"
color="green"
isChecked={isDark}
onChange={toggleColorMode}
/>
)
}
import { useColorMode, Switch } from '@chakra-ui/react' import { useColorMode, IconButton } from '@chakra-ui/react'
import { SunIcon, MoonIcon } from '@chakra-ui/icons'
export const DarkModeSwitch = () => { export const DarkModeSwitch = () => {
const { colorMode, toggleColorMode } = useColorMode() const { colorMode, toggleColorMode } = useColorMode()
const isDark = colorMode === 'dark' const isDark = colorMode === 'dark'
return ( return (
<Switch <IconButton
position="fixed" position="fixed"
top="1rem" top={4}
right="1rem" right={4}
color="green" icon={isDark ? <SunIcon /> : <MoonIcon />}
isChecked={isDark} aria-label="Toggle Theme"
onChange={toggleColorMode} colorScheme="green"
onClick={toggleColorMode}
/> />
) )
} }
import { Flex } from '@chakra-ui/react'
export const Footer = (props) => <Flex as="footer" py="8rem" {...props} />
import { Flex, Heading } from '@chakra-ui/react'
export const Hero = ({ title }) => (
<Flex justifyContent="center" alignItems="center" height="100vh">
<Heading
fontSize="10vw"
bgGradient="linear(to-l, #7928CA, #FF0080)"
bgClip="text"
>
{title}
</Heading>
</Flex>
)
Hero.defaultProps = {
title: 'with-chakra-ui',
}
...@@ -5,7 +5,7 @@ export const Hero = ({ title }: { title: string }) => ( ...@@ -5,7 +5,7 @@ export const Hero = ({ title }: { title: string }) => (
justifyContent="center" justifyContent="center"
alignItems="center" alignItems="center"
height="100vh" height="100vh"
bgGradient="linear(to-l, #7928CA, #FF0080)" bgGradient="linear(to-l, heroGradientStart, heroGradientEnd)"
bgClip="text" bgClip="text"
> >
<Heading fontSize="6vw">{title}</Heading> <Heading fontSize="6vw">{title}</Heading>
......
import { Stack } from '@chakra-ui/react'
export const Main = (props) => (
<Stack
spacing="1.5rem"
width="100%"
maxWidth="48rem"
mt="-45vh"
pt="8rem"
px="1rem"
{...props}
/>
)
import { ChakraProvider, ColorModeProvider } from '@chakra-ui/react'
import theme from '../theme'
function MyApp({ Component, pageProps }) {
return (
<ChakraProvider resetCSS theme={theme}>
<ColorModeProvider
options={{
useSystemColorMode: true,
}}
>
<Component {...pageProps} />
</ColorModeProvider>
</ChakraProvider>
)
}
export default MyApp
import NextDocument, { Html, Head, Main, NextScript } from 'next/document'
import { ColorModeScript } from '@chakra-ui/react'
export default class Document extends NextDocument {
render() {
return (
<Html>
<Head />
<body>
{/* Make Color mode to persists when you refresh the page. */}
<ColorModeScript />
<Main />
<NextScript />
</body>
</Html>
)
}
}
import {
Link as ChakraLink,
Text,
Code,
List,
ListIcon,
ListItem,
} from '@chakra-ui/react'
import { CheckCircleIcon, LinkIcon } from '@chakra-ui/icons'
import { Hero } from '../components/Hero'
import { Container } from '../components/Container'
import { Main } from '../components/Main'
import { DarkModeSwitch } from '../components/DarkModeSwitch'
import { CTA } from '../components/CTA'
import { Footer } from '../components/Footer'
const Index = () => (
<Container height="100vh">
<Hero />
<Main>
<Text>
Example repository of <Code>Next.js</Code> + <Code>chakra-ui</Code>.
</Text>
<List spacing={3} my={0}>
<ListItem>
<ListIcon as={CheckCircleIcon} color="green.500" />
<ChakraLink
isExternal
href="https://chakra-ui.com"
flexGrow={1}
mr={2}
>
Chakra UI <LinkIcon />
</ChakraLink>
</ListItem>
<ListItem>
<ListIcon as={CheckCircleIcon} color="green.500" />
<ChakraLink isExternal href="https://nextjs.org" flexGrow={1} mr={2}>
Next.js <LinkIcon />
</ChakraLink>
</ListItem>
</List>
</Main>
<DarkModeSwitch />
<Footer>
<Text>Next ❤️ Chakra</Text>
</Footer>
<CTA />
</Container>
)
export default Index
...@@ -19,12 +19,12 @@ const Index = () => ( ...@@ -19,12 +19,12 @@ const Index = () => (
<Container height="100vh"> <Container height="100vh">
<Hero /> <Hero />
<Main> <Main>
<Text> <Text color="text">
Example repository of <Code>Next.js</Code> + <Code>chakra-ui</Code> +{' '} Example repository of <Code>Next.js</Code> + <Code>chakra-ui</Code> +{' '}
<Code>TypeScript</Code>. <Code>TypeScript</Code>.
</Text> </Text>
<List spacing={3} my={0}> <List spacing={3} my={0} color="text">
<ListItem> <ListItem>
<ListIcon as={CheckCircleIcon} color="green.500" /> <ListIcon as={CheckCircleIcon} color="green.500" />
<ChakraLink <ChakraLink
......
import { extendTheme } from '@chakra-ui/react'
import { createBreakpoints } from '@chakra-ui/theme-tools'
const fonts = { mono: `'Menlo', monospace` }
const breakpoints = createBreakpoints({
sm: '40em',
md: '52em',
lg: '64em',
xl: '80em',
})
const theme = extendTheme({
colors: {
black: '#16161D',
},
fonts,
breakpoints,
icons: {
logo: {
path: (
<svg
width="3000"
height="3163"
viewBox="0 0 3000 3163"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<rect width="3000" height="3162.95" fill="none" />
<path
d="M1470.89 1448.81L2170 2488.19H820V706.392H2170L1470.89 1448.81ZM1408.21 1515.37L909.196 2045.3V2393.46H1998.84L1408.21 1515.37Z"
fill="currentColor"
/>
</svg>
),
viewBox: '0 0 3000 3163',
},
},
})
export default theme
...@@ -11,6 +11,25 @@ const breakpoints = createBreakpoints({ ...@@ -11,6 +11,25 @@ const breakpoints = createBreakpoints({
}) })
const theme = extendTheme({ const theme = extendTheme({
semanticTokens: {
colors: {
text: {
default: '#16161D',
_dark: '#ade3b8',
},
heroGradientStart: {
default: '#7928CA',
_dark: '#e3a7f9',
},
heroGradientEnd: {
default: '#FF0080',
_dark: '#fbec8f',
},
},
radii: {
button: '12px',
},
},
colors: { colors: {
black: '#16161D', black: '#16161D',
}, },
......
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