# User Theming Guide

This document explains how users of the ixfx components can customize the appearance of components through CSS variables and theme overrides.

## Overview

The ixfx component library uses a CSS variable-based theming system that allows easy customization of colors, spacing, typography, and other visual properties. Rather than hardcoding values, components reference semantic CSS variables that can be overridden to create custom themes.

## How Theming Works

Components use CSS variables for all visual properties. For example:

```css
/* Instead of hardcoded values */
background: #fff;
color: #000;

/* Components use variables */
background: var(--surface-3);
color: var(--surface-3-text);
```

These variables are defined in the base theme (`src/style.css`) and can be overridden at different scopes:
- Globally (affects all components)
- On specific components or containers
- Per-instance using inline styles

## Available Variables

The theming system includes variables for:

### Surfaces
UI elevation levels (`--surface-0` through `--surface-6`) with paired text colors (`--surface-*-text`).

### Content
Background and text colors for user content areas (`--content`, `--content-text`).

### Accent
Primary action colors (`--accent`, `--accent-text`) and surface-based accent variants.

### Feedback/Status
State indicator colors (`--error`, `--error-text`).

### Typography
Font families, weights, and sizes (`--font-family`, `--font-mono`, `--text-xs`, etc.).

### Spacing
Consistent spacing values (`--space-xs`, `--space-s`, `--space-m`, etc.).

### Borders & Radius
Border styles and corner rounding (`--border`, `--border-subtle`, `--radius-s`, etc.).

### Shadows
Elevation effects (`--shadow-m`, `--shadow-l`).

### Theme Fallbacks
All components include a centralized fallback module (`themeFallbacks` in `src/styles/fallbacks.ts`) that registers `@property` rules for every theme variable. This ensures components render correctly even when the base theme CSS isn't loaded — the fallback values mirror the dark theme defaults.

### Interaction
Disabled states and transitions (`--disabled-opacity`, `--transition`).

### Item States
Styling for list/item components (`--item-border`, `--item-bg-selected`, etc.).

### Prominence Styles
Interaction feedback for visual components (`--prominence-idle-opacity`, etc.).

## Customizing Themes

### Global Overrides

To customize themes globally, override variables on the `:root` selector:

```css
:root {
  /* Change accent color */
  --accent: oklch(60% 0.15 200); /* Blue-ish accent */
  
  /* Modify surface colors */
  --surface-3: oklch(90% 0.01 240); /* Lighter panel background */
  
  /* Adjust spacing */
  --space-m: 10px; /* Larger default spacing */
}
```

### Component-Specific Overrides

Apply theme overrides to specific components or containers:

```html
<!-- All components inside this container use custom theme -->
<div class="my-custom-theme">
  <ixfx-button>Click me</ixfx-button>
  <ixfx-panel>Content</ixfx-panel>
</div>
```

```css
.my-custom-theme {
  --accent: oklch(50% 0.2 300); /* Purple accent */
  --font-family: 'Georgia', serif; /* Different font */
}
```

### Per-Instance Overrides

Customize individual components using inline styles:

```html
<ixfx-button style="
  --accent: oklch(70% 0.15 50); /* Orange for this button */
  --space-m: 16px; /* Larger spacing just for this button */
">
  Custom Button
</ixfx-button>
```

## Icons

### Built-in Icons

The library ships with a set of SVG icons registered by default. All use `fill="currentColor"` so they inherit CSS color automatically.

| Name | Description |
|---|---|
| `add` | Plus / add icon |
| `caret-right` | Caret pointing right (used by tree expand indicators) |
| `check` | Checkmark |
| `chevron-down` | Chevron pointing down (used by split-button dropdowns) |
| `chevron-right` | Chevron pointing right |
| `close` | X / close icon |
| `error` | Error/warning icon |
| `folder-up` | Folder with up-arrow |
| `info` | Info icon |
| `warning` | Warning icon |

### `<ixfx-icon>` Element

Render any registered icon by name:

```html
<ixfx-icon name="check"></ixfx-icon>
<ixfx-icon name="chevron-down" size="24"></ixfx-icon>
<ixfx-icon name="close" label="Dismiss dialog"></ixfx-icon>
<ixfx-icon name="check" rotate="45deg"></ixfx-icon>
<ixfx-icon name="settings" prominence></ixfx-icon>
```

