# keycaps

Each letter sits on a 3D mechanical keycap that presses down as if the word were typed, with a click under the pointer.

The heading becomes a row of real 3D keycaps on a darker keyboard case, seen at an angle in the page perspective. Each cap is rounded, slightly dished and tapered, lit like plastic with a soft sheen, and the letter is printed on its top in the text color; a space becomes a wide space bar. When the heading comes into view the keys go down one after another as if someone typed the word: a quick press, a small bounce back and a flash of light on each. Keys under the pointer sink a little and a click sends one down hard. Modern sculpted caps with an accent key, beige retro caps, or dark caps with an RGB glow under them.

Module `text`, category Text. Import `@dioramajs/text/keycaps`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/text/src/keycaps.ts)

A keyboard maker's launch hero with the words Type less on white 3D keycaps that press down one after another as if typed.

```html title="index.html"
<section class="kc-launch">
  <p class="kc-kicker">Kestrel 75 · gasket-mount mechanical keyboard</p>
  <h2 class="kc-title" data-dio="keycaps">Type less</h2>
  <p class="kc-line">Lubed linear switches, double-shot PBT caps and a milled aluminum case. Ships in March, preorders open today.</p>
</section>
```

## Usage

Put it on: a short heading, a word mark or a button label. Not img, video, canvas, svg or form fields.

`start()` registers `keycaps`, so the attribute alone puts it on an element.

```html
<h1 data-dio="keycaps">Type it</h1>
```

In page transitions: Fades with the page.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'keycap' \| 'retro' \| 'glow'` | `'keycap'` | keycap draws modern sculpted caps in neutral colors with an accent key; retro thick beige caps, a little yellowed; glow dark caps with RGB light under them. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'view' \| 'hover' \| 'loop'` | `'view'` | view types the word once in view; hover types it when the pointer comes over the keys; loop types it again every few seconds. |
| `depth` | `data-dio-depth` | `number` 0 to 0.5 | `0.22` | Key travel as a share of the key size; 0 keeps the keys still. |
| `gap` | `data-dio-gap` | `number` 0 to 0.3 | `0.07` | Space between neighboring keys as a share of the key size. |
| `radius` | `data-dio-radius` | `number` 0 to 0.5 | `0.16` | Rounding of the cap corners as a share of the key; 0 is square, 0.5 round. |
| `color` (rebuilds) | `data-dio-color` | `string` | `''` | Cap color; empty picks the style color that contrasts with the text. |
| `accent` (rebuilds) | `data-dio-accent` | `string` | `''` | Color of the first key; empty uses the style accent, none matches the others. |
| `speed` | `data-dio-speed` | `number` 2 to 20 | `9` | Typing speed in keys per second. |
| `sound` | `data-dio-sound` | `boolean` | `false` | Plays a short synthesized click per key press, only after the visitor touched the page. |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `style`: `keycap`, `retro`, `glow`
  - `keycap` draws modern sculpted caps in neutral colors with an accent key.
  - `retro` thick beige caps, a little yellowed.
  - `glow` dark caps with RGB light under them.
- `trigger`: `view`, `hover`, `loop`
  - `view` types the word once in view.
  - `hover` types it when the pointer comes over the keys.
  - `loop` types it again every few seconds.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size), [`width`](/docs/api/inputs#input-width), [`x`](/docs/api/inputs#input-x), [`y`](/docs/api/inputs#input-y). Draws only while something on it moves, then stops. Only while keys move or flash; none at rest, when the keyboard stays drawn. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it shows a still frame: motion inputs are zero and time stands still. The keys are drawn but never press, flash or type.

Runs on every device. A tap presses the key under the finger hard; the hover trigger types the word on a tap.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { keycaps: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 14.6 KB the first time a page uses it. GPU: Three instanced draws: a shell of about 600 triangles per cap, switch and the case, and a soft quad per key for its shadow and its light. Memory: An atlas of the letters at twice their CSS size, kept while the element lives.

- low: The legends are rastered at 1 device pixel per CSS pixel instead of 2.

See [the quality plugin](/docs/effects/quality).

## Recipes

### RGB caps for a game launch

`style="glow"` draws dark translucent caps with an RGB light under each one. The light bleeds round the edges of the caps onto the plate and shines through the legends, and its colors run along the row like a rainbow wave while keys are typed or the pointer moves over them. `trigger="loop"` types the word again every few seconds while it is on screen, so a teaser keeps its rhythm.

```html
<h2 data-dio="keycaps" data-dio-style="glow" data-dio-trigger="loop">Press start</h2>
```

### Retro caps on a card

`style="retro"` draws thick beige caps of the 1980s, each yellowed a little differently, with a gray accent key. When the element has a background of its own, the keyboard case takes its place and its color, with its rounded corners; otherwise the case fits round the keys.

```html
<p class="mark" style="background: #3a362f; padding: 0.3em 0.5em; border-radius: 16px" data-dio="keycaps" data-dio-style="retro">1984</p>
```

### A shortcut hint that clicks

`trigger="hover"` types the word only when the pointer comes over the keys, and `sound="true"` plays a short synthesized click for each press. The sound only starts after the visitor has clicked, tapped or typed on the page, as browsers require, and it is off by default.

```html
<p data-dio="keycaps" data-dio-trigger="hover" data-dio-sound="true">Ctrl K</p>
```

## Notes

Each key is a real 3D cap in the page’s perspective: rounded, tapered toward a slightly dished top, lit as plastic with a soft sheen, on a switch, with a soft shadow on a darker plate. The keyboard leans back a little, so the fronts of the caps show and a press reads as travel. The letter is printed on the top in the text color, and a space between words becomes a wide space bar. With `style="keycap"` the caps contrast with the text: dark text sits on white caps and light text on graphite ones, the first letter on an accent key.

Keys are about 1.35 em wide, wider than the letters, so give the element room: the case grows past a narrow box, aligned the way the text is, and the keys shrink when the container is too narrow. Keep it to a short word or phrase; over 240 letters the text stays as it is.

The element keeps its text as you wrote it, so screen readers, search and find in page see the real words, while the keyboard stays drawn in their place. Frames run only while keys move or flash; at rest the keyboard draws nothing new. Under reduced motion the keys are drawn but never press. Text with `text-shadow`, `-webkit-text-stroke`, `background-clip: text` or a vertical writing mode stays as it is, with a warning in development. `style`, `trigger`, `color` and `accent` rebuild the effect when they change.

## Related

- [assemble](/docs/effects/assemble.md): Thousands of lit particles fly out of the depth around the page and gather into a heading.
- [blinds](/docs/effects/blinds.md): A heading printed across real 3D slats that turn over in a wave, lit and casting shadows, to show it.
- [counter](/docs/effects/counter.md): Stats count up to their value on real 3D mechanics: odometer drums, slot machine reels or flip cards.
- [dust](/docs/effects/dust.md): A heading crumbles into fine sand that lifts off the page and drifts away in depth as it scrolls out, or on a click.
- [emboss](/docs/effects/emboss.md): Letters pressed into or raised out of paper, leather, brushed metal, stone or plaster, lit by a light that follows the pointer.
- [extrude](/docs/effects/extrude.md): A headline becomes a solid 3D block: beveled letters with lit side walls, seen in perspective, that turn toward the pointer.
- [fire](/docs/effects/fire.md): The letters burn: flames rise from their tops, sparks drift up in depth and the firelight flickers on the page.
- [hologram](/docs/effects/hologram.md): A heading becomes a 3D hologram: projected from a glowing emitter, built of slices in depth, crossed by scan lines.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
