Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/marker-preview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@karnstack/kino": minor
---

Markers can carry a `preview` (any React content) shown in the hover preview instead of the time and `label`. The preview card is then full size, left-aligned and up to 300px wide, and still stays inside the track. `label` stays the marker's accessible name. Omit `preview` and markers look as before.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ Pass `markers` to draw dots on the timeline, for example comments or notes at a

Give a marker an `icon` (short text, usually one emoji) to draw it in place of the dot, for example reactions: `{ id: "r1", time: 79, icon: "🔥", label: "🔥 3 at 1:19" }`. The hover label wraps up to three lines. Keep labels short.

For a richer hover card, give a marker a `preview` (any React content). It is shown in place of the time and `label`, left-aligned, up to 300px wide, and stays inside the track. Keep `label` too: it is still the button's accessible name.

`Player` takes the same two props. Theme the ring around dot markers with `--kino-marker-ring` (icons have no ring). Omit `markers` and nothing changes.

## Theming
Expand Down
7 changes: 7 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ReactNode } from "react"

export type QualityLevel = {
id: string // rendition id from the engine
height: number // e.g. 1080
Expand Down Expand Up @@ -94,4 +96,9 @@ export type Marker = {
label?: string
/** Short text drawn in place of the dot, usually one emoji. */
icon?: string
/**
* Rich content shown in the hover preview instead of the time and `label`.
* `label` stays the marker button's accessible name, so set both.
*/
preview?: ReactNode
}
18 changes: 18 additions & 0 deletions src/styles/kino.css
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,24 @@
.kino .kino-preview.kino-preview-above-icon {
bottom: calc(100% + 11px);
}
/* A marker's own preview content: full size, left-aligned, wraps at 300px
(or the scrubber width, if that is smaller). */
.kino .kino-preview.kino-preview-rich {
transform: translateX(-50%);
align-items: stretch;
width: max-content;
max-width: min(300px, 100% - 8px);
padding: 10px 12px;
text-align: left;
font-size: 13px;
line-height: 1.4;
color: var(--kino-text);
white-space: normal;
overflow-wrap: anywhere;
}
.kino .kino-preview-content {
min-width: 0;
}
.kino .kino-preview-img {
border-radius: 6px;
background-repeat: no-repeat;
Expand Down
44 changes: 44 additions & 0 deletions src/ui/scrubber.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,47 @@ test("hovering a marker shows the marker's own time", () => {
.getAttribute("aria-label"),
).toBe("0:50")
})

test("hovering a marker with a preview shows the preview instead of time and label", () => {
withMarkers([
{
id: "a",
time: 50,
label: "Lee: gzip",
preview: <p data-testid="rich">Lee says gzip</p>,
},
])
stubRect(screen.getByTestId("kino-track"))
hoverMarker(screen.getByTestId("kino-marker"))
const preview = document.querySelector(".kino-preview")!
expect(preview.classList.contains("kino-preview-rich")).toBe(true)
expect(screen.getByTestId("rich").textContent).toBe("Lee says gzip")
expect(preview.querySelector(".kino-preview-time")).toBeNull()
expect(preview.querySelector(".kino-preview-label")).toBeNull()
})

test("a marker without a preview keeps the time and label preview", () => {
withMarkers([{ id: "a", time: 50, label: "Lee: gzip" }])
stubRect(screen.getByTestId("kino-track"))
hoverMarker(screen.getByTestId("kino-marker"))
const preview = document.querySelector(".kino-preview")!
expect(preview.classList.contains("kino-preview-rich")).toBe(false)
expect(preview.querySelector(".kino-preview-time")).toBeTruthy()
expect(preview.querySelector(".kino-preview-label")!.textContent).toBe(
"Lee: gzip",
)
})

test("a marker with a preview still takes its accessible name from label", () => {
withMarkers([{ id: "a", time: 50, label: "Lee: gzip", preview: <b>rich</b> }])
expect(screen.getByRole("button", { name: "Lee: gzip" })).toBeTruthy()
})

test("a rich preview above an icon marker still rises above the icon", () => {
withMarkers([{ id: "r", time: 50, icon: "🔥", preview: <b>rich</b> }])
stubRect(screen.getByTestId("kino-track"))
hoverMarker(screen.getByTestId("kino-marker"))
const cls = document.querySelector(".kino-preview")!.classList
expect(cls.contains("kino-preview-rich")).toBe(true)
expect(cls.contains("kino-preview-above-icon")).toBe(true)
})
39 changes: 27 additions & 12 deletions src/ui/scrubber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,28 @@ export function Scrubber() {
const pw = previewRef.current?.getBoundingClientRect().width ?? 0
const tw = trackRef.current?.getBoundingClientRect().width ?? 0
setDims({ pw, tw })
}, [hover, tile?.url])
}, [hover, tile?.url, hoverMarker])

// Center the preview on the cursor, but clamp so it never crosses an edge.
// A preview wider than the track is centered on the track.
let previewLeft = hover?.x ?? 0
if (hover && dims.pw > 0 && dims.tw > 0) {
const half = dims.pw / 2 + 4
previewLeft = Math.min(Math.max(hover.x, half), dims.tw - half)
previewLeft =
half * 2 >= dims.tw
? dims.tw / 2
: Math.min(Math.max(hover.x, half), dims.tw - half)
}

const rich = hoverMarker?.preview != null ? hoverMarker.preview : null
const previewClass = [
"kino-preview kino-glass",
rich != null && "kino-preview-rich",
hoverMarker?.icon && "kino-preview-above-icon",
]
.filter(Boolean)
.join(" ")

return (
<div
className="kino-scrubber"
Expand All @@ -110,11 +123,7 @@ export function Scrubber() {
{hover && (
<div
ref={previewRef}
className={
hoverMarker?.icon
? "kino-preview kino-glass kino-preview-above-icon"
: "kino-preview kino-glass"
}
className={previewClass}
style={{ left: previewLeft }}
>
{tile && (
Expand All @@ -128,11 +137,17 @@ export function Scrubber() {
}}
/>
)}
<span className="kino-preview-time">
<RollingTime value={formatTime(previewTime)} />
</span>
{hoverMarker?.label && (
<span className="kino-preview-label">{hoverMarker.label}</span>
{rich != null ? (
<div className="kino-preview-content">{rich}</div>
) : (
<>
<span className="kino-preview-time">
<RollingTime value={formatTime(previewTime)} />
</span>
{hoverMarker?.label && (
<span className="kino-preview-label">{hoverMarker.label}</span>
)}
</>
)}
</div>
)}
Expand Down
Loading