# Code

> Renders inline code snippets in a themed monospace surface. Use for commands, file paths, or technical terms in prose; renders as an anchor when given href.

Source: https://vaneui.com/docs/basic-components/code

Import: `import { Code, Button, Col, PageTitle, Row, SectionTitle, Text, Title } from "@vaneui/ui"`

Code sets apart the parts of a sentence that a reader would type or copy, such as a package name, a hook or a prop value. It renders a native `<code>` whose text does not wrap, so it suits short snippets rather than multi-line listings. For keys the reader presses, use [Kbd](/docs/basic-components/kbd), and for a phrase that only needs emphasis, a [Mark](/docs/basic-components/mark) highlight.

## Basic usage

Inline code snippets with default styling. `Code` aligns to the surrounding text's baseline, so it lines up with the text it's inlined in. Pass `href` to render `Code` as `<a>` instead of `<code>`. A focus-visible outline auto-enables when `href` is set.

```tsx demo
<Col>
  <Text>Run <Code>npm install</Code> to install packages.</Text>
  <Text>Declare a constant with <Code>{"const x = 'value'"}</Code>.</Text>
</Col>
```

## Sizes

Code elements in different sizes: `xs`, `sm`, `md`, `lg`, `xl`.

```tsx demo
<Col>
  <Text>Size xs: <Code xs>{"console.log('Hello')"}</Code></Text>
  <Text>Size sm: <Code sm>{"console.log('Hello')"}</Code></Text>
  <Text>Size md: <Code md>{"console.log('Hello')"}</Code></Text>
  <Text>Size lg: <Code lg>{"console.log('Hello')"}</Code></Text>
  <Text>Size xl: <Code xl>{"console.log('Hello')"}</Code></Text>
</Col>
```

## Appearances

Different appearance colors for inline code.

```tsx demo
<Row flexWrap>
  <Code primary>primary code</Code>
  <Code accent>accent code</Code>
  <Code secondary>secondary code</Code>
  <Code tertiary>tertiary code</Code>
  <Code success>success code</Code>
  <Code danger>danger code</Code>
  <Code warning>warning code</Code>
  <Code info>info code</Code>
  <Code inheritAppearance>inheritAppearance code</Code>
</Row>
```

## Variants

Code defaults to `outline` variant. Use `filled` for stronger visual emphasis.

```tsx demo
<Col>
  <Text>Default outline: <Code>npm install</Code> then <Code>npm run build</Code></Text>
  <Text>Filled: <Code filled>git commit</Code> then <Code filled>git push</Code></Text>
  <Text>Colored filled: <Code success filled>200 OK</Code> <Code danger filled>500 Error</Code> <Code warning filled>deprecated</Code></Text>
</Col>
```

## Shapes

Code supports `rounded` (default), `pill`, and `sharp` border-radius styles.

```tsx demo
<Row flexWrap>
  <Code rounded>rounded</Code>
  <Code pill>pill</Code>
  <Code sharp>sharp</Code>
</Row>
```

## Code in context

Code elements used within text content.

```tsx demo
<Col>
  <Text>
    To create a new React component, use <Code>{"function Component() {}"}</Code> or
    the arrow function syntax <Code>{"const Component = () => {}"}</Code>.
  </Text>
  <p>
    Install the package with <Code primary>npm i @vaneui/ui</Code> and then
    import it using <Code secondary>{'import { Button } from "@vaneui/ui"'}</Code>.
  </p>
  <Text>
    The <Code info>useState</Code> hook returns an array with two elements:
    the current state value and a setter function like <Code info>[state, setState]</Code>.
  </Text>
</Col>
```

## In heading context

Code uses an em-based geometry pipeline: at the default `md` size it renders at ~87.5% of the surrounding text's font-size, with padding and border-radius scaling proportionally. The same `<Code>` looks right inside body copy, a subheading, a section heading, or a page title.

