Skip to content

Commit 4916566

Browse files
authored
fix: panel position in scrolled container #763 (#1338)
Make sure panel positioning is correct if the panel is placed in a container that has been scrolled horizontally or vertically.
1 parent 8d4b512 commit 4916566

4 files changed

Lines changed: 17 additions & 15 deletions

File tree

examples/panel-placement-positioned/index.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</head>
1111

1212
<body>
13-
<div class="parent" style="left: 50px; right: 50px; top: 50px; bottom: 50px; padding: 16px; border: 3px solid black; position: absolute">
13+
<div class="parent" style="left: 50px; right: 50px; top: 50px; bottom: 50px; padding: 16px; border: 3px solid black; position: fixed; overflow: auto">
1414
<form>
1515
Panel placement:
1616
<input
@@ -33,6 +33,15 @@
3333
<div id="autocomplete"></div>
3434
<div id="autocomplete-right"></div>
3535
</div>
36+
37+
<div style="width: 150%; height: 150%">
38+
<!-- Content that makes the parent scrollable. -->
39+
</div>
40+
</div>
41+
42+
43+
<div style="height: 3000px; width: 3000px">
44+
<!-- Content that makes the body scrollable. -->
3645
</div>
3746

3847
<script type="module" src="env.ts"></script>

packages/autocomplete-js/src/__tests__/panelPlacement.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ describe('panelPlacement', () => {
131131

132132
await waitFor(() => {
133133
expect(document.querySelector('.aa-Panel')).toHaveStyle({
134-
top: '124px', // TOP + HEIGHT + SCROLL
134+
top: '24px', // TOP + HEIGHT
135135
left: '11px', // LEFT
136136
right: '1890px', // CLIENT_WIDTH - (LEFT + WIDTH)
137137
width: 'unset',
@@ -176,7 +176,7 @@ describe('panelPlacement', () => {
176176

177177
await waitFor(() => {
178178
expect(document.querySelector('.aa-Panel')).toHaveStyle({
179-
top: '124px', // TOP + HEIGHT + SCROLL
179+
top: '24px', // TOP + HEIGHT
180180
left: '11px', // LEFT
181181
});
182182
});
@@ -218,7 +218,7 @@ describe('panelPlacement', () => {
218218

219219
await waitFor(() => {
220220
expect(document.querySelector('.aa-Panel')).toHaveStyle({
221-
top: '124px', // TOP + HEIGHT + SCROLL
221+
top: '24px', // TOP + HEIGHT
222222
right: '1890px', // CLIENT_WIDTH - (LEFT + WIDTH)
223223
});
224224
});
@@ -263,7 +263,7 @@ describe('panelPlacement', () => {
263263

264264
await waitFor(() => {
265265
expect(document.querySelector('.aa-Panel')).toHaveStyle({
266-
top: '124px', // TOP + HEIGHT + SCROLL
266+
top: '24px', // TOP + HEIGHT
267267
left: 0,
268268
right: 0,
269269
width: 'unset',
@@ -336,7 +336,7 @@ describe('panelPlacement', () => {
336336

337337
await waitFor(() => {
338338
expect(document.querySelector('.aa-Panel')).toHaveStyle({
339-
top: '124px', // TOP + HEIGHT + SCROLL
339+
top: '24px', // TOP + HEIGHT
340340
left: '11px', // LEFT
341341
right: '1890px', // CLIENT_WIDTH - (LEFT + WIDTH)
342342
width: 'unset',

packages/autocomplete-js/src/__tests__/positioning.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ describe('Panel positioning', () => {
184184
await waitFor(() => getByTestId(panelContainer, 'panel'));
185185

186186
expect(getByTestId(panelContainer, 'panel')).toHaveStyle({
187-
top: '140px',
187+
top: '40px',
188188
left: '300px',
189189
right: '1020px',
190190
});

packages/autocomplete-js/src/getPanelPlacementStyle.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,10 @@ export function getPanelPlacementStyle({
1515
environment,
1616
}: GetPanelPlacementStyleParams) {
1717
const containerRect = container.getBoundingClientRect();
18-
// Some browsers have specificities to retrieve the document scroll position.
19-
// See https://stackoverflow.com/a/28633515/9940315
2018
const offsetParent =
2119
container.offsetParent || environment.document.documentElement;
2220

23-
const scrollTop =
24-
(environment.pageYOffset as number) ||
25-
offsetParent.scrollTop ||
26-
environment.document.body.scrollTop ||
27-
0;
28-
const top = scrollTop + container.offsetTop + containerRect.height;
21+
const top = container.offsetTop + containerRect.height;
2922

3023
switch (panelPlacement) {
3124
case 'start': {

0 commit comments

Comments
 (0)