Skip to content

Commit 732f869

Browse files
authored
infinite scroll: add phx-viewport-overrun-target (#4053)
1 parent 944c0da commit 732f869

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

assets/js/phoenix_live_view/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export const PHX_MAIN = "data-phx-main";
4343
export const PHX_ROOT_ID = "data-phx-root-id";
4444
export const PHX_VIEWPORT_TOP = "viewport-top";
4545
export const PHX_VIEWPORT_BOTTOM = "viewport-bottom";
46+
export const PHX_VIEWPORT_OVERRUN_TARGET = "viewport-overrun-target";
4647
export const PHX_TRIGGER_ACTION = "trigger-action";
4748
export const PHX_HAS_FOCUSED = "phx-has-focused";
4849
export const FOCUSABLE_INPUTS = [

assets/js/phoenix_live_view/hooks.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
PHX_LIVE_FILE_UPDATED,
44
PHX_PREFLIGHTED_REFS,
55
PHX_UPLOAD_REF,
6+
PHX_VIEWPORT_OVERRUN_TARGET,
67
} from "./constants";
78

89
import LiveUploader from "./live_uploader";
@@ -218,7 +219,8 @@ Hooks.InfiniteScroll = {
218219
scrollBefore = scrollNow;
219220
return pendingOp();
220221
}
221-
const rect = this.el.getBoundingClientRect();
222+
223+
const rect = this.findOverrunTarget();
222224
const topEvent = this.el.getAttribute(
223225
this.liveSocket.binding("viewport-top"),
224226
);
@@ -293,5 +295,23 @@ Hooks.InfiniteScroll = {
293295
}
294296
};
295297
},
298+
299+
findOverrunTarget() {
300+
let rect;
301+
const overrunTarget = this.el.getAttribute(
302+
this.liveSocket.binding(PHX_VIEWPORT_OVERRUN_TARGET),
303+
);
304+
if (overrunTarget) {
305+
const overrunEl = document.getElementById(overrunTarget);
306+
if (overrunEl) {
307+
rect = overrunEl.getBoundingClientRect();
308+
} else {
309+
throw new Error("did not find element with id " + overrunTarget);
310+
}
311+
} else {
312+
rect = this.el.getBoundingClientRect();
313+
}
314+
return rect;
315+
},
296316
};
297317
export default Hooks;

0 commit comments

Comments
 (0)