```tsx demo
<Col>
  <Text>Body text with <Code>npm install</Code> inline.</Text>
  <Title>Subheading: run <Code>npm run build</Code> first.</Title>
  <SectionTitle>Section: <Code>useState</Code> hook</SectionTitle>
  <PageTitle><Code>@vaneui/ui</Code></PageTitle>
</Col>
```

## As Link

Add `href` to render the `Code` element as an `<a>` tag. A keyboard focus-visible outline auto-renders so the link is reachable via Tab; opt out with `noFocusVisible`.

```tsx demo
<Col>
  <Text>Install <Code href="https://npmjs.com/package/@vaneui/ui" target="_blank" rel="noopener noreferrer">@vaneui/ui</Code> from npm.</Text>
  <Text>The <Code href="https://react.dev/reference/react/useState" target="_blank" rel="noopener noreferrer" info>useState</Code> hook returns a value and a setter.</Text>
  <Text>See the <Code href="/docs/getting-started/installation" filled success>install guide</Code> for setup steps.</Text>
</Col>
```

## Next.js Link integration

Use the `tag` prop to render `Code` as a Next.js `Link` for client-side navigation.

```tsx
import Link from 'next/link';
import { Code, Text } from '@vaneui/ui';

<Text>
  Read the <Code href="/docs/getting-started" tag={Link}>getting started guide</Code> for setup steps.
</Text>
```

## Frequently asked questions

### How do I show a multi-line code block?

Code is for inline snippets: it renders a `<code>` whose text does not wrap. For a listing of several lines, render a `<pre>` through your own syntax highlighter, or a `Text` with `tag="pre"` and `fontMono` for plain output such as a log. With [@vaneui/md](/docs/getting-started/markdown), a fenced code block in Markdown renders as a code block on its own.

### Why does inline code overflow its container on a phone?

Code defaults to `whitespaceNowrap`, so a long command or package name never breaks and can run past a narrow column. Pass `whitespaceNormal` to let it wrap at spaces, and add `breakWords` or `breakAll` for a long string with no spaces, such as a URL or a hash. Code takes the same `whitespace` and `wordBreak` props as the other inline components.

### Why is inline code gray when I set no appearance?

