2022-07-26 16:12:36 +08:00
|
|
|
export function isWindow(obj: any): obj is Window {
|
2020-04-01 17:38:21 +08:00
|
|
|
return obj !== null && obj !== undefined && obj === obj.window;
|
|
|
|
}
|
|
|
|
|
2024-06-15 15:39:01 +08:00
|
|
|
const getScroll = (target: HTMLElement | Window | Document | null): number => {
|
2016-09-29 18:00:23 +08:00
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-04-01 17:38:21 +08:00
|
|
|
let result = 0;
|
|
|
|
if (isWindow(target)) {
|
2024-06-15 15:39:01 +08:00
|
|
|
result = target.pageYOffset;
|
2020-04-01 17:38:21 +08:00
|
|
|
} else if (target instanceof Document) {
|
2024-06-15 15:39:01 +08:00
|
|
|
result = target.documentElement.scrollTop;
|
2022-07-26 16:12:36 +08:00
|
|
|
} else if (target instanceof HTMLElement) {
|
2024-06-15 15:39:01 +08:00
|
|
|
result = target.scrollTop;
|
2020-04-01 17:38:21 +08:00
|
|
|
} else if (target) {
|
2022-07-26 16:12:36 +08:00
|
|
|
// According to the type inference, the `target` is `never` type.
|
|
|
|
// Since we configured the loose mode type checking, and supports mocking the target with such shape below::
|
|
|
|
// `{ documentElement: { scrollLeft: 200, scrollTop: 400 } }`,
|
|
|
|
// the program may falls into this branch.
|
|
|
|
// Check the corresponding tests for details. Don't sure what is the real scenario this happens.
|
2024-06-22 21:59:12 +08:00
|
|
|
/* biome-ignore lint/complexity/useLiteralKeys: target is a never type */ /* eslint-disable-next-line dot-notation */
|
2024-06-15 15:39:01 +08:00
|
|
|
result = target['scrollTop'];
|
2016-09-29 18:00:23 +08:00
|
|
|
}
|
2022-07-26 16:12:36 +08:00
|
|
|
|
2020-04-01 17:38:21 +08:00
|
|
|
if (target && !isWindow(target) && typeof result !== 'number') {
|
2024-06-15 15:39:01 +08:00
|
|
|
result = (target.ownerDocument ?? target).documentElement?.scrollTop;
|
2020-04-01 17:38:21 +08:00
|
|
|
}
|
|
|
|
return result;
|
2024-06-15 15:39:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default getScroll;
|