@ksp-gonogo/kerbcast-react 1.7.0 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,391 @@
1
+ /**
2
+ * FeedActionBar: the shared top-right action row rendered by BOTH
3
+ * `CameraFeed` and `KerbalFaceFeed`, so a single overflow engine serves every
4
+ * feed surface (part cams, crew faces, and any future host).
5
+ *
6
+ * Partition rule (issue #6 UI spec): stateful toggles (their at-rest state
7
+ * must stay visible, e.g. auto-track, the coming REC) are PRIMARY and always
8
+ * inline. Non-stateful actions (quality, PiP, fullscreen, plain consumer
9
+ * actions) are overflow-eligible: they collapse into a ⋮ menu ONLY once the
10
+ * row is genuinely crowded (total enabled >= 4 AND >= 2 non-stateful
11
+ * eligible): a single item never collapses, since that would cost a click
12
+ * to reach the one control that exists. Order is stable (an explicit `order`
13
+ * wins, otherwise entries keep the relative position their caller gave them)
14
+ * so chips don't reflow as buttons enable/disable around them.
15
+ *
16
+ * Close/remove is a THIRD category: `pinnedTrailing`. It is never
17
+ * overflow-eligible, never counted toward the overflow threshold, and always
18
+ * renders last (to the right of the ⋮ trigger itself), so dismissing a
19
+ * feed always stays a single, persistent, rightmost click. Bar order is:
20
+ * [stateful primaries] … [⋮ overflow, non-stateful] [pinned trailing close].
21
+ *
22
+ * Deliberately minimal: an entry is just (id, label, stateful?, order?,
23
+ * pinnedTrailing?, render). The interactive node itself (icon, aria
24
+ * attributes, onClick, any submenu it opens) is built by the caller
25
+ * (CameraFeed / KerbalFaceFeed) exactly as before; this component only
26
+ * decides WHERE it goes.
27
+ */
28
+
29
+ import { Fragment, useId } from "react";
30
+ import { createPortal } from "react-dom";
31
+ import styled from "styled-components";
32
+ import { usePortalMenu } from "./menuPositioning";
33
+
34
+ /**
35
+ * A consumer-supplied action rendered into a feed's action bar. Lets a host
36
+ * page (e.g. the sidecar's spotlight toggle) inject controls without the
37
+ * library knowing what they do.
38
+ */
39
+ export interface FeedAction {
40
+ /** Stable identity for the React key. */
41
+ id: string;
42
+ /** Accessible label; used for aria-label, the native tooltip, and the overflow-menu row's visible text. */
43
+ label: string;
44
+ /** Icon node, sized by the action bar (~14px). */
45
+ icon: React.ReactNode;
46
+ /** Toggle state: renders the button highlighted and sets aria-pressed. */
47
+ active?: boolean;
48
+ onClick: () => void;
49
+ /**
50
+ * Whether this action is a stateful toggle: stays PRIMARY / always
51
+ * inline so its at-rest state is visible, never collapses into the ⋮
52
+ * overflow. Defaults to `active !== undefined`. Override when a
53
+ * tri-state/menu-driven action's "active" isn't a plain boolean, or to force
54
+ * a boolean toggle to be overflow-eligible anyway.
55
+ */
56
+ stateful?: boolean;
57
+ /**
58
+ * Stable sort key for the action bar (lower sorts first). Actions without
59
+ * an explicit order keep their natural (array) position; this exists so a
60
+ * consumer can pin an action to a specific slot instead. Chips never
61
+ * reflow as buttons enable/disable; this is what keeps that stable.
62
+ */
63
+ order?: number;
64
+ /**
65
+ * Marks this as the bar's pinned-trailing (close/remove) slot: it is NEVER
66
+ * overflow-eligible, is excluded entirely from the overflow-threshold count
67
+ * (both the total and the non-stateful-eligible count), and always renders
68
+ * last (to the right of the ⋮ trigger), so dismissing a feed is always a
69
+ * single, persistent, rightmost click. `CameraFeed`'s `trailingActions`
70
+ * prop sets this automatically; a `KerbalFaceFeed`/`actions`-only consumer
71
+ * sets it explicitly on its close action.
72
+ */
73
+ pinnedTrailing?: boolean;
74
+ }
75
+
76
+ // ---------------------------------------------------------------------------
77
+ // Partition logic (pure, unit-tested independently of any rendering)
78
+ // ---------------------------------------------------------------------------
79
+
80
+ const OVERFLOW_MIN_TOTAL = 4;
81
+ const OVERFLOW_MIN_NON_STATEFUL = 2;
82
+
83
+ interface Partitionable {
84
+ stateful?: boolean;
85
+ order?: number;
86
+ pinnedTrailing?: boolean;
87
+ }
88
+
89
+ /**
90
+ * Sort a list of action-bar entries into (primary, overflow, pinnedTrailing).
91
+ * `entries` must already be filtered down to the currently-enabled set:
92
+ * this function has no notion of "disabled", only "present".
93
+ *
94
+ * `pinnedTrailing` entries (close/remove) are pulled out FIRST and excluded
95
+ * entirely from the overflow-threshold count. Both the total and the
96
+ * non-stateful-eligible count run only over the remaining entries, then
97
+ * always returned as their own group, to be rendered last (see
98
+ * `FeedActionBar`, which places them after the ⋮ trigger).
99
+ */
100
+ export function partitionActionBarEntries<T extends Partitionable>(
101
+ entries: readonly T[],
102
+ ): { primary: T[]; overflow: T[]; pinnedTrailing: T[] } {
103
+ const sorted = [...entries].sort(
104
+ (a, b) => (a.order ?? Number.POSITIVE_INFINITY) - (b.order ?? Number.POSITIVE_INFINITY),
105
+ );
106
+ const pinnedTrailing = sorted.filter((e) => e.pinnedTrailing);
107
+ const overflowable = sorted.filter((e) => !e.pinnedTrailing);
108
+ const nonStateful = overflowable.filter((e) => !e.stateful);
109
+ const overflowActive =
110
+ overflowable.length >= OVERFLOW_MIN_TOTAL && nonStateful.length >= OVERFLOW_MIN_NON_STATEFUL;
111
+
112
+ if (!overflowActive) {
113
+ return { primary: overflowable, overflow: [], pinnedTrailing };
114
+ }
115
+ return {
116
+ primary: overflowable.filter((e) => e.stateful),
117
+ overflow: nonStateful,
118
+ pinnedTrailing,
119
+ };
120
+ }
121
+
122
+ // ---------------------------------------------------------------------------
123
+ // Entry shape + the FeedAction -> entry adapter
124
+ // ---------------------------------------------------------------------------
125
+
126
+ /**
127
+ * One slot in the bar. `render` produces the actual interactive element
128
+ * (unchanged whether it lands inline or inside the overflow menu) so callers
129
+ * keep full control of refs / aria attributes / submenus; FeedActionBar only
130
+ * decides placement and, in the overflow menu, adds the visible label.
131
+ */
132
+ export interface FeedActionBarEntry {
133
+ id: string;
134
+ label: string;
135
+ stateful?: boolean;
136
+ order?: number;
137
+ pinnedTrailing?: boolean;
138
+ render: () => React.ReactNode;
139
+ }
140
+
141
+ /**
142
+ * Adapt a plain consumer/public `FeedAction` (id/label/icon/active/onClick,
143
+ * plus the optional stateful?/order?/pinnedTrailing? flags) into a bar entry,
144
+ * rendered as the standard icon button. `stateful` defaults to
145
+ * `active !== undefined` per the #6 spec (stateful toggles, meaning
146
+ * `active !== undefined`, stay primary).
147
+ */
148
+ export function feedActionToEntry(action: FeedAction): FeedActionBarEntry {
149
+ return {
150
+ id: action.id,
151
+ label: action.label,
152
+ stateful: action.stateful ?? action.active !== undefined,
153
+ order: action.order,
154
+ pinnedTrailing: action.pinnedTrailing,
155
+ render: () => (
156
+ <OverlayIconButton
157
+ key={action.id}
158
+ type="button"
159
+ aria-label={action.label}
160
+ aria-pressed={action.active ?? undefined}
161
+ title={action.label}
162
+ $active={action.active ?? false}
163
+ onClick={action.onClick}
164
+ >
165
+ {action.icon}
166
+ </OverlayIconButton>
167
+ ),
168
+ };
169
+ }
170
+
171
+ // ---------------------------------------------------------------------------
172
+ // The bar itself
173
+ // ---------------------------------------------------------------------------
174
+
175
+ const OVERFLOW_MENU_MAX_WIDTH = 200;
176
+ const OVERFLOW_MENU_MAX_HEIGHT = 240;
177
+
178
+ export interface FeedActionBarProps {
179
+ /** Non-partitioned content rendered before the partitioned entries (e.g. the camera step buttons). */
180
+ leading?: React.ReactNode;
181
+ entries: FeedActionBarEntry[];
182
+ /** Forwarded to the root row; lets a host `styled(FeedActionBar)` override layout (e.g. KerbalFaceFeed's smaller, always-visible bar). */
183
+ className?: string;
184
+ }
185
+
186
+ /**
187
+ * Shared top-right action row. Renders `leading`, then the partitioned
188
+ * entries: stateful ones always inline, non-stateful ones inline until the
189
+ * row is crowded enough to warrant a ⋮ overflow menu, then the pinned
190
+ * trailing (close/remove) entries: always last, to the right of the ⋮.
191
+ */
192
+ export function FeedActionBar({ leading, entries, className }: FeedActionBarProps) {
193
+ const { primary, overflow, pinnedTrailing } = partitionActionBarEntries(entries);
194
+ const menuId = useId();
195
+ const overflowMenu = usePortalMenu({
196
+ maxWidth: OVERFLOW_MENU_MAX_WIDTH,
197
+ maxHeight: OVERFLOW_MENU_MAX_HEIGHT,
198
+ align: "end",
199
+ });
200
+ const closeOverflowMenu = overflowMenu.close;
201
+
202
+ if (!leading && entries.length === 0) return null;
203
+
204
+ return (
205
+ <ActionBarRow className={className}>
206
+ {leading}
207
+ {primary.map((e) => (
208
+ <Fragment key={e.id}>{e.render()}</Fragment>
209
+ ))}
210
+ {overflow.length > 0 && (
211
+ <OverlayIconButton
212
+ ref={overflowMenu.triggerRef}
213
+ type="button"
214
+ aria-label="More actions"
215
+ aria-haspopup="menu"
216
+ aria-expanded={overflowMenu.open}
217
+ aria-controls={menuId}
218
+ title="More actions"
219
+ $active={overflowMenu.open}
220
+ onClick={overflowMenu.toggle}
221
+ >
222
+ <MoreIcon />
223
+ </OverlayIconButton>
224
+ )}
225
+ {overflow.length > 0 &&
226
+ overflowMenu.open &&
227
+ overflowMenu.position &&
228
+ createPortal(
229
+ <OverflowMenu
230
+ ref={overflowMenu.menuRef}
231
+ id={menuId}
232
+ role="menu"
233
+ aria-label="More actions"
234
+ style={overflowMenu.position}
235
+ >
236
+ {overflow.map((e) => (
237
+ // The wrapping row's onClick fires in the BUBBLE phase, after
238
+ // the entry's own button click handler, so this closes the
239
+ // menu once the action has actually fired rather than
240
+ // pre-empting it.
241
+ <OverflowMenuItem key={e.id} onClick={closeOverflowMenu}>
242
+ {e.render()}
243
+ <OverflowMenuItemLabel aria-hidden="true">{e.label}</OverflowMenuItemLabel>
244
+ </OverflowMenuItem>
245
+ ))}
246
+ </OverflowMenu>,
247
+ document.body,
248
+ )}
249
+ {pinnedTrailing.map((e) => (
250
+ <Fragment key={e.id}>{e.render()}</Fragment>
251
+ ))}
252
+ </ActionBarRow>
253
+ );
254
+ }
255
+
256
+ // ---------------------------------------------------------------------------
257
+ // Icons
258
+ // ---------------------------------------------------------------------------
259
+
260
+ /* Overflow trigger: vertical ellipsis (three dots). */
261
+ function MoreIcon() {
262
+ return (
263
+ <svg
264
+ viewBox="0 0 16 16"
265
+ width={14}
266
+ height={14}
267
+ fill="currentColor"
268
+ stroke="none"
269
+ aria-hidden="true"
270
+ >
271
+ <circle cx="8" cy="3" r="1.4" />
272
+ <circle cx="8" cy="8" r="1.4" />
273
+ <circle cx="8" cy="13" r="1.4" />
274
+ </svg>
275
+ );
276
+ }
277
+
278
+ // ---------------------------------------------------------------------------
279
+ // Styled components
280
+ // ---------------------------------------------------------------------------
281
+
282
+ /** Icon-button shared by every action-bar entry (built-in or consumer). */
283
+ export const OverlayIconButton = styled.button<{ $active?: boolean }>`
284
+ position: relative;
285
+ width: 24px;
286
+ height: 24px;
287
+ display: flex;
288
+ align-items: center;
289
+ justify-content: center;
290
+ padding: 0;
291
+ /* Active-toggle fill (quality / tracking / PiP / fullscreen / custom actions
292
+ all share this button). Its own token so a consumer can recolour just the
293
+ action-row highlight without touching the general accent; defaults to
294
+ --kerbcast-accent, so the sidecar web page (which maps its accent onto that)
295
+ is unchanged. */
296
+ background: ${(p) =>
297
+ p.$active
298
+ ? "var(--kerbcast-action-active, var(--kerbcast-accent, #00ff88))"
299
+ : "rgba(0, 0, 0, 0.5)"};
300
+ border: 1px solid
301
+ ${(p) =>
302
+ p.$active
303
+ ? "var(--kerbcast-action-active, var(--kerbcast-accent, #00ff88))"
304
+ : "rgba(255, 255, 255, 0.3)"};
305
+ border-radius: 3px;
306
+ color: ${(p) => (p.$active ? "#000" : "#fff")};
307
+ cursor: pointer;
308
+ transition: background 0.12s, border-color 0.12s, color 0.12s;
309
+
310
+ @media (hover: hover) {
311
+ &:hover {
312
+ background: ${(p) =>
313
+ p.$active
314
+ ? "var(--kerbcast-action-active, var(--kerbcast-accent, #00ff88))"
315
+ : "rgba(0, 0, 0, 0.7)"};
316
+ border-color: rgba(255, 255, 255, 0.6);
317
+ }
318
+ }
319
+
320
+ &:focus-visible {
321
+ outline: 2px solid var(--kerbcast-accent, #00ff88);
322
+ outline-offset: 2px;
323
+ }
324
+
325
+ &:disabled {
326
+ opacity: 0.4;
327
+ }
328
+
329
+ @media (prefers-reduced-motion: reduce) {
330
+ transition: none;
331
+ }
332
+ `;
333
+
334
+ /* Top-right cluster of overlay controls (custom actions + built-ins + overflow). */
335
+ export const ActionBarRow = styled.div`
336
+ position: absolute;
337
+ top: 6px;
338
+ right: 8px;
339
+ z-index: 3;
340
+ display: flex;
341
+ align-items: center;
342
+ gap: 4px;
343
+ opacity: 0;
344
+ pointer-events: none;
345
+ transition: opacity 0.15s;
346
+
347
+ @media (prefers-reduced-motion: reduce) {
348
+ transition: none;
349
+ }
350
+ `;
351
+
352
+ /* The ⋮ overflow dropdown: same portal/anchor contract as CameraFeed's other
353
+ menus (fixed position set inline from the trigger rect) so tile clipping
354
+ cannot cut it off. */
355
+ const OverflowMenu = styled.div`
356
+ position: fixed;
357
+ z-index: 1000;
358
+ min-width: 140px;
359
+ max-width: min(200px, calc(100vw - 16px));
360
+ max-height: min(40vh, 240px);
361
+ display: flex;
362
+ flex-direction: column;
363
+ padding: 4px;
364
+ gap: 2px;
365
+ background: rgba(0, 0, 0, 0.85);
366
+ border: 1px solid rgba(255, 255, 255, 0.3);
367
+ border-radius: 4px;
368
+ overflow-x: hidden;
369
+ overflow-y: auto;
370
+ `;
371
+
372
+ const OverflowMenuItem = styled.div`
373
+ display: flex;
374
+ align-items: center;
375
+ gap: 8px;
376
+ padding: 2px 4px;
377
+ border-radius: 3px;
378
+
379
+ @media (hover: hover) {
380
+ &:hover {
381
+ background: rgba(255, 255, 255, 0.1);
382
+ }
383
+ }
384
+ `;
385
+
386
+ const OverflowMenuItemLabel = styled.span`
387
+ font-size: 11px;
388
+ letter-spacing: 0.04em;
389
+ color: #fff;
390
+ white-space: nowrap;
391
+ `;
@@ -20,7 +20,8 @@
20
20
  import { useEffect, useRef, useState } from "react";
