|
| 1 | +# Pulse Universal — API reference |
| 2 | + |
| 3 | +Canonical API surface shared across every framework wrapper. Every `@pulse/*` package exposes the same shapes — only the **idiomatic syntax** changes (Vue refs vs React hooks vs Svelte stores vs Angular services). |
| 4 | + |
| 5 | +## Components |
| 6 | + |
| 7 | +### `<PulsePlayer />` / `<pulse-player>` — inline card |
| 8 | + |
| 9 | +| Prop / Attribute | Type | Default | Meaning | |
| 10 | +| --- | --- | --- | --- | |
| 11 | +| `variant` | `PulseVariant` | `'auto'` | Mood: `auto`, `transparent`, `solid`, `dark`, `light`, `sunset`, `midnight`, `aurora`, `vinyl`, `custom` | |
| 12 | +| `accentColor` / `accent-color` | `string` | inherits theme | CSS colour — overrides `--pulse-accent` | |
| 13 | +| `tracks` | `Track[]` | engine default | Playlist override | |
| 14 | +| `ambientEq` / `ambient-eq` | `boolean` | `false` | 12-bar pure-CSS EQ behind chrome | |
| 15 | +| `dataFab` / `data-fab` | `boolean` | `false` | Force the disc shape regardless of width | |
| 16 | +| `resizable` | `boolean` | `false` | Bottom-right drag-to-resize handle | |
| 17 | +| `githubUrl` / `github-url` | `string` | — | Turns the GitHub icon into a link | |
| 18 | +| `spotifyUrl` / `spotify-url` | `string` | — | Turns the Spotify icon into a link | |
| 19 | + |
| 20 | +### `<PulseFab />` / `<pulse-fab>` — floating action button |
| 21 | + |
| 22 | +| Prop / Attribute | Type | Default | Meaning | |
| 23 | +| --- | --- | --- | --- | |
| 24 | +| `variant` | `PulseVariant` | `'auto'` | Same as `<PulsePlayer />` | |
| 25 | +| `pulso` | `boolean` | `false` | Heartbeat ring while audio plays | |
| 26 | +| `showMenu` / `show-menu` | `boolean` | `false` | Palette + Pulso/Fullscreen popover | |
| 27 | +| `draggable` | `boolean` | `false` | Pointer drag to reposition | |
| 28 | +| `persistKey` / `persist-key` | `string` | `'pulse-fab-pos'` | `localStorage` key for the persisted position | |
| 29 | + |
| 30 | +### Events (typed `EventMap` from `@pulse/types`) |
| 31 | + |
| 32 | +| Event | Payload | Fired by | |
| 33 | +| --- | --- | --- | |
| 34 | +| `play` | `{ track: Track, time: number }` | `engine.toggle()` when transitioning to playing | |
| 35 | +| `pause` | `{ track: Track, time: number }` | `engine.toggle()` when transitioning to paused | |
| 36 | +| `trackchange` | `{ from: number, to: number, track: Track }` | `engine.next()`, `engine.prev()`, `engine.loadTrack(i)` | |
| 37 | +| `error` | `{ track: Track, reason: ErrorReason, detail?: unknown }` | autoplay rejection (`'play-rejected'`), `<audio>` error event (`'media-error'`), `<audio>` stalled event (`'stalled'`) | |
| 38 | + |
| 39 | +| Framework | Listener syntax | |
| 40 | +| --- | --- | |
| 41 | +| Vue 3 | `@play="…"`, `@pause="…"`, `@trackchange="…"`, `@error="…"` | |
| 42 | +| React | `onPlay={…}`, `onPause={…}`, `onTrackChange={…}`, `onError={…}` | |
| 43 | +| Svelte 5 | `onpulse-play={…}` (property) or `on:pulse-play={…}` (directive) | |
| 44 | +| Angular | `(pulse-play)="…"`, `(pulse-pause)="…"`, `(pulse-trackchange)="…"`, `(pulse-error)="…"` | |
| 45 | +| Web Components / vanilla | `el.addEventListener('pulse-play', …)` | |
| 46 | + |
| 47 | +## Keyboard shortcuts (host element with focus) |
| 48 | + |
| 49 | +| Key | Action | |
| 50 | +| --- | --- | |
| 51 | +| `Space`, `K` | Toggle play / pause | |
| 52 | +| `J`, `←` | Previous track | |
| 53 | +| `L`, `→` | Next track | |
| 54 | + |
| 55 | +Handler ignores keypresses when the target is an `<input>`, `<textarea>`, or `contenteditable` element. The host defaults to `tabIndex="0"`; set `tabindex="-1"` to skip in tab order. |
| 56 | + |
| 57 | +## Engine — `@pulse/core` `PulseEngine` |
| 58 | + |
| 59 | +The framework-agnostic class every wrapper consumes via the shared singleton (`getSharedEngine()` / `setSharedEngine()`). |
| 60 | + |
| 61 | +### State (`PulseState`) |
| 62 | + |
| 63 | +```ts |
| 64 | +interface PulseState { |
| 65 | + currentTrack: number // index into the playlist |
| 66 | + isPlaying: boolean |
| 67 | + currentTime: number // seconds |
| 68 | + duration: number // seconds |
| 69 | + isVisible: boolean // FAB visibility flag (used by MiniPlayer) |
| 70 | + hasBeenOpened: boolean // sticky once true |
| 71 | + ambientEq: boolean // ambient EQ visualisation toggle |
| 72 | + playCount: number // privacy-friendly per-session counter |
| 73 | + pauseCount: number |
| 74 | + trackChangeCount: number |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +### Computed |
| 79 | + |
| 80 | +```ts |
| 81 | +engine.track // Track — clamped to a valid index |
| 82 | +engine.progress // number — 0..100 percentage of duration |
| 83 | +``` |
| 84 | + |
| 85 | +### Actions |
| 86 | + |
| 87 | +```ts |
| 88 | +engine.toggle() // play / pause |
| 89 | +engine.next() // → next track (loops) |
| 90 | +engine.prev() // → previous OR restart current if >3s in |
| 91 | +engine.loadTrack(index: number) // jump to a specific track |
| 92 | +engine.seek(fraction: number) // 0..1 of duration |
| 93 | +engine.setAudioTracks(tracks: Track[]) // replace the playlist |
| 94 | +engine.setAmbientEq(on: boolean) // flip the EQ flag |
| 95 | +engine.open() // show the FAB |
| 96 | +engine.close() // pause + hide the FAB |
| 97 | +engine.dispose() // tear down audio graph + listeners |
| 98 | +engine.fmt(seconds: number): string // → '0:42' |
| 99 | +``` |
| 100 | + |
| 101 | +### Event bus |
| 102 | + |
| 103 | +```ts |
| 104 | +import type { EventMap } from '@pulse/types' |
| 105 | + |
| 106 | +const off = engine.subscribe<'play'>('play', ({ track, time }) => { |
| 107 | + analytics.track('play', { id: track.title, time }) |
| 108 | +}) |
| 109 | +// later: |
| 110 | +off() |
| 111 | +``` |
| 112 | + |
| 113 | +`subscribe<E extends keyof EventMap>(event: E, cb: (payload: EventMap[E]) => void): Unsubscribe` |
| 114 | + |
| 115 | +Listener errors are caught + logged so a bad consumer can't break the engine. |
| 116 | + |
| 117 | +### State change subscription (framework adapters) |
| 118 | + |
| 119 | +```ts |
| 120 | +const off = engine.onStateChange((state: Readonly<PulseState>) => { |
| 121 | + // Project into Vue refs / React state / Svelte runes / Angular signal |
| 122 | +}) |
| 123 | +``` |
| 124 | + |
| 125 | +Used by the framework wrappers internally; consumers usually use the framework-specific hook (`usePulseAudio()`). |
| 126 | + |
| 127 | +## Types — `@pulse/types` |
| 128 | + |
| 129 | +Re-exported by every wrapper: |
| 130 | + |
| 131 | +```ts |
| 132 | +import type { |
| 133 | + Track, |
| 134 | + PulseVariant, // 'auto' | 'transparent' | 'solid' | 'dark' | 'light' | 'sunset' | 'midnight' | 'aurora' | 'vinyl' | 'custom' |
| 135 | + PulseState, |
| 136 | + EventMap, |
| 137 | + AudioEvent, // keyof EventMap |
| 138 | + EventListener, |
| 139 | + ErrorReason, // 'play-rejected' | 'media-error' | 'stalled' |
| 140 | + Unsubscribe, // () => void |
| 141 | +} from '@pulse/<framework>' |
| 142 | + |
| 143 | +import { ALL_VARIANTS } from '@pulse/<framework>' |
| 144 | +``` |
| 145 | + |
| 146 | +## Theming — `@pulse/tokens` |
| 147 | + |
| 148 | +CSS variables exposed at the `[data-variant='X']` attribute level. Both the Vue v2.3.4 chrome (uses the bare CSS file) and the Web Component Shadow DOM (consumes via Lit's `unsafeCSS(variantsCss)`) read the same source of truth. |
| 149 | + |
| 150 | +```ts |
| 151 | +import { variantsCss, baseCss } from '@pulse/tokens' |
| 152 | + |
| 153 | +// String exports for Shadow DOM consumers (Lit, Stencil, …) |
| 154 | +``` |
| 155 | + |
| 156 | +```css |
| 157 | +/* Document-level consumers */ |
| 158 | +@import '@pulse/tokens/variants.css'; |
| 159 | +@import '@pulse/tokens/base.css'; |
| 160 | +@import '@pulse/tokens/animations.css'; |
| 161 | +``` |
| 162 | + |
| 163 | +## See also |
| 164 | + |
| 165 | +- [`docs/universal/ARCHITECTURE.md`](./ARCHITECTURE.md) — dependency graph, build orchestration |
| 166 | +- [`docs/universal/FEATURE_MATRIX.md`](./FEATURE_MATRIX.md) — what works in each framework |
| 167 | +- [`docs/universal/BLOCKERS.md`](./BLOCKERS.md) — what isn't done and why |
| 168 | +- [`docs/frameworks/`](../frameworks/) — per-framework integration guides |
0 commit comments