> ## 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.

# Menu – Code

> Menus provide the user with an actionable list of options.

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="menu-playground" customHeight="400px" fullWidth>
      ```tsx lines expandable theme={null}
      import { Menu } from "@servicetitan/anvil2";

      function App() {
        return (
          <div style={{ minHeight: "284px" }}>
            <Menu label="Click me!">
              <Menu.Item label="Item 1" />
              <Menu.Item label="Item 2" />
              <Menu.Item label="Item 3" />
            </Menu>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## Common Examples

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

    function ExampleComponent() {
      return (
        <Menu label="Click me!">
          <Menu.Item label="Item 1" />
          <Menu.Item label="Item 2" />
          <Menu.Item label="Item 3" />
        </Menu>
      );
    }
    ```

    ### Creating simple dropdown menus

    The `Menu` and `Menu.Item` components can be used to compose basic dropdown menus.

    <LiveCode showCode example="menu-item" customHeight="400px" fullWidth>
      ```tsx lines expandable theme={null}
      import { Menu, Divider } from "@servicetitan/anvil2";

      function App() {
        return (
          <div style={{ minHeight: "284px" }}>
            <Menu label="Actions">
              <Menu.Item label="Save" onClick={console.log} />
              <Menu.Item label="Save As" onClick={console.log} />
              <Menu.Item label="Rename" onClick={console.log} />
              <Divider spacing="half" />
              <Menu.Item label="Delete" onClick={console.log} />
            </Menu>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Nesting menus in dropdowns

    To create nested dropdown menus, simply add a `Menu` as a child of another `Menu`.

    <LiveCode showCode example="menu-menu" customHeight="400px" fullWidth>
      ```tsx lines expandable theme={null}
      import { Menu } from "@servicetitan/anvil2";

      function App() {
        return (
          <div style={{ minHeight: "284px" }}>
            <Menu label="Actions">
              <Menu label="Move">
                <Menu.Item label="Move to Location A" onClick={console.log} />
                <Menu.Item label="Move to Location B" onClick={console.log} />
                <Menu.Item label="Move to Location C" onClick={console.log} />
              </Menu>
              <Menu label="Export">
                <Menu.Item label="Export as JPG" onClick={console.log} />
                <Menu.Item label="Export as PDF" onClick={console.log} />
                <Menu.Item label="Export as PNG" onClick={console.log} />
              </Menu>
            </Menu>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Creating custom menu triggers

    To use a custom trigger element, use the `trigger` prop of the `Menu`, and omit the `label` prop (and any other prop meant to be passed to the default [Button](/docs/web/components/button/code/) trigger).

    <LiveCode showCode example="menu-trigger" customHeight="400px" fullWidth>
      ```tsx lines expandable theme={null}
      import {
        Menu,
        ButtonCompound,
        Flex,
        Avatar,
        Text,
        Divider,
      } from "@servicetitan/anvil2";

      function App() {
        return (
          <div style={{ minHeight: "284px" }}>
            <Menu
              trigger={(props) => (
                <ButtonCompound {...props}>
                  <Flex gap="4" alignItems="center">
                    <Avatar name="Jon Snow" size="large" />
                    <Text size="large">Jon Snow</Text>
                  </Flex>
                </ButtonCompound>
              )}
            >
              <Menu label="Add to Group">
                <Menu.Item label="Customers" />
                <Menu.Item label="Technicians" />
                <Menu.Item label="Office Employees" />
                <Divider spacing="half" />
                <Menu.Item label="New Group" />
              </Menu>
              <Menu.Item label="Edit Contact" />
              <Divider spacing="half" />
              <Menu.Item label="Delete" />
            </Menu>
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## React Accessibility

    When using a custom trigger, make sure that the trigger:

    * Can receive focus via keyboard when tabbing.
    * Opens the menu when the user presses enter.
    * Closes the menu and returns focus to the trigger when the user presses escape.

    For more guidance on accessible menu interactions, see [keyboard navigation and focus management best practices](/docs/accessibility/custom-components).
  </Tab>

  <Tab title="Default Menu Props">
    ```tsx theme={null}
    <Menu
      label="Click me!"
      open={false}
      disableAutoHeight={false}
      appearance="secondary"
      size="medium"
    >
      <Menu.Item label="Item 1" />
      <Menu.Item label="Item 2" />
    </Menu>
    ```

    ## Default `Menu` Props

    Use the `trigger` prop to determine what element should open the menu. If `trigger` is omitted, a `Button` is used as the trigger by default. In this case, all props available to the [`Button` component](/docs/web/components/button/design) can be used on the `Menu`. In addition, the following props are available:

    <ParamField path="label" type="string">
      Alternatively, `aria-label` or `aria-labelledby` can be used.
    </ParamField>

    <ParamField path="disableAutoHeight" type="boolean">
      This will stop the menu from automatically deciding which way to open based on
      the trigger's location on the screen.
    </ParamField>

    <ParamField path="open" type="boolean" />
  </Tab>

  <Tab title="Custom Trigger Props">
    ```tsx theme={null}
    <Menu
      trigger={(props) => <CustomButton {...props}>Custom Trigger</CustomButton>}
      open={false}
      disableAutoHeight={false}
    >
      <Menu.Item label="Item 1" />
      <Menu.Item label="Item 2" />
    </Menu>
    ```

    ## Custom Trigger `Menu` Props

    When using the `trigger` prop to render a custom trigger, the following props are available:

    <ParamField path="disableAutoHeight" type="boolean">
      This will stop the menu from automatically deciding which way to open based on
      the trigger's location on the screen.
    </ParamField>

    <ParamField path="open" type="boolean" />

    <ParamField path="trigger" type="(TriggerReturnProps) => ReactElement" />
  </Tab>

  <Tab title="TriggerReturnProps">
    ```tsx theme={null}
    <Menu
      trigger={(props) => (
        <button
          {...props}
          aria-controls={props["aria-controls"]}
          aria-expanded={props["aria-expanded"]}
          aria-haspopup={props["aria-haspopup"]}
          onClick={props.onClick}
        >
          Custom Trigger
        </button>
      )}
    >
      <Menu.Item label="Item 1" />
    </Menu>
    ```

    ## `TriggerReturnProps`

    The following props are passed as an object parameter in the custom trigger callback function when using the trigger prop. The function should return a ReactElement which should have the TriggerReturnProps passed to (see [Creating custom menu triggers](/docs/web/components/menu/code#creating-custom-menu-triggers)).

    <ParamField path="aria-controls" type="string">
      Identifies the element (or elements) whose contents or presence are controlled
      by the current element.
    </ParamField>

    <ParamField path="aria-expanded" type="boolean">
      Indicates whether the element, or another grouping element it controls, is
      currently expanded or collapsed.
    </ParamField>

    <ParamField path="aria-haspopup" type={`"boolean" | "dialog" | "menu" | "true" | "false" | "grid" | "listbox" | "tree"`}>
      Indicates the availability and type of interactive popup element, such as menu
      or dialog, that can be triggered by an element.
    </ParamField>

    <ParamField path="data-focus-inside" type="boolean" />

    <ParamField path="data-open" type="boolean" />

    <ParamField path="id" type="string" />

    <ParamField path="onClick" type="() => void" />

    <ParamField path="onFocus" type="() => void" />

    <ParamField path="onKeyDown" type="() => void" />

    <ParamField path="onKeyUp" type="() => void" />

    <ParamField path="onMouseDown" type="() => void" />

    <ParamField path="onPointerDown" type="() => void" />
  </Tab>

  <Tab title="Menu.Item Props">
    ```tsx theme={null}
    <Menu.Item
      label="Menu Item"
      disabled={false}
      icon={EditIcon}
      onClick={() => console.log("Clicked")}
    />
    ```

    ## `Menu.Item` Props

    In addition to the props listed below, the `Menu.Item` component can accept any valid HTML `button` props.

    <ParamField path="label" type="ReactNode" required />

    <ParamField path="disabled" type="boolean" default="false" />

    <ParamField path="icon" type="Svg">
      The SVGR library imports `*.svg` files as React components, so `icon` accepts
      a React component.
    </ParamField>
  </Tab>
</Tabs>
