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

# Button – Code

> Buttons are clickable UI elements that trigger an action or event.

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="button-playground" fullWidth screenshot>
      ```tsx lines expandable theme={null}
      import { Button } from "@servicetitan/anvil2";
      import Star from "@servicetitan/anvil2/assets/icons/material/round/star.svg";

      function App() {
        return (
          <Button
            appearance="primary"
            size="medium"
            icon={Star}
            onClick={console.log}
          >
            Click me
          </Button>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## Common Examples

    ```tsx theme={null}
    import { Button, ButtonCompound, ButtonLink, Card } from "@servicetitan/anvil2";

    function ExampleComponent() {
      return (
        <>
          <Button onClick={console.log}>Click me</Button>
          <ButtonCompound onClick={console.log}>
            <Card>Click me</Card>
          </ButtonCompound>
          <ButtonLink href="#">Click me</ButtonLink>
        </>
      );
    }
    ```

    The `Button` component can be used in the same way that you would use an HTML `button` element, while the `ButtonLink` component can be used in the same way that you would use an HTML `a` element.

    ### Form Submit

    The `type` prop can be used to alter the default functionality of the button. A common use case is to use `type="submit"` in order to submit the content of a form.

    ```tsx theme={null}
    <form onSubmit={handleSubmit}>
      <TextInput name="firstName" label="First Name" required />
      <Button type="submit">Submit</Button>
    </form>
    ```

    ### Button links

    In general, [Links](/docs/web/components/link/design) should be used for navigating to other pages or website. To create a link that is styled like a button, use the `ButtonLink` component.

    ```tsx theme={null}
    <div>
      <div>
        <BodyText>
          A project label can be attached to expense items like timesheet activities
          to provide a visual representation of expense spending.
        </BodyText>
        <br />
        <br />
        <ButtonLink href="#">Learn more</ButtonLink>
      </div>
    </div>
    ```

    ### Using button compounds to make components interactive

    <Columns rows={2}>
      <div>
        To add interactivity to components that are usually static, such as avatars, use the `ButtonCompound` component.

        ```tsx theme={null}
        <ButtonCompound shape="circular" onClick={handleClick}>
          <Avatar {...avatarProps} />
        </ButtonCompound>
        ```
      </div>

      <div>
        <LiveCode showCode example="button-avatar" screenshot fullWidth>
          ```tsx lines expandable theme={null}
          import { ButtonCompound, Avatar } from "@servicetitan/anvil2";

          function App() {
            return (
              <ButtonCompound shape="circular" onClick={console.log}>
                <Avatar name="Rose Tico" size="large" status="online" />
              </ButtonCompound>
            );
          }

          export default App;
          ```
        </LiveCode>
      </div>
    </Columns>

    #### Interactive cards

    To make cards clickable, use the [`InteractiveCard`](/docs/web/components/interactive-card/code) component. It supports nested interactive elements without accessibility violations.

    <LiveCode showCode example="button-card" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { ButtonCompound, Card, Text } from "@servicetitan/anvil2";

      function App() {
        return (
          <ButtonCompound onClick={console.log}>
            <Card flexDirection="column">
              <Text variant="headline" el="h2">
                Interactive Card
              </Text>
              <Text>You can click this card!</Text>
            </Card>
          </ButtonCompound>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### AI Mark

    Use the `aiMark` prop to display an AI indicator. `aiMark` accepts a `boolean`, or one of:

    * `"mark"`
    * `"chat"`
    * `"edit"`
    * `"form"`
    * `"mic"`
    * `"search"`

    The `aiMark` prop cannot be used together with a before icon (a single `icon` Svg or `icon={{ before: Svg }}`).

    | Button usage              | `aiMark`                  | `icon` allowed?                                                                                              |
    | ------------------------- | ------------------------- | ------------------------------------------------------------------------------------------------------------ |
    | With `children`           | `true` or an AI icon name | Only after: use `icon={{ after: Svg }}` or omit `icon`. You cannot use a before icon or a single `icon` Svg. |
    | Icon-only (no `children`) | `true` or an AI icon name | No. Do not pass `icon`. Only the AI icon is shown. Always provide `aria-label`.                              |

    <LiveCode example="button-aimark" screenshot fullWidth>
      ```tsx lines theme={null}
      import { Button, Grid } from "@servicetitan/anvil2";
      import ArrowForwardIcon from "@servicetitan/anvil2/assets/icons/material/round/arrow_forward.svg";

      function App() {
        return (
          <Grid templateColumns="repeat(2, max-content)" gap="6" placeItems="center">
            <Button appearance="primary" aiMark aria-label="AI draft estimate" />
            <Button appearance="primary" aiMark>
              Summarize Visit
            </Button>
            <Button
              appearance="secondary"
              aiMark="search"
              aria-label="Search similar jobs"
            />
            <Button
              appearance="secondary"
              aiMark="form"
              icon={{ after: ArrowForwardIcon }}
            >
              Autofill Work Order
            </Button>
          </Grid>
        );
      }

      export default App;
      ```
    </LiveCode>

    **With children:**

    ```tsx theme={null}
    <Button appearance="primary" aiMark>Generate</Button>
    <Button appearance="secondary" aiMark icon={{ after: ArrowForward }}>Report</Button>
    ```

    **Icon-only AI:**

    ```tsx theme={null}
    <Button aria-label="AI action" aiMark />
    <Button aria-label="AI chat" aiMark="chat" />
    ```

    ### Minimum width for buttons

    If a `Button` component has any `children`, it will have a `5rem` minimum width. This is not applied in icon-only buttons using the `icon` prop. In some cases, such as when adding a `Badge` as a child of a `Button` component without any text, this can lead to unexpected width changes.

    Either override the `width` and `minWidth` of the `Button` component, or make the `Badge` a sibling of the `Button` inside of a `div` with relative positioning.

    ```tsx theme={null}
    // option 1
    <Button
      aria-label="filters button"
      icon={FilterIcon}
      style={{ width: '2rem', minWidth: '2rem' }}
    >
      <Badge aria-label="2 active filters">2</Badge>
    </Button>

    // option 2
    <div style={{ position: 'relative', display: 'inline-block '}}>
      <Button aria-label="filters button" icon={FilterIcon} />
      <Badge aria-label="2 active filters">2</Badge>
    </div>
    ```

    ## React Accessibility

    ### Icon-only buttons

    ```tsx theme={null}
    <Button icon={Edit} aria-label="Edit" />
    ```

    <Check>**Do**</Check>

    Use aria-label or aria-labelledby for a button without any text.

    ```tsx theme={null}
    <Button icon={Edit} />
    ```

    <Danger>**Don't**</Danger>

    Provide only an icon for screen readers to read.

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

  <Tab title="Button Props">
    ```tsx theme={null}
    <Button
      appearance="primary"
      size="medium"
      icon={StarIcon}
      loading={false}
      aiMark={true}
      onClick={() => console.log("Clicked")}
    >
      Click me
    </Button>
    ```

    ## `Button` Props

    <ParamField path="aiMark" type={`boolean | "mark" | "chat" | "edit" | "form" | "mic" | "search"`} default="false">
      Shows an AI indicator. When set with button text, a leading `icon` (plain Svg or `icon.before`) is not rendered—the AI icon is used instead. On an icon-only button, `icon` is not rendered. Use `icon={{ after: Svg }}` for a trailing icon with text.
    </ParamField>

    <ParamField path="appearance" type={`"primary" | "secondary" | "ghost" | "danger" | "danger-secondary"`} default="secondary" />

    <ParamField path="children" type="ReactNode" />

    <ParamField path="icon" type="Svg | { before: Svg } | { after: Svg }">
      The SVGR library imports `*.svg` files as React components, so `icon` can
      accept either a React component, or an object with `before` or `after` key
      to specify the placement of the icon.
    </ParamField>

    <ParamField path="loading" type="boolean" default="false">
      Setting this to `true` will put the button into a loading state.
    </ParamField>

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

  <Tab title="ButtonCompound Props">
    ```tsx theme={null}
    <ButtonCompound shape="circular" onClick={handleClick}>
      <Avatar name="John Doe" />
    </ButtonCompound>
    ```

    ## `ButtonCompound` Props

    <ParamField path="shape" type={`"pill" | "circular" | "rounded"`} default="rounded" />
  </Tab>

  <Tab title="ButtonLink Props">
    ```tsx theme={null}
    <ButtonLink
      href="/page"
      appearance="primary"
      size="medium"
      icon={StarIcon}
      loading={false}
      disabled={false}
    >
      Click me
    </ButtonLink>
    ```

    ## `ButtonLink` Props

    <ParamField path="appearance" type={`"primary" | "secondary" | "ghost" | "danger" | "danger-secondary"`} default="secondary" />

    <ParamField path="children" type="ReactNode" />

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

    <ParamField path="icon" type="Svg | { before: Svg } | { after: Svg }">
      The SVGR library imports `*.svg` files as React components, so `icon` can
      accept either a React component, or an object with `before` or `after` key
      to specify the placement of the icon.
    </ParamField>

    <ParamField path="loading" type="boolean" default="false">
      Setting this to `true` will put the button into a loading state.
    </ParamField>

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