Files
kuentagym/website/site.js
T
Duarte Santos 1290db11b5 website: fix the dialog, align the rail, and add a first-visit intro
Tested in a real browser this time, at 1440, 1024, 390 and 360, with touch
emulation and with reduced motion — which is how the first three of these were
found, and none of them were visible from reading the markup.

**The dialog never opened where you could see it.** `* { margin: 0 }` at the top
of this file removes the margin:auto a browser uses to centre a modal <dialog>,
and without a position of its own it resolved to `position: absolute`, so
`inset: 0` centred it inside the document rather than the viewport — near the top
of the page. Opening a card scrolled the whole page up to meet it, showed a
clipped corner on desktop and nothing at all on a phone. It is pinned to the
viewport now, centred on both axes, capped at the screen height and scrollable
inside if the text is long. Measured: centred and fully in frame at all four
sizes, with no scroll jump.

**The rail sat 34px left of everything above it.** Its gutter was half the
leftover width, which lands on the wrap's outer edge, not on the text inside it —
the wrap's own padding was missing from the sum. Cards now line up exactly with
the paragraph above them (measured: 0px). Using 100% instead of 100vw also keeps
the scrollbar out of the arithmetic. The screenshot rail below inherits the same
fix.

**The intro.** The hero settles in on the first load of a visit: a short rise and
fade, staggered eyebrow to headline to lead to buttons to phone. Deliberately the
same movement as the scroll reveals further down, so it reads as one behaviour
rather than an effect. Armed in the inline head script, because site.js is
deferred and would have let the hero paint and then hide it again. Once per tab,
so a reload is not a splash screen. Off entirely for prefers-reduced-motion, and
the animation ends on opacity 1 regardless of whether the cleanup ever runs — the
hero cannot get stuck invisible.

No horizontal overflow at any of the four widths. Arrows confirmed present for a
mouse and absent for touch.
2026-08-25 22:50:28 +02:00

249 lines
12 KiB
JavaScript

