Skip to content

Commit f4cc4b4

Browse files
committed
fix: prevent unwanted auto scrolling with Popover
In a previous commit for the Drawer `.scrollIntoView` was added, this change allows one to override that behaviour
1 parent 40ac6b6 commit f4cc4b4

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

libs/web-components/src/components/focus-trap/FocusTrap.svelte

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
<svelte:options customElement="goa-focus-trap" />
1+
<svelte:options
2+
customElement={{
3+
tag: "goa-focus-trap",
4+
props: {
5+
preventScrollIntoView: {
6+
attribute: "prevent-scroll-into-view",
7+
type: "Boolean",
8+
},
9+
},
10+
}}
11+
/>
212

313
<script lang="ts">
414
import { onMount, tick } from "svelte";
@@ -7,6 +17,8 @@
717
// allow for outside control of whether focus trap should re-focus the first element is open/closed (see Drawer)
818
export let open: boolean = false;
919
20+
export let preventScrollIntoView: boolean = false;
21+
1022
// Private
1123
1224
let rootEl: HTMLElement;
@@ -150,7 +162,11 @@
150162
// If the focus element is at the bottom, we want to automatically scroll down to the focused element (when user uses keyboard)
151163
const focusedElement = e?.target;
152164
153-
if (focusedElement && focusedElement instanceof HTMLElement) {
165+
if (
166+
!preventScrollIntoView &&
167+
focusedElement &&
168+
focusedElement instanceof HTMLElement
169+
) {
154170
focusedElement.scrollIntoView({ behavior: "smooth", block: "center" });
155171
}
156172
}

libs/web-components/src/components/menu-button/MenuButton.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
on:_open={open}
132132
padded="false"
133133
tabindex="-1"
134+
prevent-scroll-into-view={true}
134135
>
135136
<goa-button
136137
bind:this={_targetEl}

libs/web-components/src/components/popover/Popover.svelte

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
type: "Boolean",
99
attribute: "disable-global-close-popover",
1010
},
11+
preventScrollIntoView: {
12+
attribute: "prevent-scroll-into-view",
13+
type: "Boolean",
14+
},
1115
},
1216
}}
1317
/>
@@ -35,6 +39,9 @@
3539
export let width: string = "";
3640
export let height: "full" | "wrap-content" = "wrap-content";
3741
42+
// flag passed to the FocusTrap that will prevent unwanted scrolling
43+
export let preventScrollIntoView: boolean = false;
44+
3845
// allows to override the default padding when content needs to be flush with boundries
3946
export let padded: string = "true";
4047
@@ -401,7 +408,7 @@
401408
style("padding", _padded ? "var(--goa-space-m)" : "0"),
402409
)}
403410
>
404-
<goa-focus-trap open="true">
411+
<goa-focus-trap open="true" prevent-scroll-into-view={preventScrollIntoView || undefined}>
405412
<div bind:this={_focusTrapEl}>
406413
<slot />
407414
</div>

0 commit comments

Comments
 (0)