import { Component } from '@angular/core';
import { WrTypewriter } from 'ngwr/animations/typewriter';
@Component({ imports: [WrTypewriter] })
export class MyComponent {}Typewriter
A line typed character by character behind a caret, then held, deleted and followed by the next, or typed once and left standing. Nothing around it moves while it types.
Installation
Import the class, and @use the entry point in your global styles. Nothing warns you when the @use is missing.
Basic
<wr-typewriter> is inline, so it sits in a paragraph like text. Pass the lines as lines; the first is what the server and a still page show. It types once most of the host is in view, to a rhythm of 35 to 55 ms per character varied from a hash of the text, so the same line always types the same way; a space and a punctuation mark add a beat. After the last line the caret blinks three times and fades. The demos on this page set trigger to load, so they type the moment they are made and Replay types them again; see From first paint for why.
<p><wr-typewriter [lines]="['Signal Forms, asserted as a user would fill them.']" /></p>Several lines
With more than one line each finished line stands for hold ms, 1800 unset, is deleted at about twice the typing pace, and the next one follows. The box is as wide and as tall as the longest line wraps to at the current width from the first paint, so what is below never moves.
<p><wr-typewriter [lines]="['Types the first line.', 'Deletes it, then the second.', 'Stops here.']" hold="1200" /></p>Loop
loop cycles through the lines instead of stopping on the last. The typing pauses when the host leaves the viewport or the document is hidden, and resumes where it was.
<p>ngwr is <wr-typewriter [lines]="['accessible.', 'tree-shakable.', 'zoneless.']" loop /></p>From first paint
trigger="view", the default, types once most of the host is in view and leaves a host that was already in view when it booted whole, so a reload mid-page never erases a line the reader is looking at: a view demo on a docs page would stand finished, which is why the demos here use load. trigger="load" types regardless, for a hero. delay is the pause before the first character. The caret width, its blink and its fade are CSS variables.
<h1><wr-typewriter [lines]="['Typed from first paint.']" trigger="load" delay="300" /></h1>.hero p {
--wr-typewriter-caret: 3px;
--wr-typewriter-blink: 0.8s;
}Accessibility
| Part | Behaviour |
|---|---|
| Screen readers | The box is aria-hidden and a visually hidden copy beside it carries every line once, so a reader hears the text, never a letter at a time. The copy is out of the selection, so a copy of the line gives the typed text. |
| Reduced motion | Under prefers-reduced-motion: reduce the first line stands whole with no caret. The demos on this page follow the same setting. |
| Server | The server renders the first line whole, readable without script, and that is what a still page shows, so nothing swaps at hydration. Write the first line as the one that stands alone. |
| Layout | Every line is laid out in the same cell, the ones not on show invisible, so the box takes the longest line's size from the first paint and a centred or wrapped line never jitters. |
API
| Name | Description | Type | Default |
|---|---|---|---|
linesrequired | The lines, in the order they type. The first is what the server and a still page show. | readonly string[] | — |
loop | Cycle through the lines instead of stopping on the last. | boolean | false |
hold | How long a finished line stands before it is deleted for the next, in ms. | number | 1800 |
delay | Pause before the first character, in ms, from the moment the host is in view: room for the eye to land above it. | number | 0 |
trigger | view types once most of the host is in view and leaves a host that was already in view at boot whole. load types at once, wherever the host is. | WrTypewriterTrigger | 'view' |
CSS variables
Set these on the element itself, with a rule more specific than its Declared on selector. In a global stylesheet, put a class of yours on an ancestor it really has: <body> for an overlay panel, which renders outside your markup. In a component stylesheet the selector alone works, but only for an element in that component's own template. A value on a wrapper or :root is shadowed, because ngwr/animations/typewriter declares each default on its own element.
| Variable | Default | Declared on |
|---|---|---|
--wr-typewriter-blink | 1s | .wr-typewriter |
--wr-typewriter-caret | 2px | .wr-typewriter |
--wr-typewriter-fade | 0.4s | .wr-typewriter |
See also
Animation Rotate RevealOne word of a sentence gives way to the next from a list, in place: the letters leaving rise out of the line while the letters arriving climb in under them, and the slot eases to the new width so the rest of the sentence glides.Animation DecodeA readout locking on: every character shows random glyphs that change quickly, then settles on the real one, left to right.Guide Server-side renderingPrerender and hydrate an ngwr app without mismatches.