Skip to content

Commit 9f3160b

Browse files
fix(Link): prevent text selection on icon element (#3051)
1 parent 4a60f3b commit 9f3160b

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

packages/components/src/components/Link/Link.browser.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,40 @@ test("button in a disabled link exposes its disabled state", async () => {
5858
.element(page.getByTestId("button"))
5959
.toHaveAttribute("data-disabled", "true");
6060
});
61+
62+
test("selecting the whole external link does not select any part of the icon", async () => {
63+
const { container } = await render(<Link target="_blank">mittwald.de</Link>);
64+
const link = container.querySelector('[role="link"]') as HTMLElement;
65+
66+
const range = document.createRange();
67+
range.selectNodeContents(link);
68+
const selection = window.getSelection();
69+
selection?.removeAllRanges();
70+
selection?.addRange(range);
71+
72+
expect(selection?.toString()).toBe("mittwald.de");
73+
});
74+
75+
test("selecting the whole download link does not select any part of the icon", async () => {
76+
const { container } = await render(<Link download>Download the plans</Link>);
77+
const link = container.querySelector('[role="link"]') as HTMLElement;
78+
79+
const range = document.createRange();
80+
range.selectNodeContents(link);
81+
const selection = window.getSelection();
82+
selection?.removeAllRanges();
83+
selection?.addRange(range);
84+
85+
expect(selection?.toString()).toBe("Download the plans");
86+
});
87+
88+
test("the link icon is excluded from text selection", async () => {
89+
const { container } = await render(<Link target="_blank">mittwald.de</Link>);
90+
const icon = container.querySelector("svg") as SVGElement;
91+
const style = getComputedStyle(icon);
92+
93+
expect(
94+
style.getPropertyValue("user-select") ||
95+
style.getPropertyValue("-webkit-user-select"),
96+
).toBe("none");
97+
});

packages/components/src/components/Link/Link.module.d.scss.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ declare const classNames: {
22
readonly link: "link";
33
readonly inline: "inline";
44
readonly icon: "icon";
5+
readonly linkIcon: "linkIcon";
56
readonly "size-s": "size-s";
67
readonly button: "button";
7-
readonly linkIcon: "linkIcon";
88
readonly light: "light";
99
readonly dark: "dark";
1010
readonly "dark-static": "dark-static";

packages/components/src/components/Link/Link.module.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
height: var(--link--icon-height);
3737
}
3838

39+
.linkIcon {
40+
-webkit-user-select: none;
41+
user-select: none;
42+
}
43+
3944
&[aria-disabled] {
4045
color: var(--link--color--disabled);
4146
}

0 commit comments

Comments
 (0)