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

# Window – Code

> The Window provides a docked chat panel that expands to a fullscreen AppShell.

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

      function App() {
        return (
          <div style={{ position: "fixed", inset: 0, background: "#e9ecf1" }}>
            <Window
              chat={
                <div
                  style={{
                    display: "flex",
                    alignItems: "center",
                    justifyContent: "center",
                    height: "100%",
                  }}
                >
                  <Text text="Chat content" />
                </div>
              }
            />
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## Common Examples

    To use the `Window`, pass the required `chat` slot. Add `header`, `rail`, and `artifact` as needed. Docked, the window shows `header` and `chat`. Fullscreen, it reveals the full shell. `header` and `chat` keep the same React instance across the transition.

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

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

    ### Open control

    Uncontrolled, the window owns open state, seeded by `defaultOpen` (`true`). Pass `open` and `onOpenChange` to control it from a store.

    <LiveCode showCode example="ai-kit-window-open-control" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { useRef } from "react";
      import {
        AppHeader,
        Button,
        CartoTheme,
        Text,
        Window,
        useAppSurface,
      } from "@servicetitan/anvil2-ai-kit";
      import type { AppSurfaceHandle } from "@servicetitan/anvil2-ai-kit";
      import { IconMaximize2, IconX } from "@servicetitan/anvil2-icons";
      import "@servicetitan/anvil2-ai-kit/styles.css";

      function WindowHeader() {
        const { toggleSize, close } = useAppSurface();
        return (
          <AppHeader
            title="Title of conversation"
            trailingActions={[
              {
                id: "size",
                icon: <IconMaximize2 />,
                label: "Maximize",
                onPress: toggleSize,
              },
              { id: "close", icon: <IconX />, label: "Close", onPress: close },
            ]}
          />
        );
      }

      function App() {
        const ctrl = useRef<AppSurfaceHandle>(null);

        return (
          <CartoTheme>
            <div style={{ position: "fixed", inset: 0, background: "#e9ecf1" }}>
              <div style={{ position: "fixed", top: 24, left: 24 }}>
                <Button label="Open chat" onPress={() => ctrl.current?.open()} />
              </div>
              <Window
                controlRef={ctrl}
                header={<WindowHeader />}
                chat={
                  <div
                    style={{
                      display: "flex",
                      alignItems: "center",
                      justifyContent: "center",
                      height: "100%",
                    }}
                  >
                    <Text text="Chat content" />
                  </div>
                }
              />
            </div>
          </CartoTheme>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Maximize and restore

    Uncontrolled size is seeded by `defaultState` (`"docked"`). Pass `state` and `onStateChange` to own it. `useAppSurface().maximize()` and `restore()` drive the same transitions.

    <LiveCode showCode example="ai-kit-window-maximize-and-restore" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import {
        AppHeader,
        CartoTheme,
        Text,
        Window,
        useAppSurface,
      } from "@servicetitan/anvil2-ai-kit";
      import { IconMaximize2, IconX } from "@servicetitan/anvil2-icons";
      import "@servicetitan/anvil2-ai-kit/styles.css";

      function WindowHeader() {
        const { toggleSize, close } = useAppSurface();
        return (
          <AppHeader
            title="Title of conversation"
            trailingActions={[
              {
                id: "size",
                icon: <IconMaximize2 />,
                label: "Maximize",
                onPress: toggleSize,
              },
              { id: "close", icon: <IconX />, label: "Close", onPress: close },
            ]}
          />
        );
      }

      function App() {
        return (
          <CartoTheme>
            <div style={{ position: "fixed", inset: 0, background: "#e9ecf1" }}>
              <Window
                header={<WindowHeader />}
                chat={
                  <div
                    style={{
                      display: "flex",
                      alignItems: "center",
                      justifyContent: "center",
                      height: "100%",
                    }}
                  >
                    <Text text="Chat content" />
                  </div>
                }
              />
            </div>
          </CartoTheme>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Window states

    Docked is a non-modal floating panel. Fullscreen is a modal takeover over a scrim. Esc or scrim click returns to docked.

    <LiveCode showCode example="ai-kit-window-window-states" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import {
        AppHeader,
        CartoTheme,
        Text,
        Window,
        useAppSurface,
      } from "@servicetitan/anvil2-ai-kit";
      import { IconMinimize2, IconX } from "@servicetitan/anvil2-icons";
      import "@servicetitan/anvil2-ai-kit/styles.css";

      function WindowHeader() {
        const { toggleSize, close } = useAppSurface();
        return (
          <AppHeader
            title="Title of conversation"
            trailingActions={[
              {
                id: "size",
                icon: <IconMinimize2 />,
                label: "Restore",
                onPress: toggleSize,
              },
              { id: "close", icon: <IconX />, label: "Close", onPress: close },
            ]}
          />
        );
      }

      function App() {
        return (
          <CartoTheme>
            <div style={{ position: "fixed", inset: 0, background: "#e9ecf1" }}>
              <Window
                defaultState="fullscreen"
                header={<WindowHeader />}
                chat={
                  <div
                    style={{
                      display: "flex",
                      alignItems: "center",
                      justifyContent: "center",
                      height: "100%",
                    }}
                  >
                    <Text text="Chat content" />
                  </div>
                }
              />
            </div>
          </CartoTheme>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Drag

    Spread `dragHandleProps` from `useAppSurface()` onto a leading header action. The dragged position survives maximize and restore, and resets to the default corner only on close. The props are inert when fullscreen.

    <LiveCode showCode example="ai-kit-window-drag" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import {
        AppHeader,
        CartoTheme,
        Text,
        Window,
        useAppSurface,
      } from "@servicetitan/anvil2-ai-kit";
      import { IconGrip, IconMaximize2, IconX } from "@servicetitan/anvil2-icons";
      import "@servicetitan/anvil2-ai-kit/styles.css";

      // A marker at the panel's default anchor (bottom-right, 24px inset — matching
      // the docked panel's own resting corner) so a screenshot of the dragged panel
      // still shows where it started, proving it moved.
      function DefaultCornerMarker() {
        return (
          <div
            style={{
              position: "fixed",
              insetBlockEnd: 24,
              insetInlineEnd: 24,
              width: 120,
              height: 48,
              border: "2px dashed #99a3ad",
              borderRadius: 8,
              display: "flex",
              alignItems: "center",
              justifyContent: "center",
            }}
          >
            <Text size="fine" text="Default corner" />
          </div>
        );
      }

      function WindowHeader() {
        const { toggleSize, close, dragHandleProps } = useAppSurface();
        return (
          <AppHeader
            title="Title of conversation"
            leadingActions={[
              {
                id: "drag",
                icon: <IconGrip />,
                label: "Move window",
                dragHandleProps,
              },
            ]}
            trailingActions={[
              {
                id: "size",
                icon: <IconMaximize2 />,
                label: "Maximize",
                onPress: toggleSize,
              },
              { id: "close", icon: <IconX />, label: "Close", onPress: close },
            ]}
          />
        );
      }

      function App() {
        return (
          <CartoTheme>
            <div style={{ position: "fixed", inset: 0, background: "#e9ecf1" }}>
              <DefaultCornerMarker />
              <Window
                header={<WindowHeader />}
                chat={
                  <div
                    style={{
                      display: "flex",
                      alignItems: "center",
                      justifyContent: "center",
                      height: "100%",
                    }}
                  >
                    <Text text="Chat content" />
                  </div>
                }
              />
            </div>
          </CartoTheme>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Region intents

    `showArtifact()` and `showRail()` from a docked window maximize first, then open the region, in one call. The same `AppSurfaceHandle` is documented on `AppShell`.

    ## Internationalization

    `Window` owns `ai-kit.window.label` as the accessible name of the fullscreen dialog. Override it with `aria-label`. Region chrome uses the `AppShell` message IDs.

    ## React Accessibility

    * Fullscreen traps focus over a scrim. Esc restores to docked.
    * Docked drag also supports arrow-key nudging through `dragHandleProps`.
    * The fullscreen dialog's accessible name defaults to the localized window label.
  </Tab>

  <Tab title="Window Props">
    ```tsx theme={null}
    <Window
      header={header}
      chat={chat}
      rail={rail}
      artifact={artifact}
      open={open}
      onOpenChange={setOpen}
      state={state}
      onStateChange={setState}
    />
    ```

    ## `Window` Props

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

    <ParamField path="chat" type="ReactNode" required>
      Center conversation. Preserved across docked and fullscreen.
    </ParamField>

    <ParamField path="artifact" type="ReactNode">
      Fullscreen-only right region. Omit to render no artifact.
    </ParamField>

    <ParamField path="className" type="string">
      Class merged onto the root. Override docked corner or size here.
    </ParamField>

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

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

    <ParamField path="defaultOpen" type="boolean" default="true">
      Initial open state when uncontrolled.
    </ParamField>

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

    <ParamField path="defaultState" type={`"docked" | "fullscreen"`} default="docked">
      Initial size state when uncontrolled.
    </ParamField>

    <ParamField path="header" type="ReactNode">
      Header shown in both states and preserved across the transition.
    </ParamField>

    <ParamField path="onOpenChange" type="(open: boolean) => void">
      Fired whenever open state changes. Required to drive a controlled window.
    </ParamField>

    <ParamField path="onStateChange" type={`(state: "docked" | "fullscreen") => void`}>
      Fired whenever size state changes. Required to drive a controlled window.
    </ParamField>

    <ParamField path="open" type="boolean">
      Controlled open state.
    </ParamField>

    <ParamField path="rail" type="ReactNode">
      Fullscreen-only left region. Omit to render no rail.
    </ParamField>

    <ParamField path="state" type={`"docked" | "fullscreen"`}>
      Controlled size state.
    </ParamField>
  </Tab>
</Tabs>
