@@ -10,7 +10,9 @@ import { buildExternalLinkRel, EXTERNAL_LINK_TARGET } from "../lib/external-link
1010import { formatRelativeTimestamp } from "../lib/format.ts" ;
1111import {
1212 GITHUB_HOVERCARD_OPEN_DELAY_MS ,
13+ gitHubFilesChangedUrl ,
1314 githubLinkAnchorFromEvent ,
15+ gitHubProfileUrl ,
1416 parseGitHubLinkTarget ,
1517 type GitHubLinkTarget ,
1618} from "./github-link-target.ts" ;
@@ -126,23 +128,39 @@ function appendMetric(parent: HTMLElement, className: string, text: string): voi
126128 appendTextElement ( parent , "span" , `github-link-hovercard__metric ${ className } ` , text ) ;
127129}
128130
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+
129147function renderLoading ( card : HTMLDivElement ) : void {
130148 card . replaceChildren ( ) ;
131149 card . dataset . loading = "true" ;
132150 card . removeAttribute ( "data-state" ) ;
133- 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 ) ;
134155}
135156
136157function renderUnavailable ( card : HTMLDivElement ) : void {
137158 card . replaceChildren ( ) ;
138159 card . dataset . loading = "false" ;
139160 card . dataset . state = "unavailable" ;
140- appendTextElement (
141- card ,
142- "div" ,
143- "github-link-hovercard__unavailable" ,
144- t ( "githubPreview.unavailable" ) ,
145- ) ;
161+ const label = t ( "githubPreview.unavailable" ) ;
162+ card . setAttribute ( "aria-label" , label ) ;
163+ appendTextElement ( card , "div" , "github-link-hovercard__unavailable" , label ) ;
146164}
147165
148166function renderPreview ( card : HTMLDivElement , preview : GitHubPreview ) : void {
@@ -161,10 +179,12 @@ function renderPreview(card: HTMLDivElement, preview: GitHubPreview): void {
161179 stateDot . setAttribute ( "aria-hidden" , "true" ) ;
162180 badge . append ( stateDot , document . createTextNode ( state . label ) ) ;
163181 header . append ( badge ) ;
164- 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 (
165185 header ,
166- "span" ,
167186 "github-link-hovercard__repo" ,
187+ preview . href ,
168188 `${ preview . owner } /${ preview . repo } #${ preview . number } ` ,
169189 ) ;
170190 appendTextElement (
@@ -174,40 +194,36 @@ function renderPreview(card: HTMLDivElement, preview: GitHubPreview): void {
174194 formatRelativeTimestamp ( Date . parse ( preview . updatedAt ) ) ,
175195 ) ;
176196
177- const title = document . createElement ( "a" ) ;
178- title . className = "github-link-hovercard__title" ;
179- // Reuse the href the card was activated with; the target is already
180- // resolved and validated by parseGitHubLinkTarget, so no new parsing here.
181- title . href = preview . href ;
182- title . target = EXTERNAL_LINK_TARGET ;
183- title . rel = buildExternalLinkRel ( ) ;
184- title . textContent = preview . title ;
185-
186197 const footer = document . createElement ( "div" ) ;
187198 footer . className = "github-link-hovercard__footer" ;
188- const author = document . createElement ( "span" ) ;
189- 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+ ) ;
190205 if ( preview . avatarDataUrl ) {
191206 const avatar = document . createElement ( "img" ) ;
192207 avatar . className = "github-link-hovercard__avatar" ;
193208 avatar . alt = "" ;
194209 avatar . decoding = "async" ;
195210 avatar . referrerPolicy = "no-referrer" ;
196211 avatar . src = preview . avatarDataUrl ;
197- author . append ( avatar ) ;
212+ author . prepend ( avatar ) ;
198213 }
199- author . append ( document . createTextNode ( preview . login ) ) ;
200- footer . append ( author ) ;
201214
202215 const metrics = document . createElement ( "span" ) ;
203216 metrics . className = "github-link-hovercard__metrics" ;
204217 if ( preview . kind === "pull" ) {
205218 appendMetric ( metrics , "github-link-hovercard__metric--additions" , `+${ preview . additions ?? 0 } ` ) ;
206219 appendMetric ( metrics , "github-link-hovercard__metric--deletions" , `−${ preview . deletions ?? 0 } ` ) ;
207220 const files = preview . changedFiles ?? 0 ;
208- 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 (
209224 metrics ,
210- "" ,
225+ "github-link-hovercard__metric github-link-hovercard__metric--files" ,
226+ gitHubFilesChangedUrl ( preview ) ,
211227 t ( files === 1 ? "githubPreview.file" : "githubPreview.files" , {
212228 count : String ( files ) ,
213229 } ) ,
@@ -223,7 +239,9 @@ function renderPreview(card: HTMLDivElement, preview: GitHubPreview): void {
223239 ) ;
224240 }
225241 footer . append ( metrics ) ;
226- card . append ( header , title , footer ) ;
242+ card . append ( header ) ;
243+ appendCardLink ( card , "github-link-hovercard__title" , preview . href , preview . title ) ;
244+ card . append ( footer ) ;
227245 card . setAttribute (
228246 "aria-label" ,
229247 t ( "githubPreview.ariaLabel" , {
@@ -250,14 +268,16 @@ export class GitHubLinkHovercardProvider extends ReactiveElement {
250268 private card : HTMLDivElement | null = null ;
251269 private cardFocusInside = false ;
252270 private closeTimer : number | null = null ;
253- private describedBy : string | null = null ;
254271 private focusInside = false ;
255272 private openTimer : number | null = null ;
256273 private pointerInside = false ;
257274 private pointerOverCard = false ;
258275 private renderedPreview : GitHubPreview | null = null ;
259276 private renderedUnavailable = false ;
260277 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 ;
261281 private readonly previewTask = new Task ( this , {
262282 autoRun : false ,
263283 args : ( ) => [ this . activeTarget ] as const ,
@@ -392,6 +412,9 @@ export class GitHubLinkHovercardProvider extends ReactiveElement {
392412 } ;
393413
394414 private readonly handleFocusIn = ( event : Event ) => {
415+ if ( this . suppressFocusOpen ) {
416+ return ;
417+ }
395418 const anchor = githubLinkAnchorFromEvent ( event ) ;
396419 const target = anchor ? parseGitHubLinkTarget ( anchor . href ) : null ;
397420 if ( ! anchor || ! target ) {
@@ -447,9 +470,46 @@ export class GitHubLinkHovercardProvider extends ReactiveElement {
447470 private readonly handleKeyDown = ( event : KeyboardEvent ) => {
448471 if ( event . key === "Escape" ) {
449472 this . close ( ) ;
473+ return ;
450474 }
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 ( ) ;
451487 } ;
452488
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+
453513 private readonly handleClick = ( ) => {
454514 this . close ( ) ;
455515 } ;
@@ -476,7 +536,10 @@ export class GitHubLinkHovercardProvider extends ReactiveElement {
476536 this . close ( ) ;
477537 this . activeAnchor = anchor ;
478538 this . activeTarget = target ;
479- 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" ) ;
480543 this . activeAnchorObserver . observe ( this , { childList : true , subtree : true } ) ;
481544 this . openTimer = window . setTimeout ( ( ) => {
482545 this . openTimer = null ;
@@ -493,8 +556,9 @@ export class GitHubLinkHovercardProvider extends ReactiveElement {
493556 card . id = `openclaw-github-hovercard-${ nextHovercardId } ` ;
494557 card . className = "github-link-hovercard" ;
495558 card . dataset . open = "true" ;
496- card . setAttribute ( "role" , "tooltip" ) ;
497- 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" ) ;
498562 this . renderedPreview = null ;
499563 this . renderedUnavailable = false ;
500564 renderLoading ( card ) ;
@@ -504,12 +568,11 @@ export class GitHubLinkHovercardProvider extends ReactiveElement {
504568 card . addEventListener ( "pointerleave" , this . handleCardPointerLeave ) ;
505569 card . addEventListener ( "focusin" , this . handleCardFocusIn ) ;
506570 card . addEventListener ( "focusout" , this . handleCardFocusOut ) ;
571+ card . addEventListener ( "keydown" , this . handleCardKeyDown ) ;
507572 document . body . append ( card ) ;
508573 this . card = card ;
509- anchor . setAttribute (
510- "aria-describedby" ,
511- this . describedBy ? `${ this . describedBy } ${ card . id } ` : card . id ,
512- ) ;
574+ anchor . setAttribute ( "aria-controls" , card . id ) ;
575+ anchor . setAttribute ( "aria-expanded" , "true" ) ;
513576 this . listenForViewportChanges ( ) ;
514577 this . positionCard ( ) ;
515578
@@ -575,11 +638,9 @@ export class GitHubLinkHovercardProvider extends ReactiveElement {
575638 this . activeAnchorObserver . disconnect ( ) ;
576639 void this . previewTask . run ( [ null ] ) ;
577640 if ( this . activeAnchor ) {
578- if ( this . describedBy === null ) {
579- this . activeAnchor . removeAttribute ( "aria-describedby" ) ;
580- } else {
581- this . activeAnchor . setAttribute ( "aria-describedby" , this . describedBy ) ;
582- }
641+ this . activeAnchor . removeAttribute ( "aria-controls" ) ;
642+ this . activeAnchor . removeAttribute ( "aria-expanded" ) ;
643+ this . activeAnchor . removeAttribute ( "aria-haspopup" ) ;
583644 }
584645 this . card ?. remove ( ) ;
585646 this . card = null ;
@@ -588,7 +649,6 @@ export class GitHubLinkHovercardProvider extends ReactiveElement {
588649 this . activeAnchor = null ;
589650 this . activeTarget = null ;
590651 this . activeTrigger = null ;
591- this . describedBy = null ;
592652 this . cardFocusInside = false ;
593653 this . focusInside = false ;
594654 this . pointerInside = false ;
0 commit comments