# Snackbar

A small floating toolbar anchored in visual proximity to another element on the page (the "target"), built on the native Popover and CSS Anchor Positioning APIs.

```html
<ixfx-snackbar for="something">
  <button>Bold</button>
</ixfx-snackbar>
<div id="something">Hello</div>
```

The user sees "Hello". Clicking it shows the snackbar's toolbar anchored above it.

---

## `ixfx-snackbar`

Unlike `ixfx-menu-trigger` / `ixfx-colour-picker-popup`, the trigger is not slotted content inside the component — it's an external element referenced by `for`. The host element itself is the popover (`popover="auto"`); there's no inner wrapper div, since the entire slotted content is the panel.

### Properties / attributes

| Attribute / Property | Type | Default | Description |
|---|---|---|---|
| `for` | string | `''` | `id` of the target element to anchor to and attach interaction listeners on |
| `mode` | `'explicit'` \| `'manual'` \| `'implicit'` | `'explicit'` | How the snackbar is invoked (see below) |
| `placement` | `'top'` \| `'bottom'` \| `'left'` \| `'right'` | `'top'` | Which side of the target the panel appears on; reflected attribute |
| `dismiss` | `'auto'` \| `'manual'` | `'auto'` | Whether a click bubbling up from inside the snackbar closes it (see below) |
| `show-delay` | number (ms) | `150` | `implicit` mode only — debounce before showing once the target/panel is tickled |
| `hide-delay` | number (ms) | `500` | `implicit` mode only — delay before auto-hiding once the target/panel is no longer tickled |

### Modes

| Mode | Behavior |
|---|---|
| `explicit` | Clicking the target toggles the snackbar. If the target has no existing `tabindex`/focusability, `tabindex="0"` and `role="button"` are added so it's keyboard-operable too (removed again if `for` changes or the component disconnects). |
| `implicit` | Hovering or focusing the target shows the snackbar after `show-delay` ms (cancelled if the pointer/focus leaves before then, so a quick pass-over doesn't open it); it auto-hides `hide-delay` ms after the pointer/focus leaves, unless the pointer enters or focus moves into the panel itself first. |
| `manual` | No automatic listeners are attached to the target at all — only `.show()` / `.hide()` / `.toggle()` calls open or close it. |

In all three modes, native popover light-dismiss still applies: Escape or clicking outside closes the snackbar.

Only one `ixfx-snackbar` is ever open at a time on a page, regardless of how each was invoked — this comes for free from `popover="auto"` (every unrelated auto popover is closed as part of the platform's "show popover" algorithm whenever a new one shows), not from any bookkeeping in this component.

### Dismiss

| `dismiss` | Behavior |
|---|---|
| `auto` (default) | Any `click` event that bubbles up from inside the snackbar (e.g. clicking a toolbar button) closes it — the common "pick an action, panel closes" pattern. A control that needs to stay open after being clicked can call `event.stopPropagation()`. |
| `manual` | Clicks inside the snackbar are ignored; it's only closed by calling `.hide()` (or native light-dismiss — Escape / click-outside). |

This is independent of `mode` — it governs how the snackbar closes, not how it opens.

### Methods

- `show()`, `hide()`, `toggle()` — thin wrappers over the native `showPopover()`/`hidePopover()`/`togglePopover()`.

Because the popover is `auto`, clicking outside (or pressing Escape) closes it *before* the click that triggered the dismissal dispatches. `toggle()` accounts for this: if a light-dismiss close happened in the same click, the toggle is a no-op rather than immediately re-opening the snackbar — so a target/button that calls `.toggle()` still works as an open/close switch even though the trigger lives outside the popover.

### Getters

- `isOpen: boolean`

### Slots

| Slot | Description |
|---|---|
| default | Toolbar content (buttons, etc.), laid out as a flex row |

### Events

No custom events. Because the host itself is the popover, the native `toggle` (`ToggleEvent`) fires directly on `ixfx-snackbar`:

```js
document.querySelector('ixfx-snackbar').addEventListener('toggle', e => {
  console.log(e.newState); // 'open' | 'closed'
});
```

### CSS custom properties

Visual chrome comes from `popupSurfaceStyles` (shared with `ixfx-menu-trigger` / `ixfx-colour-picker-popup`):

| Property | Description |
|---|---|
| `--surface-5` | Background |
| `--surface-6` | Border color |
| `--surface-5-text` | Text color |
| `--shadow-m` | Drop shadow |
| `--radius-l` | Corner radius |
| `--space-s` | Padding / gap between slotted items and between panel and target |

### Constraints

- The target element referenced by `for` must exist in the DOM (by `id`, resolved against `getRootNode()`) whenever `for` is set or changes — there's no polling/observation for a target that appears later.
- In `implicit` mode, the target must already be focusable for the focus-triggered (keyboard) path to work; `ixfx-snackbar` only auto-adds `tabindex`/`role` in `explicit` mode.
