Popover

Popover is a non-modal dialog that floats around a trigger.

    SourceTheme Source@chakra-ui/popover

The Popover 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: content
  • B: header
  • C: body
  • D: footer
  • E: popper
  • F: arrow
  • G: closeButton

Theming properties#

The Popover doesn't have any default properties that affect the theming, however it is possible to create the following properties:

  • size
  • variant
  • colorScheme

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 { popoverAnatomy as parts } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers } from '@chakra-ui/react'
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(parts.keys)

Customizing the default theme#

import { popoverAnatomy as parts } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers } from '@chakra-ui/react'
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(parts.keys)
const baseStyle = definePartsStyle({
// define the part you're going to style
body: {
bg: 'gray.800', // change the background of the body to gray.800
},
content: {
padding: 3, // change the padding of the content
},
})
export const popoverTheme = defineMultiStyleConfig({ baseStyle })

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

import { extendTheme } from '@chakra-ui/react'
import { popoverTheme } from './theme/components/popover.ts'
export const theme = extendTheme({
components: { Popover: popoverTheme },
})

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

Adding a custom size#

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

import { popoverAnatomy as parts } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers } from '@chakra-ui/react'
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(parts.keys)
const sizes = {
xl: definePartsStyle({
header: defineStyle({
padding: 14
}),
content: defineStyle({
fontSize: "2xl",
marginLeft: 6
})
}),
}
export const popoverTheme = defineMultiStyleConfig({ sizes })
// Now we can use the new `xl` size
<Popover size="xl">...</Popover>

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 variant. Here's how we can do that:

import { popoverAnatomy as parts } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(parts.keys)
const custom = definePartsStyle({
content: defineStyle({
padding: 7
bg: "gray.700"
}),
footer: defineStyle({
fontSize: "xl"
})
})
export const popoverTheme = defineMultiStyleConfig({
variants: { custom },
})
// Now we can use the new `custom` variant
<Popover variant="custom">...</Popover>

Creating the default properties#

Let's assume we want to create the default property for the Popover. Here's how we can do that:

import { popoverAnatomy as parts } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'
const { defineMultiStyleConfig } =
createMultiStyleConfigHelpers(parts.keys)
//Let's assume we need each popover to have red content
const defaultVariant = definePartsStyle({
content: {
bg: "red.200"
}
})
export const popoverTheme = defineMultiStyleConfig({
variant: {default: defaultVariant}
defaultProps: {
variant: default
},
})
//Now by default, each Popover will have a 'default' variant with red content.

Showcase#

import {
  Popover,
  PopoverTrigger,
  PopoverContent,
  PopoverHeader,
  PopoverBody,
  PopoverArrow,
  Button,
  Flex,
  Box,
  IconButton,
  useColorMode,
  Text,
  HStack,
  Avatar,
  AvatarBadge,
  VStack,
  StackDivider,
} from "@chakra-ui/react";
import { FaMoon, FaSun } from "react-icons/fa";

const CustomButton = ({ label, notifications, colorMode }) => (
  <Box
    h="40px"
    cursor="pointer"
    p={2}
    _hover={{
      borderBottomWidth: "2px",
      borderBottomColor: colorMode === "light" ? "blue.600" : "blue.200",
    }}
  >
    <HStack>
      <Box>
        <Text fontSize={"sm"}>{label}</Text>
      </Box>
      {notifications && (
        <Box
          bg={colorMode === "light" ? "blue.50" : "blue.300"}
          pl={1.5}
          pr={1.5}
          borderRadius="full"
        >
          <Text fontSize={"xs"}>{notifications}</Text>
        </Box>
      )}
    </HStack>
  </Box>
);

const Notification = ({
  name,
  src,
  action,
  time,
  hasBadge,
  colorMode,
  ...rest
}) => (
  <Box
    bg={
      hasBadge
        ? colorMode === "light"
          ? "gray.100"
          : "gray.600"
        : "transparent"
    }
    {...rest}
  >
    <Flex gap={3} p={2}>
      <Box>
        <Avatar size="sm" name={name} src={src}>
          {hasBadge && (
            <AvatarBadge
              placement="top-start"
              bg={colorMode === "light" ? "blue.600" : "blue.300"}
              borderWidth="2px"
              boxSize="12px"
            />
          )}
        </Avatar>
      </Box>

      <VStack>
        <Box>
          <Text fontSize={"sm"}>
            <b>{name}</b> {action}
          </Text>
        </Box>
        <Box alignSelf={"flex-start"} mt="0 !important">
          <Text fontSize={"xs"} fontWeight="lighter">
            {time}
          </Text>
        </Box>
      </VStack>
    </Flex>
  </Box>
);

const PopoverExample = ({ name, colorMode, ...rest }) => (
  <Box>
    <Popover size="md" {...rest}>
      <PopoverTrigger>
        <Button variant="outline">{name}</Button>
      </PopoverTrigger>
      <PopoverContent>
        <PopoverArrow />
        <PopoverHeader>
          <Flex justify="space-between" align="center" p={2}>
            <Box>
              <Text as="b">Notifications</Text>
            </Box>
            <Box>
              <Button size="xs" colorScheme={"blue"} variant="ghost">
                Mark all as read
              </Button>
            </Box>
          </Flex>
          <Flex gap={4}>
            <CustomButton
              label={"All"}
              notifications={2}
              colorMode={colorMode}
            />
            <CustomButton label={"Following"} colorMode={colorMode} />
            <CustomButton label={"Archive"} colorMode={colorMode} />
          </Flex>
        </PopoverHeader>
        <PopoverBody>
          <VStack
            justify="flex-start"
            divider={<StackDivider bg="gray.600" m={"0 !important"} />}
            align="stretch"
          >
            <Notification
              name="Segun Adebayo"
              action="created Chakra UI"
              time="10 min ago"
              src="https://bit.ly/sage-adebayo"
              hasBadge={true}
              colorMode={colorMode}
            />
            <Notification
              name="Dan Abramov"
              action="created Redux"
              time="1 hour ago"
              src="https://bit.ly/dan-abramov"
              hasBadge={true}
              colorMode={colorMode}
            />
            <Notification
              name="Gleb Koshcheev"
              action="added theme docs"
              time="12:10pm"
              colorMode={colorMode}
            />
            <Notification
              name="Jacob Jones"
              action="deleted profile image"
              time="11:30am"
              colorMode={colorMode}
            />
          </VStack>
        </PopoverBody>
      </PopoverContent>
    </Popover>
  </Box>
);

export default function App() {
  const { toggleColorMode, colorMode } = useColorMode();
  return (
    <Box pos="relative">
      <Flex
        direction="column"
        gap={3}
        align="center"
        p={8}
        alignItems={"center"}
        justify="center"
      >
        <PopoverExample name={"Themed Popover"} colorMode={colorMode} />

        <PopoverExample
          name={"Themed XL Popover"}
          size="xl"
          colorMode={colorMode}
        />
        <PopoverExample
          name={"Themed XL Rounded Popover"}
          size="xl"
          variant="rounded"
          colorMode={colorMode}
        />
      </Flex>
      <Box>
        <IconButton
          aria-label="toggle theme"
          rounded="full"
          size="xs"
          position="fixed"
          bottom={4}
          left={4}
          onClick={toggleColorMode}
          icon={colorMode === "dark" ? <FaSun /> : <FaMoon />}
        />
      </Box>
    </Box>
  );
}
Edit this page on GitHub

Proudly made inNigeria by Segun Adebayo

Deployed by â–² Vercel