Skip to main content

Theming

The Heading component is a single part component. All of the styling is applied directly to the h2 element.

To learn more about styling single part components, visit the Component Style page

Theming properties

The properties that affect the theming of the Heading component are:

  • size: The size of the divider. Defaults to xl.

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 custom = defineStyle({
color: "yellow.500",
fontFamily: "mono",
fontWeight: "semibold"
// let's also provide dark mode alternatives
_dark: {
color: 'yellow.300',
}
})

After customizing the heading theme, we can import it in our theme file and add it in the components property:

import { extendTheme } from "@chakra-ui/react";
import { headingTheme } from "./components/heading";

export const theme = extendTheme({
components: { Heading: headingTheme },
});

This is a crucial step to make sure that any changes that we make to the heading theme are applied.

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 underline = defineStyle({
color: "orange.500",
borderBottom: "2px",
borderRadius: "10",
fontFamily: "serif"
// let's also provide dark mode alternatives
_dark: {
color: 'orange.400',
},
_hover: {
borderColor: "red.200",
_dark: {
borderColor: "red.300"
}
}
})

// Now we can use the new `underline` variant
<Heading variant="underline">...</Heading>

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.

Changing the default properties

import { defineStyleConfig } from '@chakra-ui/react'

export const headingheme = defineStyleConfig({
defaultProps: {
size: 'xl',
variant: 'custom',
colorScheme: 'brand',
},
})

// This saves you time, instead of manually setting the size,
// variant every time you use a heading:
<Heading size="xl" variant="underline">...</Heading>

Showcase

import { ChakraProvider, extendTheme } from "@chakra-ui/react";
import Heading from "./heading";
import { headingTheme } from "./heading-theme";

const theme = extendTheme({
  components: {
    Heading: headingTheme,
  }
});

const App = () => {
  return (
    <ChakraProvider theme={theme}>
      <Heading />
    </ChakraProvider>
  );
}

export default App;