# Icon Registry

A centralized SVG icon system for `@ixfx/components`. Icons are stored by name, can be overridden globally, and all components that use a given icon re-render automatically when it changes.

## Built-in icons

All built-ins are [Lucide](https://lucide.dev/icons/) icons (stroke-based, `viewBox 0 0 24 24`, `fill="none"`, colored via `currentColor`), registered in `src/icons/defaults.ts`.

| Name | Lucide icon | Used by |
|---|---|---|
| `chevron-down` | [chevron-down](https://lucide.dev/icons/chevron-down) | `<ixfx-split-button>`, `<ixfx-panel>` |
| `chevron-right` | [chevron-right](https://lucide.dev/icons/chevron-right) | `<ixfx-split-button>` |
| `chevron-up` | [chevron-up](https://lucide.dev/icons/chevron-up) | `<ixfx-data-grid>` sort indicator |
| `chevron-left` | [chevron-left](https://lucide.dev/icons/chevron-left) | `<ixfx-incr-search>` prev-match |
| `caret-right` | [chevron-right](https://lucide.dev/icons/chevron-right) (CSS mask) | tree-basic expand indicator |
| `check` | [check](https://lucide.dev/icons/check) | `<ixfx-menu-check-item>` |
| `close` | [x](https://lucide.dev/icons/x) | `<ixfx-panel>`, `<ixfx-split-button>`, `<ixfx-incr-search>` |
| `add` | [plus](https://lucide.dev/icons/plus) | Available for use |
| `error` | [circle-alert](https://lucide.dev/icons/circle-alert) | Available for use |
| `info` | [info](https://lucide.dev/icons/info) | Available for use |
| `warning` | [triangle-alert](https://lucide.dev/icons/triangle-alert) | Available for use |
| `file` | [file](https://lucide.dev/icons/file) | Available for use |
| `folder` | [folder](https://lucide.dev/icons/folder) | Available for use |
| `folder-up` | [folder-up](https://lucide.dev/icons/folder-up) | Available for use |
| `search` | [search](https://lucide.dev/icons/search) | Available for use |
| `menu` | [menu](https://lucide.dev/icons/menu) | `<ixfx-multicol-list>` column options |
| `ellipsis-vertical` | [ellipsis-vertical](https://lucide.dev/icons/ellipsis-vertical) | `<ixfx-menu-trigger>` default glyph |

## Overriding a built-in icon

Call `registerIcon` before or after the library loads — components re-render automatically.

```ts
import { registerIcon } from '@ixfx/components';

registerIcon('chevron-down', `<svg viewBox="0 0 24 24" fill="currentColor">
  <path d="M7 10l5 5 5-5H7z"/>
</svg>`);
```

The split-button's toggle arrow updates immediately on the next render cycle.

## Adding a custom icon

```ts
import { registerIcon } from '@ixfx/components';

registerIcon('my-logo', `<svg viewBox="0 0 24 24" fill="currentColor">
  <path d="..."/>
</svg>`);
```

Then use it anywhere in your app:

```html
<ixfx-icon name="my-logo"></ixfx-icon>
```

## `<ixfx-icon>` element

Renders an icon from the registry by name. Re-renders automatically if the icon is replaced via `registerIcon`.

```html
<!-- Basic usage -->
<ixfx-icon name="chevron-down"></ixfx-icon>

<!-- Fixed pixel size -->
<ixfx-icon name="close" size="32"></ixfx-icon>

<!-- Accessible label (removes aria-hidden, adds aria-label) -->
<ixfx-icon name="close" label="Close dialog"></ixfx-icon>

<!-- Rotate the icon (e.g. for expand/collapse chevrons) -->
<ixfx-icon name="chevron-down" rotate="-90deg"></ixfx-icon>

<!-- Prominence mode for idle/tickled/disabled states -->
<ixfx-icon name="settings" prominence></ixfx-icon>
```

### Attributes

| Attribute | Type | Default | Description |
|---|---|---|---|
| `name` | `string` | `''` | Registry key of the icon to render |
| `size` | `number` | — | Explicit `width`/`height` in px. Omit to size via CSS (`1em × 1em` default) |
| `label` | `string` | `''` | Accessible label. When set, adds `aria-label` and removes `aria-hidden` |
| `rotate` | `string` | `''` | CSS angle (e.g. `"90deg"`, `"-90deg"`) to rotate the icon. Animatable with `--transition` |
| `prominence` | `boolean` | `false` | When set, enables prominence styling (opacity transitions on hover/disabled) |

### Sizing via CSS

By default `<ixfx-icon>` is `1em × 1em`, inheriting the surrounding font size. Override with CSS:

```css
ixfx-icon {
  width: 24px;
  height: 24px;
}
```

Or inline:

```html
<ixfx-icon name="check" style="width:20px;height:20px"></ixfx-icon>
```

### Rotation

Use the `rotate` attribute to rotate an icon by a CSS angle. The rotation is applied as a CSS transform and is animatable:

```html
<!-- Collapsed state: chevron points right -->
<ixfx-icon name="chevron-down" rotate="-90deg"></ixfx-icon>

<!-- Expanded state: chevron points down (no rotation) -->
<ixfx-icon name="chevron-down"></ixfx-icon>
```

The rotation uses a CSS variable internally, so it transitions smoothly when the attribute changes (requires `--transition` to be set).

### Prominence mode

The `prominence` attribute enables automatic opacity styling for idle, hover/tickled, and disabled states. When present, the icon uses CSS custom properties:

```css
ixfx-icon {
  opacity: var(--prominence-idle-opacity, 1);   /* default */
}

ixfx-icon:hover {
  opacity: var(--prominence-tickled-opacity, 1);
}

ixfx-icon[disabled] {
  opacity: var(--prominence-disabled-opacity, 0.5);
}
```

Without `prominence`, the icon uses `--opacity` (defaults to 1).

## CSS custom properties

| Property | Default | Description |
|---|---|---|
| `--opacity` | `1` | Icon opacity when `prominence` is NOT set |
| `--prominence-idle-opacity` | `0.6` | Opacity when `prominence` is set and idle |
| `--prominence-tickled-opacity` | `1` | Opacity on hover (when `prominence` is set) |
| `--prominence-disabled-opacity` | `0.5` | Opacity when disabled (when `prominence` is set) |
| `--transition` | `0.15s ease-out` | Transition duration for opacity and rotation |

## API

```ts
import { registerIcon, getIcon, hasIcon, svgToDataUri, iconBus } from '@ixfx/components';

// Add or replace an icon. Notifies all subscribers.
registerIcon(name: string, svg: string): void

// Retrieve an SVG string by name. Returns undefined if not found.
getIcon(name: string): string | undefined

// Check whether an icon is registered.
hasIcon(name: string): boolean

// Convert an SVG string to a CSS data-URI (e.g. for mask-image).
// svgToDataUri('<svg .../>') → 'url("data:image/svg+xml,...")'
svgToDataUri(svg: string): string

// EventTarget that emits 'ixfx-icon-change' CustomEvents when any icon changes.
// event.detail.name contains the changed icon name.
iconBus: EventTarget
```

## SVG format conventions for custom icons

Match [Lucide](https://lucide.dev/icons/)'s style so custom icons sit consistently alongside the built-ins:

- `viewBox="0 0 24 24"` — consistent with built-ins
- No `width` or `height` attributes on `<svg>` — let CSS control sizing
- `fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"` for inline icons — inherits CSS `color`
- `stroke="black"` (same attributes otherwise) for CSS mask icons — color comes from `background-color`
- `aria-hidden="true"` on the `<svg>` itself — the `<ixfx-icon>` wrapper handles accessibility

## Subscribing to icon changes

Use `iconBus` if you need to react to registry changes outside of a component:

```ts
import { iconBus } from '@ixfx/components';
import type { IconChangedEvent } from '@ixfx/components';

iconBus.addEventListener('ixfx-icon-change', (e) => {
  const { name } = (e as IconChangedEvent).detail;
  console.log(`Icon "${name}" was updated`);
});
```

## Importing raw SVG strings

The built-in SVG strings are exported as constants if you need them directly:

```ts
import { ICON_CHEVRON_DOWN, ICON_CARET_RIGHT, ICON_CHECK, ICON_CLOSE } from '@ixfx/components';
```

## Notes

- The registry is a module singleton. If multiple copies of `@ixfx/components` are loaded in the same page (e.g. version mismatch in a monorepo), each copy has its own registry and bus — overrides in one will not propagate to the other.
- `registerIcon` uses `unsafeHTML` internally via `<ixfx-icon>`. Never pass untrusted user input as an SVG string.
