> ## Documentation Index
> Fetch the complete documentation index at: https://anvil.servicetitan.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Popover – Code

> Popovers are floating containers that open on demand.

export const LiveCode = ({children, customHeight, clickToLoad, example, fullWidth, fullHeight, hideCodeInLiveCode, screenshot, screenshotOnly, showCode: showCodeProp}) => {
  const SCREENSHOTS_BASE = "https://servicetitan.github.io/anvil2-docs-live-code/screenshots";
  const STACKBLITZ_BASE = "https://stackblitz.com/github/servicetitan/anvil2-docs-live-code/tree/main/examples";
  const [showCodeBlock, setShowCodeBlock] = useState(showCodeProp ?? false);
  const [isLocalOverride, setIsLocalOverride] = useState(false);
  useEffect(() => {
    const examplePath = `/images/live-code-screenshots-tmp/${example}.png`;
    fetch(examplePath, {
      method: "HEAD"
    }).then(r => {
      if (r.ok) setIsLocalOverride(true);
    }).catch(() => {});
  }, [example]);
  const screenshotBase = isLocalOverride ? "/images/live-code-screenshots-tmp" : SCREENSHOTS_BASE;
  if (screenshotOnly) {
    return <Frame className="flex flex-col">
        <div className="flex dark:hidden" style={{
      justifyContent: "center",
      alignItems: "center",
      width: fullWidth ? "100%" : "50%",
      minHeight: fullHeight ? "284px" : undefined,
      background: "#FFFFFF"
    }}>
          <img srcset={`${screenshotBase}/${example}.png, ${screenshotBase}/${example}-2x.png 2x`} src={`${screenshotBase}/${example}.png`} alt={example} noZoom />
        </div>
        <div className="hidden dark:flex" style={{
      justifyContent: "center",
      alignItems: "center",
      width: fullWidth ? "100%" : "50%",
      minHeight: fullHeight ? "284px" : undefined,
      background: "#141414"
    }}>
          <img srcset={`${screenshotBase}/${example}-dark.png, ${screenshotBase}/${example}-dark-2x.png 2x`} src={`${screenshotBase}/${example}-dark.png`} alt={example} noZoom />
        </div>
      </Frame>;
  }
  if (screenshot) {
    return <Frame className="flex flex-col -mb-2">
        <div className="flex dark:hidden bg-white dark:bg-codeblock border border-gray-950/10 dark:border-white/10 dark:twoslash-dark rounded-2xl overflow-hidden" style={{
      justifyContent: "center",
      alignItems: "center",
      width: fullWidth ? "100%" : "50%",
      minHeight: fullHeight ? "284px" : undefined
    }}>
          <img srcset={`${screenshotBase}/${example}.png, ${screenshotBase}/${example}-2x.png 2x`} src={`${screenshotBase}/${example}.png`} alt={example} noZoom />
        </div>

        <div className="hidden dark:flex bg-white dark:bg-codeblock border border-gray-950/10 dark:border-white/10 dark:twoslash-dark rounded-2xl overflow-hidden" style={{
      background: "#141414",
      justifyContent: "center",
      alignItems: "center",
      width: fullWidth ? "100%" : "50%",
      minHeight: fullHeight ? "284px" : undefined
    }}>
          <img srcset={`${screenshotBase}/${example}-dark.png, ${screenshotBase}/${example}-dark-2x.png 2x`} src={`${screenshotBase}/${example}-dark.png`} alt={example} noZoom />
        </div>

        <div className="flex justify-end items-center text-xs py-2 px-1 gap-4">
          {!showCodeProp ? <button className="inline-flex justify-end items-center text-gray-700 dark:text-gray-50 hover:text-blue-500 dark:hover:text-blue-300 transition-colors group self-end gap-1 cursor-pointer" onClick={() => setShowCodeBlock(!showCodeBlock)} style={{
      appearance: "none"
    }}>
              <Icon icon="code" size="12px" className="group-hover:bg-blue-500 dark:group-hover:bg-blue-300" />
              <span>{showCodeBlock ? "Hide code" : "Show code"}</span>
            </button> : null}

          <a className="inline-flex justify-end items-center hover:text-blue-500 dark:hover:text-blue-300 transition-colors group self-end gap-1" href={`${STACKBLITZ_BASE}/${example}?file=src/App.tsx`} target="_blank" rel="noreferrer">
            <Icon icon="bolt" size="12px" className="group-hover:bg-blue-500 dark:group-hover:bg-blue-300" />
            <span>StackBlitz demo</span>
          </a>
        </div>

        <div className="grid transition-[grid-template-rows] duration-300 ease-in-out overflow-auto overflow-y-hidden overflow-x-auto" style={showCodeBlock ? {
      gridTemplateRows: "1fr"
    } : {
      gridTemplateRows: "0fr"
    }}>
          <div style={{
      minHeight: 0,
      overflowX: "auto",
      overflowY: "hidden",
      marginBlockStart: "-1.25rem",
      marginBlockEnd: "-1.5rem"
    }}>
            {children}
          </div>
        </div>
      </Frame>;
  } else {
    return <div style={{
      display: "flex",
      width: fullWidth ? "100%" : "50%",
      minHeight: customHeight ? customHeight : "316px",
      resize: "vertical",
      overflow: "auto"
    }}>
        <iframe title={example} style={{
      flex: 1,
      width: fullWidth ? "100%" : "50%",
      minHeight: customHeight ? customHeight : "316px"
    }} src={`${STACKBLITZ_BASE}/${example}?embed=1&hideNavigation=1&hideExplorer=1&terminalHeight=0&file=src/App.tsx${clickToLoad ? "&ctl=1" : ""}${hideCodeInLiveCode ? "&view=preview" : ""}`} allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" />
      </div>;
  }
};

