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

# Side Nav – Code

> Side Navs are used to display a list of links to move between pages.

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="sidenav-playground" fullWidth screenshot>
      ```tsx lines expandable theme={null}
      import { SideNav } from "@servicetitan/anvil2";

      function App() {
        return (
          <SideNav aria-label="Page Name">
            <SideNav.Link id="home">Home</SideNav.Link>
            <SideNav.Link id="dashboard">Dashboard</SideNav.Link>
            <SideNav.Group label="Reports">
              <SideNav.Link id="reports-all">All</SideNav.Link>
              <SideNav.Link id="reports-custom">Custom</SideNav.Link>
            </SideNav.Group>
            <SideNav.Collapsible label="Settings">
              <SideNav.Link id="settings-global">Global Settings</SideNav.Link>
              <SideNav.Link id="settings-user" active>
                User Preferences
              </SideNav.Link>
            </SideNav.Collapsible>
          </SideNav>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## Common Examples

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

    function ExampleComponent() {
      return (
        <SideNav>
          <SideNav.Item id="item-1">Item 1</SideNav.Item>
          <SideNav.Item id="item-2">Item 2</SideNav.Item>
        </SideNav>
      );
    }
    ```

    ### Grouping side nav items

    Side nav items can be grouped in two different ways, using `SideNav.Group` or `SideNav.Collapsible`.

    <LiveCode showCode example="sidenav-group" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { SideNav, type SideNavLinkProps } from "@servicetitan/anvil2";
      import { useState, type MouseEvent } from "react";

      function App() {
        const [active, setActive] = useState("2");
        const handleOnClick = (
          _e: MouseEvent<HTMLAnchorElement>,
          id: SideNavLinkProps["id"],
        ) => {
          setActive(id);
        };

        return (
          <SideNav aria-label="Side Nav Title">
            <SideNav.Group label="Group Item 1">
              <SideNav.Link id="1" onClick={handleOnClick} active={active === "1"}>
                Side Nav Item 1
              </SideNav.Link>
              <SideNav.Link id="2" onClick={handleOnClick} active={active === "2"}>
                Side Nav Item 2
              </SideNav.Link>
              <SideNav.Link id="3" onClick={handleOnClick} active={active === "3"}>
                Side Nav Item 3
              </SideNav.Link>
            </SideNav.Group>
            <SideNav.Group label="Group Item 2">
              <SideNav.Link id="4" onClick={handleOnClick} active={active === "4"}>
                Side Nav Item 4
              </SideNav.Link>
              <SideNav.Link id="5" onClick={handleOnClick} active={active === "5"}>
                Side Nav Item 5
              </SideNav.Link>
              <SideNav.Link id="6" onClick={handleOnClick} active={active === "6"}>
                Side Nav Item 6
              </SideNav.Link>
            </SideNav.Group>
          </SideNav>
        );
      }

      export default App;
      ```
    </LiveCode>

    #### Using SideNav.Group

    Use the `SideNav.Group` component to group multiple links.

    <LiveCode showCode example="sidenav-collapsible" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { SideNav, type SideNavLinkProps } from "@servicetitan/anvil2";
      import { useState, type MouseEvent } from "react";

      function App() {
        const [active, setActive] = useState("2");
        const handleOnClick = (
          _e: MouseEvent<HTMLAnchorElement>,
          id: SideNavLinkProps["id"],
        ) => {
          setActive(id);
        };

        return (
          <SideNav aria-label="Side Nav Title">
            <SideNav.Collapsible defaultExpanded label="Parent Item 1">
              <SideNav.Link id="1" onClick={handleOnClick} active={active === "1"}>
                Child Item 1
              </SideNav.Link>
              <SideNav.Link id="2" onClick={handleOnClick} active={active === "2"}>
                Child Item 2
              </SideNav.Link>
              <SideNav.Link id="3" onClick={handleOnClick} active={active === "3"}>
                Child Item 3
              </SideNav.Link>
            </SideNav.Collapsible>

            <SideNav.Collapsible label="Parent Item 2">
              <SideNav.Link id="4" onClick={handleOnClick} active={active === "4"}>
                Child Item 1
              </SideNav.Link>
              <SideNav.Link id="5" onClick={handleOnClick} active={active === "5"}>
                Child Item 2
              </SideNav.Link>
              <SideNav.Link id="6" onClick={handleOnClick} active={active === "6"}>
                Child Item 3
              </SideNav.Link>
            </SideNav.Collapsible>

            <SideNav.Collapsible defaultExpanded label="Parent Item 3">
              <SideNav.Link id="7" onClick={handleOnClick} active={active === "7"}>
                Child Item 1
              </SideNav.Link>
              <SideNav.Link id="8" onClick={handleOnClick} active={active === "8"}>
                Child Item 2
              </SideNav.Link>
              <SideNav.Link id="9" onClick={handleOnClick} active={active === "9"}>
                Child Item 3
              </SideNav.Link>
            </SideNav.Collapsible>
          </SideNav>
        );
      }

      export default App;
      ```
    </LiveCode>

    #### Using SideNav.Collapsible

    To make groups of items collapsible, use the `SideNav.Collapsible` component.

    ### Routing with Side Nav Links

    As of version `1.16.9`, the `pathname` prop of the `SideNav.Link` component enables native browser routing using the `history.pushState()` function. This is essentially what `react-router-dom` does, and this method prevents extra dependencies in the `@servicetitan/anvil2` package.

    ```tsx theme={null}
    <SideNav.Link pathname="/example/path">Example Path</SideNav.Link>
    ```

    ## React Accessibility

    ### Multiple Navigation Landmark

    When there is more than one navigation landmark on a page, each should have a unique label. For example, if a `SideNav` exists on the same page as another navigational element, such as an app bar using a `nav` element, they should both have unique labels.

    Reference: [W3 Navigation Landmark](https://www.w3.org/WAI/ARIA/apg/patterns/landmarks/examples/navigation.html)

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

      function App() {
        return (
          <Flex direction="column">
            <Text el="h2" variant="headline" id="first-nav">
              First Navigation Landmark
            </Text>
            <SideNav aria-labelledby="first-nav">
              <SideNav.Link id="first-01">Item 1-1</SideNav.Link>
              <SideNav.Link id="first-02">Item 1-2</SideNav.Link>
            </SideNav>
            <Text el="h2" variant="headline" id="second-nav">
              Second Navigation Landmark
            </Text>
            <SideNav aria-labelledby="second-nav">
              <SideNav.Link id="second-01">Item 2-1</SideNav.Link>
              <SideNav.Link id="second-02">Item 2-2</SideNav.Link>
            </SideNav>
          </Flex>
        );
      }

      export default App;
      ```
    </LiveCode>

    For more guidance on semantic page structure and navigation landmarks, see [semantic markup best practices](/docs/accessibility/semantic-markup#sections-articles-and-asides).
  </Tab>

  <Tab title="SideNav Props">
    ```tsx theme={null}
    import { SideNav } from "@servicetitan/anvil2";

    function ExampleComponent() {
    return <SideNav aria-label="Main navigation">...</SideNav>;
    }

    ```

    ## `SideNav` Props

    The `SideNav` component can accept any valid HTML `nav` element props.
  </Tab>

  <Tab title="SideNav.Link Props">
    ```tsx theme={null}
    import { SideNav } from "@servicetitan/anvil2";

    function ExampleComponent() {
      return (
        <SideNav.Link
          id="link-1"
          pathname="/example"
          active={false}
          exact={false}
        >
          Link Text
        </SideNav.Link>
      );
    }
    ```

    ## `SideNav.Link` Props

    The `SideNav.Link` component can accept any valid HTML `a` props, as well as the following:

    <ParamField path="id" type="string" required>
      Unique identifier for the navigation link. Required to enable keyboard navigation.
    </ParamField>

    <ParamField path="active" type="boolean">
      When true, marks the link as the current active page.
    </ParamField>

    <ParamField path="disabled" type="boolean">
      When true, disables the link and prevents interaction.
    </ParamField>

    <ParamField path="pathname" type="string">
      Pathname for client-side routing. Using this will trigger History API navigation.
    </ParamField>

    <ParamField path="search" type="string">
      Optional search parameters for the URL. Can only be used if `pathname` has a value.
    </ParamField>
  </Tab>

  <Tab title="SideNav.Group Props">
    ```tsx theme={null}
    import { SideNav } from "@servicetitan/anvil2";

    function ExampleComponent() {
    return (

    <SideNav.Group label="Group Label">
      <SideNav.Link id="link-1">Link 1</SideNav.Link>
    </SideNav.Group>
    ); }

    ```

    ## `SideNav.Group` Props

    The `SideNav.Group` component can accept any valid HTML `li` props, as well as the following:

    <ParamField path="label" type="string" required>
      The label text displayed for the navigation group.
    </ParamField>
  </Tab>

  <Tab title="SideNav.Collapsible Props">
    ```tsx theme={null}
    import { SideNav } from "@servicetitan/anvil2";

    function ExampleComponent() {
      return (
        <SideNav.Collapsible label="Collapsible Group" defaultExpanded={false}>
          <SideNav.Link id="link-1">Link 1</SideNav.Link>
        </SideNav.Collapsible>
      );
    }
    ```

    ## `SideNav.Collapsible` Props

    The `SideNav.Collapsible` component can accept any valid HTML `li` props, as well as the following:

    <ParamField path="label" type="string" required>
      The label text displayed for the collapsible section.
    </ParamField>

    <ParamField path="defaultExpanded" type="boolean" default="false">
      Initial expansion state for uncontrolled usage.
    </ParamField>

    <ParamField path="expanded" type="boolean">
      Controlled expansion state.
    </ParamField>
  </Tab>
</Tabs>
