Keyboard Key

The keyboard key component exists to show which key or combination of keys performs a given action.

Source@chakra-ui/react

The Kbd component is a single part component. All of the styling is applied directly to the kbd element.

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

Theming properties#

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

  • variant: The visual variant of the Kbd.
  • colorScheme: The color scheme of the Kbd.
  • size: The size of the Kbd.

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 dashy = defineStyle({
border: '1px dashed', // change the appearance of the border
borderRadius: 0, // remove the border radius
})
export const kbdTheme = defineStyleConfig({
variants: { dashy },
})

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

import { extendTheme } from '@chakra-ui/react'
import { kbdTheme } from './components/kbd'
export const theme = extendTheme({
components: { Kbd: kbdTheme },
})

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

Adding a custom size#

Let's assume we want to include an extra large kbd size. Here's how we can do that:

import { defineStyle, defineStyleConfig } from '@chakra-ui/react'
const xl = defineStyle({
fontSize: 'xl',
})
export const kbdTheme = defineStyleConfig({
sizes: { xl },
})
// Now we can use the new `xl` size
<Kbd size='xl'>option</Kbd> or <Kbd>command</Kbd>

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({
background: 'orange.500',
color: 'white',
fontFamily: 'serif',
fontWeight: 'normal',
// let's also provide dark mode alternatives
_dark: {
background: 'orange.300',
color: 'orange.800',
}
})
export const kbdTheme = defineStyleConfig({
variants: { brandPrimary }
})
// Now we can use the new `brandPrimary` variant
<Kbd variant='brandPrimary'>...</Kbd>

Using a custom color scheme#

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:

import { extendTheme } from '@chakra-ui/react'
export const theme = extendTheme({
colors: {
brand: {
100: '#9520e4',
...
200: '#33006d',
...
800: '#1a0044',
}
}
})

Then, we can use the custom color scale as the color scheme for the kbd:

<Kbd colorPalette='brand'>...</Kbd>

Changing the default properties#

Let's assume we want to change the default size, variant or color scheme of every Kbd in our app. Here's how we can do that:

import { defineStyleConfig } from '@chakra-ui/react'
export const kbdTheme = defineStyleConfig({
defaultProps: {
size: 'xl',
variant: 'outline',
colorScheme: 'brand',
},
})
// This saves you time, instead of manually setting the size,
// variant and color scheme every time you use a kbd:
<Kbd size="xl" variant="outline" colorPalette="brand">...</Kbd>

Showcase#

import {
  Kbd,
  Box, HStack, IconButton, useColorMode
} from "@chakra-ui/react";
import { FaMoon, FaSun } from "react-icons/fa";

export default function App() {
  const { toggleColorMode, colorMode } = useColorMode();

  return (
    <Box position="relative" h="100vh">
      <HStack spacing={8} p={8}>
        <p>Default style: &nbsp; 
          <Kbd>command</Kbd> + <Kbd>shift</Kbd>
        </p>
      </HStack>
      <HStack spacing={8} p={8}>
          <p>Themed solid kbd: &nbsp;
            <Kbd variant='solid'>command</Kbd> + <Kbd variant='solid'>shift</Kbd>
          </p>
          <p>Themed outline kbd: &nbsp;
            <Kbd variant='outline'>command</Kbd> + <Kbd variant='outline'>shift</Kbd>
          </p>
      </HStack>
      <HStack spacing={8} p={8}>
          <p>Themed custom kbd: &nbsp;
            <Kbd variant='custom'>command</Kbd> + <Kbd variant='custom'>shift</Kbd>
          </p>
          <p>XL Kbd: &nbsp;
            <Kbd size='xl'>command</Kbd> + <Kbd size='xl'>shift</Kbd>
          </p>
      </HStack>
      <IconButton
        aria-label="toggle theme"
        rounded="full"
        size="xs"
        position="absolute"
        bottom={4}
        left={4}
        onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />}
      />
    </Box>
  )
}
Edit this page on GitHub

Proudly made inNigeria by Segun Adebayo

Deployed by â–² Vercel