Select

Select component is a component that allows users pick a value from predefined options.

    SourceTheme Source@chakra-ui/select

The Select component is a multipart component. The styling needs to be applied to each part specifically.

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

Anatomy#

  • A: field
  • B: icon

Theming properties#

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

  • variant: The visual variant of the button. Defaults to outline.
  • size: The size of the button. Defaults to md.

Theming utilities#

  • createMultiStyleConfigHelpers: a function that returns a set of utilities for creating style configs for a multipart component (definePartsStyle and defineMultiStyleConfig).
  • definePartsStyle: a function used to create multipart style objects.
  • defineMultiStyleConfig: a function used to define the style configuration for a multipart component.
import { selectAnatomy } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers } from '@chakra-ui/react'
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(selectAnatomy.keys)

Customizing the default theme#

import { selectAnatomy } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers } from '@chakra-ui/react'
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(selectAnatomy.keys)
const baseStyle = definePartsStyle({
// define the part you're going to style
field: {
background: 'blue.100',
},
icon: {
color: 'blue.400',
},
})
export const selectTheme = defineMultiStyleConfig({ baseStyle })

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

import { extendTheme } from '@chakra-ui/react'
import { selectTheme } from './components/select'
export const theme = extendTheme({
components: { Select: selectTheme },
})

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

Adding a custom size#

Let's assume we want to create a select with a custom size. Here's how we can do that:

import { selectAnatomy } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(selectAnatomy.keys)
const xl = defineStyle({
fontSize: 'lg',
px: '4',
h: '12',
})
const sizes = {
xl: definePartsStyle({ field: xl, icon: xl }),
}
export const selectTheme = defineMultiStyleConfig({ sizes })
// Now we can use the new `xl` size
<Select 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 create a custom variant of the select component. Here's how to do that:

import { selectAnatomy } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(selectAnatomy.keys)
const brandPrimary = definePartsStyle({
field: {
background: "purple.100",
border: "1px dashed",
borderColor: "purple.200",
borderRadius: "full"
},
icon: {
color: "purple.400"
}
})
export const selectTheme = defineMultiStyleConfig({
variants: { brandPrimary },
})
// Now we can use the new `brandPrimary` variant
<Select variant="brandPrimary" ... />

Changing the default properties#

Let's assume we want to change the default size and variant of every select in our application. Here's how we can do that:

import { selectAnatomy } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'
const { defineMultiStyleConfig } = createMultiStyleConfigHelpers(
selectAnatomy.keys,
)
export const selectTheme = defineMultiStyleConfig({
defaultProps: {
size: 'xl',
variant: 'brandPrimary',
},
})
// This saves you time, instead of manually setting the size and variant every time you use a select:
<Select size="xl" variant="pill" ... />

Showcase#

import {
  Box,
  IconButton,
  Select,
  SimpleGrid,
  useColorMode,
  Text
} from "@chakra-ui/react";
import { FaMoon, FaSun } from "react-icons/fa";
import { MdArrowDropDown } from "react-icons/md";

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

  return (
    <Box position="relative" h="100vh">
      <SimpleGrid gap={12} p={12} columns={2}>
        <Select variant="outline" placeholder="Themed outline select">
          <option value="option1">Option 1</option>
          <option value="option2">Option 2</option>
          <option value="option3">Option 3</option>
        </Select>
        <Select icon={<MdArrowDropDown />} variant="filled" placeholder="Themed filled select">
          <option value="option1">Option 1</option>
          <option value="option2">Option 2</option>
          <option value="option3">Option 3</option>
        </Select>
        <Select variant="flushed" placeholder="Themed flushed select">
          <option value="option1">Option 1</option>
          <option value="option2">Option 2</option>
          <option value="option3">Option 3</option>
        </Select>
        <Select variant="brandPrimary" placeholder="Custom variant select">
          <option value="option1">Option 1</option>
          <option value="option2">Option 2</option>
          <option value="option3">Option 3</option>
        </Select>
      </SimpleGrid>
      <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