Theming
The Divider
component is a single part component. All of the styling is
applied directly to the chakra.hr
element.
To learn more about styling single part components, visit the Component Style page
Theming properties
The properties that affect the theming of the Divider
component are:
variant
: The visual variant of the divider. Defaults tosolid
.colorScheme
: The color scheme of the divider. No default value.size
: The size of the divider. No default value.
Theming utilities
defineStyle
: a function used to create style objects.defineStyleConfig
: a function used to define the style configuration for a single part component.
import { defineStyle, defineStyleConfig } from "@chakra-ui/react";
Customizing the default theme
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'
const thick = defineStyle({
borderWidth: '5px', // change the width of the border
borderStyle: "solid" // change the style of the border
borderRadius: 10, // set border radius to 10
})
export const dividerTheme = defineStyleConfig({
variants: { thick },
})
After customizing the divider theme, we can import it in our theme file and add it in the components property:
import { extendTheme } from "@chakra-ui/react";
import { dividerTheme } from "./components/divider";
export const theme = extendTheme({
components: { Divider: dividerTheme },
});
This is a crucial step to make sure that any changes that we make to the divider theme are applied.
Adding a custom size
Let's assume we want to include an extra large divider size. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'
const xl = defineStyle({
border: "10px solid",
borderRadius: 'lg',
})
export const dividerTheme = defineStyleConfig({
sizes: { xl },
})
// Now we can use the new `xl` size
<Divider size="xl" />
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. You can learn more about the CLI tool here.
Adding a custom variant
Let's assume we want to include a custom branded variant. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'
const brandPrimary = defineStyle({
borderWidth: '3px',
borderStyle: 'dashed',
borderColor: 'orange.500',
// let's also provide dark mode alternatives
_dark: {
borderColor: 'orange.300',
}
})
export const dividerTheme = defineStyleConfig({
variants: { brandPrimary },
})
// Now we can use the new `brandPrimary` variant
<Divider variant="brandPrimary" />
Changing the default properties
import { defineStyleConfig } from '@chakra-ui/react'
export const dividerTheme = defineStyleConfig({
defaultProps: {
size: 'xl',
variant: 'thick',
colorScheme: 'brand',
},
})
// This saves you time, instead of manually setting the size,
// variant and color scheme every time you use a divider:
<Divider size="xl" variant="thick" colorScheme="brand" />
Showcase
import { ChakraProvider, extendTheme } from "@chakra-ui/react"; import Divider from "./divider"; import { dividerTheme } from "./divider-theme"; const theme = extendTheme({ components: { Divider: dividerTheme, } }); const App = () => { return ( <ChakraProvider theme={theme}> <Divider /> </ChakraProvider> ); } export default App;