// Behaviour for the openGym site: the phone menu, the scroll reveals, the demo frame,
// and the two things that come from the GitLab API (repo counts, release timeline).
// Every one of them fails soft — the page is complete without any of this running.
const GL_PROJECT = 'https://gitlab.com/api/v4/projects/DuarteSantos8%2Fopengym'
/* ------------------------------------------------------------------ phone menu */
;(() => {
const btn = document.querySelector('.nav-toggle')
const menu = document.getElementById('menu')
if (!btn || !menu) return
const close = () => {
menu.classList.remove('open')
btn.setAttribute('aria-expanded', 'false')
// menu-open drives the dimming scrim; overflow keeps the page still underneath.
document.body.classList.remove('menu-open')
document.body.style.overflow = ''
}
btn.addEventListener('click', () => {
const open = menu.classList.toggle('open')
btn.setAttribute('aria-expanded', String(open))
document.body.classList.toggle('menu-open', open)
// The sheet scrolls on its own; letting the page scroll behind it is the classic
// mobile-menu bug where the reader loses their place on close.
document.body.style.overflow = open ? 'hidden' : ''
})
// The scrim is body::after, so it has no element of its own to listen on — a tap
// that lands outside both the sheet and the button is a tap on the scrim.
document.addEventListener('click', e => {
if (!document.body.classList.contains('menu-open')) return
if (e.target.closest('#menu') || e.target.closest('.nav-toggle')) return
close()
})
// Tapping a link inside the sheet navigates, so the sheet has to get out of the way —
// in-page anchors especially, which do not reload anything.
menu.addEventListener('click', e => { if (e.target.closest('a')) close() })
document.addEventListener('keydown', e => { if (e.key === 'Escape') close() })
// Rotating to landscape can widen past the breakpoint while the sheet is open, which
// would leave the body scroll-locked with no visible sheet to close.
addEventListener('resize', () => { if (innerWidth > 780) close() })
})()
/* --------------------------------------------------------------- scroll reveal */
;(() => {
const items = document.querySelectorAll('.rv')
if (!items.length) return
const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches
if (reduce || !('IntersectionObserver' in window)) {
items.forEach(el => el.classList.add('in'))
return
}
const io = new IntersectionObserver((entries, obs) => {
for (const e of entries) {
if (!e.isIntersecting) continue
e.target.classList.add('in')
obs.unobserve(e.target) // one-way: nothing re-hides on the way back up
}
}, { rootMargin: '0px 0px -12% 0px', threshold: .06 })
items.forEach(el => io.observe(el))
})()
/* ------------------------------------------------------------------ demo frame
The demo is the whole frontend bundle. It is only worth loading when the framed
phone is actually on screen, and never on a narrow viewport, where the CSS hides
the frame in favour of a plain "open it full-screen" card. */
;(() => {
const slot = document.querySelector('[data-demo]')
if (!slot) return
const mount = () => {
if (slot.dataset.mounted) return
if (!slot.offsetParent) return // frame hidden — phone layout
slot.dataset.mounted = '1'
const f = document.createElement('iframe')
f.src = slot.dataset.demo
f.title = 'openGym live demo'
f.loading = 'lazy'
f.style.cssText = 'width:100%;height:100%;border:0;display:block;border-radius:34px;background:#000'
slot.appendChild(f)
}
if (!('IntersectionObserver' in window)) { mount(); return }
const io = new IntersectionObserver((entries, obs) => {
for (const e of entries) if (e.isIntersecting) { mount(); obs.disconnect() }
}, { rootMargin: '600px' }) // load just before it scrolls into view
io.observe(slot)
// A rotation or a resized window can reveal the frame long after that first check.
addEventListener('resize', mount, { passive: true })
})()
/* ------------------------------------------------------- repo counts (nav + specs)
Points at gitlab.com while the GitHub account is suspended; switch the two API URLs
back to api.github.com once it is restored.
The cache keys carry a _gl suffix: a visitor with a still-warm sessionStorage entry
from the gitea era would otherwise be read with the old field names and show NaN. */
;(async () => {
const set = (id, v) => document.querySelectorAll('[data-gh="' + id + '"]').forEach(el => { el.textContent = v })
try {
let d = null
const cached = sessionStorage.getItem('repo_meta_gl')
if (cached) d = JSON.parse(cached)
else {
const r = await fetch(GL_PROJECT)
if (!r.ok) return
const j = await r.json()
// An unauthenticated project response carries star_count and forks_count and nothing
// else countable — open_issues_count is only there for a logged-in caller, so reading
// it here printed "undefined" on the live site. The issues endpoint answers it in the
// X-Total header instead, and GitLab lists that header in Access-Control-Expose-Headers,
// so the browser is allowed to read it. per_page=1 keeps the body to a single issue.
let issues = ''
try {
const ri = await fetch(GL_PROJECT + '/issues?state=opened&per_page=1')
if (ri.ok) issues = ri.headers.get('X-Total') || ''
} catch (e) { /* count stays blank rather than wrong */ }
d = { stars_count: j.star_count, forks_count: j.forks_count, open_issues_count: issues }
sessionStorage.setItem('repo_meta_gl', JSON.stringify(d))
}
set('stars', '★ ' + d.stars_count)
set('stars-n', d.stars_count)
set('forks-n', d.forks_count)
// Leave the placeholder standing rather than writing an empty box.
if (d.open_issues_count !== '' && d.open_issues_count != null) set('issues-n', d.open_issues_count)
} catch (e) { /* offline / rate-limited — leave placeholders */ }
})()
/* -------------------------------------------------------------- about timeline
Built from the published GitLab releases, so the page updates itself with every
release. The static entries marked data-fallback stay in place when the API is
unreachable; the hand-written first entry is always kept.
GitLab's release objects differ from Gitea's: the notes are `description` (not `body`),
the date is `released_at` (not `published_at`), the web link sits in `_links.self`, and
there is no draft/prerelease pair — a not-yet-released one is `upcoming_release`. */
;(async () => {
const tl = document.getElementById('milestones')
if (!tl) return
try {
let rel = null
const cached = sessionStorage.getItem('repo_releases_gl')
if (cached) rel = JSON.parse(cached)
else {
const r = await fetch(GL_PROJECT + '/releases?per_page=100')
if (!r.ok) return
rel = (await r.json()).filter(x => !x.upcoming_release)
.map(x => ({ tag: x.tag_name, name: x.name, at: x.released_at, body: x.description || '', url: (x._links && x._links.self) || ('https://gitlab.com/DuarteSantos8/opengym/-/releases/' + x.tag_name) }))
sessionStorage.setItem('repo_releases_gl', JSON.stringify(rel))
}
if (!rel.length) return
const fmt = d => new Date(d).toLocaleDateString('en-GB', { year: 'numeric', month: 'long', day: 'numeric' })
// first paragraph of the notes (hard-wrapped lines rejoined), markdown crudely stripped
const blurb = md => {
const lines = md.replace(/\r/g, '').split('\n')
let start = lines.findIndex(l => l.trim() && !l.trim().startsWith('#'))
if (start < 0) return ''
let para = []
for (let i = start; i < lines.length && lines[i].trim(); i++) para.push(lines[i].trim())
const txt = para.join(' ').replace(/\[([^\]]*)\]\([^)]*\)/g, '$1').replace(/[*_`>]/g, '')
return txt.length > 220 ? txt.slice(0, 217).replace(/\s+\S*$/, '') + '…' : txt
}
tl.querySelectorAll('[data-fallback]').forEach(el => el.remove())
for (const x of rel.slice().reverse()) { // oldest → newest, matching the timeline
const li = document.createElement('li')
const title = x.name && x.name !== x.tag ? x.name : x.tag
li.innerHTML = '<b></b><span class="when"></span><p></p>'
li.querySelector('b').textContent = title.startsWith(x.tag) ? title : x.tag + ' — ' + title
li.querySelector('.when').textContent = fmt(x.at)
const p = li.querySelector('p')
p.textContent = blurb(x.body) + ' '
const a = document.createElement('a')
a.href = x.url; a.rel = 'noopener'; a.textContent = 'notes →'
p.appendChild(a)
tl.appendChild(li)
}
} catch (e) { /* fallback entries stay */ }
})()
/* The "and the rest of it" rail: each card opens its own full text in a shared dialog, and
the arrows scroll the rail for pointers that cannot swipe. Everything the dialog shows is
read out of the card itself, so this only ever re-presents text that is already on the
page — with JS off the cards stay unclamped and there is nothing to open. */
;(() => {
const rail = document.querySelector('.frail')
const dlg = document.querySelector('.fdlg')
if (!rail || !dlg || typeof dlg.showModal !== 'function') return
const use = dlg.querySelector('.fdlg-ic use')
const title = dlg.querySelector('#fdlg-t')
const body = dlg.querySelector('.fdlg-b')
const go = dlg.querySelector('.fdlg-go')
rail.querySelectorAll('.fcard').forEach(card => {
card.addEventListener('click', () => {
const href = card.querySelector('.fcard-link')?.dataset.href
use.setAttribute('href', card.querySelector('.fcard-ic use').getAttribute('href'))
title.textContent = card.querySelector('.fcard-t').textContent
body.textContent = card.querySelector('.fcard-b').textContent
if (href) {
go.href = href
go.textContent = card.querySelector('.fcard-link').textContent + ' →'
go.hidden = false
} else {
go.hidden = true
}
dlg.showModal()
})
})
dlg.querySelector('.fdlg-x').addEventListener('click', () => dlg.close())
// Clicking the backdrop closes it: the click lands on the dialog itself, outside its box.
dlg.addEventListener('click', e => {
const r = dlg.getBoundingClientRect()
const inside = e.clientX >= r.left && e.clientX <= r.right && e.clientY >= r.top && e.clientY <= r.bottom
if (!inside) dlg.close()
})
const nav = document.querySelector('.frail-nav')
if (nav) {
nav.hidden = false
const [prev, next] = nav.querySelectorAll('.rnav')
const step = () => (rail.querySelector('.fcard')?.offsetWidth || 260) + 16
nav.addEventListener('click', e => {
const btn = e.target.closest('.rnav')
if (btn) rail.scrollBy({ left: Number(btn.dataset.dir) * step(), behavior: 'smooth' })
})
const sync = () => {
prev.disabled = rail.scrollLeft < 4
next.disabled = rail.scrollLeft > rail.scrollWidth - rail.clientWidth - 4
}
rail.addEventListener('scroll', sync, { passive: true })
addEventListener('resize', sync)
sync()
}
})()
/* The hero intro is armed in the inline <head> script, before the first paint — a deferred
script would arm it too late and the hero would flash. All this does is take the class off
once the last element has landed, so nothing stays mid-animation. */
;(() => {
const root = document.documentElement
if (!root.classList.contains('intro')) return
const done = () => root.classList.remove('intro')
const last = document.querySelector('.hero-stage') || document.querySelector('.hero .actions')
if (last) last.addEventListener('animationend', done, { once: true })
setTimeout(done, 2400) // if the animation never fires, do not leave the hero hidden
})()