Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add with-chakra-ui-typescript example #16561

Merged
merged 19 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
43c5632
add with-chakra-ui-typescript example
tomasdisk Aug 25, 2020
599d229
Merge branch 'canary' into examples/with-chakra-ui-typescript
tomasdisk Aug 25, 2020
44562b3
fixed nextjs version
tomasdisk Aug 27, 2020
eeec8b6
Merge branch 'examples/with-chakra-ui-typescript' of github.com:tomas…
tomasdisk Aug 27, 2020
d0042e3
Merge branch 'canary' into examples/with-chakra-ui-typescript
tomasdisk Aug 27, 2020
0c8aee4
Update examples/with-chakra-ui-typescript/package.json
tomasdisk Aug 30, 2020
fbc04a4
Update examples/with-chakra-ui-typescript/package.json
tomasdisk Aug 30, 2020
6e14c5f
Update examples/with-chakra-ui-typescript/README.md
tomasdisk Aug 30, 2020
69d508b
Update examples/with-chakra-ui-typescript/README.md
tomasdisk Aug 30, 2020
5bb2d6d
Merge branch 'canary' into examples/with-chakra-ui-typescript
tomasdisk Aug 30, 2020
d16d548
Merge branch 'canary' into examples/with-chakra-ui-typescript
tomasdisk Aug 30, 2020
cd2b4dc
Merge branch 'canary' into examples/with-chakra-ui-typescript
tomasdisk Aug 30, 2020
648b566
Merge branch 'canary' into examples/with-chakra-ui-typescript
tomasdisk Sep 11, 2020
141b242
Update to ChakraUi RC V1
tomasdisk Sep 14, 2020
6fad133
Merge branch 'examples/with-chakra-ui-typescript' of github.com:tomas…
tomasdisk Sep 14, 2020
df45abc
Merge branch 'canary' into examples/with-chakra-ui-typescript
tomasdisk Sep 14, 2020
aa5ea97
Merge branch 'canary' into examples/with-chakra-ui-typescript
tomasdisk Sep 17, 2020
346270d
readme and next.js version updated
tomasdisk Sep 21, 2020
eec0747
Merge branch 'canary' into examples/with-chakra-ui-typescript
kodiakhq[bot] Sep 21, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/with-chakra-ui-typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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
52 changes: 52 additions & 0 deletions examples/with-chakra-ui-typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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 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.

