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

# Chip – Code

> Chips visually label and organize statuses, metadata, and objects.

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

      function App() {
        return (
          <Chip
            label="Ben Ho"
            size="medium"
            color="#3e3e5f"
            onClick={() => alert("chip click")}
            onClose={() => alert("chip close")}
          />
        );
      }

      export default App;
      ```
    </LiveCode>

    ## Common Examples

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

    function ExampleComponent() {
      return (
        <Chip
          label="Cassian Andor"
          size="medium"
          color="#3e3e5f"
          onClick={console.log}
          onClose={console.log}
        />
      );
    }
    ```

    ### Basic Chip

    `Chip` components must at least have their `label` prop defined, which will define the text that is displayed within the `Chip`.

    <LiveCode showCode example="chip-label" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Chip } from "@servicetitan/anvil2";

      function App() {
        return <Chip label="Ben Ho" />;
      }

      export default App;
      ```
    </LiveCode>

    ### Closeable Chip

    Adding an `onClose` prop handler to the `Chip` component will render a clickable close button alongside the `Chip`.

    <LiveCode showCode example="chip-onclose" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Chip } from "@servicetitan/anvil2";

      function App() {
        return <Chip label="Ben Ho" onClose={() => alert("chip close")} />;
      }

      export default App;
      ```
    </LiveCode>

    ### Clickable Chip

    Adding an `onClick` prop handler to the `Chip` component will render the text inside the `Chip` as a clickable button.

    <LiveCode showCode example="chip-onclick" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Chip } from "@servicetitan/anvil2";

      function App() {
        return <Chip label="Ben Ho" onClick={() => alert("chip click")} />;
      }

      export default App;
      ```
    </LiveCode>

    ### Chip with Avatar

    Adding an `avatar` prop with a URL to an image will render an `Avatar` inside of the `Chip` as a prefix before the label. Note that the `avatar` prop cannot be used with the `icon` prop.

    <LiveCode showCode example="chip-avatar" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Chip, Flex } from "@servicetitan/anvil2";
      import dog01 from "../assets/dog-01.png";
      import dog02 from "../assets/dog-02.png";

      function App() {
        return (
          <Flex gap="6">
            <div>
              <Chip
                label="Ben Ho"
                onClose={() => alert("chip close")}
                color="#3e3e5f"
                avatar={dog01}
                textWrap
              />
            </div>

            <div style={{ width: "10rem" }}>
              <Chip
                label="Patrick Buckingham"
                onClose={() => alert("chip close")}
                color="#3e3e5f"
                avatar={dog02}
                textWrap
              />
            </div>
          </Flex>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Chip with Icon

    Adding an `icon` prop with an imported SVG will render an `Icon` inside of the `Chip` as a prefix before the label. Note that the `icon` prop cannot be used with the `avatar` prop.

    <LiveCode showCode example="chip-icon" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Chip, Flex } from "@servicetitan/anvil2";
      import Info from "@servicetitan/anvil2/assets/icons/material/round/info.svg";
      import Warning from "@servicetitan/anvil2/assets/icons/material/round/warning.svg";

      function App() {
        return (
          <Flex gap="6">
            <div>
              <Chip
                label="Ben Ho"
                onClose={() => alert("chip close")}
                color="#3e3e5f"
                icon={Info}
                textWrap
              />
            </div>

            <div style={{ width: "10rem" }}>
              <Chip
                label="Patrick Buckingham"
                onClose={() => alert("chip close")}
                color="#3e3e5f"
                icon={Warning}
                textWrap
              />
            </div>
          </Flex>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Chip with AI Mark

    Adding the `aiMark` prop displays the `AiMark` icon inside the chip to indicate AI-generated content. On default chips (no color), a gradient AI mark is shown. On colored chips, the AI mark inherits the chip's text color.

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

      function App() {
        return (
          <Flex gap="4" alignItems="center">
            <Chip label="AI job summary" aiMark />
            <Chip label="AI job summary" aiMark size="small" />
          </Flex>
        );
      }

      export default App;
      ```
    </LiveCode>

    #### AI Mark with Color

    When a `color` prop is set alongside `aiMark`, the AI mark uses the default type, inheriting the chip's text color for visual cohesion.

    <LiveCode showCode example="chip-aimark-color" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Chip, Flex } from "@servicetitan/anvil2";

      function App() {
        return (
          <Flex gap="4" alignItems="center" wrap="wrap">
            <Chip label="AI job summary" aiMark color="#3e3e5f" />
            <Chip label="AI job summary" aiMark color="#7D165B" />
            <Chip label="AI job summary" aiMark color="#83ba43" />
            <Chip label="AI job summary" aiMark color="#DE9500" />
          </Flex>
        );
      }

      export default App;
      ```
    </LiveCode>

    #### AI Mark with Interactive Chips

    When `aiMark` is used with `onClick`, the AI mark will animate when the chip is hovered or focused.

    <LiveCode showCode example="chip-aimark-interactive" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Chip, Flex } from "@servicetitan/anvil2";

      function App() {
        return (
          <Flex gap="4" alignItems="center" wrap="wrap">
            <Chip
              label="AI estimate draft"
              aiMark
              onClick={() => alert("chip click")}
            />
            <Chip
              label="AI estimate draft"
              aiMark
              onClose={() => alert("chip close")}
            />
            <Chip
              label="AI estimate draft"
              aiMark
              color="#3e3e5f"
              onClick={() => alert("chip click")}
              onClose={() => alert("chip close")}
            />
          </Flex>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## React Accessibility

    * The `Chip` component won't be focusable unless it has an `onClick` or `onClose` prop handler defined.

    For more guidance on accessible interaction components, see [button accessibility best practices](/docs/accessibility/labels-and-ctas#buttons-and-links).
  </Tab>

  <Tab title="Chip Props">
    ```tsx theme={null}
    <Chip
      label="Cassian Andor"
      avatar="https://example.com/avatar.jpg"
      color="#3e3e5f"
      icon={StarIcon}
      size="medium"
      onClick={() => console.log("Clicked")}
      onClose={(e) => console.log("Closed")}
    />
    ```

    ## `Chip` Props

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

    <ParamField path="aiMark" type="boolean">
      Displays an AI mark icon in the chip to indicate AI-generated content. Uses gradient style on default chips and inherited color style on colored chips.
    </ParamField>

    <ParamField path="avatar" type="string">
      **Note:** Only works when `icon` is not set
    </ParamField>

    <ParamField path="color" type="string" default="#14141429">
      Several color formats work, such as HEX, RGB, HSL, HSV, etc.
    </ParamField>

    <ParamField path="icon" type="Svg">
      **Note:** Only works when `avatar` is not set
    </ParamField>

    <ParamField path="onClick" type="() => void">
      Setting this will make the chip clickable and focusable.
      <Note>When `size="small"`, only one of `onClick` or `onClose` can be used, not both.</Note>
    </ParamField>

    <ParamField path="onClose" type="(e: MouseEvent | KeyboardEvent) => void">
      Setting this will render the chip's close button.

      <Note>When `size="small"`, only one of `onClick` or `onClose` can be used, not both.</Note>
    </ParamField>

    <ParamField path="size" type={`"small" | "medium"`} default="medium" />

    <ParamField path="textWrap" type="boolean">
      Wraps text when it overflows.
    </ParamField>
  </Tab>
</Tabs>