<Tabs>
  <Tab title="Implementation">
    <LiveCode showCode example="popover-playground" fullWidth screenshot>
      ```tsx lines expandable theme={null}
      import { Popover, Flex } from "@servicetitan/anvil2";

      function App() {
        return (
          <div>
            <Flex
              justifyContent="center"
              style={{ paddingBlockStart: "4rem", paddingInline: "7rem" }}
            >
              <Popover placement="top" defaultOpen>
                <Popover.Button>Toggle Popover</Popover.Button>
                <Popover.Content>
                  The <code>Popover.Content</code> accepts a <code>ReactNode</code>.
                </Popover.Content>
              </Popover>
            </Flex>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## Common Examples

    ```tsx theme={null}
    import { Popover } from "@servicetitan/anvil2";

    const ExampleComponent = () => {
      return (
        <Popover>
          <Popover.Button>Click me!</Popover.Button>
          <Popover.Content>This is the popover content.</Popover.Content>
        </Popover>
      );
    };
    ```

    ### Popover basics

    To build a popover, three components are required:

    * `Popover`: the configurable parent component
    * A way to control the open state of the popover. One of:
      * `Popover.Button`: an [Anvil2 Button](/docs/web/components/button/code)
      * `Popover.Trigger`: used to render a custom trigger component
    * `Popover.Content`: the content rendered within the popover

    ```tsx theme={null}
    <Popover>
      <Popover.Button>Trigger Button</Popover.Button>
      <Popover.Content>Popover content. Accepts a ReactNode.</Popover.Content>
    </Popover>
    ```

    ### Popover placement

    Use the `placement` prop on the `Popover` to change the default positioning. The popover will reposition itself if there is not enough space in the default placement.

    ```tsx theme={null}
    <Popover placement="top">...</Popover>
    ```

    <LiveCode showCode example="popover-placement" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Popover, Flex, Grid } from "@servicetitan/anvil2";

      function App() {
        const btnShared = { style: { width: "100%" }, size: "small" as const };

        return (
          <div>
            <Flex justifyContent="center" style={{ margin: "6rem 8rem" }}>
              <Grid templateColumns="repeat(3, 7rem)" gap="2">
                <Popover placement="top-start" defaultOpen>
                  <Popover.Button {...btnShared}>top-start</Popover.Button>
                  <Popover.Content>Content</Popover.Content>
                </Popover>
                <Popover placement="top" defaultOpen>
                  <Popover.Button {...btnShared}>top</Popover.Button>
                  <Popover.Content>Content</Popover.Content>
                </Popover>
                <Popover placement="top-end" defaultOpen>
                  <Popover.Button {...btnShared}>top-end</Popover.Button>
                  <Popover.Content>Content</Popover.Content>
                </Popover>
                <Popover placement="left-start" defaultOpen>
                  <Popover.Button {...btnShared}>left-start</Popover.Button>
                  <Popover.Content>Content</Popover.Content>
                </Popover>
                <div />
                <Popover placement="right-start" defaultOpen>
                  <Popover.Button {...btnShared}>right-start</Popover.Button>
                  <Popover.Content>Content</Popover.Content>
                </Popover>
                <Popover placement="left" defaultOpen>
                  <Popover.Button {...btnShared}>left</Popover.Button>
                  <Popover.Content>Content</Popover.Content>
                </Popover>
                <div />
                <Popover placement="right" defaultOpen>
                  <Popover.Button {...btnShared}>right</Popover.Button>
                  <Popover.Content>Content</Popover.Content>
                </Popover>
                <Popover placement="left-end" defaultOpen>
                  <Popover.Button {...btnShared}>left-end</Popover.Button>
                  <Popover.Content>Content</Popover.Content>
                </Popover>
                <div />
                <Popover placement="right-end" defaultOpen>
                  <Popover.Button {...btnShared}>right-end</Popover.Button>
                  <Popover.Content>Content</Popover.Content>
                </Popover>
                <Popover placement="bottom-start" defaultOpen>
                  <Popover.Button {...btnShared}>bottom-start</Popover.Button>
                  <Popover.Content>Content</Popover.Content>
                </Popover>
                <Popover placement="bottom" defaultOpen>
                  <Popover.Button {...btnShared}>bottom</Popover.Button>
                  <Popover.Content>Content</Popover.Content>
                </Popover>
                <Popover placement="bottom-end" defaultOpen>
                  <Popover.Button {...btnShared}>bottom-end</Popover.Button>
                  <Popover.Content>Content</Popover.Content>
                </Popover>
              </Grid>
            </Flex>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Controlling popovers

    Popovers can be controlled or uncontrolled (default). Control the popover using the `open` prop.

    #### Uncontrolled (default)

    <LiveCode showCode example="popover-uncontrolled" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Popover, Flex } from "@servicetitan/anvil2";

      function App() {
        return (
          <div>
            <Flex justifyContent="center" style={{ paddingTop: "4rem" }}>
              <Popover placement="top" defaultOpen>
                <Popover.Button>Toggle Popover</Popover.Button>
                <Popover.Content>Content</Popover.Content>
              </Popover>
            </Flex>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    #### Controlled

    <LiveCode showCode example="popover-controlled" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Popover, Flex } from "@servicetitan/anvil2";
      import { useState } from "react";

      function App() {
        const [isOpen, setIsOpen] = useState(true);

        return (
          <div>
            <Flex justifyContent="center" style={{ paddingTop: "4rem" }}>
              <Popover placement="top" open={isOpen}>
                <Popover.Button onClick={() => setIsOpen((open) => !open)}>
                  Toggle Popover
                </Popover.Button>
                <Popover.Content>Content</Popover.Content>
              </Popover>
            </Flex>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Using a custom trigger

    To use something other than an [Anvil2 Button](/docs/web/components/button/code) to trigger the popover, use the `Popover.Trigger` component. The `Popover.Trigger` passes a set of props to the `children` that should be passed to an interactive HTML element, such as a `button` or `input`.

    ```tsx theme={null}
    <Popover>
      <Popover.Trigger>
        {(props) => <button {...props}>Open Popover</button>}
      </Popover.Trigger>
      <Popover.Content>Content</Popover.Content>
    </Popover>
    ```

    ### Popover close button

    The `Popover.Close` button can be used to close the popover from within the popover content.

    <LiveCode showCode example="popover-close" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Popover, Flex, Text } from "@servicetitan/anvil2";
      import Close from "@servicetitan/anvil2/assets/icons/material/round/close.svg";

      function App() {
        return (
          <div>
            <Flex justifyContent="center" style={{ paddingTop: "5rem" }}>
              <Popover placement="top" defaultOpen>
                <Popover.Button>Toggle Popover</Popover.Button>
                <Popover.Content>
                  <Flex alignItems="center" gap="2">
                    <Text>Press the close button:</Text>
                    <Popover.Close icon={Close} />
                  </Flex>
                </Popover.Content>
              </Popover>
            </Flex>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    On controlled popovers, the `onClick` prop should be used to update the open state:

    ```tsx theme={null}
    <Popover.Close onClick={() => setIsOpenState(false)} icon={Close} />
    ```

    ### Disabling default closing behavior

    By default, an [uncontrolled popover](#controlling-popovers) closes if either the user clicks outside of the popover, or the user presses the "Escape" key. Both of these behaviors can be disabled with props:

    #### Disable close on outside click

    <LiveCode showCode example="popover-disablecloseonclickoutside" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Popover, Flex } from "@servicetitan/anvil2";

      function App() {
        return (
          <div>
            <Flex
              justifyContent="center"
              style={{ paddingBlockStart: "4rem", paddingInline: "7rem" }}
            >
              <Popover disableCloseOnClickOutside placement="top" defaultOpen>
                <Popover.Button>Toggle Popover</Popover.Button>
                <Popover.Content>
                  This popover will not close if you click outside.
                </Popover.Content>
              </Popover>
            </Flex>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    #### Disable close on "Escape" press

    <LiveCode showCode example="popover-disablecloseonescape" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Popover, Flex } from "@servicetitan/anvil2";

      function App() {
        return (
          <div>
            <Flex
              justifyContent="center"
              style={{ paddingBlockStart: "4rem", paddingInline: "9rem" }}
            >
              <Popover disableCloseOnEscape placement="top" defaultOpen>
                <Popover.Button>Toggle Popover</Popover.Button>
                <Popover.Content>
                  This popover will not close if you press the &quot;escape&quot; key.
                </Popover.Content>
              </Popover>
            </Flex>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Enabling trapped focus

    By default, the popover content does not trap focus so that users can navigate the content with a keyboard without naturally. To enable this behavior, set the `modal` prop of the `Popover` to `true`.

    #### Default without trapping

    <LiveCode showCode example="popover-modal-false" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Popover, Flex, Text, Button } from "@servicetitan/anvil2";

      function App() {
        return (
          <div>
            <Flex
              justifyContent="center"
              style={{ paddingBlockStart: "9rem", paddingInline: "7rem" }}
            >
              <Popover placement="top" defaultOpen>
                <Popover.Button>Toggle Popover</Popover.Button>
                <Popover.Content>
                  <Flex gap="2" direction="column" style={{ maxWidth: "20rem" }}>
                    <Text>
                      Pressing &quot;tab&quot; will trap focus between this button and
                      the popover trigger button.
                    </Text>
                    <Button>Example Button</Button>
                  </Flex>
                </Popover.Content>
              </Popover>
            </Flex>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    #### Trapped focus

    <LiveCode showCode example="popover-modal" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Popover, Flex, Text, Button } from "@servicetitan/anvil2";

      function App() {
        return (
          <div>
            <Flex
              gap="4"
              justifyContent="center"
              style={{ paddingBlockStart: "9rem", paddingInline: "7rem" }}
            >
              <Popover placement="top" modal defaultOpen>
                <Popover.Button>Toggle Popover</Popover.Button>
                <Popover.Content>
                  <Flex gap="2" direction="column" style={{ maxWidth: "20rem" }}>
                    <Text>
                      Pressing &quot;tab&quot; will focus on this button, then go to
                      the button outside of this popover.
                    </Text>
                    <Button>Example Button</Button>
                  </Flex>
                </Popover.Content>
              </Popover>
              <Button>Example Button</Button>
            </Flex>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Using with Anvil1

    `Popover` from version `1.23.0` uses HTML popover API which utilizes `top-layer`. This feature puts what's inside the `top-layer` to be on the top of entire the HTML. Because of this, there are cases where it does not work well with Anvil1's portaled components.

    * While having the `Popover` open, if a portaled Anvil1 component is triggered to open from within, the `Popover` will be on top always. This is something we can potentially adjust in Anvil1 and we have a spike ticket internally for it. If you encounter this issue, please reach out to us.
    * While having the `Popover` open, if a `Drawer` or `Dialog` from Anvil1 is triggered to open, not within the `Popover`, the `Popover` will be on top of the `Drawer` or `Dialog`.

    ## React Accessibility

    Most of the accessibility considerations for managing `aria` props and keyboard focus are handled internally. There are some things to keep in mind while working with popovers:

    * Always test keyboard navigation for interactive elements within popovers. The user should be able to easily access the elements in a logical order.
    * If the popover includes a form, be sure to follow accessible best practices for form building.
    * When building a custom trigger using `Popover.Trigger`, be sure to pass the props to an interactive element, as certain `aria` props are included to properly connect the trigger to the content.

    For more guidance on creating accessible components, see [building accessible custom components best practices](/docs/accessibility/custom-components).
  </Tab>

  <Tab title="Popover Props">
    ```tsx theme={null}
    <Popover
      defaultOpen={false}
      disableAutoUpdate={false}
      disableCaret={false}
      disableCloseOnEscape={false}
      disableCloseOnClickOutside={false}
      modal={true}
      noPadding={false}
      open={false}
      openOnHover={false}
      placement="bottom"
      onClose={() => {}}
      onClickOutside={() => {}}
      onOpenAnimationStart={() => {}}
      onOpenAnimationComplete={() => {}}
      onCloseAnimationStart={() => {}}
      onCloseAnimationComplete={() => {}}
    >
      <Popover.Button>Trigger</Popover.Button>
      <Popover.Content>Content</Popover.Content>
    </Popover>
    ```

    ## `Popover` Props

    <ParamField path="children" type="ReactNode">
      Should include `Popover.Button` or `Popover.Trigger` and `Popover.Content`.
    </ParamField>

    <ParamField path="defaultOpen" type="boolean">
      Controls the default open state on uncontrolled popovers.
    </ParamField>

    <ParamField path="disableAutoUpdate" type="boolean">
      Disables all focus management entirely. Useful to delay focus management until
      after a transition completes or some other conditional state.
    </ParamField>

    <ParamField path="disableCaret" type="boolean">
      Removes the caret between the trigger and content.
    </ParamField>

    <ParamField path="disableCloseOnEscape" type="boolean">
      See [Disabling default closing
      behavior](/docs/web/components/popover/code#disabling-default-closing-behavior)
    </ParamField>

    <ParamField path="disableCloseOnClickOutside" type="boolean">
      See [Disabling default closing
      behavior](/docs/web/components/popover/code#disabling-default-closing-behavior)
    </ParamField>

    <ParamField path="modal" type="boolean" default="true">
      Traps focus within the popover content.
    </ParamField>

    <ParamField path="noPadding" type="boolean">
      Removes padding from popover content.
    </ParamField>

    <ParamField path="onClickOutside" type="() => void">
      Callback when clicking outside the popover.
    </ParamField>

    <ParamField path="onClose" type="() => void">
      Called when the popover is closed, except after mouse exits when `openOnHover`
      is `true`.
    </ParamField>

    <ParamField path="onCloseAnimationComplete" type="() => void">
      Callback when closing animation completes.
    </ParamField>

    <ParamField path="onCloseAnimationStart" type="() => void">
      Callback when closing animation starts.
    </ParamField>

    <ParamField path="onOpenAnimationComplete" type="() => void">
      Callback when opening animation completes.
    </ParamField>

    <ParamField path="onOpenAnimationStart" type="() => void">
      Callback when opening animation starts.
    </ParamField>

    <ParamField path="open" type="boolean">
      Use to control open state of the popover.
    </ParamField>

    <ParamField path="openOnHover" type="boolean">
      Opens the popover on hover over the trigger. In most cases, use a
      [Tooltip](/docs/web/components/tooltip/code) instead.
    </ParamField>

    <ParamField path="placement" type={`"bottom" | "bottom-end" | "bottom-start" | "left" | "left-end" | "left-start" | "right" | "right-end" | "right-start" | "top" | "top-end" | "top-start"`} default="bottom" />
  </Tab>

  <Tab title="Popover.Button Props">
    ```tsx theme={null}
    <Popover.Button appearance="primary" size="medium" onClick={() => {}}>
      Trigger Button
    </Popover.Button>
    ```

    ## `Popover.Button` Props

    The `Popover.Button` component accepts any valid [Anvil2 `Button` component props](/docs/web/components/button/code#prop-table), with one difference — the `children` prop is required in `Popover.Button`.
  </Tab>

  <Tab title="Popover.Content Props">
    ```tsx theme={null}
    <Popover.Content className="custom-class" id="popover-content">
      Popover content
    </Popover.Content>
    ```

    ## `Popover.Content` Props

    The `Popover.Content` component can accept any valid HTML `div` props.
  </Tab>

  <Tab title="Popover.Trigger Props">
    ```tsx theme={null}
    <Popover.Trigger>
      {(data) => (
        <button {...data} aria-expanded={data["aria-expanded"]} onClick={data.onClick}>
          Custom Trigger
        </button>
      )}
    </Popover.Trigger>
    ```

    ## `Popover.Trigger` Props

    <ParamField path="children" type="(data) => ReactElement">
      The `data` parameter includes `"aria-expanded"`, a `ref` for the trigger
      component, and some additional accessibility fields.
    </ParamField>
  </Tab>
</Tabs>
