# Web SDK: The avatar editor element

`<sona-avatar-editor>`, the player-facing avatar creator as one element ([guide](../../guides/web-editor.md)). `defineAvatarEditor()` registers it; the page opens it, the player makes or changes an avatar, and `open()` resolves with the manifest.

```js
import { defineAvatarEditor } from './sona-renderer.js';

defineAvatarEditor();   // registers <sona-avatar-editor>
const editor = document.createElement('sona-avatar-editor');
document.body.append(editor);
editor.sceneOptions = { appId: 'your-app' };
const manifest = await editor.open({ mode: 'quick' });
if (manifest) save(manifest);   // null when the player cancelled
```

## SonaAvatarEditor properties

| Field | Type | Meaning |
|---|---|---|
| `mode` | `AvatarEditorMode` | The screen. |
| `manifest` | `string \| null` | The manifest being edited (base64), or the attribute while closed. |
| `isOpen` | `boolean` | True while open. Read only. |
| `changed` | `boolean` | True when the avatar differs from the one it opened on. Read only. |
| `scene` | `SonaScene \| null` | The editor's scene while open (the SDK scene it draws in), or null. Read only. |
| `sceneOptions` | `Partial<CreateOptions>` | Options passed to SonaRenderer.create for the editor's scene (appId, host, pixelRatio, ...). Set before open. |
| `gamepad` | `boolean` | Read the standard gamepad while open. Default true. |
| `startScreen` | `'body' \| 'appearance' \| 'wardrobe'` | The full creator's first screen: "body" (default), "appearance" or "wardrobe". Set before open. |
| `itemFilter` | `((row: WardrobeItem) => boolean) \| null` | (row) => false leaves an item out of every list and every generated look. Set before open. |

## editor.open()

```ts
interface SonaAvatarEditor {
  open(
    opts?: { mode?: AvatarEditorMode; manifest?: string },
  ): Promise<string | null>;
}
```

Opens the editor. Resolves with the finished manifest on Done, or null on Cancel.

| Parameter | Type | Meaning |
|---|---|---|
| `opts.mode` (optional) | `AvatarEditorMode` | The screen. Default: the mode attribute, else "full". |
| `opts.manifest` (optional) | `string` | The manifest to start from. Default: the manifest attribute; none starts a new avatar. |

Returns `Promise<string | null>`.

## editor.done()

```ts
interface SonaAvatarEditor {
  done(): void;
}
```

Done: fires done with the manifest and closes.

## editor.cancel()

```ts
interface SonaAvatarEditor {
  cancel(): void;
}
```

Cancel: fires cancel and closes.

## editor.close()

```ts
interface SonaAvatarEditor {
  close(): void;
}
```

Closes without an event (the open promise gives null).

## editor.undo()

```ts
interface SonaAvatarEditor {
  undo(): void;
}
```

Undo the last edit.

## editor.redo()

```ts
interface SonaAvatarEditor {
  redo(): void;
}
```

Redo.

## editor.addEventListener()

```ts
interface SonaAvatarEditor {
  addEventListener<K extends keyof AvatarEditorEventMap>(
    type: K,
    fn: (e: AvatarEditorEventMap[K]) => void,
    opts?: boolean | AddEventListenerOptions,
  ): void;
  addEventListener(
    type: string,
    fn: EventListenerOrEventListenerObject,
    opts?: boolean | AddEventListenerOptions,
  ): void;
}
```

- `K`: Listens for one of the editor's events, its detail typed by AvatarEditorEventMap.
- `string`: Any other event, as on any element.

## defineAvatarEditor

```ts
declare const defineAvatarEditor: (
  name?: string,
) => SonaAvatarEditorConstructor;
```

Registers the avatar editor element, by default as `<sona-avatar-editor>`, and returns its class. Call once per page; a second call returns the same class.

## AvatarEditorMode

```ts
type AvatarEditorMode = 'full' | 'quick' | 'wardrobe';
```

The three screens: the whole creator, the quick creator (eight generated looks, then one tweak) and the wardrobe picker.

## AvatarEditorEventMap

The detail of the editor's events.

| Field | Type | Meaning |
|---|---|---|
| `open` | `CustomEvent<{ mode: AvatarEditorMode }>` | The editor opened (it then loads). |
| `change` | `CustomEvent<{ manifest: string; change: { op: string; [k: string]: unknown } }>` | An edit, an undo, a redo or a chosen look. `manifest` is the avatar now (base64). |
| `done` | `CustomEvent<{ manifest: string }>` | Done: the finished manifest. The editor closes. |
| `cancel` | `CustomEvent<Record<string, never>>` | Cancelled. The editor closes. |
| `error` | `CustomEvent<{ error: unknown; code: string \| null; message: string }>` | The scene or the catalogue could not be loaded, or the preview failed. |
