Skip to main content

REF_CENTER_JS

Constant REF_CENTER_JS 

Source
pub const REF_CENTER_JS: &str = r#"
/* bc:center */
(function(id) {
    const reg = window.__bcRefs;
    const r = reg && reg.byId.get(Number(id));
    const el = r && typeof r.deref === 'function' ? r.deref() : r;
    if (!el || !el.isConnected) return JSON.stringify({ gone: true });
    try { el.scrollIntoView({ block: 'center', inline: 'center' }); } catch (e) {}
    const rect = el.getBoundingClientRect();
    const vw = window.innerWidth, vh = window.innerHeight;
    const x1 = Math.max(0, rect.left), y1 = Math.max(0, rect.top);
    const x2 = Math.min(vw, rect.right), y2 = Math.min(vh, rect.bottom);
    if (x2 - x1 <= 1 || y2 - y1 <= 1) {
        return JSON.stringify({ error: 'element has no visible box (hidden, zero-size, or outside the viewport after scrolling)' });
    }
    return JSON.stringify({ x: (x1 + x2) / 2, y: (y1 + y2) / 2 });
})
"#;
Expand description

(id) → centre of the element in viewport CSS px after scrolling it into view, clipped to the viewport.