Avatar editor
Choose a pixel buddy’s colour, face and accessories.
Demo
Usage
import { AvatarEditor, PixelBuddy, seededBuddy, type AvatarCustomization } from '@tuttn/slant'
// Controlled: keep what the person picks, then draw it anywhere with PixelBuddy.
const [avatar, setAvatar] = useState<AvatarCustomization>(() => user.avatar ?? seededBuddy(user.id))
<AvatarEditor seed={user.id} name={user.name} value={avatar} onValueChange={setAvatar} />
<PixelBuddy seed={user.id} avatar={avatar} />
// Back to the seeded buddy.
setAvatar(seededBuddy(user.id))
// Uncontrolled: starts from the seeded buddy (or defaultValue) and keeps its own.
<AvatarEditor seed={session.id} name="Release notes" onValueChange={saveAvatar} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
seed | string | — | The seed the buddy is drawn from (a user or session id): it sets the starting buddy and every preview's colours. |
name | ReactNode | — | Shown under the preview: the person's or session's name. |
value | AvatarCustomization | — | The customization, controlled. Leave it out to let the editor keep its own. |
defaultValue | AvatarCustomization | seededBuddy(seed) | The customization an uncontrolled editor starts from. |
onValueChange | (value: AvatarCustomization) => void | — | Called with the whole new customization on every change: a pick, a valid hex colour, the colour picker, Randomize. |
previewSize | number | 156 | The preview's width and height, in pixels. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, the ref and other attributes go on the root, a group named messages.buddy. |
What it saves
The value is an AvatarCustomization: version: 1, a colour that is a palette id ({ kind: 'palette', id: 'teal' }) or a custom #RRGGBB fill ({ kind: 'custom', fill: '#3366CC' }), a face, an accent and an accessory. Palette colours are saved by id, so the palette can grow without breaking saved buddies. Store it as it comes and hand it to PixelBuddy's avatar. To go back to the seeded buddy, set seededBuddy(seed).
Keyboard
Colour, Expression, Accent and Accessory are four radio groups, each named by its visible heading. Tab moves between the groups and reaches only the checked option of each; the arrow keys move and select at once (Left and Right swap under right-to-left text), and Home and End jump to the first and last option. While a custom colour is chosen no swatch is checked, and Tab lands on the first.
Custom colour
Under the swatches, the native colour picker and a hex field choose any colour. The hex field keeps what you type to itself until it is a full #RRGGBB, then commits it; an incomplete value is marked invalid when you leave the field, and Escape puts back the committed colour. A swatch, the picker or Randomize replaces whatever was typed. A custom fill takes whichever face ink reads better on it.
Strings and motion
The headings, colour names, option names and Randomize come from the root's messages (buddyColor, buddyColors, buddyFaces, buddyRandomize and the rest). The preview hops when a pick or Randomize changes it, and every option's buddy hops under the pointer; off screen, and under reduced motion, nothing moves.