## 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-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
```

### Download manually

Download the example:

```bash
curl https://codeload.github.com/vercel/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-chakra-ui-typescript
cd with-chakra-ui-typescript
```

Install it and run:

```bash
npm install
npm run dev
# or
yarn
yarn dev
```

tomasdisk marked this conversation as resolved.
Show resolved Hide resolved
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)).

## Notes

Next.js and chakra-ui has built-in TypeScript declarations, so we'll get autocompletion for their modules straight away.
tomasdisk marked this conversation as resolved.
Show resolved Hide resolved

A `type-check` script is also added to `package.json`, which runs TypeScript's `tsc` CLI in `noEmit` mode to run type-checking separately. You can then include this, for example, in your `test` scripts.

> All credits for [with-chakra-ui](https://github.com/vercel/next.js/tree/canary/examples/with-chakra-ui) and [with-typescript](https://github.com/vercel/next.js/tree/canary/examples/with-typescript) contributors.
tomasdisk marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions examples/with-chakra-ui-typescript/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
26 changes: 26 additions & 0 deletions examples/with-chakra-ui-typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "with-chakra-ui-typescript",
"version": "1.0.0",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start",
"type-check": "tsc"
tomasdisk marked this conversation as resolved.
Show resolved Hide resolved
},
"dependencies": {
"@chakra-ui/core": "^0.8.0",
"@emotion/core": "^10.0.35",
"@emotion/styled": "^10.0.27",
"emotion-theming": "^10.0.27",
"next": "^9.5.2",
tomasdisk marked this conversation as resolved.
Show resolved Hide resolved
"react": "^16.13.1",
"react-dom": "^16.13.1"
},
"devDependencies": {
"@types/node": "^14.6.0",
"@types/react": "^16.9.46",
"@types/react-dom": "^16.9.8",
"typescript": "4.0.2"
},
"license": "ISC"
tomasdisk marked this conversation as resolved.
Show resolved Hide resolved
}
31 changes: 31 additions & 0 deletions examples/with-chakra-ui-typescript/src/components/CTA.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Link as ChakraLink, Button } from '@chakra-ui/core'

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%" variant="outline" variantColor="green">
chakra-ui
</Button>
</ChakraLink>

<ChakraLink
isExternal
href="https://github.com/vercel/next.js/blob/canary/examples/with-chakra-ui-typescript"
flexGrow={3}
mx={2}
>
<Button width="100%" variant="solid" variantColor="green">
View Repo
</Button>
</ChakraLink>
</Container>
)
19 changes: 19 additions & 0 deletions examples/with-chakra-ui-typescript/src/components/Container.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Flex, useColorMode, FlexProps } from '@chakra-ui/core'

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}
/>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useColorMode, Switch } from '@chakra-ui/core'

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}
/>
)
}
5 changes: 5 additions & 0 deletions examples/with-chakra-ui-typescript/src/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Flex, FlexProps } from '@chakra-ui/core'

export const Footer = (props: FlexProps) => (
<Flex as="footer" py="8rem" {...props} />
)
11 changes: 11 additions & 0 deletions examples/with-chakra-ui-typescript/src/components/Hero.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Flex, Heading } from '@chakra-ui/core'

export const Hero = ({ title }: { title: string }) => (
<Flex justifyContent="center" alignItems="center" height="100vh">
<Heading fontSize="6vw">{title}</Heading>
</Flex>
)

Hero.defaultProps = {
title: 'with-chakra-ui-typescript',
}
13 changes: 13 additions & 0 deletions examples/with-chakra-ui-typescript/src/components/Main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Stack, StackProps } from '@chakra-ui/core'

export const Main = (props: StackProps) => (
<Stack
spacing="1.5rem"
width="100%"
maxWidth="48rem"
mt="-45vh"
pt="8rem"
px="1rem"
{...props}
/>
)
17 changes: 17 additions & 0 deletions examples/with-chakra-ui-typescript/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ThemeProvider, CSSReset, ColorModeProvider } from '@chakra-ui/core'

import theme from '../theme'
import { AppProps } from 'next/app'

function MyApp({ Component, pageProps }: AppProps) {
return (
<ThemeProvider theme={theme}>
<ColorModeProvider>
tomasdisk marked this conversation as resolved.
Show resolved Hide resolved
<CSSReset />
<Component {...pageProps} />
</ColorModeProvider>
</ThemeProvider>
)
}

export default MyApp
56 changes: 56 additions & 0 deletions examples/with-chakra-ui-typescript/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
Link as ChakraLink,
Text,
Code,
Icon,
List,
ListIcon,
ListItem,
} from '@chakra-ui/core'

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> +{' '}
<Code>typescript</Code>.
</Text>

<List spacing={3} my={0}>
<ListItem>
<ListIcon icon="check-circle" color="green.500" />
<ChakraLink
isExternal
href="https://chakra-ui.com"
flexGrow={1}
mr={2}
>
Chakra UI <Icon name="external-link" mx="2px" />
</ChakraLink>
</ListItem>
<ListItem>
<ListIcon icon="check-circle" color="green.500" />
<ChakraLink isExternal href="https://nextjs.org" flexGrow={1} mr={2}>
Next.js <Icon name="external-link" mx="2px" />
</ChakraLink>
</ListItem>
</List>
</Main>

<DarkModeSwitch />
<Footer>
<Text>Next ❤️ Chakra</Text>
</Footer>
<CTA />
</Container>
)

export default Index
38 changes: 38 additions & 0 deletions examples/with-chakra-ui-typescript/src/theme.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { theme as chakraTheme } from '@chakra-ui/core'

const fonts = { ...chakraTheme.fonts, mono: `'Menlo', monospace` }

const breakpoints = ['40em', '52em', '64em']

const theme = {
...chakraTheme,
colors: {
...chakraTheme.colors,
black: '#16161D',
},
fonts,
breakpoints,
icons: {
...chakraTheme.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
23 changes: 23 additions & 0 deletions examples/with-chakra-ui-typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"allowJs": true,
"alwaysStrict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"jsx": "preserve",
"lib": ["dom", "es2017"],
"module": "esnext",
"moduleResolution": "node",
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "esnext"
},
"exclude": ["node_modules"],
"include": ["**/*.ts", "**/*.tsx"]
}