Skip to content

[Spike] Investigate a feasible programmatic focus detection #9549

Closed

Description

@weronikaolejniczak

The purpose of this task is to investigate whether we can easily detect if focus was made programmatically (.focus()) or through user interaction (mouse, keyboard).

Context

Current behavior

Focus return triggers unwanted tooltips. Closing a modal or popover returns focus to the trigger, which unconditionally fires the tooltip. When an overlay closes and the browser returns focus to the trigger, the tooltip fires immediately. Disorienting after confirmation modals.

Expected behavior

Focus return does not trigger tooltips. Closing a modal, flyout, or popover does not re-show the tooltip on the trigger element.

Notes

  • For modal, popover, etc. we know what the trigger is and when we are closing these components. How do we return focus to the trigger while communicating not to show the tooltip?

Acceptance criteria

  • The real question is answered: is it really a problem? What cases are these? Do we always want to not show the tooltip on return focus?... That's 3 questions.
  • Both of these are valid conclusions:
    • there's a feasible solution that addressed the problem,
    • the solutions are too complex for the value they offer.
  • (if applicable) Implementation task is created

Activity

  1. changed the title [-][Spike] Investigate if there's a feasible way to detect programatic return focus[/-] [+][Spike] Investigate a feasible programmatic focus detection[/+] on Apr 1, 2026
  2. ghudgins commented on Apr 7, 2026

    @ghudgins

    thanks @weronikaolejniczak - this is exactly what i'm hoping we can fix broadly. it'd close a bunch of age old papercuts if we can. i'm hopeful!

  3. weronikaolejniczak commented on Apr 7, 2026

    @weronikaolejniczak
    ContributorAuthor

    @ghudgins if you have them handy, could you drop them here in the comment? 🙏🏻 Thank you, Graham!

  4. ghudgins commented on Apr 7, 2026

    @ghudgins

    oh I don't have any issues to link, but I've seen this issue forever and I think the date picker pushed me over the edge.

  5. weronikaolejniczak commented on Apr 8, 2026

    @weronikaolejniczak
    ContributorAuthor

    Current behavior

    Behavior WCAG-compliant? Standard tooltip behavior In code
    Any focus event → tooltip shows ⚠️ Partially, correct for keyboard navigation, unexpected for programmatic focus return Typically only intentional keyboard focus triggers the tooltip onFocus → setState({ hasFocus: true }) → showToolTip() → setTimeout(250ms) → setState({ visible: true }) + toolTipManager.registerTooltip()
    Mouse hover → tooltip shows ✅ Yes Yes onMouseOver → showToolTip() → same timeout flow
    Only one tooltip visible at a time ✅ Not required but not in conflict Common convention toolTipManager.registerTooltip() calls all previously registered hideToolTip callbacks before registering the new one; previously raised on e.g. #5883
    Keyboard-focused + mouse moves out → tooltip stays ✅ Yes, 1.4.13 Persistent (focus trigger still active) Yes onMouseOut checks !this.state.hasFocus before calling hideToolTip()
    Mouse over + Tab away → tooltip closes ⚠️ Violation, SC 1.4.13 Persistent requires content to remain visible while the hover trigger is active; Tab removes focus but the mouse is still over the trigger Rarely implemented, not an expected behavior onBlur → setState({ hasFocus: false }) → hideToolTip() unconditionally, regardless of mouse position
    Escape → closes tooltip ✅ Yes, 1.4.13 Dismissible Yes onEscapeKey → setState({ hasFocus: false }) → hideToolTip()
    Click trigger → tooltip persists on hover out ✅ Yes, 1.4.13 Persistent (both hover and focus triggers active) Many tooltips close on click but WCAG-conformant ones must not when focus remains After click: hasFocus: true; onMouseOut checks !hasFocus → does not call hideToolTip()
    Focus returned from modal/flyout/popover → tooltip shows ⚠️ Technically compliant (focus trigger fires) but unexpected and not addressed by WCAG Same onFocus flow, no distinction between intentional keyboard focus and programmatic focus return

    WCAG requirements

    Code Level Requirement (exact) Met? Reference
    SC 1.4.13 - Persistent AA "The additional content remains visible until the hover or focus trigger is removed, the user dismisses it, or its information is no longer valid." ⚠️ Partially, violated when Tab away while mouse is still over trigger https://www.w3.org/TR/WCAG22/#content-on-hover-or-focus
    SC 1.4.13 - Dismissible AA "A mechanism is available to dismiss the additional content without moving pointer hover or keyboard focus, unless the additional content communicates an input error or does not obscure or replace other content." ✅ Yes, Escape key dismisses https://www.w3.org/TR/WCAG22/#content-on-hover-or-focus
    SC 1.4.13 - Hoverable AA "If pointer hover can trigger the additional content, then the pointer can be moved over the additional content without the additional content disappearing." ✅ Yes https://www.w3.org/TR/WCAG22/#content-on-hover-or-focus
    ARIA - Tooltip role - "The element that serves as the tooltip container has role tooltip." ✅ Yes, role="tooltip" set on EuiToolTipPopover https://www.w3.org/WAI/ARIA/apg/patterns/tooltip/
    ARIA - Trigger relationship — "The element that triggers the tooltip references the tooltip element with aria-describedby." ✅ Yes, set on child with cloneElement when isVisible && id https://www.w3.org/WAI/ARIA/apg/patterns/tooltip/
    ARIA - Keyboard: Escape - "Escape: Dismisses the Tooltip." ✅ Yes, onEscapeKey handles this https://www.w3.org/WAI/ARIA/apg/patterns/tooltip/

    WCAG references

    Additional notes

    These requirements are not about hover/focus interactions but are still required to conform with a11y guidelines:

    Requirement Source Met? Notes
    Tooltip must not contain interactive content (links, buttons) WAI-ARIA APG ⚠️ Not enforced EUI does not restrict what content prop accepts; consumers can pass interactive elements, which would be inaccessible; we can enforce with ESLint rule
    Tooltip must not receive keyboard focus WAI-ARIA APG ✅ Yes EuiToolTipPopover has no tabIndex; portalled but not in the tab order
    Color contrast of tooltip text must meet 4.5:1 (normal text) or 3:1 (large text) SC 1.4.3 (Level AA) ✅ Yes -
    The tooltip must not obscure the trigger element it describes SC 1.4.13 Dismissible intent ⚠️ Not guaranteed Position is calculated via findPopoverPosition with a default offset of 16px but edge cases near viewport boundaries may cause overlap

    What we are trying to fix

    Tooltip should not re-open when focus is programmatically returned to the trigger

    When a modal, flyout, popover or any focus trap closes, focus is typically returned to the element that triggered it. If that element has a tooltip, the tooltip currently re-opens even though the user did not intentionally navigate to it. This is disruptive for mouse users who clicked to open an overlay, then closed it.

    WCAG SC 1.4.13 does not explicitly address programmatic focus return. The requirement to show on focus applies to intentional keyboard navigation; suppressing the tooltip for programmatic returns is a UX improvement that does not conflict with any normative criterion, provided it is not suppressed for keyboard users who deliberately navigate to the trigger (e.g. Tab).

    WCAG requirements relevant to the fix

    Requirement Impact on fix
    SC 1.4.13 Persistent Fix must not suppress tooltip for keyboard-navigated focus
    SC 1.4.13 Dismissible Any solution must preserve Escape-to-dismiss behavior
    ARIA - Keyboard focus triggers tooltip Any solution must continue showing the tooltip when the user tabs to the trigger intentionally

    Potential solutions

    Viable candidates

    1. relatedTarget detached node check

    In onFocus check whether event.relatedTarget is no longer in the document. If the overlay unmounted before returning focus, the previous active element is detached and isConnected is false.

    const onFocus = (e: FocusEvent) => {
      const fromDetached = e.relatedTarget instanceof Node && !e.relatedTarget.isConnected;
      if (!fromDetached) showToolTip();
    };

    Covers: Any overlay, EUI or third-party, that unmounts its DOM before returning focus (EuiModal, most EuiPopover usages).

    Does not cover:

    • EuiFlyout with includeFixedHeadersInFocusTrap or unmounted-but-kept-in-tree patterns, relatedTarget is still connected.
    • First-ever page focus - relatedTarget is null, which also passes the check (tooltip shows), so no special handling needed there.
    • Race condition: if React returns focus synchronously during cleanup but the overlay's DOM is removed asynchronously (e.g. after a state flush), isConnected may still be true at the moment onFocus fires.

    2. Tab keydown tracking

    Add a module-level flag in tool_tip_manager.ts driven by a single document keydown listener. Set true on Tab/Shift+Tab, reset to false after the next focus event is consumed.

    // tool_tip_manager.ts
    let lastKeyWasTab = false;
    document.addEventListener('keydown', (e) => { lastKeyWasTab = e.key === 'Tab'; });
    export const consumeLastKeyWasTab = () => { const v = lastKeyWasTab; lastKeyWasTab = false; return v; };
    // onFocus in EuiToolTip
    const onFocus = () => {
      if (consumeLastKeyWasTab()) showToolTip();
    };

    Covers: All overlays for mouse users (no Tab pressed → tooltip suppressed). Keyboard users who Tab to the trigger always get the tooltip.

    Does not cover: Keyboard user presses Escape to close a modal → focus returns to trigger → last key was Escape, not Tab → tooltip suppressed. The user navigated to the trigger via keyboard at some earlier point but no Tab immediately preceded focus return. Suboptimal for keyboard-only users, though WCAG does not require re-showing on programmatic return.

    3. isMouseOver + Tab keydown combo (BEST)

    Combines the isMouseOver ref (from mouseenter/mouseleave on the anchor) with the lastKeyWasTab flag above. Show the tooltip in onFocus only if either signal is active. Also fix onBlur to only hide when the mouse is not over the trigger, this simultaneously fixes the SC 1.4.13 Persistent violation.

    // onFocus
    const onFocus = () => {
      if (isMouseOverRef.current || consumeLastKeyWasTab()) showToolTip();
    };
    
    // onBlur
    const onBlur = () => {
      if (!isMouseOverRef.current) hideToolTip();
    };

    Covers:

    • Mouse user hovers → focus returns programmatically → isMouseOver is false → tooltip suppressed ✅
    • Keyboard user Tabs to trigger → lastKeyWasTab is true → tooltip shows ✅
    • Mouse user hovers + Tabs away → isMouseOver is true in onBlur → tooltip stays (fixes SC 1.4.13 Persistent) ✅

    Does not cover: Keyboard user presses Escape to close a modal → lastKeyWasTab is false, isMouseOver is false → tooltip suppressed. Same trade-off as solution 2 — acceptable, WCAG does not require re-showing on programmatic return.

    4. :focus-visible check (ACCEPTABLE)

    Works for mouse users but fails for keyboard navigation. When a keyboard user presses Escape to close a modal, the browser's input modality is still "keyboard", so :focus-visible is true on the programmatically returned focus - the tooltip shows. Browser handling of :focus-visible on programmatic .focus() calls is also not normatively specified and differs across browsers.

    EDIT: After further investigation this can be absolutely acceptable. Checked in Chrome (which has a tooltip re-open issue), Safari (which doesn't have this issue with or without the fix) and Firefox (which has a tooltip re-open issue). For mouse users, the tooltip doesn't show on focus return, for keyboard users it does.

    Requires coordinated changes

    5. Shared flag from EUI overlay components

    Before returning focus, each EUI overlay sets a module-level isFocusReturnPending flag. EuiToolTip.onFocus checks and clears it.

    The most reliable and explicit approach - no heuristics, no race conditions but requires changes to EuiModal, EuiFlyout, EuiPopover, EuiFocusTrap, and any other component that manages focus return. Does not cover third-party focus traps. Creates coupling between overlay components and tooltip that would need to be maintained as new overlay components are added.

    Ruled out

    6. Mouse position tracking alone

    Cannot work standalone. A keyboard user Tabbing to the trigger has the mouse somewhere unrelated on the screen - isMouseOver would be false on focus - tooltip never shows. This is a WCAG violation (keyboard focus must trigger the tooltip). Only valid as part of solution 3.

    7. triggerOnFocus prop

    Not a solution - a manual escape hatch. Requires updating every EuiButtonIcon + overlay callsite individually. Any site that omits it retains the broken behavior. Setting it globally to false would violate the WCAG keyboard focus requirement.

    Summary

    There is an existing unaddressed WCAG gap: SC 1.4.13 Persistent is currently violated when the user has the mouse over the trigger and presses Tab to move focus elsewhere — the tooltip closes unconditionally in onBlur.

    The best solution is solution 3 (isMouseOver + Tab keydown tracking) because:

    • It fixes UX-3 for both mouse and keyboard users without touching any overlay component.
    • It simultaneously fixes the SC 1.4.13 Persistent gap (mouse over + Tab away) at no extra cost.
    • It works for any focus trap, EUI or third-party.

    We might get away with :focus-visible check which is the simplest if we can accept the trade-off that the tooltip is still showing on keyboard navigation (e.g. pressing Escape to exit the modal/flyout).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions