Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

In-slide hover-reveal, nested states, animation robustness

nyblnetc6ca5a468196
diff --git a/slides/src/editor/canvas.ts b/slides/src/editor/canvas.ts
index f1ffa792..523bba1e 100644
--- a/slides/src/editor/canvas.ts
+++ b/slides/src/editor/canvas.ts
@@ -91,7 +91,16 @@ export class SlideCanvas {
render() {
this.commitTextEdit()
- const next = renderSlide(this.store.slide, this.store.doc)
+ const slide = this.store.slide
+ const next = renderSlide(slide, this.store.doc)
+ // hover-reveal slides: preview one set at a time (panel picks which);
+ // hidden sets are display:none so they don't block selection
+ if (slide.elements.some((e) => e.showOnHover)) {
+ const active = this.store.hoverPreview ?? slide.hover?.default ?? null
+ for (const node of next.querySelectorAll<HTMLElement>('[data-show-on-hover]')) {
+ if (node.dataset.showOnHover !== active) node.style.display = 'none'
+ }
+ }
if (this.surface) this.surface.replaceWith(next)
else this.scaleHost.appendChild(next)
this.surface = next
diff --git a/slides/src/editor/editor.ts b/slides/src/editor/editor.ts
index 307c0d21..ab074579 100644
--- a/slides/src/editor/editor.ts
+++ b/slides/src/editor/editor.ts
@@ -150,24 +150,18 @@ export class Editor {
}
private rebuildSidebar() {
+ // States sit in doc order right after their parent and render nested —
+ // smaller, indented, dimmed — so the structure reads at a glance.
this.sidebar.innerHTML = ''
const slides = this.store.doc.slides
slides.forEach((slide, i) => {
- if (!slide.stateOf) this.sidebar.appendChild(this.makeThumb(slide, i, false))
+ const item = this.makeThumb(slide, i, !!slide.stateOf)
+ if (slide.stateOf) item.classList.add('ed-thumb-state')
+ this.sidebar.appendChild(item)
})
const add = btn(ICONS.plus, 'New slide', () => this.addSlide())
add.classList.add('ed-add-slide')
this.sidebar.appendChild(add)
-
- if (slides.some((s) => s.stateOf)) {
- const divider = div('ed-side-divider')
- divider.textContent = 'Interactive states'
- divider.title = 'Hidden from arrow-key flow; opened by clicking linked elements'
- this.sidebar.appendChild(divider)
- slides.forEach((slide, i) => {
- if (slide.stateOf) this.sidebar.appendChild(this.makeThumb(slide, i, true))
- })
- }
this.highlightSidebar()
}
diff --git a/slides/src/editor/panels.ts b/slides/src/editor/panels.ts
index 7eed1aab..bf8571ba 100644
--- a/slides/src/editor/panels.ts
+++ b/slides/src/editor/panels.ts
@@ -93,15 +93,37 @@ export class PropsPanel {
this.host.appendChild(stateHint)
this.row('Hover', this.select(
- ['none', 'focus-group'],
+ ['none', 'focus-group', 'reveal'],
slide.hover?.type ?? 'none',
(v) => this.edit(() => {
- this.store.slide.hover = v === 'focus-group' ? { type: 'focus-group', dim: slide.hover?.dim ?? 0.15 } : undefined
+ this.store.slide.hover = v === 'none'
+ ? undefined
+ : { ...(this.store.slide.hover ?? {}), type: v as 'focus-group' | 'reveal' }
}, true)))
- if (slide.hover) {
+ if (slide.hover?.type === 'focus-group') {
this.row('Hover dim', this.number(slide.hover.dim ?? 0.15, 0.01, (v, fin) =>
this.edit(() => { if (this.store.slide.hover) this.store.slide.hover.dim = Math.min(Math.max(v, 0), 1) }, fin)))
}
+ if (slide.hover?.type === 'reveal') {
+ const sets = [...new Set(slide.elements.map((e) => e.showOnHover).filter(Boolean))] as string[]
+ const defIn = document.createElement('input')
+ defIn.type = 'text'
+ defIn.placeholder = sets[0] ?? 'set name'
+ defIn.value = slide.hover.default ?? ''
+ defIn.addEventListener('change', () =>
+ this.edit(() => { if (this.store.slide.hover) this.store.slide.hover.default = defIn.value || undefined }, true))
+ this.row('Default set', defIn)
+ if (sets.length) {
+ this.row('Preview set', this.select(sets, this.store.hoverPreview ?? slide.hover.default ?? sets[0], (v) => {
+ this.store.hoverPreview = v
+ this.store.emit('current') // re-render canvas without touching the doc
+ }))
+ const revealHint = document.createElement('p')
+ revealHint.className = 'ed-hint'
+ revealHint.innerHTML = 'While presenting, hovering an element whose <b>group</b> matches a set name shows that set. Use Preview to edit each set.'
+ this.host.appendChild(revealHint)
+ }
+ }
this.section('Speaker notes')
const notes = document.createElement('textarea')
@@ -168,6 +190,43 @@ export class PropsPanel {
['none', 'kenburns'], el.fx?.ambient ?? 'none',
(v) => setFx({ ambient: v === 'none' ? undefined : 'kenburns' })))
+ // continuous loop animation
+ const loop = el.fx?.loop
+ this.row('Loop', this.select(
+ ['none', 'dash-march', 'motion-path'],
+ loop?.type ?? 'none',
+ (v) => setFx({
+ loop: v === 'none' ? undefined
+ : v === 'dash-march' ? { type: 'dash-march', distance: 18, duration: (loop as any)?.duration ?? 1.4 }
+ : { type: 'motion-path', path: (loop as any)?.path ?? 'M 0 0 L 100 0', duration: (loop as any)?.duration ?? 3 },
+ })))
+ if (loop) {
+ this.row('Loop secs', this.number(loop.duration ?? 2, 0.1, (v, fin) =>
+ this.mutate(el.id, (e) => { if (e.fx?.loop) e.fx.loop.duration = Math.max(v, 0.1) }, fin)))
+ if (loop.type === 'motion-path') {
+ const path = document.createElement('input')
+ path.type = 'text'
+ path.value = loop.path
+ path.title = 'SVG path the element travels along, relative to its position'
+ path.addEventListener('change', () =>
+ this.mutate(el.id, (e) => { if (e.fx?.loop?.type === 'motion-path') e.fx.loop.path = path.value }, true))
+ this.row('Path', path)
+ }
+ }
+
+ // hover-reveal set membership
+ const soh = document.createElement('input')
+ soh.type = 'text'
+ soh.placeholder = 'always visible'
+ soh.value = el.showOnHover ?? ''
+ soh.title = "Only visible while an element with this group is hovered (slide hover: 'reveal')"
+ soh.addEventListener('change', () =>
+ this.mutate(el.id, (e) => {
+ if (soh.value) e.showOnHover = soh.value
+ else delete e.showOnHover
+ }, true))
+ this.row('Show on hover', soh)
+
// group tag (hover focus & interaction targeting)
const group = document.createElement('input')
group.type = 'text'
@@ -220,13 +279,17 @@ export class PropsPanel {
clone.transition = 'morph'
delete (clone as any).hover // inherit nothing implicit; user can re-enable
if (src.hover) clone.hover = { ...src.hover }
- const targetIdx = this.store.doc.slides.length
+ // insert right after the parent and its existing states, keeping the
+ // family together in the slide list
+ const parentIdx = this.store.doc.slides.findIndex((s) => s.id === clone.stateOf)
+ let insertAt = parentIdx + 1
+ while (this.store.doc.slides[insertAt]?.stateOf === clone.stateOf) insertAt++
this.store.commit(() => {
- this.store.doc.slides.push(clone)
+ this.store.doc.slides.splice(insertAt, 0, clone)
const live = this.store.element(el.id)
if (live) live.link = clone.id
}, 'slides')
- this.store.goTo(targetIdx)
+ this.store.goTo(insertAt)
}
/** Human label for a slide in pickers. */
diff --git a/slides/src/model.ts b/slides/src/model.ts
index e1b39d02..8a0da961 100644
--- a/slides/src/model.ts
+++ b/slides/src/model.ts
@@ -35,6 +35,12 @@ export interface ElementBase {
link?: string
/** semantic group tag — hover focus and multi-element behaviours target it */
group?: string
+ /**
+ * In-slide hover reveal: this element is only visible while an element
+ * whose `group` equals this value is hovered (slide.hover type 'reveal').
+ * The slide's hover.default set is shown when nothing is hovered.
+ */
+ showOnHover?: string
}
export interface TextElement extends ElementBase {
@@ -109,8 +115,13 @@ export interface Slide {
* after the parent.
*/
stateOf?: string
- /** present-mode hover behaviour: focus the hovered element's group */
- hover?: { type: 'focus-group'; dim?: number }
+ /**
+ * present-mode hover behaviour:
+ * - focus-group: dim every element outside the hovered element's group
+ * - reveal: show the showOnHover set matching the hovered group
+ * (`default` names the set visible when nothing is hovered)
+ */
+ hover?: { type: 'focus-group' | 'reveal'; dim?: number; default?: string }
}
export interface BentoDoc {
diff --git a/slides/src/present.ts b/slides/src/present.ts
index 461fd16f..d5d0b4de 100644
--- a/slides/src/present.ts
+++ b/slides/src/present.ts
@@ -9,7 +9,7 @@ import { gsap } from 'gsap'
import { Flip } from 'gsap/Flip'
import { MotionPathPlugin } from 'gsap/MotionPathPlugin'
import type { BentoDoc, Slide, SlideElement } from './model'
-import { renderSlide } from './render'
+import { applyElementFrame, renderSlide } from './render'
gsap.registerPlugin(Flip, MotionPathPlugin)
@@ -43,7 +43,10 @@ export function startPresentation(
const section = document.createElement('section')
// Morph slides swap instantly; the Flip animation supplies the motion.
section.dataset.transition = slide.transition === 'morph' ? 'none' : slide.transition
- section.appendChild(renderSlide(slide, doc))
+ const surface = renderSlide(slide, doc)
+ // reveal slides start with only the default hover set visible
+ if (slide.hover?.type === 'reveal') applyRevealSet(surface, slide.hover.default ?? null, slide.hover.default)
+ section.appendChild(surface)
if (slide.notes) {
const aside = document.createElement('aside')
aside.className = 'notes'
@@ -101,6 +104,8 @@ export function startPresentation(
: false,
// swipes would walk into hidden state slides; arrows are handled below
touch: !doc.slides.some((s) => s.stateOf),
+ // heavy decks: paint only the neighbourhood of the current slide
+ viewDistance: 1,
keyboardCondition: null,
plugins: [RevealNotes],
})
@@ -153,7 +158,20 @@ export function startPresentation(
if (!to) return
const fromIdx = from ? [...slidesEl.children].indexOf(from) : -1
const toIdx = [...slidesEl.children].indexOf(to)
- if (from) gsap.killTweensOf(from.querySelectorAll('.bento-el'))
+ if (from) {
+ // Kill the outgoing slide's tweens, then restore model frames —
+ // a tween killed during its delay would otherwise leave the element
+ // stuck at its "from" state (invisible) for every future visit.
+ gsap.killTweensOf(from.querySelectorAll('.bento-el'))
+ const fromSlide = doc.slides[fromIdx]
+ for (const el of fromSlide?.elements ?? []) {
+ const node = from.querySelector<HTMLElement>(`[data-el-id="${CSS.escape(el.id)}"]`)
+ if (node) applyElementFrame(node, el)
+ }
+ if (fromSlide?.hover?.type === 'reveal') {
+ applyRevealSet(from, null, fromSlide.hover.default)
+ }
+ }
const forward = toIdx > fromIdx
// Morph forward into a morph slide, and un-morph when backing out of one.
const morphing =
@@ -213,13 +231,17 @@ function fxNodes(slide: Slide, section: HTMLElement): Array<[SlideElement, HTMLE
/** Staggered entrance animations + count-ups for the incoming slide. */
function runEnterFx(slide: Slide, section: HTMLElement) {
const entering = fxNodes(slide, section)
- .filter(([el]) => el.fx!.enter || el.fx!.countUp)
+ // reveal-set members are shown/hidden by hover, never by entrance tweens
+ .filter(([el]) => (el.fx!.enter || el.fx!.countUp) && !el.showOnHover)
.sort((a, b) => (a[0].fx!.order ?? 0) - (b[0].fx!.order ?? 0))
// Delay derives from fx.order when set (equal order ⇒ elements enter
// together — how a diagram reveals band-by-band), else from list position.
entering.forEach(([el, node], i) => {
const fx = el.fx!
const step = fx.order ?? i
+ // motion-path loops own the transform — an entrance tween on the same
+ // node would fight it and freeze the dot off its path
+ if (fx.loop?.type === 'motion-path') return
if (fx.enter) {
gsap.fromTo(
node,
@@ -236,6 +258,26 @@ function runEnterFx(slide: Slide, section: HTMLElement) {
}
if (fx.countUp) runCountUp(node)
})
+ settleGuarantee(entering.map(([el, node]) => [node, el]))
+}
+
+/**
+ * Wall-clock safety net: on starved render loops (throttled tabs, weak
+ * machines) tween progress crawls — guarantee every animated element lands
+ * on its final model state instead of lingering half-invisible.
+ */
+function settleGuarantee(pairs: Array<[HTMLElement, SlideElement]>) {
+ if (!pairs.length) return
+ setTimeout(() => {
+ for (const [node, el] of pairs) {
+ if (!node.isConnected) continue
+ const tweens = gsap.getTweensOf(node)
+ if (tweens.some((t) => t.progress() < 1)) {
+ gsap.killTweensOf(node)
+ applyElementFrame(node, el)
+ }
+ }
+ }, 2800)
}
/** Animate every number in the element's text from 0 to its final value. */
@@ -308,18 +350,36 @@ function runAmbientFx(slide: Slide, section: HTMLElement) {
}
}
+/** Show only the showOnHover set for `group` (falling back to the default). */
+function applyRevealSet(root: HTMLElement, group: string | null, def?: string | null) {
+ const active = group ?? def ?? null
+ for (const node of root.querySelectorAll<HTMLElement>('[data-show-on-hover]')) {
+ const show = node.dataset.showOnHover === active
+ node.style.transition = 'opacity .18s ease'
+ node.style.opacity = show ? '' : '0'
+ node.style.pointerEvents = show ? '' : 'none'
+ }
+}
+
/**
- * Hover focus: on slides with hover:'focus-group', pointing at any grouped
- * element dims every element belonging to a different group.
+ * Hover behaviours. focus-group: pointing at a grouped element dims every
+ * element outside its group. reveal: pointing at a grouped element shows the
+ * matching showOnHover set (in-slide content swap — no state slides needed).
*/
function wireHoverFocus(slide: Slide, section: HTMLElement) {
- if (slide?.hover?.type !== 'focus-group' || section.dataset.hoverWired) return
+ if (!slide?.hover || section.dataset.hoverWired) return
section.dataset.hoverWired = '1'
+ const mode = slide.hover.type
const dim = slide.hover.dim ?? 0.13
+ const def = slide.hover.default ?? null
let current: string | null = null
const apply = (group: string | null) => {
if (group === current) return
current = group
+ if (mode === 'reveal') {
+ applyRevealSet(section, group, def)
+ return
+ }
for (const node of section.querySelectorAll<HTMLElement>('[data-group]')) {
const other = group !== null && node.dataset.group !== group
node.style.transition = 'opacity .25s ease'
@@ -371,18 +431,33 @@ function runMorph(
}
}
- // Unmatched incoming elements simply fade/rise in.
- const entering = [...toEls.values()].filter((n) => !fromEls.has(n.dataset.flipId!))
+ // Unmatched incoming elements fade/rise in — to their MODEL opacity
+ // (clearProps would wipe reveal-set hiding and dimmed-state opacities).
+ const toSlide = doc.slides[toIdx]
+ const activeSet = toSlide?.hover?.type === 'reveal' ? (toSlide.hover.default ?? null) : null
+ const entering: Array<[HTMLElement, number]> = []
+ for (const n of toEls.values()) {
+ const id = n.dataset.flipId!
+ if (fromEls.has(id)) continue
+ const m = toModel.get(id)
+ if (m?.showOnHover && m.showOnHover !== activeSet) continue // hover-revealed, stays hidden
+ entering.push([n, m?.opacity ?? 1])
+ }
if (entering.length) {
- gsap.from(entering, {
- opacity: 0,
- y: 14,
- duration: 0.45,
- delay: MORPH_DURATION * 0.4,
- stagger: { amount: Math.min(0.45, entering.length * 0.03) },
- ease: 'power2.out',
- clearProps: 'opacity,transform',
+ const spread = Math.min(0.45, entering.length * 0.03)
+ entering.forEach(([n, opacity], i) => {
+ gsap.fromTo(n,
+ { opacity: 0, y: 14 },
+ {
+ opacity, y: 0, duration: 0.45,
+ delay: MORPH_DURATION * 0.4 + (spread * i) / entering.length,
+ ease: 'power2.out',
+ })
})
+ settleGuarantee(entering.map(([n]) => {
+ const m = toModel.get(n.dataset.flipId!)
+ return [n, m!] as [HTMLElement, SlideElement]
+ }).filter(([, m]) => !!m))
}
if (!matchedFrom.length) return
diff --git a/slides/src/render.ts b/slides/src/render.ts
index 34d9d30d..4b6e76c6 100644
--- a/slides/src/render.ts
+++ b/slides/src/render.ts
@@ -186,6 +186,7 @@ export function renderElement(el: SlideElement, doc: BentoDoc, opts: RenderOpts
node.dataset.flipId = el.id
if (el.link) node.dataset.link = el.link
if (el.group) node.dataset.group = el.group
+ if (el.showOnHover) node.dataset.showOnHover = el.showOnHover
applyElementFrame(node, el)
switch (el.type) {
diff --git a/slides/src/store.ts b/slides/src/store.ts
index e79a46ae..820db4dc 100644
--- a/slides/src/store.ts
+++ b/slides/src/store.ts
@@ -17,6 +17,8 @@ export class Store {
currentIndex = 0
selection: string[] = []
dirty = false
+ /** editor-only: which showOnHover set the canvas previews (never saved) */
+ hoverPreview: string | null = null
private undoStack: string[] = []
private redoStack: string[] = []
@@ -104,6 +106,7 @@ export class Store {
if (clamped === this.currentIndex) return
this.currentIndex = clamped
this.selection = []
+ this.hoverPreview = null
this.emit('current')
this.emit('selection')
}
diff --git a/slides/src/styles.css b/slides/src/styles.css
index d6dd8481..109e7a95 100644
--- a/slides/src/styles.css
+++ b/slides/src/styles.css
@@ -166,15 +166,21 @@ html, body {
.ed-add-slide { border: 1.5px dashed var(--line); justify-content: center; color: var(--muted); }
-.ed-side-divider {
- font-size: 10px;
- font-weight: 700;
- text-transform: uppercase;
- letter-spacing: 0.08em;
- color: var(--muted);
- border-top: 1px solid var(--line);
- padding-top: 10px;
- margin-top: 4px;
+/* interactive-state thumbs nest under their parent slide */
+.ed-thumb-state {
+ width: 116px;
+ margin-left: 32px;
+ margin-top: -4px;
+ opacity: 0.72;
+ border-style: dashed;
+}
+.ed-thumb-state:hover, .ed-thumb-state.active { opacity: 1; }
+.ed-thumb-state .ed-thumb-num {
+ font-size: 9px;
+ max-width: 90px;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
}
.ed-btn-block {
@@ -222,7 +228,7 @@ html, body {
.bento-slide { position: relative; overflow: hidden; }
-.bento-el { position: absolute; }
+.bento-el { position: absolute; contain: layout style; }
.bento-el-text .bento-text-inner {
overflow-wrap: break-word;
outline: none;