# Animated Label

Rolling text changes and width transitions with the complete Zenos Text styling API.

Animated Label rolls between short labels and eases the space they occupy. Its interaction is inspired by [Koala UI's Animated Label](https://www.koalaui.com/docs/components/animated-label), implemented here with native browser animations and Zenos [Text](/components/text/). No additional animation package is needed.

## Install

```bash
pnpm dlx @zentara-co/zenos-cli add animated-label
npx @zentara-co/zenos-cli add animated-label
yarn dlx @zentara-co/zenos-cli add animated-label
bunx @zentara-co/zenos-cli add animated-label
```

The CLI copies `animated-label.tsx` into your chosen directory. Import it from your application-owned path:

```tsx
import { AnimatedLabel } from "./components/animated-label";
```

## In a button

```tsx
import { useId, useState } from "react";
import { Button, Stack, Text } from "@zentara-co/zenos-ui";
import { AnimatedLabel } from "./components/animated-label";

function AnimatedLabelExample() {
  const [expanded, setExpanded] = useState(false);
  const detailsId = useId();
  return (
    <Stack gap="3" align="center">
      <Button
        variant="primary"
        aria-expanded={expanded}
        aria-controls={detailsId}
        onClick={() => setExpanded(!expanded)}
      >
        <AnimatedLabel
          as="span"
          swapKey={expanded ? "less" : "more"}
          weight="medium"
          className="text-inherit"
        >
          {expanded ? "Show less" : "Show more"}
        </AnimatedLabel>
      </Button>
      <Text id={detailsId} size="s" color="subtle" hidden={!expanded}>
        Additional details are visible while the disclosure is expanded.
      </Text>
    </Stack>
  );
}
```

Change `swapKey` when the label's meaning changes. An ordinary rerender leaves the motion alone. Keep icons outside the animated label when they should remain stationary or rotate independently. Use `as="span"` inside buttons so the markup stays phrasing content, and `className="text-inherit"` to inherit the button's foreground color instead of Text's default color.

## Typography and direction

```tsx
import { useState } from "react";
import { Button, Stack } from "@zentara-co/zenos-ui";
import { AnimatedLabel } from "./components/animated-label";

function AnimatedLabelTypographyExample() {
  const [complete, setComplete] = useState(false);
  const label = complete ? "Review complete" : "Pending";
  return (
    <Stack gap="4" align="start">
      <AnimatedLabel
        as="span"
        role="status"
        swapKey={label}
        type="heading"
        size="h3"
        color={complete ? "success" : "default"}
        align="start"
        duration={600}
        direction="up"
      >
        {label}
      </AnimatedLabel>
      <Button variant="primary" onClick={() => setComplete(!complete)}>
        Toggle review status
      </Button>
    </Stack>
  );
}
```

The component accepts every Text styling prop: `as`, `type`, `size`, `weight`, `color`, `align`, `italic`, `truncate`, `maxLines`, `className`, and `style`. Native props and `ref` belong to the rendered Text root. `align` holds the corresponding logical edge during width changes and also controls Text alignment. Use `start` for text in a paragraph or list; the default is `center` for button labels.

`direction="up"` lifts the outgoing label and brings the new one from below. Use `down` to reverse it. `duration` is in milliseconds, and `easing` accepts a CSS easing string. An interrupted transition starts from the width currently on screen.

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| children | ReactNode | — | Current non-interactive label content. |
| swapKey | string \| number | — | Required identity for the displayed label. A change starts a transition. |
| animated | boolean | true | Enables rolling and width transitions. |
| duration | number | 200 | Animation duration in milliseconds. Zero swaps immediately. |
| easing | string | cubic-bezier(0.16, 1, 0.3, 1) | Timing curve for the roll and width transition. |
| direction | up \| down | up | Direction of the outgoing label. |
| align | TextAlign | center | Text alignment and the edge held during width transitions. |
| Text props / ref | TextProps<C> | — | Typography, polymorphic root semantics, native props, and root ref. |

## Accessibility and styling

Only the current label is exposed to assistive technology. The outgoing snapshot is inert, hidden, and removed when motion finishes. Its previous color and font remain in place during the exit. The first render does not animate. Reduced motion or missing browser animation support produces an immediate text change; changing the system preference also stops active transitions.

Use short, non-interactive labels. The rolling viewport clips its contents, so keep focusable controls outside it. Add live-region semantics only when a status needs to be announced. Stable slots are `animated-label`, `animated-label-viewport`, `animated-label-current`, and `animated-label-outgoing`.

Source: https://zenos-ui.pages.dev/recipes/animated-label/