@@ -6,10 +6,13 @@ import { ReactiveElement } from "lit";
66import type { ControlUiGitHubPreview } from "../../../src/gateway/control-ui-contract.js" ;
77import type { GatewayBrowserClient } from "../api/gateway.ts" ;
88import { i18n , t } from "../i18n/index.ts" ;
9+ import { buildExternalLinkRel , EXTERNAL_LINK_TARGET } from "../lib/external-link.ts" ;
910import { formatRelativeTimestamp } from "../lib/format.ts" ;
1011import {
1112 GITHUB_HOVERCARD_OPEN_DELAY_MS ,
13+ gitHubFilesChangedUrl ,
1214 githubLinkAnchorFromEvent ,
15+ gitHubProfileUrl ,
1316 parseGitHubLinkTarget ,
1417 type GitHubLinkTarget ,
1518} from "./github-link-target.ts" ;
@@ -19,6 +22,9 @@ const FAILURE_CACHE_MS = 30_000;
1922const CACHE_LIMIT = 100 ;
2023const VIEWPORT_PADDING = 12 ;
2124const CARD_GAP = 10 ;
25+ // Traversal grace: the pointer has to cross CARD_GAP of unowned page between the
26+ // link and the card, and neither surface is hovered during that crossing.
27+ const CLOSE_DELAY_MS = 120 ;
2228
2329type GitHubPreview = GitHubLinkTarget & ControlUiGitHubPreview ;
2430
@@ -122,23 +128,39 @@ function appendMetric(parent: HTMLElement, className: string, text: string): voi
122128 appendTextElement ( parent , "span" , `github-link-hovercard__metric ${ className } ` , text ) ;
123129}
124130
131+ function appendCardLink (
132+ parent : HTMLElement ,
133+ className : string ,
134+ href : string ,
135+ text : string ,
136+ ) : HTMLAnchorElement {
137+ const link = document . createElement ( "a" ) ;
138+ link . className = className ;
139+ link . href = href ;
140+ link . target = EXTERNAL_LINK_TARGET ;
141+ link . rel = buildExternalLinkRel ( ) ;
142+ link . textContent = text ;
143+ parent . append ( link ) ;
144+ return link ;
145+ }
146+
125147function renderLoading ( card : HTMLDivElement ) : void {
126148 card . replaceChildren ( ) ;
127149 card . dataset . loading = "true" ;
128150 card . removeAttribute ( "data-state" ) ;
129- appendTextElement ( card , "div" , "github-link-hovercard__loading" , t ( "githubPreview.loading" ) ) ;
151+ const label = t ( "githubPreview.loading" ) ;
152+ // The card is a dialog, so every render state has to leave it with a name.
153+ card . setAttribute ( "aria-label" , label ) ;
154+ appendTextElement ( card , "div" , "github-link-hovercard__loading" , label ) ;
130155}
131156
132157function renderUnavailable ( card : HTMLDivElement ) : void {
133158 card . replaceChildren ( ) ;
134159 card . dataset . loading = "false" ;
135160 card . dataset . state = "unavailable" ;
136- appendTextElement (
137- card ,
138- "div" ,
139- "github-link-hovercard__unavailable" ,
140- t ( "githubPreview.unavailable" ) ,
141- ) ;
161+ const label = t ( "githubPreview.unavailable" ) ;
162+ card . setAttribute ( "aria-label" , label ) ;
163+ appendTextElement ( card , "div" , "github-link-hovercard__unavailable" , label ) ;
142164}
143165
144166function renderPreview ( card : HTMLDivElement , preview : GitHubPreview ) : void {
@@ -157,10 +179,12 @@ function renderPreview(card: HTMLDivElement, preview: GitHubPreview): void {
157179 stateDot . setAttribute ( "aria-hidden" , "true" ) ;
158180 badge . append ( stateDot , document . createTextNode ( state . label ) ) ;
159181 header . append ( badge ) ;
160- appendTextElement (
182+ // Every link on the card reuses the href it was activated with; that target is
183+ // already resolved and validated by parseGitHubLinkTarget, so no reparsing here.
184+ appendCardLink (
161185 header ,
162- "span" ,
163186 "github-link-hovercard__repo" ,
187+ preview . href ,
164188 `${ preview . owner } /${ preview . repo } #${ preview . number } ` ,
165189 ) ;
166190 appendTextElement (
@@ -170,35 +194,36 @@ function renderPreview(card: HTMLDivElement, preview: GitHubPreview): void {
170194 formatRelativeTimestamp ( Date . parse ( preview . updatedAt ) ) ,
171195 ) ;
172196
173- const title = document . createElement ( "div" ) ;
174- title . className = "github-link-hovercard__title" ;
175- title . textContent = preview . title ;
176-
177197 const footer = document . createElement ( "div" ) ;
178198 footer . className = "github-link-hovercard__footer" ;
179- const author = document . createElement ( "span" ) ;
180- author . className = "github-link-hovercard__author" ;
199+ const author = appendCardLink (
200+ footer ,
201+ "github-link-hovercard__author" ,
202+ gitHubProfileUrl ( preview . login ) ,
203+ preview . login ,
204+ ) ;
181205 if ( preview . avatarDataUrl ) {
182206 const avatar = document . createElement ( "img" ) ;
183207 avatar . className = "github-link-hovercard__avatar" ;
184208 avatar . alt = "" ;
185209 avatar . decoding = "async" ;
186210 avatar . referrerPolicy = "no-referrer" ;
187211 avatar . src = preview . avatarDataUrl ;
188- author . append ( avatar ) ;
212+ author . prepend ( avatar ) ;
189213 }
190- author . append ( document . createTextNode ( preview . login ) ) ;
191- footer . append ( author ) ;
192214
193215 const metrics = document . createElement ( "span" ) ;
194216 metrics . className = "github-link-hovercard__metrics" ;
195217 if ( preview . kind === "pull" ) {
196218 appendMetric ( metrics , "github-link-hovercard__metric--additions" , `+${ preview . additions ?? 0 } ` ) ;
197219 appendMetric ( metrics , "github-link-hovercard__metric--deletions" , `−${ preview . deletions ?? 0 } ` ) ;
198220 const files = preview . changedFiles ?? 0 ;
199- appendMetric (
221+ // The diff-size chip's natural next step is the files-changed view; issues
222+ // have no such view, so their comment count stays plain text.
223+ appendCardLink (
200224 metrics ,
201- "" ,
225+ "github-link-hovercard__metric github-link-hovercard__metric--files" ,
226+ gitHubFilesChangedUrl ( preview ) ,
202227 t ( files === 1 ? "githubPreview.file" : "githubPreview.files" , {
203228 count : String ( files ) ,
204229 } ) ,
@@ -214,7 +239,9 @@ function renderPreview(card: HTMLDivElement, preview: GitHubPreview): void {
214239 ) ;
215240 }
216241 footer . append ( metrics ) ;
217- card . append ( header , title , footer ) ;
242+ card . append ( header ) ;
243+ appendCardLink ( card , "github-link-hovercard__title" , preview . href , preview . title ) ;
244+ card . append ( footer ) ;
218245 card . setAttribute (
219246 "aria-label" ,
220247 t ( "githubPreview.ariaLabel" , {
@@ -234,14 +261,23 @@ export class GitHubLinkHovercardProvider extends ReactiveElement {
234261 private readonly cache = new Map < string , CacheEntry > ( ) ;
235262 private activeAnchor : HTMLAnchorElement | null = null ;
236263 private activeTarget : GitHubLinkTarget | null = null ;
264+ // Which surface opened the current card: gates whether focus landing inside
265+ // the portaled card (e.g. clicking the title link) can hold it open, so a
266+ // pointer-driven open still fully releases on mouse-out (see handleCardPointerLeave).
267+ private activeTrigger : "focus" | "pointer" | null = null ;
237268 private card : HTMLDivElement | null = null ;
238- private describedBy : string | null = null ;
269+ private cardFocusInside = false ;
270+ private closeTimer : number | null = null ;
239271 private focusInside = false ;
240272 private openTimer : number | null = null ;
241273 private pointerInside = false ;
274+ private pointerOverCard = false ;
242275 private renderedPreview : GitHubPreview | null = null ;
243276 private renderedUnavailable = false ;
244277 private stopI18n : ( ( ) => void ) | null = null ;
278+ // Spans the synchronous focus() that hands focus back to the trigger, so the
279+ // card the user just dismissed cannot reopen under them (handleCardKeyDown).
280+ private suppressFocusOpen = false ;
245281 private readonly previewTask = new Task ( this , {
246282 autoRun : false ,
247283 args : ( ) => [ this . activeTarget ] as const ,
@@ -340,12 +376,45 @@ export class GitHubLinkHovercardProvider extends ReactiveElement {
340376 return ;
341377 }
342378 this . pointerInside = false ;
343- if ( ! this . focusInside ) {
344- this . close ( ) ;
379+ this . scheduleClose ( ) ;
380+ } ;
381+
382+ private readonly handleCardPointerEnter = ( ) => {
383+ this . pointerOverCard = true ;
384+ this . clearCloseTimer ( ) ;
385+ } ;
386+
387+ private readonly handleCardPointerLeave = ( ) => {
388+ this . pointerOverCard = false ;
389+ // A pointer-opened card must release fully on mouse-out even if a click
390+ // inside the card (e.g. the title link) left it focused; otherwise it
391+ // would stay stuck open with nothing left driving the intent.
392+ if ( this . activeTrigger === "pointer" ) {
393+ this . cardFocusInside = false ;
394+ }
395+ this . scheduleClose ( ) ;
396+ } ;
397+
398+ // The card is portaled to document.body, so focus landing on its title link
399+ // never reaches the provider's delegated focusin/focusout listeners; track it
400+ // directly so keyboard users can tab into the link without losing the card.
401+ private readonly handleCardFocusIn = ( ) => {
402+ this . cardFocusInside = true ;
403+ this . clearCloseTimer ( ) ;
404+ } ;
405+
406+ private readonly handleCardFocusOut = ( event : FocusEvent ) => {
407+ if ( event . relatedTarget instanceof Node && this . card ?. contains ( event . relatedTarget ) ) {
408+ return ;
345409 }
410+ this . cardFocusInside = false ;
411+ this . scheduleClose ( ) ;
346412 } ;
347413
348414 private readonly handleFocusIn = ( event : Event ) => {
415+ if ( this . suppressFocusOpen ) {
416+ return ;
417+ }
349418 const anchor = githubLinkAnchorFromEvent ( event ) ;
350419 const target = anchor ? parseGitHubLinkTarget ( anchor . href ) : null ;
351420 if ( ! anchor || ! target ) {
@@ -362,17 +431,85 @@ export class GitHubLinkHovercardProvider extends ReactiveElement {
362431 return ;
363432 }
364433 this . focusInside = false ;
365- if ( ! this . pointerInside ) {
434+ this . scheduleClose ( ) ;
435+ } ;
436+
437+ private clearCloseTimer ( ) : void {
438+ if ( this . closeTimer !== null ) {
439+ window . clearTimeout ( this . closeTimer ) ;
440+ this . closeTimer = null ;
441+ }
442+ }
443+
444+ // Hover intent spans link and card: dismissal waits out the traversal grace and
445+ // re-checks every surface that holds the card open, so moving from one to the
446+ // other keeps it alive and only leaving both closes it.
447+ private scheduleClose ( ) : void {
448+ this . clearCloseTimer ( ) ;
449+ if ( this . hoverIntentHeld ) {
450+ return ;
451+ }
452+ // Without a card there is only a pending open timer, and no gap to cross:
453+ // cancel the open now instead of granting grace to a card that never showed.
454+ if ( ! this . card ) {
366455 this . close ( ) ;
456+ return ;
367457 }
368- } ;
458+ this . closeTimer = window . setTimeout ( ( ) => {
459+ this . closeTimer = null ;
460+ if ( ! this . hoverIntentHeld ) {
461+ this . close ( ) ;
462+ }
463+ } , CLOSE_DELAY_MS ) ;
464+ }
465+
466+ private get hoverIntentHeld ( ) : boolean {
467+ return this . pointerInside || this . pointerOverCard || this . focusInside || this . cardFocusInside ;
468+ }
369469
370470 private readonly handleKeyDown = ( event : KeyboardEvent ) => {
371471 if ( event . key === "Escape" ) {
372472 this . close ( ) ;
473+ return ;
373474 }
475+ // The card is portaled to document.body and never lands next to its trigger
476+ // in the tab sequence; forward Tab in, and let the card hand focus back
477+ // (handleCardKeyDown), so its links stay keyboard-reachable at all.
478+ if ( event . key !== "Tab" || event . shiftKey || event . target !== this . activeAnchor ) {
479+ return ;
480+ }
481+ const [ first ] = this . cardFocusables ( ) ;
482+ if ( ! first ) {
483+ return ;
484+ }
485+ event . preventDefault ( ) ;
486+ first . focus ( ) ;
374487 } ;
375488
489+ private readonly handleCardKeyDown = ( event : KeyboardEvent ) => {
490+ if ( event . key !== "Escape" && event . key !== "Tab" ) {
491+ return ;
492+ }
493+ // Tab moves between the card's own links normally and only exits at the edge
494+ // of that run: the card has no tab-sequence neighbour, so leaving it lands on
495+ // the trigger like Escape does instead of dropping focus to the document.
496+ const focusables = this . cardFocusables ( ) ;
497+ const edge = event . shiftKey ? focusables [ 0 ] : focusables . at ( - 1 ) ;
498+ if ( event . key === "Tab" && document . activeElement !== edge ) {
499+ return ;
500+ }
501+ event . preventDefault ( ) ;
502+ const anchor = this . activeAnchor ;
503+ this . close ( ) ;
504+ this . suppressFocusOpen = true ;
505+ anchor ?. focus ( { preventScroll : true } ) ;
506+ this . suppressFocusOpen = false ;
507+ } ;
508+
509+ private cardFocusables ( ) : HTMLElement [ ] {
510+ return [ ...( this . card ?. querySelectorAll < HTMLElement > ( "a[href]" ) ?? [ ] ) ] ;
511+ }
512+
376513 private readonly handleClick = ( ) => {
377514 this . close ( ) ;
378515 } ;
@@ -384,6 +521,7 @@ export class GitHubLinkHovercardProvider extends ReactiveElement {
384521 delay : number ,
385522 ) : void {
386523 this . activate ( anchor , target , delay ) ;
524+ this . activeTrigger = trigger ;
387525 if ( trigger === "pointer" ) {
388526 this . pointerInside = true ;
389527 } else {
@@ -398,7 +536,10 @@ export class GitHubLinkHovercardProvider extends ReactiveElement {
398536 this . close ( ) ;
399537 this . activeAnchor = anchor ;
400538 this . activeTarget = target ;
401- this . describedBy = anchor . getAttribute ( "aria-describedby" ) ;
539+ // Announce the popup affordance as soon as the link is recognized; show()
540+ // flips the state once the card exists, close() takes the whole set away.
541+ anchor . setAttribute ( "aria-haspopup" , "dialog" ) ;
542+ anchor . setAttribute ( "aria-expanded" , "false" ) ;
402543 this . activeAnchorObserver . observe ( this , { childList : true , subtree : true } ) ;
403544 this . openTimer = window . setTimeout ( ( ) => {
404545 this . openTimer = null ;
@@ -415,17 +556,23 @@ export class GitHubLinkHovercardProvider extends ReactiveElement {
415556 card . id = `openclaw-github-hovercard-${ nextHovercardId } ` ;
416557 card . className = "github-link-hovercard" ;
417558 card . dataset . open = "true" ;
418- card . setAttribute ( "role" , "tooltip" ) ;
419- card . setAttribute ( "aria-live" , "polite" ) ;
559+ // A tooltip may not own controls: its content is flattened and unreachable.
560+ // The card is a non-modal dialog instead, named by the render functions.
561+ card . setAttribute ( "role" , "dialog" ) ;
420562 this . renderedPreview = null ;
421563 this . renderedUnavailable = false ;
422564 renderLoading ( card ) ;
565+ // The card is portaled to document.body, so the provider's delegated pointer
566+ // listeners never see it; it reports its own hover to keep intent shared.
567+ card . addEventListener ( "pointerenter" , this . handleCardPointerEnter ) ;
568+ card . addEventListener ( "pointerleave" , this . handleCardPointerLeave ) ;
569+ card . addEventListener ( "focusin" , this . handleCardFocusIn ) ;
570+ card . addEventListener ( "focusout" , this . handleCardFocusOut ) ;
571+ card . addEventListener ( "keydown" , this . handleCardKeyDown ) ;
423572 document . body . append ( card ) ;
424573 this . card = card ;
425- anchor . setAttribute (
426- "aria-describedby" ,
427- this . describedBy ? `${ this . describedBy } ${ card . id } ` : card . id ,
428- ) ;
574+ anchor . setAttribute ( "aria-controls" , card . id ) ;
575+ anchor . setAttribute ( "aria-expanded" , "true" ) ;
429576 this . listenForViewportChanges ( ) ;
430577 this . positionCard ( ) ;
431578
@@ -487,24 +634,25 @@ export class GitHubLinkHovercardProvider extends ReactiveElement {
487634 window . clearTimeout ( this . openTimer ) ;
488635 this . openTimer = null ;
489636 }
637+ this . clearCloseTimer ( ) ;
490638 this . activeAnchorObserver . disconnect ( ) ;
491639 void this . previewTask . run ( [ null ] ) ;
492640 if ( this . activeAnchor ) {
493- if ( this . describedBy === null ) {
494- this . activeAnchor . removeAttribute ( "aria-describedby" ) ;
495- } else {
496- this . activeAnchor . setAttribute ( "aria-describedby" , this . describedBy ) ;
497- }
641+ this . activeAnchor . removeAttribute ( "aria-controls" ) ;
642+ this . activeAnchor . removeAttribute ( "aria-expanded" ) ;
643+ this . activeAnchor . removeAttribute ( "aria-haspopup" ) ;
498644 }
499645 this . card ?. remove ( ) ;
500646 this . card = null ;
501647 this . renderedPreview = null ;
502648 this . renderedUnavailable = false ;
503649 this . activeAnchor = null ;
504650 this . activeTarget = null ;
505- this . describedBy = null ;
651+ this . activeTrigger = null ;
652+ this . cardFocusInside = false ;
506653 this . focusInside = false ;
507654 this . pointerInside = false ;
655+ this . pointerOverCard = false ;
508656 this . stopListeningForViewportChanges ( ) ;
509657 }
510658
0 commit comments