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

# Text – Code

> The text component enables designers and developers to use properly styled typography.

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

      function App() {
        return (
          <Flex direction="column">
            <Text variant="headline" el="h2" size="large">
              Headline
            </Text>
            <Text variant="eyebrow" size="large">
              Eyebrow
            </Text>
            <Text variant="body">Body text. Lorem ipsum dolor sit amet.</Text>
          </Flex>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## Common Examples

    ```tsx theme={null}
    import { Flex, Text } from "@servicetitan/anvil2";

    function ExampleComponent() {
      return (
        <Flex direction="column">
          <Text variant="headline" el="h2">
            Headline text
          </Text>
          <Text variant="eyebrow" size="large">
            Eyebrow
          </Text>
          <Text>Regular body text.</Text>
        </Flex>
      );
    }
    ```

    ### Text variants

    The `Text` component accepts one of three options for the `variant` prop:

    * `"body"` (default): renders as HTML `p` or `span` element, if `inline` is set to `true`
    * `"eyebrow"`: renders as HTML `span` element
    * `"headline"`: renders as HTML heading element (`h1` through `h4`) element, based on `el` prop

    #### Body

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

      function App() {
        return (
          <Flex direction="column">
            <Text size="small" variant="body">
              Small body text
            </Text>
            <Text size="medium" variant="body">
              Medium body text
            </Text>
            <Text size="large" variant="body">
              Large body text
            </Text>
            <Text size="xlarge" variant="body">
              X-Large body text
            </Text>
          </Flex>
        );
      }

      export default App;
      ```
    </LiveCode>

    #### Eyebrow

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

      function App() {
        return (
          <Flex direction="column">
            <Text size="small" variant="eyebrow">
              Eyebrow text
            </Text>
            <Text size="medium" variant="eyebrow">
              Eyebrow text
            </Text>
            <Text size="large" variant="eyebrow">
              Eyebrow text
            </Text>
          </Flex>
        );
      }

      export default App;
      ```
    </LiveCode>

    #### Headline

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

      function App() {
        return (
          <Flex direction="column">
            <Text el="h4" size="small" variant="headline">
              H4 Headline
            </Text>
            <Text el="h3" size="medium" variant="headline">
              H3 Headline
            </Text>
            <Text el="h2" size="large" variant="headline">
              H2 Headline
            </Text>
            <Text el="h1" size="xlarge" variant="headline">
              H1 Headline
            </Text>
          </Flex>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Nesting body text

    When using body text within body text, make sure to use the `inline` prop on any of the child `Body` components to render a `span` instead of `p`.

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

      function App() {
        return (
          <Text size="medium" variant="body">
            The outer text is in a <code>p</code> element, but the{" "}
            <Text variant="body" inline style={{ fontWeight: 700 }}>
              inner text
            </Text>{" "}
            is in a <code>span</code> element.
          </Text>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Subdued body text

    If `subdued` is set to `true` on a body text, it will use a de-emphasized color:

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

      function App() {
        return (
          <Flex gap="6">
            <Text variant="body">Regular</Text>
            <Text variant="body" subdued>
              Subdued
            </Text>
          </Flex>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### The useAccessibleColor hook

    The Anvil2 `useAccessibleColor` hook makes it easy to determine which text color to use given a specific background color without making it inaccessible. This hook accepts two parameters, and returns an object with both `foreground` and `background` colors. In the case that no color can be made accessible with the given color, the `background` color will be adjusted until it can.

    ```tsx theme={null}
    // accepts any valid css color format and hex with no #
    const { foreground, background } = useAccessibleColor("0066CC");
    ```

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

      function App() {
        const { foreground: fg1, background: bg1 } = useAccessibleColor("#0066CC");

        const { foreground: fg2, background: bg2 } =
          useAccessibleColor("hsl(339,100,94)");

        return (
          <Flex gap="2">
            <Card style={{ backgroundColor: bg1 }}>
              <Text style={{ color: fg1 }} inline>
                This is accessible 🎉
              </Text>
            </Card>
            <Card style={{ backgroundColor: bg2 }}>
              <Text style={{ color: fg2 }} inline>
                This is accessible 🎉
              </Text>
            </Card>
          </Flex>
        );
      }

      export default App;
      ```
    </LiveCode>

    **Note:** Anvil2 components and tokens already account for accessible colors internally, so this hook should only be needed when working with custom colors.
  </Tab>

  <Tab title="Text Props">
    ```tsx theme={null}
    <Text variant="body" size="medium" inline={false} subdued={false} el="h2">
      Text content
    </Text>
    ```

    ## `Text` Props

    Depending on the variant, the `Text` component can accept any valid HTML `header`, `p`, or `span` props, in addition to the ones listed below. See the [Text variants section](/docs/web/components/text/code#text-variants) for specifics.

    <ParamField path="el" type={`"h1" | "h2" | "h3" | "h4" | "h5" | "h6"`}>
      Only valid when `variant="headline"`. Required when using headline variant.
    </ParamField>

    <ParamField path="aiMark" type={`boolean | AiMarkWithTooltipOrPopoverConfig`}>
      Only valid when `variant="headline"`. When set, renders the AI mark inline after the headline text. Use `true` for the sparkle only, or pass tooltip or popover configuration. See [AI Mark](/docs/web/components/ai-mark/code).
    </ParamField>

    <ParamField path="inline" type="boolean">
      Only valid when `variant="body"`.
    </ParamField>

    <ParamField path="size" type={`"small" | "medium" | "large" | "xlarge"`} default="medium">
      Note: `"xlarge"` is not available for `variant="eyebrow"`.
    </ParamField>

    <ParamField path="subdued" type="boolean">
      Only valid when `variant="body"`.
    </ParamField>

    <ParamField path="variant" type={`"body" | "eyebrow" | "headline"`} default="body" />
  </Tab>
</Tabs>
