# HTML Widgets

## Add to a slide

```ts
const slide = presentation.slides.getItem(0);
const widget = slide.widgets.add({
  name: "interactive-content",
  html: `<!doctype html><button onclick="this.textContent='Selected'">Choose</button>`,
  position: { left: 72, top: 120, width: 640, height: 360 },
});
```

Use nonempty HTML with valid Unicode. Geometry uses slide pixels. HTML runs in
compatible interactive viewers; static rendering uses the optional preview image
or a placeholder.

## Read, edit, and inspect

```ts
const widgets = slide.widgets.items;
const selected = slide.widgets.getById(widget.id);
const originalHtml = widget.html;
widget.html = "<!doctype html><button>Updated</button>";
widget.position = { left: 80, top: 128, width: 640, height: 360 };
widget.interactionDisabled = false;

const inspection = await presentation.inspect({
  select: '$.slides[*].elements[?(@.kind == "widget")]',
});
const records = inspection.records;
const savedHtml = widget.html;
```

Inspection exposes widget IDs, names, frames, asset references, preview metadata,
and interaction settings. Read `widget.html` to verify the exact source. Replacing
HTML affects this widget while other widgets retain their original source.

## Static preview

```ts
const preview = presentation.images.add({
  data: previewBytes,
  contentType: "image/png",
});
widget.preview = {
  assetId: preview.id,
  contentType: "image/png",
  widthPx: 640,
  heightPx: 360,
};
```

`previewBytes` contains the preview image's bytes. Preview images do not execute
the HTML.

## Remove

```ts
widget.delete();
// Or: slide.widgets.deleteById(widgetId);
```

## Creation options

```ts
type WidgetAddOptions = {
  html: string;
  name?: string;
  position?: {
    left?: number;
    top?: number;
    width?: number;
    height?: number;
    rotation?: number;
    horizontalFlip?: boolean;
    verticalFlip?: boolean;
  };
  interactionDisabled?: boolean;
  preview?: {
    assetId: string;
    fallbackAssetId?: string;
    contentType?: string;
    widthPx?: number;
    heightPx?: number;
  };
};
```

Preview asset IDs refer to images in `presentation.images`; pixel dimensions,
when supplied, are positive integers.
