Input

Input component is a component that is used to get user input in a text field.

SourceTheme Source@chakra-ui/input

The Input 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: addon
  • B: field
  • C: element

Theming properties#

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

  • variant: The visual variant of the input. Defaults to outline.
  • size: The size of the input. 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 { inputAnatomy } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers } from '@chakra-ui/react'
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(inputAnatomy.keys)

Customizing the default theme#

import { inputAnatomy } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers } from '@chakra-ui/react'
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(inputAnatomy.keys)
const baseStyle = definePartsStyle({
// define the part you're going to style
field: {
fontFamily: 'mono', // change the font family
color: 'teal.500', // change the input text color
},
})
export const inputTheme = defineMultiStyleConfig({ baseStyle })

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

import { extendTheme } from '@chakra-ui/react'
import { inputTheme } from './components/input.ts'
export const theme = extendTheme({
components: { Input: inputTheme },
})

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

Adding a custom size#

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

import { inputAnatomy } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(inputAnatomy.keys)
const xl = defineStyle({
fontSize: 'lg',
px: '4',
h: '12',
})
const sizes = {
xl: definePartsStyle({ field: xl, addon: xl }),
}
export const inputTheme = defineMultiStyleConfig({ sizes })
// Now we can use the new `xl` size
<Input 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 pill variant. Here's how we can do that:

import { inputAnatomy } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(inputAnatomy.keys)
const pill = definePartsStyle({
field: {
border: '1px solid',
borderColor: 'gray.200',
background: 'gray.50',
borderRadius: 'full',
// Let's also provide dark mode alternatives
_dark: {
borderColor: 'gray.600',
background: 'gray.800',
},
},
addon: {
border: '1px solid',
borderColor: 'gray.200',
background: 'gray.200',
borderRadius: 'full',
color: 'gray.500',
_dark: {
borderColor: 'gray.600',
background: 'gray.600',
color: 'gray.400',
},
},
})
export const inputTheme = defineMultiStyleConfig({
variants: { pill },
})
// Now we can use the new `pill` variant
<Input variant="pill" ... />

Changing the default properties#

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

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

Showcase#

import { Box, SimpleGrid, GridItem, Icon, IconButton, Input, InputGroup, InputLeftAddon, InputRightElement, useColorMode } from "@chakra-ui/react";
import { FaMoon, FaSun, FaPhone } from "react-icons/fa";

export default function App() {
  const { toggleColorMode, colorMode } = useColorMode();
  return (
    <Box position="relative" h="100vh">
      <SimpleGrid gap={12} p={12} columns={2}>
        <Input placeholder="Themed Outline Input" />
        <Input placeholder="Themed Filled Input" variant="filled" />
        <GridItem colSpan={2}>
          <InputGroup variant="custom" colorScheme="purple">
            <InputLeftAddon>Phone:</InputLeftAddon>
            <Input placeholder="Themed Custom Input" />
            <InputRightElement pointerEvents="none">
              <Icon as={FaPhone} color="green.400" />
            </InputRightElement>
          </InputGroup>
        </GridItem>
      </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