// ==UserScript== // @name Bluesky: keep nested profile hover cards open // @namespace https://mcint.io/ // @version 0.1.0 // @description Keeps a profile hover card open while the pointer is in a card opened from inside it (bluesky-social/social-app#8559). // @match https://bsky.app/* // @run-at document-start // @grant none // ==/UserScript== // Why this is needed: social-app's hover cards render through a context-based // (src/components/Portal.tsx), so a card opened from a mention inside // another card is a *sibling* of that card in both the DOM and the React tree. // Moving onto the child therefore fires onPointerLeave on the parent, which // hides after 150 ms + 200 ms fade and, on unmount, takes the child with it. // // What this does: tracks which card spawned which, and while the pointer is in // a descendant card, replays pointerover on each ancestor card (React turns a // pointerover with no relatedTarget into onPointerEnter, i.e. 'hovered-card'). // When the pointer leaves the chain, it replays pointerout so they hide as usual. ;(function install(win) { 'use strict' const doc = win.document const parentOf = new Map() // card root element -> parent card root | null let current = null // card root under the pointer, if any let faking = false // Card root = floating-ui wrapper
whose // only child is
// (ProfileHoverCard/index.web.tsx). Update here if the markup changes. function isCardRoot(el) { if (el.nodeType !== 1 || !el.style || el.childElementCount !== 1) return false if (el.style.position !== 'absolute' && el.style.position !== 'fixed') return false const inner = el.firstElementChild if (!inner.style || inner.style.willChange !== 'transform') return false return /\bfade(In|Out)\b/.test( inner.style.animationName || inner.style.animation || inner.getAttribute('style') || '', ) } function cardOf(node) { for (let n = node; n && n !== doc; n = n.parentNode) { if (parentOf.has(n)) return n } return null } function ancestors(card) { const out = [] for (let p = card ? parentOf.get(card) : null; p; p = parentOf.get(p)) out.push(p) return out } function fake(type, el) { if (!el.isConnected) return faking = true try { el.dispatchEvent( new win.PointerEvent(type, { bubbles: true, composed: true, pointerType: 'mouse', relatedTarget: null, }), ) } finally { faking = false } } function setCurrent(next) { if (next === current) return const keep = new Set(ancestors(next)) for (const a of ancestors(current)) { if (a !== next && !keep.has(a)) fake('pointerout', a) // -> 'unhovered-card' } for (const a of keep) fake('pointerover', a) // -> 'hovered-card' current = next } win.addEventListener( 'pointerover', e => { if (!faking) setCurrent(cardOf(e.target)) }, true, ) win.addEventListener( 'pointerout', e => { if (!faking && e.relatedTarget == null) setCurrent(null) // left the window }, true, ) new win.MutationObserver(records => { for (const r of records) { for (const n of r.addedNodes) { // A card that appears while the pointer is inside another card was // opened from a mention in that card. if (!parentOf.has(n) && isCardRoot(n)) parentOf.set(n, current) } for (const n of r.removedNodes) { if (!parentOf.has(n)) continue if (current === n) setCurrent(null) parentOf.delete(n) } } }).observe(doc.documentElement, {childList: true, subtree: true}) })(typeof unsafeWindow !== 'undefined' ? unsafeWindow : window)