Skip to content
This repository was archived by the owner on Jan 23, 2024. It is now read-only.

Commit a6bc0b5

Browse files
committed
docs: added heading theming docs following the new template
1 parent 7b66c9c commit a6bc0b5

File tree

2 files changed

+249
-0
lines changed

2 files changed

+249
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
module.exports = {
2+
App: `import { Box, SimpleGrid, IconButton, Center, Heading, useColorMode } from "@chakra-ui/react";
3+
import { FaMoon, FaSun } from "react-icons/fa";
4+
export default function App() {
5+
const { toggleColorMode, colorMode } = useColorMode();
6+
return (
7+
<Box position="relative" h="100vh">
8+
<SimpleGrid gap={12} p={12} columns={2}>
9+
<Heading>Default heading</Heading>
10+
<Heading variant="custom">Themed heading</Heading>
11+
<Heading variant="brand">Another themed heading</Heading>
12+
<Heading variant="thicc">Hover me(in dark mode)</Heading>
13+
</SimpleGrid>
14+
<IconButton
15+
rounded="full"
16+
size="xs"
17+
position="absolute"
18+
bottom={4}
19+
left={4}
20+
onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />}
21+
/>
22+
</Box>
23+
);
24+
}`,
25+
Index: `import * as React from "react";
26+
import { createRoot } from "react-dom/client";
27+
import { ChakraProvider, extendTheme } from "@chakra-ui/react";
28+
import App from "./App";
29+
import { headingTheme } from "./theme/components/Heading.ts";
30+
const theme = extendTheme({
31+
components: {
32+
Heading: headingTheme,
33+
}
34+
});
35+
const container = document.getElementById("root");
36+
const root = createRoot(container!);
37+
root.render(
38+
<ChakraProvider theme={theme}>
39+
<App />
40+
</ChakraProvider>
41+
);`,
42+
HeadingTheme: `import { defineStyle, defineStyleConfig } from "@chakra-ui/styled-system"
43+
const brandPrimary = defineStyle({
44+
color: "blue.500",
45+
border: "0px",
46+
// let's also provide dark mode alternatives
47+
_dark: {
48+
color: 'blue.300',
49+
}
50+
})
51+
52+
const custom = defineStyle({
53+
color: "yellow.500",
54+
border: "0px",
55+
// let's also provide dark mode alternatives
56+
_dark: {
57+
color: 'yellow.300',
58+
}
59+
})
60+
61+
const thicc = defineStyle({
62+
color: "orange.500",
63+
border: "0px",
64+
bg: "blue.200",
65+
p: "3",
66+
borderRadius: "10",
67+
transition: 'transform 0.15s ease-in-out, background 0.15s ease-in-out',
68+
// let's also provide dark mode alternatives
69+
_dark: {
70+
color: 'orange.400',
71+
bg: "blue.200"
72+
},
73+
_hover: {
74+
bg: "blue.100",
75+
color: "purple.300",
76+
transform: "scale(1, 1)",
77+
_dark: {
78+
bg: "blue.100",
79+
color: "purple.300",
80+
transform: "scale(3, 3)",
81+
}
82+
}
83+
})
84+
85+
export const headingTheme = defineStyleConfig({
86+
variants: {
87+
brand: brandPrimary,
88+
"custom": custom,
89+
"thicc": thicc
90+
},
91+
})`,
92+
}