**Attributes:**

| Attribute | Type | Description |
|---|---|---|
| `name` | `string` | Registry name of the icon |
| `size` | `number` | Fixed pixel size |
| `label` | `string` | Accessible label; adds `aria-label` |
| `rotate` | `string` | CSS angle (e.g. `"90deg"`) |
| `prominence` | `boolean` | Enables idle/hover/disabled opacity |

Icon sizing is `1em × 1em` by default — override with CSS or the `size` attribute:

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

### Adding a Custom SVG Icon

Register an icon before use — it can be done at any time; components update automatically.

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

registerIcon('my-logo', `<svg viewBox="0 0 24 24" fill="currentColor">
  <path d="M12 2L4 12h5v8h6v-8h5L12 2z"/>
</svg>`);
```

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

### Font Icons (Icon Fonts)

Icon fonts such as Nerd Fonts are supported alongside SVG icons. Set the font family globally once, then register individual icons by name and code point.

```ts
import { setIconFont, registerFontIcon } from '@ixfx/components';

// Set the font family (load it via CSS first)
setIconFont('"Font Awesome 6 Free"');

// Register a gear icon by code point
registerFontIcon('gear', 0xf013);

// Or by literal character
registerFontIcon('heart', '\uf004');
```

Use font icons exactly like SVG icons — no API change:

```html
<ixfx-icon name="gear"></ixfx-icon>
<ixfx-button icon="gear">Settings</ixfx-button>
```

Font icons inherit `color` and size to `1em` automatically. Rotation and prominence attributes work the same as for SVG icons.

> **Different fonts, different code points.** Icon fonts use different Private Use Area code points for the same glyphs. Switching fonts requires re-registering icons with the appropriate code points for that font. See the [icons demo](/demo/loader.html?demo=icons) for a live example of font switching.

### Overriding a Built-in Icon

Both `registerIcon` (SVG) and `registerFontIcon` overwrite any existing entry for the same name. To swap a built-in icon globally:

```ts
// Replace the built-in chevron-down with a custom SVG
registerIcon('chevron-down', `<svg viewBox="0 0 24 24" fill="currentColor">
  <path d="M7 10l5 5 5-5H7z"/>
</svg>`);

// Replace it with a font icon instead
registerFontIcon('chevron-down', 0xe0b0);
```

All components that use the overridden name update automatically on the next render.

## Complete Base Theme Reference

Below is the full set of CSS variables used by the library. Copy this into your own stylesheet as a starting point and override the values you want to change.

```css
/* ========================================
   ixfx Components — Base Theme Reference
   ========================================
   All colours use OKLCH for perceptual uniformity.
   Dark theme is the default. Light theme overrides
   are nested under @media (prefers-color-scheme: light).
   ======================================== */

