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

# App Shell – Code

> The AppShell provides header, rail, chat, and artifact regions with internal show and hide state.

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="ai-kit-app-shell" fullWidth screenshot>
      ```tsx lines expandable theme={null}
      import { AppShell, Text } from "@servicetitan/anvil2-ai-kit";

      function App() {
        return (
          <div style={{ width: "70rem", height: 400 }}>
            <AppShell
              chat={
                <div
                  style={{
                    display: "flex",
                    alignItems: "center",
                    justifyContent: "center",
                    height: "100%",
                    padding: 24,
                    backgroundColor: "#eeeeee",
                  }}
                >
                  <Text text="Chat region" />
                </div>
              }
            />
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## Common Examples

    To use the `AppShell`, pass the required `chat` region. Add `header`, `rail`, and `artifact` as needed. The kit forbids `children`. `chat` is always inline and never leaves the layout.

    ```tsx theme={null}
    import { AppShell } from "@servicetitan/anvil2-ai-kit";

    function ExampleComponent() {
      return (
        <AppShell
          header={<AppHeader title="Atlas" />}
          rail={<ChatHistory items={chats} />}
          chat={<ChatLayout conversation={messages} prompt={prompt} />}
          artifact={<TabbedLayout tabs={tabs} />}
        />
      );
    }
    ```

    ### Regions

    A region shows inline when it fits and becomes a modal drawer when it cannot. The rail auto-yields before the artifact as the shell narrows.

    <LiveCode showCode example="ai-kit-app-shell-regions" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import {
        AppHeader,
        AppShell,
        CartoTheme,
        Text,
        useAppSurface,
      } from "@servicetitan/anvil2-ai-kit";
      import { IconPanelLeft, IconPanelRight } from "@servicetitan/anvil2-icons";
      import "@servicetitan/anvil2-ai-kit/styles.css";

      function ShellHeader() {
        const { toggleRail, toggleArtifact } = useAppSurface();
        return (
          <AppHeader
            title="Title of conversation"
            leadingActions={[
              {
                id: "rail",
                icon: <IconPanelLeft />,
                label: "Toggle rail",
                onPress: toggleRail,
              },
            ]}
            trailingActions={[
              {
                id: "artifact",
                icon: <IconPanelRight />,
                label: "Toggle artifact panel",
                onPress: toggleArtifact,
              },
            ]}
          />
        );
      }

      function RegionPlaceholder({
        label,
        background,
      }: {
        label: string;
        background: string;
      }) {
        return (
          <div
            style={{
              display: "flex",
              alignItems: "center",
              justifyContent: "center",
              height: "100%",
              padding: 24,
              backgroundColor: background,
            }}
          >
            <Text text={label} />
          </div>
        );
      }

      function App() {
        return (
          <CartoTheme>
            <div style={{ width: "70rem", height: 400 }}>
              <AppShell
                header={<ShellHeader />}
                rail={
                  <RegionPlaceholder label="Rail region" background={"#dce4ea"} />
                }
                chat={
                  <RegionPlaceholder label="Chat region" background={"#eeeeee"} />
                }
                artifact={
                  <RegionPlaceholder label="Artifact region" background={"#9ecaff"} />
                }
              />
            </div>
          </CartoTheme>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Initial visibility

    `defaultRailCollapsed` and `defaultArtifactCollapsed` start a region collapsed on first paint, without a close transition.

    <LiveCode showCode example="ai-kit-app-shell-initial-visibility" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { useEffect, useRef } from "react";
      import {
        AppHeader,
        AppShell,
        CartoTheme,
        Text,
        useAppSurface,
        type AppSurfaceHandle,
      } from "@servicetitan/anvil2-ai-kit";
      import { IconPanelLeft, IconPanelRight } from "@servicetitan/anvil2-icons";
      import "@servicetitan/anvil2-ai-kit/styles.css";

      function ShellHeader() {
        const { toggleRail, toggleArtifact } = useAppSurface();
        return (
          <AppHeader
            title="Title of conversation"
            leadingActions={[
              {
                id: "rail",
                icon: <IconPanelLeft />,
                label: "Toggle rail",
                onPress: toggleRail,
              },
            ]}
            trailingActions={[
              {
                id: "artifact",
                icon: <IconPanelRight />,
                label: "Toggle artifact panel",
                onPress: toggleArtifact,
              },
            ]}
          />
        );
      }

      function RegionPlaceholder({
        label,
        background,
      }: {
        label: string;
        background: string;
      }) {
        return (
          <div
            style={{
              display: "flex",
              alignItems: "center",
              justifyContent: "center",
              height: "100%",
              padding: 24,
              backgroundColor: background,
            }}
          >
            <Text text={label} />
          </div>
        );
      }

      function App() {
        const controlRef = useRef<AppSurfaceHandle>(null);
        useEffect(() => controlRef.current?.hideRail(), []);

        return (
          <CartoTheme>
            <div style={{ width: "70rem", height: 400 }}>
              <AppShell
                controlRef={controlRef}
                header={<ShellHeader />}
                rail={
                  <RegionPlaceholder label="Rail region" background={"#dce4ea"} />
                }
                chat={
                  <RegionPlaceholder label="Chat region" background={"#eeeeee"} />
                }
                artifact={
                  <RegionPlaceholder label="Artifact region" background={"#9ecaff"} />
                }
              />
            </div>
          </CartoTheme>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Artifact width

    `defaultArtifactWidth` is `"regular"` (560px) or `"large"` (a 720px floor that fills available width and collapses the rail). It is a starting width only; the artifact stays drag-resizable.

    <LiveCode showCode example="ai-kit-app-shell-artifact-width" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import {
        AppHeader,
        AppShell,
        CartoTheme,
        Text,
        useAppSurface,
      } from "@servicetitan/anvil2-ai-kit";
      import { IconPanelLeft, IconPanelRight } from "@servicetitan/anvil2-icons";
      import "@servicetitan/anvil2-ai-kit/styles.css";

      function LargeArtifactHeader() {
        const { toggleRail, toggleArtifact } = useAppSurface();
        return (
          <AppHeader
            title="Title of conversation"
            leadingActions={[
              {
                id: "rail",
                icon: <IconPanelLeft />,
                label: "Toggle rail",
                onPress: toggleRail,
              },
            ]}
            trailingActions={[
              {
                id: "artifact",
                icon: <IconPanelRight />,
                label: "Toggle artifact panel (large)",
                onPress: () => toggleArtifact({ width: "large" }),
              },
            ]}
          />
        );
      }

      function RegionPlaceholder({
        label,
        background,
      }: {
        label: string;
        background: string;
      }) {
        return (
          <div
            style={{
              display: "flex",
              alignItems: "center",
              justifyContent: "center",
              height: "100%",
              padding: 24,
              backgroundColor: background,
            }}
          >
            <Text text={label} />
          </div>
        );
      }

      function App() {
        return (
          <CartoTheme>
            <div style={{ width: "70rem", height: 400 }}>
              <AppShell
                header={<LargeArtifactHeader />}
                rail={
                  <RegionPlaceholder label="Rail region" background={"#dce4ea"} />
                }
                chat={
                  <RegionPlaceholder label="Chat region" background={"#eeeeee"} />
                }
                artifact={
                  <RegionPlaceholder label="Artifact region" background={"#9ecaff"} />
                }
                defaultArtifactWidth="large"
              />
            </div>
          </CartoTheme>
        );
      }

      export default App;
      ```
    </LiveCode>

    <LiveCode showCode example="ai-kit-app-shell-artifact-resize" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import {
        AppHeader,
        AppShell,
        CartoTheme,
        Text,
        useAppSurface,
      } from "@servicetitan/anvil2-ai-kit";
      import { IconPanelLeft, IconPanelRight } from "@servicetitan/anvil2-icons";
      import "@servicetitan/anvil2-ai-kit/styles.css";

      function ShellHeader() {
        const { toggleRail, toggleArtifact } = useAppSurface();
        return (
          <AppHeader
            title="Title of conversation"
            leadingActions={[
              {
                id: "rail",
                icon: <IconPanelLeft />,
                label: "Toggle rail",
                onPress: toggleRail,
              },
            ]}
            trailingActions={[
              {
                id: "artifact",
                icon: <IconPanelRight />,
                label: "Toggle artifact panel",
                onPress: toggleArtifact,
              },
            ]}
          />
        );
      }

      function RegionPlaceholder({
        label,
        background,
      }: {
        label: string;
        background: string;
      }) {
        return (
          <div
            style={{
              display: "flex",
              alignItems: "center",
              justifyContent: "center",
              height: "100%",
              padding: 24,
              backgroundColor: background,
            }}
          >
            <Text text={label} />
          </div>
        );
      }

      function App() {
        return (
          <CartoTheme>
            <div style={{ width: "76rem", height: 400 }}>
              <AppShell
                header={<ShellHeader />}
                chat={
                  <RegionPlaceholder label="Chat region" background={"#eeeeee"} />
                }
                artifact={
                  <RegionPlaceholder label="Artifact region" background={"#9ecaff"} />
                }
              />
            </div>
          </CartoTheme>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Auto-collapse

    Dragging the artifact past the point where chat would hit its minimum detaches it into a drawer. Dragging back re-attaches it inline.

    <LiveCode showCode example="ai-kit-app-shell-auto-collapse" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import {
        AppHeader,
        AppShell,
        CartoTheme,
        Text,
        useAppSurface,
      } from "@servicetitan/anvil2-ai-kit";
      import { IconPanelLeft, IconPanelRight } from "@servicetitan/anvil2-icons";
      import "@servicetitan/anvil2-ai-kit/styles.css";

      function ShellHeader() {
        const { toggleRail, toggleArtifact } = useAppSurface();
        return (
          <AppHeader
            title="Title of conversation"
            leadingActions={[
              {
                id: "rail",
                icon: <IconPanelLeft />,
                label: "Toggle rail",
                onPress: toggleRail,
              },
            ]}
            trailingActions={[
              {
                id: "artifact",
                icon: <IconPanelRight />,
                label: "Toggle artifact panel",
                onPress: toggleArtifact,
              },
            ]}
          />
        );
      }

      function RegionPlaceholder({
        label,
        background,
      }: {
        label: string;
        background: string;
      }) {
        return (
          <div
            style={{
              display: "flex",
              alignItems: "center",
              justifyContent: "center",
              height: "100%",
              padding: 24,
              backgroundColor: background,
            }}
          >
            <Text text={label} />
          </div>
        );
      }

      function App() {
        return (
          <CartoTheme>
            <div style={{ width: 760, height: 400, border: "1px solid #ccc" }}>
              <AppShell
                header={<ShellHeader />}
                rail={
                  <RegionPlaceholder label="Rail region" background={"#dce4ea"} />
                }
                chat={
                  <RegionPlaceholder label="Chat region" background={"#eeeeee"} />
                }
                artifact={
                  <RegionPlaceholder label="Artifact region" background={"#9ecaff"} />
                }
              />
            </div>
          </CartoTheme>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### `useAppSurface`

    Region show, hide, and toggle live on `AppSurfaceHandle`. From a parent, pass `controlRef`. From inside the tree, call `useAppSurface()`.

    ```tsx theme={null}
    const { toggleRail, showArtifact } = useAppSurface();

    return (
      <AppHeader
        title="Atlas"
        trailingActions={[
          { icon: <IconPanelLeft />, label: "Toggle rail", onPress: toggleRail },
          {
            icon: <IconPanelRight />,
            label: "Show artifact",
            onPress: () => showArtifact({ width: "large" }),
          },
        ]}
      />
    );
    ```

    Window methods on the handle (`open`, `maximize`, …) are no-ops in a bare shell. `isOpen` and `isFullscreen` read `true` because the shell is the full surface.

    ## Internationalization

    `AppShell` owns these localized strings:

    * `ai-kit.appShell.resizeRail`
    * `ai-kit.appShell.resizeArtifact`
    * `ai-kit.appShell.railDrawer`
    * `ai-kit.appShell.artifactDrawer`

    Mount `AiKitIntlProvider` inside `CartoTheme` to switch them.

    ## React Accessibility

    * Rail and artifact drawers expose localized names.
    * Resize handles expose localized names for the rail and artifact.
  </Tab>

  <Tab title="AppShell Props">
    ```tsx theme={null}
    <AppShell
      header={header}
      rail={rail}
      chat={chat}
      artifact={artifact}
      controlRef={controlRef}
    />
    ```

    ## `AppShell` Props

    The `AppShell` accepts the following props and forwards remaining `<div>` attributes except `children` and `style`.

    <ParamField path="chat" type="ReactNode" required>
      Center region. Always inline.
    </ParamField>

    <ParamField path="artifact" type="ReactNode">
      Right region. Omit to render no artifact.
    </ParamField>

    <ParamField path="className" type="string">
      Class merged onto the root element.
    </ParamField>

    <ParamField path="controlRef" type="Ref<AppSurfaceHandle>">
      Imperative surface handle for a parent. Prefer `useAppSurface` inside the
      tree. `ref` still forwards the root `<div>`.
    </ParamField>

    <ParamField path="defaultArtifactCollapsed" type="boolean" default="false">
      Starts the artifact collapsed on first paint.
    </ParamField>

    <ParamField path="defaultArtifactWidth" type={`"regular" | "large"`} default="regular">
      Starting artifact width preset. The artifact stays drag-resizable.
    </ParamField>

    <ParamField path="defaultRailCollapsed" type="boolean" default="false">
      Starts the rail collapsed on first paint.
    </ParamField>

    <ParamField path="header" type="ReactNode">
      Full-width top region. Omit for no header.
    </ParamField>

    <ParamField path="rail" type="ReactNode">
      Left region. Fixed width; never drag-resized. Omit to render no rail.
    </ParamField>
  </Tab>

  <Tab title="AppSurfaceHandle">
    ```tsx theme={null}
    const surface = useAppSurface();
    surface.showArtifact({ width: "large" });
    ```

    ## `AppSurfaceHandle`

    The same handle drives a bare `AppShell` and a `Window`. Region intents in a docked window maximize first, then apply.

    <ParamField path="artifactPresentation" type={`"inline" | "drawer" | "hidden"`} required>
      How the artifact is currently presented. Read-only.
    </ParamField>

    <ParamField path="close" type="() => void" required>
      Hides the window. No-op without a window.
    </ParamField>

    <ParamField path="dragHandleProps" type="WindowDragHandleProps" required>
      Spread onto a header action to move a docked window. Inert otherwise.
    </ParamField>

    <ParamField path="hideArtifact" type="() => void" required>
      Hides the artifact.
    </ParamField>

    <ParamField path="hideRail" type="() => void" required>
      Hides the rail.
    </ParamField>

    <ParamField path="isFullscreen" type="boolean" required>
      Whether the surface is fullscreen. Always `true` in a bare shell.
    </ParamField>

    <ParamField path="isOpen" type="boolean" required>
      Whether the window is open. Always `true` in a bare shell.
    </ParamField>

    <ParamField path="maximize" type="() => void" required>
      Expands the window to fullscreen. No-op without a window.
    </ParamField>

    <ParamField path="open" type="() => void" required>
      Shows the window. No-op without a window.
    </ParamField>

    <ParamField path="restore" type="() => void" required>
      Restores the window to docked. No-op without a window.
    </ParamField>

    <ParamField path="showArtifact" type="(options?: { width?: 'regular' | 'large' }) => void" required>
      Shows the artifact. Maximizes a docked window first.
    </ParamField>

    <ParamField path="showRail" type="() => void" required>
      Shows the rail. Maximizes a docked window first.
    </ParamField>

    <ParamField path="toggle" type="() => void" required>
      Toggles the window open or closed. No-op without a window.
    </ParamField>

    <ParamField path="toggleArtifact" type="(options?: { width?: 'regular' | 'large' }) => void" required>
      Toggles the artifact. Maximizes first when docked and hidden.
    </ParamField>

    <ParamField path="toggleRail" type="() => void" required>
      Toggles the rail. Maximizes first when docked and hidden.
    </ParamField>

    <ParamField path="toggleSize" type="() => void" required>
      Toggles docked and fullscreen. No-op without a window.
    </ParamField>
  </Tab>
</Tabs>
