Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tooltip-text-selection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stackoverflow/stacks-svelte": patch
---

Keep a popover tooltip open when selecting and copying its text after focusing the trigger.
16 changes: 10 additions & 6 deletions packages/stacks-svelte/src/components/Popover/Popover.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,16 @@
};

const onFocusOut = (e: FocusEvent) => {
if (
tooltip ||
!pstate.visible ||
!pstate.closeOnFocusLeave ||
!dismissible
) {
if (tooltip) {
// Starting a text selection can blur the trigger while the tooltip is pressed.
// Qualify :active so it also matches non-link elements in quirks mode.
if (!content?.matches(":scope:active")) {
closeTooltip();
}
return;
}

if (!pstate.visible || !pstate.closeOnFocusLeave || !dismissible) {
return;
}

Expand Down
49 changes: 48 additions & 1 deletion packages/stacks-svelte/src/components/Popover/Popover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import sinon from "sinon";
import { expect } from "@open-wc/testing";
import { render, screen, waitFor } from "@testing-library/svelte";
import userEvent from "@testing-library/user-event";
import { setViewport } from "@web/test-runner-commands";
import { resetMouse, sendMouse, setViewport } from "@web/test-runner-commands";
import { createSvelteComponentsSnippet } from "../../../test-utils";

import {
Expand Down Expand Up @@ -1862,6 +1862,53 @@ describe("Popover", () => {
clock.restore();
});

it("keeps the tooltip open while selecting text after focusing its trigger", async () => {
const clock = sinon.useFakeTimers({
shouldAdvanceTime: true,
shouldClearNativeTimers: true,
});
const { container } = render(Popover, {
props: {
...defaultProps,
tooltip: true,
children: createSvelteComponentsSnippet([
defaultChildren.reference,
defaultChildren.content,
]),
},
});

// A focusable ancestor can receive focus as the drag begins.
container.tabIndex = -1;
await userEvent.tab();
await clock.runAllAsync();
await tick();
const text = screen.getByText("Popover Content");
const { left, right, top, height } = text.getBoundingClientRect();
const y = Math.round(top + height / 2);
try {
await sendMouse({
type: "move",
position: [Math.ceil(left), y],
});
await sendMouse({ type: "down" });
await clock.runAllAsync();
await tick();
expect(screen.getByRole("tooltip")).to.exist;

await sendMouse({
type: "move",
position: [Math.floor(right), y],
});
await sendMouse({ type: "up" });
expect(window.getSelection()?.toString()).to.equal(
"Popover Content"
);
} finally {
await resetMouse();
}
});

it("keep the tooltip content visible when the user tab from the reference to a focusable element within the content", async () => {
const clock = sinon.useFakeTimers({
shouldAdvanceTime: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@
role || (pstate.tooltip ? "tooltip" : "dialog")
);

const onFocusOut = (e: FocusEvent) => {
pstate.closeTooltip();
pstate.onFocusOut(e);
};

$effect(() => {
pstate.closeOnFocusLeave =
!pstate.tooltip &&
Expand All @@ -110,7 +105,7 @@
onmouseenter={supportsHover ? pstate.openTooltip : undefined}
onmouseleave={supportsHover ? pstate.closeTooltip : undefined}
onfocusin={pstate.openTooltip}
onfocusout={onFocusOut}
onfocusout={pstate.onFocusOut}
data-popper-placement={pstate.computedPlacement}
bind:this={contentElement}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
window.addEventListener("click", clearOutsidePointer, true);
ref.addEventListener("click", onClick);
ref.addEventListener("focusin", openTooltipOnKeyboardFocus);
ref.addEventListener("focusout", pstate.closeTooltip);
ref.addEventListener("focusout", pstate.onFocusOut);
ref.setAttribute("aria-describedby", `${pstate.id}-popover`);
return () => {
hoverMedia.removeEventListener("change", onHoverSupportChange);
Expand All @@ -153,7 +153,7 @@
window.removeEventListener("click", clearOutsidePointer, true);
ref.removeEventListener("click", onClick);
ref.removeEventListener("focusin", openTooltipOnKeyboardFocus);
ref.removeEventListener("focusout", pstate.closeTooltip);
ref.removeEventListener("focusout", pstate.onFocusOut);
};
};

Expand Down