-- Reusable Hyperscript behaviours and helper functions for the site.
-- Loaded from base.html before the Hyperscript runtime, so every behaviour
-- defined here can be applied with _="install Name(...)" on any page.
-- See HYPERSCRIPT_CONVENTIONS.md, section 4, before adding to this file.
-- Copies text to the clipboard on click and confirms in a status element.
-- Parameters are copied to element-scoped variables in init: a default set on
-- a parameter there is local to the init block and invisible to the handlers.
-- The write API is called directly: Hyperscript's own `clipboard` reads the
-- clipboard while resolving the target, which makes the browser ask permission.
behavior CopyToClipboard(text, status, doneLabel, resetAfter)
init
set :text to text
default :text to my @data-copy
set :status to status
default :status to me
set :doneLabel to doneLabel
default :doneLabel to "Copied"
set :resetAfter to resetAfter
default :resetAfter to 2s
set :idleLabel to the textContent of :status
end
on click queue none
call navigator.clipboard.writeText(:text)
put :doneLabel into :status
add .is-done to me
wait :resetAfter
put :idleLabel into :status
remove .is-done from me
end
on exception(error)
put "Copy failed" into :status
end
end
-- Scales a fixed-width frame to fit its box by setting the ratio the CSS reads.
def ui.fitFrame(box)
set frame to the first in box
call box.style.setProperty("--frame-scale", box.clientWidth / frame.offsetWidth)
end
behavior FitFrame
init
call ui.fitFrame(me)
end
on resize from window throttled at 100ms
call ui.fitFrame(me)
end
end
-- Swaps a project screenshot for the live site, which is kept in a
-- so the iframe does not load until the visitor asks for it.
behavior LiveSite
on click from .screen__load in me
set template to the first in me
set shot to the first .screen__shot in me
call shot.replaceChildren(template.content.cloneNode(true))
add .is-live to me
add @hidden to the first .screen__idle in me
remove @hidden from the first .screen__live in me
remove @hidden from the first .screen__note in me
end
end