Skip to content

Commit f58407d

Browse files
crisbetojelbourn
authored andcommitted
fix(clipboard): scroll position changing while copying on some browsers (#20073)
The clipboard module works by creating a `textarea` dynamically, focusing it and copying its content. In order to prevent it from affecting the page layout, we put it at `-999em` off-screen, but the problem is that some browsers will try to move it into the view on focus by scrolling up. These changes use `position: fixed` and anchor the textarea to the top of the page so vertically it's within the viewport. (cherry picked from commit a6f1a33)
1 parent 216759b commit f58407d

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/cdk/clipboard/pending-copy.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ export class PendingCopy {
2626
const textarea = this._textarea = this._document.createElement('textarea');
2727
const styles = textarea.style;
2828

29-
// Hide the element for display and accessibility. Set an
30-
// absolute position so the page layout isn't affected.
31-
styles.opacity = '0';
32-
styles.position = 'absolute';
33-
styles.left = styles.top = '-999em';
29+
// Hide the element for display and accessibility. Set a fixed position so the page layout
30+
// isn't affected. We use `fixed` with `top: 0`, because focus is moved into the textarea
31+
// for a split second and if it's off-screen, some browsers will attempt to scroll it into view.
32+
styles.position = 'fixed';
33+
styles.top = styles.opacity = '0';
34+
styles.left = '-999em';
3435
textarea.setAttribute('aria-hidden', 'true');
3536
textarea.value = text;
3637
this._document.body.appendChild(textarea);

0 commit comments

Comments
 (0)