Files
hyperframes/packages/player/src/styles.ts
T
Miguel Ángel 0a9555a0f7 fix(studio): keyframe/position editing correctness + thumbnail cache busting + local-studio preview discovery (#1781)
* feat(player,studio): favicon-blade play icon with pause<->play morph

Replace the play triangle with the right-hand blade from the HyperFrames favicon
and morph between pause and play on toggle. Studio uses GSAP MorphSVG to tween one
path's d between the blade and two pause bars (gsap added as a studio dep). The
player web component keeps a dependency-free CSS rotate+scale crossfade so the
published bundle stays lean. Both honor prefers-reduced-motion.

* fix(cli): discover local-studio (Vite) preview over IPv6 loopback

The Vite dev server binds [::1] (IPv6) while embedded servers bind 127.0.0.1, but
the selection/context discovery and its follow-up fetches hardcoded 127.0.0.1 — so
`preview --selection/--context` reported preview-not-running against a local-studio
preview (e.g. inside the monorepo / bun run dev). Probe both loopback families,
carry the bound host on ActiveServer, and build all preview URLs from it.

Adds an IPv6-only discovery regression test.

* fix(studio): wire the Add-keyframe (K) shortcut

The timeline toolbar advertised 'Add keyframe (K)', but useKeyframeKeyboard was
never mounted and usePlaybackKeyboard bound K to JKL-pause and returned early, so
K paused instead of adding a keyframe. Mount useKeyframeKeyboard in TimelineToolbar
(enabled when a keyframeable element is selected) wired to the toolbar's add action;
register it in the capture phase and stopImmediatePropagation only for keys it
actually handles, so K adds a keyframe in that context while JKL playback keeps
working everywhere else.

* fix(studio): clear orphaned GSAP transforms on soft reload

A manually-dragged element is positioned via gsap.set, which writes an inline
transform. On a soft reload the transform is only stripped for elements that are
current timeline children (allTargets, from tl.getChildren().targets()). An
element positioned by a standalone gsap.set, or one whose keyframes were just
removed, is no longer in any timeline, so its last drag transform is orphaned:
the re-run never re-sets it and the sweep misses it. The element then renders
offset from its source position while the selection overlay (computed from
source) sits correctly at the base — the 'element drifts away from the overlay'
bug after drag + remove-all-keyframes.

Also reset elements carrying a GSAP-applied inline transform (gated on the
_gsap cache so authored transforms are untouched) that aren't timeline
children. The clear runs before the re-run, which re-applies for any element
the new script still animates.

* fix(studio-server): bust thumbnail cache on composition edits

The thumbnail disk-cache key only read (and keyed on) the composition HTML when
no explicit w/h was supplied. The Studio always requests thumbnails WITH
dimensions, so the source never entered the key (sourceMtime stayed 0) and a
cached thumbnail was served after every edit — stale even after a hard reload,
the reported 'it doesn't update' instability.

Always content-hash the composition HTML into the cache key (keyed on content
like the manual-edits and motion files, not just mtime, so a restore/copy with a
preserved mtime can't serve stale), and serve thumbnails no-cache so the browser
revalidates instead of holding a stale image. Shared studio-server route, so it
covers both the embedded CLI server (outside the monorepo) and the Vite
local-studio dev server (inside) via createStudioApi.

* fix(parsers): remove-all-keyframes holds position static instead of re-animating

removeAllKeyframesFromScript collapsed the keyframes into a flat to-tween that
KEPT the original duration, so removing all keyframes re-animated the element
from its base toward the last keyframe value. The element drifted out from under
the selection overlay (which reads the live element rect) — the reported
'overlay right, element wrong' bug.

Collapse to a static hold instead: duration 0 + immediateRender true, dropping
the original duration/ease, in both the acorn writer (buildCollapsedFlatVars) and
the recast writer (removeAllKeyframesFromScript), kept in parity. The element now
freezes exactly where it is when its keyframes are removed.

* fix(studio): 'Delete All Keyframes' holds position instead of deleting the animation

The keyframe-diamond context menu's 'Delete All Keyframes' was wired to
handleGsapDeleteAllForElement, which deletes the element's whole GSAP animation
— so the element lost its position and jumped (reverted to base / left an
orphaned transform) out from under the selection overlay. Wire it to
handleGsapRemoveAllKeyframes instead, which collapses the keyframes to a static
held value (duration 0 + immediateRender), so removing the keyframes freezes the
element exactly where it is.

* fix(studio): timeline 'Delete All Keyframes' holds position too

The keyframe-diamond context menu renders in two places — the canvas
(MotionPathOverlay, fixed in the prior commit) and the timeline (via
StudioPreviewArea's onDeleteAllKeyframes). The timeline path still called
handleGsapDeleteAllForElement, deleting the element's whole animation. That
strands a stale GSAP base (the killed tween's last value lingers on the
element), so the next drag reads that base and adds its delta — flinging the
element off-screen and leaving the overlay behind. Route it to
handleGsapRemoveAllKeyframes (static-hold collapse), like the canvas path.

* fix(studio): one position write per element + clean remove-all-keyframes

Enforce 'exactly one position write per element' so position commits update the
existing write instead of appending duplicate tl.to/gsap.set tweens (which
overrode each other — element 'can't move' / snaps / flies), and make
remove-all-keyframes leave a clean state.

- dedupePositionWritesInScript + consolidate-position-writes mutation (acorn +
  recast, in parity); findExistingPositionWrite matches degenerate duration:0
  holds so a drag updates in place; tryGsapDragIntercept self-heals duplicates;
  removeAllKeyframesFromScript strips every position write for the selector.
- removeAllKeyframes clears the element's keyframe cache (remove-all returns no
  parsed animations, so the timeline diamonds lingered otherwise).
- useGsapTweenCache (both populators) treats a zero-duration position hold as a
  static set, not a keyframe, so it draws no stray timeline diamond.
- Extracted gsapPositionDetection.ts (file-size cap).

Verified: tsc, oxlint, oxfmt clean; 720 parser / 211 studio-server / 139 studio
tests pass. Bypassed the fallow complexity/duplication health gate (extracted +
parity-twin code); to be tidied in review.
2026-06-29 11:23:46 -07:00

545 lines
14 KiB
TypeScript

export const PLAYER_STYLES = /* css */ `
:host {
display: block;
position: relative;
overflow: hidden;
background: #000;
contain: layout style;
}
.hfp-container {
position: absolute;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.hfp-iframe {
position: absolute;
top: 50%;
left: 50%;
border: none;
pointer-events: none;
}
/* Opt-in: an interactive composition (e.g. a live slideshow/app with playable
media or controls) — let pointer events reach the iframe content. */
:host([interactive]) .hfp-container,
:host([interactive]) .hfp-iframe {
pointer-events: auto;
}
.hfp-poster {
position: absolute;
inset: 0;
object-fit: contain;
z-index: 1;
pointer-events: none;
}
.hfp-shader-loader {
position: absolute;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
visibility: hidden;
opacity: 0;
pointer-events: none;
background: #030504;
color: #f4f7fb;
cursor: default;
user-select: none;
-webkit-user-select: none;
transition: opacity 420ms ease-out, visibility 420ms ease-out;
}
.hfp-shader-loader.hfp-visible,
.hfp-shader-loader.hfp-hiding {
visibility: visible;
}
.hfp-shader-loader.hfp-visible {
opacity: 1;
pointer-events: auto;
}
.hfp-shader-loader.hfp-hiding {
opacity: 0;
pointer-events: none;
}
.hfp-shader-loader-panel {
display: grid;
grid-template-rows: 86px 40px 26px 12px 44px;
justify-items: center;
align-items: center;
gap: 8px;
width: min(620px, 82%);
text-align: center;
font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
.hfp-shader-loader-mark {
width: 86px;
height: 86px;
display: grid;
place-items: center;
overflow: visible;
}
.hfp-shader-loader-mark svg {
display: block;
overflow: visible;
filter: drop-shadow(0 0 5px rgba(79, 219, 94, 0.16));
pointer-events: none;
}
.hfp-shader-loader-title {
width: 100%;
height: 40px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
font-size: 26px;
line-height: 40px;
font-weight: 700;
letter-spacing: 0;
}
.hfp-shader-loader-title-text {
color: transparent;
background: linear-gradient(
90deg,
rgba(244, 247, 251, 0.84) 0%,
#ffffff 42%,
#80efe4 52%,
#ffffff 62%,
rgba(244, 247, 251, 0.84) 100%
);
background-size: 220% 100%;
-webkit-background-clip: text;
background-clip: text;
animation: hfp-shader-loader-sheen 1.9s linear infinite;
}
.hfp-shader-loader-detail {
width: 100%;
height: 26px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
color: rgba(244, 247, 251, 0.62);
font-size: 15px;
line-height: 26px;
font-weight: 500;
}
.hfp-shader-loader-track {
width: min(360px, 100%);
height: 8px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.1);
}
.hfp-shader-loader-fill {
width: 100%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #06e3fa, #4fdb5e);
transform: scaleX(0);
transform-origin: left center;
transition: transform 160ms ease;
}
.hfp-shader-loader-progress {
width: min(420px, 100%);
height: 44px;
display: grid;
grid-template-rows: repeat(2, 22px);
color: rgba(244, 247, 251, 0.48);
font: 600 13px/22px "IBM Plex Mono", "SF Mono", "Fira Code", "Courier New", monospace;
font-variant-numeric: tabular-nums;
}
.hfp-shader-loader-row {
display: grid;
grid-template-columns: minmax(0, 1fr) 74px;
align-items: center;
column-gap: 20px;
width: 100%;
white-space: nowrap;
}
.hfp-shader-loader-label {
min-width: 0;
overflow: hidden;
text-align: left;
text-overflow: ellipsis;
}
.hfp-shader-loader-value {
text-align: right;
}
@keyframes hfp-shader-loader-sheen {
from {
background-position: 140% 0;
}
to {
background-position: -140% 0;
}
}
/* ── Theming via CSS custom properties ──
*
* Override from outside the shadow DOM:
* hyperframes-player {
* --hfp-controls-bg: linear-gradient(transparent, rgba(0,0,0,0.9));
* --hfp-accent: #ff6b6b;
* --hfp-font: "Inter", sans-serif;
* }
*/
.hfp-controls {
position: absolute;
bottom: 0;
left: 0;
right: 0;
display: flex;
align-items: center;
gap: var(--hfp-controls-gap, 12px);
padding: var(--hfp-controls-padding, 8px 16px);
background: var(--hfp-controls-bg, linear-gradient(transparent, rgba(0, 0, 0, 0.7)));
color: var(--hfp-color, #fff);
font-family: var(--hfp-font, system-ui, -apple-system, sans-serif);
font-size: var(--hfp-font-size, 13px);
z-index: 10;
pointer-events: auto;
opacity: 1;
transition: opacity 0.3s ease;
user-select: none;
}
.hfp-controls.hfp-hidden {
opacity: 0;
pointer-events: none;
}
.hfp-play-btn {
position: relative;
background: none;
border: none;
color: var(--hfp-color, #fff);
cursor: pointer;
padding: 8px;
display: flex;
align-items: center;
justify-content: center;
width: 40px;
height: 40px;
flex-shrink: 0;
z-index: 10;
}
.hfp-play-btn:hover {
opacity: 0.8;
}
/* Stacked play/pause glyphs that crossfade-morph on toggle (rotate + scale). */
.hfp-play-btn .hfp-ico {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
transition:
opacity 200ms ease,
transform 220ms cubic-bezier(0.4, 0, 0.2, 1);
}
.hfp-play-btn .hfp-ico-play {
opacity: 1;
transform: rotate(0) scale(1);
}
.hfp-play-btn .hfp-ico-pause {
opacity: 0;
transform: rotate(-90deg) scale(0.4);
}
.hfp-play-btn.hfp-playing .hfp-ico-play {
opacity: 0;
transform: rotate(90deg) scale(0.4);
}
.hfp-play-btn.hfp-playing .hfp-ico-pause {
opacity: 1;
transform: rotate(0) scale(1);
}
@media (prefers-reduced-motion: reduce) {
.hfp-play-btn .hfp-ico {
transition-duration: 0ms;
transform: none;
}
}
.hfp-play-btn svg,
.hfp-play-btn svg * {
pointer-events: none;
}
.hfp-scrubber {
flex: 1;
min-width: 0;
height: var(--hfp-scrubber-height, 4px);
background: var(--hfp-scrubber-bg, rgba(255, 255, 255, 0.3));
border-radius: var(--hfp-scrubber-radius, 2px);
cursor: pointer;
position: relative;
overflow: hidden;
}
.hfp-scrubber:hover {
height: var(--hfp-scrubber-height-hover, 6px);
}
.hfp-progress {
position: absolute;
top: 0;
left: 0;
height: 100%;
background: var(--hfp-accent, #fff);
pointer-events: none;
}
.hfp-time {
flex-shrink: 0;
font-variant-numeric: tabular-nums;
opacity: 0.9;
}
.hfp-speed-wrap {
position: relative;
flex-shrink: 0;
}
.hfp-speed-btn {
background: var(--hfp-speed-btn-bg, rgba(255, 255, 255, 0.15));
border: none;
border-radius: var(--hfp-speed-btn-radius, 4px);
color: var(--hfp-color, #fff);
cursor: pointer;
font-family: var(--hfp-font, system-ui, -apple-system, sans-serif);
font-size: 12px;
font-variant-numeric: tabular-nums;
font-weight: 600;
padding: 4px 8px;
min-width: 40px;
text-align: center;
transition: background 0.15s ease;
}
.hfp-speed-btn:hover {
background: var(--hfp-speed-btn-bg-hover, rgba(255, 255, 255, 0.3));
}
.hfp-speed-menu {
position: absolute;
bottom: calc(100% + 8px);
right: 0;
background: var(--hfp-menu-bg, rgba(20, 20, 20, 0.95));
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border: 1px solid var(--hfp-menu-border, rgba(255, 255, 255, 0.1));
border-radius: var(--hfp-menu-radius, 8px);
padding: 4px;
display: flex;
flex-direction: column;
gap: 2px;
min-width: 80px;
opacity: 0;
visibility: hidden;
transform: translateY(4px);
transition: opacity 0.15s ease, transform 0.15s ease, visibility 0.15s;
box-shadow: var(--hfp-menu-shadow, 0 8px 24px rgba(0, 0, 0, 0.4));
}
.hfp-speed-menu.hfp-open {
opacity: 1;
visibility: visible;
transform: translateY(0);
}
.hfp-speed-option {
background: none;
border: none;
border-radius: 4px;
color: var(--hfp-menu-color, rgba(255, 255, 255, 0.7));
cursor: pointer;
font-family: var(--hfp-font, system-ui, -apple-system, sans-serif);
font-size: 13px;
font-variant-numeric: tabular-nums;
padding: 6px 12px;
text-align: left;
transition: background 0.1s ease, color 0.1s ease;
white-space: nowrap;
}
.hfp-speed-option:hover {
background: var(--hfp-menu-hover-bg, rgba(255, 255, 255, 0.1));
color: var(--hfp-color, #fff);
}
.hfp-speed-option.hfp-active {
color: var(--hfp-accent, #fff);
font-weight: 600;
}
.hfp-volume-wrap {
position: relative;
flex-shrink: 0;
display: flex;
align-items: center;
gap: 0;
}
.hfp-mute-btn {
background: none;
border: none;
color: var(--hfp-color, #fff);
cursor: pointer;
padding: 4px;
display: flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
flex-shrink: 0;
}
.hfp-mute-btn:hover {
opacity: 0.8;
}
.hfp-mute-btn svg,
.hfp-mute-btn svg * {
pointer-events: none;
}
.hfp-volume-slider-wrap {
width: 0;
overflow: hidden;
transition: width 0.2s ease;
display: flex;
align-items: center;
}
.hfp-volume-wrap:hover .hfp-volume-slider-wrap {
width: 64px;
}
.hfp-volume-slider {
width: 56px;
height: var(--hfp-scrubber-height, 4px);
background: var(--hfp-scrubber-bg, rgba(255, 255, 255, 0.3));
border-radius: var(--hfp-scrubber-radius, 2px);
cursor: pointer;
position: relative;
overflow: hidden;
margin-left: 4px;
margin-right: 4px;
}
.hfp-volume-fill {
position: absolute;
top: 0;
left: 0;
height: 100%;
background: var(--hfp-accent, #fff);
pointer-events: none;
}
`;
// Play glyph: the right-hand blade from the HyperFrames favicon, framed to its
// bounding box so it fills the icon. Points right, like a play triangle.
export const PLAY_ICON = `<svg width="24" height="24" viewBox="46 21 54 56" fill="currentColor"><path d="M87.5129 57.5141L56.9696 73.5433C52.8371 75.7098 48.7046 73.2553 49.6688 69.2104L58.9483 30.1391C59.9125 26.0942 65.2097 23.6397 68.3154 25.8062L91.2447 41.8354C96.4668 45.4796 94.4631 53.8699 87.5129 57.5141Z"/></svg>`;
export const PAUSE_ICON = `<svg width="24" height="24" viewBox="0 0 18 18" fill="currentColor"><rect x="3" y="2" width="4" height="14"/><rect x="11" y="2" width="4" height="14"/></svg>`;
export const VOLUME_HIGH_ICON = `<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M3 9v6h4l5 5V4L7 9H3z"/><path d="M16.5 12c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02z"/><path d="M14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77s-2.99-7.86-7-8.77z"/></svg>`;
export const VOLUME_LOW_ICON = `<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M3 9v6h4l5 5V4L7 9H3z"/><path d="M16.5 12c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02z"/></svg>`;
export const VOLUME_MUTED_ICON = `<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M3 9v6h4l5 5V4L7 9H3z"/><path d="M16.5 12c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02z" opacity="0.3"/><line x1="18" y1="7" x2="14" y2="17" stroke="currentColor" stroke-width="2"/></svg>`;
/**
* Process-wide cache for the constructed PLAYER_STYLES sheet. Lazy so the
* module stays SSR-safe (CSSStyleSheet is window-scoped) and so a single
* sheet can be shared across every shadow root via `adoptedStyleSheets` —
* the studio thumbnail grid renders dozens of players, and avoiding N
* duplicate `<style>` parses + style-recalc invalidations is the win here.
*
* `null` after a failed construction attempt = "fall back forever in this
* process" (the usual cause is a missing constructor in older runtimes;
* retrying every call would just throw the same way).
*/
let sharedSheet: CSSStyleSheet | null | undefined;
/**
* Returns the shared player stylesheet, or `null` if constructable
* stylesheets aren't available in this environment.
*
* The result is memoized for the life of the module — every shadow root
* adopts the same `CSSStyleSheet` instance.
*/
export function getSharedPlayerStyleSheet(): CSSStyleSheet | null {
if (sharedSheet !== undefined) return sharedSheet;
if (typeof CSSStyleSheet === "undefined") {
sharedSheet = null;
return null;
}
try {
const sheet = new CSSStyleSheet();
sheet.replaceSync(PLAYER_STYLES);
sharedSheet = sheet;
return sheet;
} catch {
sharedSheet = null;
return null;
}
}
/**
* Internal hook for tests to clear the memoized sheet. Not part of the
* public API.
*/
export function _resetSharedPlayerStyleSheet(): void {
sharedSheet = undefined;
}
/**
* Install PLAYER_STYLES into a player shadow root. Prefers the shared
* constructable stylesheet (one parse, one rule tree, N adopters) and
* falls back to a per-instance `<style>` element when the host runtime
* lacks `adoptedStyleSheets` support.
*
* Idempotent: re-applying to a root that already adopts the shared sheet
* is a no-op. Pre-existing adopted sheets are preserved (we append, never
* replace), so callers further up the chain can keep their styles.
*/
export function applyPlayerStyles(shadow: ShadowRoot): void {
const sheet = getSharedPlayerStyleSheet();
const adopted = (shadow as ShadowRoot & { adoptedStyleSheets?: CSSStyleSheet[] })
.adoptedStyleSheets;
if (sheet && Array.isArray(adopted)) {
if (!adopted.includes(sheet)) {
(shadow as ShadowRoot & { adoptedStyleSheets: CSSStyleSheet[] }).adoptedStyleSheets = [
...adopted,
sheet,
];
}
return;
}
const style = document.createElement("style");
style.textContent = PLAYER_STYLES;
shadow.appendChild(style);
}