:root {
  /* ── Surfaces (depth: higher = more elevated) ── */
  --surface-0: oklch(5% 0.02 240);
  --surface-1: oklch(8% 0.02 240);
  --surface-2: oklch(12% 0.02 240);
  --surface-3: oklch(15% 0.02 240);   /* default UI plane */
  --surface-4: oklch(22% 0.02 240);   /* buttons, inputs */
  --surface-5: oklch(28% 0.02 240);   /* menus, popovers */
  --surface-6: oklch(35% 0.02 240);   /* active/pressed, hover in popups */

  /* ── Text on Surfaces ── */
  --surface-2-text: #fff;
  --surface-3-text: oklch(75% 0.02 240);
  --surface-4-text: #fff;
  --surface-5-text: #fff;
  --surface-6-text: #fff;
  --surface-muted-text: oklch(55% 0.02 240);

  /* ── Content (user content areas — canvas, editors) ── */
  --content: oklch(10% 0.01 240);
  --content-text: #fff;

  /* ── Accent ── */
  --accent: oklch(60% 0.15 140);
  --accent-text: #fff;

  /* ── Feedback / Status ── */
  --error: oklch(60% 0.15 20);
  --error-text: #fff;

  /* ── Typography ── */
  --font-family: system-ui, sans-serif;
  --font-mono: ui-monospace, monospace;
  --font-weight: 400;
  --font-weight-bold: 600;
  --text-xs: 10px;
  --text-s: 11px;
  --text-m: 12px;
  --text-l: 13px;
  --text-xl: 16px;
  --text-2xl: 20px;

  /* ── Spacing ── */
  --space-xs: 3px;
  --space-s: 4px;
  --space-m: 8px;
  --space-l: 12px;
  --space-xl: 16px;
  --space-2xl: 20px;

  /* ── Borders ── */
  --border: 1px solid oklch(25% 0.02 240);
  --border-subtle: 1px solid oklch(18% 0.02 240);
  --radius-s: 3px;
  --radius-m: 4px;
  --radius-l: 6px;
  --radius-xl: 8px;

  /* ── Shadows ── */
  --shadow-m: 0 4px 12px rgba(0, 0, 0, 0.3);
  --shadow-l: 0 4px 24px rgba(0, 0, 0, 0.5);

  /* ── Interaction ── */
  --disabled-opacity: 0.5;
  --transition: 0.15s ease-out;

  /* ── Item States ── */
  --item-bg: transparent;
  --item-bg-tickled: var(--surface-5);
  --item-bg-selected: var(--accent);
  --item-bg-selected-tickled: color-mix(in oklch, var(--accent) 80%, oklch(0% 0 0));
  --item-text-tickled: var(--surface-5-text);
  --item-text-selected: var(--accent-text);
  --item-border: 1px solid var(--surface-5);
  --item-border-tickled: 1px solid color-mix(in oklch, var(--accent) 40%, transparent);
  --item-border-selected: 1px dashed var(--accent);
  --item-border-active: 2px solid var(--accent);
  --item-border-dragged: 2px dotted color-mix(in oklch, var(--accent) 80%, transparent);
  --item-border-disabled: 1px solid var(--surface-4);
  --item-padding: var(--space-s) var(--space-m);
  --item-border-radius: var(--radius-m);
  --item-icon-spacing: var(--space-s);

  /* ── Item Label Styling ── */
  --item-label-font-style-default: normal;
  --item-label-font-style-selected: italic;
  --item-label-font-style-tickled: normal;
  --item-label-text-decoration-default: none;
  --item-label-text-decoration-selected: none;
  --item-label-text-decoration-tickled: underline;
  --item-label-text-underline-offset-default: auto;
  --item-label-text-underline-offset-tickled: 3px;

  /* ── Prominence (filter-based idle/tickled/active) ── */
  --prominence-idle-opacity: 0.6;
  --prominence-idle-saturate: 0.4;
  --prominence-tickled-opacity: 0.85;
  --prominence-tickled-saturate: 0.8;
  --prominence-disabled-opacity: 0.3;
  --prominence-disabled-saturate: 0.1;

  /* ── Miscellaneous ── */
  --transparent-bg: oklch(30% 0.02 240);
  --plot-color: var(--accent);
}

