Skip to content

Commit

Permalink
Prevent default on escape key press when dismissing tooltip (#2414)
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus authored Dec 1, 2023
1 parent 92412f2 commit 48a2405
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-glasses-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Prevent other Overlays closing when Escape is pressed while Tooltips are open
13 changes: 9 additions & 4 deletions app/components/primer/alpha/tool_tip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const DIRECTION_CLASSES = [
'tooltip-ne',
'tooltip-se',
'tooltip-nw',
'tooltip-sw'
'tooltip-sw',
]

function closeOpenTooltips(except?: Element) {
Expand Down Expand Up @@ -272,11 +272,11 @@ class ToolTipElement extends HTMLElement {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore popoverTargetElement is not in the type definition
this.control.popoverTargetElement?.addEventListener('beforetoggle', this, {
signal
signal,
})
this.ownerDocument.addEventListener('focusout', focusOutListener)
this.ownerDocument.addEventListener('focusin', focusInListener)
this.ownerDocument.addEventListener('keydown', this, {signal})
this.ownerDocument.addEventListener('keydown', this, {signal, captures: true})

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / Final

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / Final

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / Analyze

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / static

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / Accessibility

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / Lib (6.1.1, 2.7)

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / Lib (7.0.3, 3.0)

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / Components (6.1.1, 2.7)

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / CSS coverage

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / Lib (7.1.1, 3.2)

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / Components (7.0.3, 3.0)

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / Lib (main, 3.1)

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / Components (7.1.1, 3.2)

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / System

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / Lib (main, 3.2)

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / Components (main, 3.1)

No overload matches this call.

Check failure on line 279 in app/components/primer/alpha/tool_tip.ts

View workflow job for this annotation

GitHub Actions / Components (main, 3.2)

No overload matches this call.
}

disconnectedCallback() {
Expand All @@ -301,6 +301,11 @@ class ToolTipElement extends HTMLElement {
const isOpeningOtherPopover = event.type === 'beforetoggle' && event.currentTarget !== this
const shouldHide = isMouseLeaveFromButton || isEscapeKeydown || isMouseDownOnButton || isOpeningOtherPopover

if (showing && isEscapeKeydown) {
event.stopImmediatePropagation()
event.preventDefault()
}

await Promise.resolve()
if (!showing && shouldShow && !isPopoverOpen(this)) {
this.#showReason = event.type === 'mouseenter' ? 'mouse' : 'focus'
Expand Down Expand Up @@ -408,7 +413,7 @@ class ToolTipElement extends HTMLElement {
const position = getAnchoredPosition(this, this.control, {
side: this.#side,
align: this.#align,
anchorOffset: TOOLTIP_OFFSET
anchorOffset: TOOLTIP_OFFSET,
})
const anchorSide = position.anchorSide
const align = position.anchorAlign
Expand Down
20 changes: 20 additions & 0 deletions test/system/alpha/tooltip_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,25 @@ def test_tooltip_hidden_after_focus_change

assert_selector("tool-tip[for='dialog-show-my-dialog']", visible: :hidden)
end

def test_tooltip_escape_hides_only_tooltip
visit_preview(:tooltip_inside_primer_overlay)

find("button[popovertarget]").click

assert_selector("anchored-position[popover]", visible: :visible)

assert_selector("tool-tip[for='overlay-button']", visible: :hidden)

find("button#overlay-button").hover

assert_selector("tool-tip[for='overlay-button']", visible: :visible)

find("button#overlay-button").send_keys(:escape)

assert_selector("tool-tip[for='overlay-button']", visible: :hidden)

assert_selector("anchored-position[popover]", visible: :visible)
end
end
end

0 comments on commit 48a2405

Please sign in to comment.