21
21
  import styled from "styled-components";
22
22
 
23
- import { FeedAction, CameraStreamHook } from "./CameraFeed";
23
+ import { CameraStreamHook, FeedAction } from "./CameraFeed";
24
+ import { FeedActionBar, feedActionToEntry } from "./FeedActionBar";
24
25
  import { useKerbcastStream } from "./hooks/useKerbcastStream";
25
26
  import { useKerbcastCameras } from "./hooks/useKerbcastCameras";
26
27
  import { useReportDisplaySize } from "./hooks/useReportDisplaySize";
@@ -131,27 +132,18 @@ const OverlayLayer = styled.div`
131
132
  inset: 0;
132
133
  `;
133
134
 
134
- const ActionBar = styled.div`
135
- position: absolute;
135
+ /*
136
+ * The shared FeedActionBar is hover-revealed by default (CameraFeed's larger
137
+ * frame fades it in on hover/focus). A tiny face tile has no such affordance,
138
+ * so this override keeps the original always-visible, corner-hugging look;
139
+ * layout/button styling (flex row, gap, the shared OverlayIconButton) still
140
+ * comes from FeedActionBar itself.
141
+ */
142
+ const KerbalActionBarRow = styled(FeedActionBar)`
136
143
  top: 4px;
137
144
  right: 4px;
138
- display: flex;
139
- gap: 4px;
140
- z-index: 2;
141
- `;
142
-
143
- const ActionButton = styled.button<{ $active?: boolean }>`
144
- display: inline-flex;
145
- align-items: center;
146
- justify-content: center;
147
- width: 22px;
148
- height: 22px;
149
- padding: 0;
150
- border: none;
151
- border-radius: 3px;
152
- cursor: pointer;
153
- color: #fff;
154
- background: ${(p) => (p.$active ? "rgba(255,255,255,0.28)" : "rgba(0,0,0,0.45)")};
145
+ opacity: 1;
146
+ pointer-events: auto;
155
147
  `;
156
148
 
157
149
  export function KerbalFaceFeed({
@@ -214,21 +206,7 @@ export function KerbalFaceFeed({
214
206
  </StandbyLayer>
215
207
  )}
216
208
  {showActions && actions && actions.length > 0 && (
217
- <ActionBar>
218
- {actions.map((a) => (
219
- <ActionButton
220
- key={a.id}
221
- type="button"
222
- aria-label={a.label}
223
- title={a.label}
224
- aria-pressed={a.active}
225
- $active={a.active}
226
- onClick={a.onClick}
227
- >
228
- {a.icon}
229
- </ActionButton>
230
- ))}
231
- </ActionBar>
209
+ <KerbalActionBarRow entries={actions.map(feedActionToEntry)} />
232
210
  )}
233
211
  {children && <OverlayLayer>{children}</OverlayLayer>}
234
212
  </Frame>