content/docs/components/heading/theming.mdx

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,160 @@
22
id: heading
33
scope: theming
44
---
5+
6+
The `Heading` component is a single part component. All of the styling is applied
7+
directly to the `h2` element.
8+
9+
> To learn more about styling single part components, visit the [Component Style](/docs/styled-system/component-style#styling-single-part-components) page
10+
## Theming properties
11+
12+
The properties that affect the theming of the `Heading` component are:
13+
14+
- `variant`: The visual variant of the divider.
15+
- `colorScheme`: The color scheme of the divider.
16+
- `size`: The size of the divider. Defaults to `xl`.
17+
18+
## Theming utilities
19+
20+
- `defineStyle`: a function used to create style objects.
21+
- `defineStyleConfig`: a function used to define the style configuration for a single part component.
22+
23+
```js
24+
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'
25+
```
26+
27+
## Customizing the default theme
28+
29+
```js
30+
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'
31+
32+
const custom = defineStyle({
33+
color: "yellow.500",
34+
border: "0px",
35+
// let's also provide dark mode alternatives
36+
_dark: {
37+
color: 'yellow.300',
38+
}
39+
})
40+
```
41+
After customizing the divider theme, we can import it in our theme file and add it in the components property:
42+
43+
```js
44+
import { extendTheme } from '@chakra-ui/react'
45+
import { headingTheme } from './components/divider.ts'
46+
47+
export const theme = extendTheme({
48+
components: { Heading: headingTheme },
49+
})
50+
```
51+
52+
> This is a crucial step to make sure that any changes that we make to the divider theme are applied.
53+
54+
Every time you're adding anything new to the theme, you'd need to run the cli command to get proper autocomplete in your IDE. Here's how you can do that:
55+
56+
```bash
57+
# run it once:
58+
npx chakra-cli tokens path/to/theme.ts
59+
# or run it in watch mode:
60+
npx chakra-cli tokens path/to/theme.ts --watch
61+
```
62+
63+
## Adding a custom variant
64+
65+
Let's assume we want to include a custom branded variant. Here's how we can do that:
66+
67+
```js
68+
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'
69+
70+
const thicc = defineStyle({
71+
color: "orange.500",
72+
border: "0px",
73+
bg: "blue.200",
74+
p: "3",
75+
borderRadius: "10",
76+
transition: 'transform 0.15s ease-in-out, background 0.15s ease-in-out',
77+
// let's also provide dark mode alternatives
78+
_dark: {
79+
color: 'orange.400',
80+
bg: "blue.200"
81+
},
82+
_hover: {
83+
bg: "blue.100",
84+
color: "purple.300",
85+
transform: "scale(1, 1)",
86+
_dark: {
87+
bg: "blue.100",
88+
color: "purple.300",
89+
transform: "scale(3, 3)",
90+
}
91+
}
92+
})
93+
94+
// Now we can use the new `thicc` variant
95+
<Divider variant="thicc">...</Divider>
96+
```
97+
98+
## Using a custom color scheme
99+
100+
Let's assume we want to use our own custom color scale based on our brand. We'd need to define the color scale first in the main theme file:
101+
102+
```js
103+
import { extendTheme } from '@chakra-ui/react'
104+
export const theme = extendTheme({
105+
colors: {
106+
brand: {
107+
50: '#f7fafc',
108+
...
109+
500: '#718096',
110+
...
111+
900: '#171923',
112+
}
113+
}
114+
})
115+
```
116+
117+
Then, we can use the custom color scale as the color scheme for the divider:
118+
119+
```jsx
120+
<Divider colorScheme='brand'>...</Divider>
121+
```
122+
123+
## Changing the default properties
124+
125+
```js
126+
import { defineStyleConfig } from '@chakra-ui/react'
127+
128+
export const dividerTheme = defineStyleConfig({
129+
defaultProps: {
130+
size: 'xl',
131+
variant: 'custom',
132+
colorScheme: 'brand',
133+
},
134+
})
135+
136+
// This saves you time, instead of manually setting the size,
137+
// variant and color scheme every time you use a divider:
138+
<Divider size="xl" variant="thicc" colorScheme="brand">...</Divider>
139+
```
140+
141+
## Showcase
142+
143+
import {
144+
App,
145+
Index,
146+
HeadingTheme,
147+
} from 'configs/sandpack-contents/component-theming/heading'
148+
149+
<SandpackEmbed
150+
files={{
151+
'/theme/components/Heading.ts': HeadingTheme,
152+
'/App.tsx': App,
153+
'/index.tsx': {
154+
code: Index,
155+
hidden: true,
156+
},
157+
}}
158+
dependencies={{
159+
'react-icons': '^4.4.0',
160+
}}
161+
/>

0 commit comments

Comments
 (0)