Code defaults to the `secondary` appearance, a neutral tint. Pass another appearance, such as `info` or `success`, on a snippet that should stand out, as in [Appearances](#appearances). To change the default for an app, set `themeDefaults={{ code: { info: true } }}` on a `ThemeProvider`; Code has a flat theme node.

## Props

| Prop | Category | Default | Description |
| --- | --- | --- | --- |
| `primary` | Appearance |  | Primary color appearance (gray) |
| `accent` | Appearance |  | Accent color appearance (rose) |
| `secondary` | Appearance | yes | Secondary color appearance (gray) |
| `tertiary` | Appearance |  | Tertiary color appearance |
| `success` | Appearance |  | Success color appearance (green) |
| `danger` | Appearance |  | Danger color appearance (red) |
| `warning` | Appearance |  | Warning color appearance (amber) |
| `info` | Appearance |  | Info color appearance (cyan) |
| `inheritAppearance` | Appearance |  | Inherit appearance from parent — suppresses own data-appearance/data-variant, uses parent's CSS variables |
| `disabled` | Disabled |  | Disable the component — reduces opacity, changes cursor to not-allowed, and prevents interaction |
| `flex1` | Flex |  | Take up remaining space (= `flex-1`, i.e. `flex: 1 1 0%`) |
| `flexAuto` | Flex |  | Grow but respect intrinsic size (= `flex-auto`, i.e. `flex: 1 1 auto`) |
| `flexNone` | Flex |  | Don't grow and don't shrink (= `flex-none`, i.e. `flex: none`) |
| `fontSans` | Font Family |  | Sans-serif font family (default) — emits font-sans |
| `fontSerif` | Font Family |  | Serif font family — emits font-serif |
| `fontMono` | Font Family | yes | Monospace font family — emits font-mono |
| `fontHeading` | Font Family |  | Heading font family (defaults to sans, customizable via --font-heading) — emits font-heading |
| `italic` | Font Style |  | Italic font style |
| `notItalic` | Font Style |  | Not italic (normal) font style |
| `fontThin` | Font Weight |  | Thin font weight (100) — emits font-thin |
| `fontExtralight` | Font Weight |  | Extra light font weight (200) — emits font-extralight |
| `fontLight` | Font Weight |  | Light font weight (300) — emits font-light |
| `fontNormal` | Font Weight | yes | Normal font weight (400) — emits font-normal |
| `fontMedium` | Font Weight |  | Medium font weight (500) — emits font-medium |
| `fontSemibold` | Font Weight |  | Semibold font weight (600) — emits font-semibold |
| `fontBold` | Font Weight |  | Bold font weight (700) — emits font-bold |
| `fontExtrabold` | Font Weight |  | Extra bold font weight (800) — emits font-extrabold |
| `fontBlack` | Font Weight |  | Black font weight (900) — emits font-black |
| `inheritSize` | Inherit Size |  | Inherit font-size and line-height from the nearest parent typography element |
| `noInheritSize` | Inherit Size |  | Keep own font-size; do not inherit from parent |
| `padding` | Padding | yes | Enable internal padding |
| `paddingX` | Padding |  | Enable only horizontal padding |
| `paddingY` | Padding |  | Enable only vertical padding |
| `noPadding` | Padding |  | Disable internal padding |
| `pill` | Shape |  | Fully rounded corners (circular) |
| `sharp` | Shape |  | No rounded corners (square) |
| `rounded` | Shape | yes | Medium rounded corners (default) |
| `noShrink` | Shrink |  | Prevent the flex item from shrinking below its content size (= `shrink-0`) |
| `xs` | Size |  | Extra small size |
| `sm` | Size |  | Small size |
| `md` | Size | yes | Medium size (default) |
| `lg` | Size |  | Large size |
| `xl` | Size |  | Extra large size |
| `skeleton` | Skeleton |  | Show the component as a loading placeholder in its own size and shape. On a container, every component inside it becomes a placeholder too |
| `textLeft` | Text Align |  | Align text to left (physical side, does not flip under RTL) |
| `textCenter` | Text Align |  | Align text to center |
| `textRight` | Text Align |  | Align text to right (physical side, does not flip under RTL) |
| `textJustify` | Text Align |  | Justify text |
| `textStart` | Text Align |  | Align text to the reading-direction start (left in LTR, right in RTL) |
| `textEnd` | Text Align |  | Align text to the reading-direction end (right in LTR, left in RTL) |
| `underline` | Text Decoration |  | Add underline decoration below text |
| `lineThrough` | Text Decoration |  | Add strikethrough/line-through decoration across text |
| `noUnderline` | Text Decoration |  | Remove ALL text-decoration — underline, line-through, and overline (text-decoration: none) |
| `overline` | Text Decoration |  | Add overline decoration above text |
| `uppercase` | Text Transform |  | Transform text to uppercase |
| `lowercase` | Text Transform |  | Transform text to lowercase |
| `capitalize` | Text Transform |  | Capitalize first letter of each word |
| `normalCase` | Text Transform |  | Normal text case (no transformation) |
| `filled` | Variant |  | Filled variant - solid background with contrasting text color |
| `outline` | Variant | yes | Outline variant - transparent background with border and colored text (default) |
| `ghost` | Variant |  | Ghost variant - transparent background, no border, appearance-colored text, tinted hover background |
| `breakNormal` | Word Break |  | Reset breaking - overflow-wrap and word-break normal |
| `breakWords` | Word Break |  | Break long words to prevent overflow - overflow-wrap: break-word |
| `breakAll` | Word Break |  | Break between any two characters - word-break: break-all |
| `breakKeep` | Word Break |  | Do not break CJK text - word-break: keep-all |

Layout and utility props (Border, Cursor, Display, Flex Direction, Focus Visible, Gap, Height, Hide, Items, Justify, Overflow, Position, Responsive Sizing, Responsive Width, Ring, Shadow, Show, Transition, Transparent, Whitespace, Width, Wrap) are listed on [Common Props](https://vaneui.com/docs/reference/common-props.md).