@media (prefers-color-scheme: light) {
  :root {
    --surface-0: oklch(70% 0 0);
    --surface-1: oklch(80% 0.01 240);
    --surface-2: oklch(85% 0.01 240);
    --surface-3: oklch(90% 0.01 240);
    --surface-4: oklch(94% 0.01 240);
    --surface-5: oklch(96% 0.01 240);
    --surface-6: oklch(99% 0.01 240);

    --surface-2-text: oklch(15% 0.02 240);
    --surface-3-text: oklch(25% 0.02 240);
    --surface-4-text: oklch(15% 0.02 240);
    --surface-5-text: oklch(10% 0.02 240);
    --surface-6-text: oklch(10% 0.02 240);
    --surface-muted-text: oklch(45% 0.02 240);

    --content: oklch(100% 0 0);
    --content-text: oklch(10% 0.02 240);

    --accent: oklch(45% 0.15 140);
    --accent-text: #fff;

    --error: oklch(55% 0.15 20);
    --error-text: #fff;

    --border: 1px solid oklch(80% 0.01 240);
    --border-subtle: 1px solid oklch(88% 0.01 240);

    --shadow-m: 0 2px 8px rgba(0, 0, 0, 0.1);
    --shadow-l: 0 4px 16px rgba(0, 0, 0, 0.15);

    --prominence-idle-opacity: 0.55;
    --prominence-idle-saturate: 0.35;
    --prominence-tickled-opacity: 0.8;
    --prominence-tickled-saturate: 0.75;
    --prominence-disabled-opacity: 0.25;
    --prominence-disabled-saturate: 0.1;

    --transparent-bg: oklch(85% 0.01 240);

    --item-border: 1px solid var(--surface-4);
    --item-border-tickled: 1px solid color-mix(in oklch, var(--accent) 50%, transparent);
    --item-border-selected: 1px dashed var(--accent);
    --item-border-active: 2px solid var(--accent);
    --item-border-dragged: 2px dotted var(--accent);
    --item-border-disabled: 1px solid var(--surface-3);
    --item-bg-tickled: var(--surface-4);
    --item-bg-selected-tickled: color-mix(in oklch, var(--accent) 80%, oklch(0% 0 0));
    --item-text-tickled: var(--surface-4-text);
    --item-bg-selected: var(--accent);
    --item-text-selected: var(--accent-text);

    --item-label-font-style-default: normal;
    --item-label-font-style-selected: italic;
    --item-label-font-style-tickled: normal;
    --item-label-text-decoration-default: none;
    --item-label-text-decoration-selected: none;
    --item-label-text-decoration-tickled: underline;
    --item-label-text-underline-offset-default: auto;
    --item-label-text-underline-offset-tickled: 3px;
  }
}
```

## Using Demo Themes as Examples

The library includes several demo themes that showcase different customization approaches:

### Cyberpunk Theme
- Monospace typography
- Sharp corners (0px radius)
- Custom accent hue (cyan/blue)
- Modified surface colors

### Retro Theme
- Serif typography
- Larger border radii
- Warm accent colors (orange/brown)
- Dashed borders for selected items

### Flat Theme
- Minimal styling
- High contrast colors
- No deboss effects

You can inspect the demo themes in:
- `demo/shared/base-themes.css` - Base dark/light theme definitions
- `demo/shared/demo-themes.css` - Theme-specific overrides

## Best Practices

1. **Use Semantic Variables**: Always prefer `--surface-3` over hardcoded colors for better theme compatibility
2. **Maintain Contrast**: Ensure text remains readable against background colors
3. **Test Both Themes**: Verify customizations work in both dark and light modes
4. **Leverage Inheritance**: Set variables on parent elements to affect child components
5. **Use Theme Fallbacks in Shadow DOM**: When building custom elements that use these variables in their shadow DOM, import and include the `themeFallbacks` module in your `static styles`:

   ```ts
   import { themeFallbacks } from '@ixfx/components/styles/fallbacks.js';

   class MyWidget extends LitElement {
     static styles = [themeFallbacks, css`
       :host {
         background: var(--surface-3);
         color: var(--surface-3-text);
       }
     `];
   }
   ```

   This registers `@property` rules so the variables resolve correctly in shadow DOM even when no `:root` theme is loaded. Do not use per-usage CSS fallbacks (`var(--surface-3, #ccc)`) — the `@property` approach is centralized and automatically stays in sync with the theme.

## Theme Activation

The system automatically detects theme preference via `prefers-color-scheme` media query. To force a specific theme:

```html
<!-- Force dark theme -->
<html class="theme-dark">
  <!-- components -->
</html>

<!-- Force light theme -->
<html class="theme-light">
  <!-- components -->
</html>
```

## Migration Notes

If you're upgrading from previous versions:
- All color values now use OKLCH for perceptual uniformity
- Variable names remain stable but values may have been adjusted
- Custom themes may need adjustment for the new color system

## Further Reading

For implementation details, see:
- [`docs/infra-theming.md`](infra-theming.md) - Internal theming specification
- [`docs-user/icons.md`](./icons.md) - Full icon registry reference
- [`demo/loader.html?demo=icons`](/demo/loader.html?demo=icons) - Interactive icons demo
- [`src/style.css`] - Base theme definitions
- [`demo/shared/`] - Example theme implementations