|
2 | 2 | id: heading
|
3 | 3 | scope: theming
|
4 | 4 | ---
|
| 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