Skip to content

Commit 3589fd2

Browse files
authored
Resolve <body> to the root scrolling element, as well (#10)
* Check types during unit tests ⚙️ * Resolve the `<body>` to the document's `scrollingElement` * Try another way of formatting during commit * formatting
1 parent a6e1561 commit 3589fd2

File tree

9 files changed

+24
-15
lines changed

9 files changed

+24
-15
lines changed

.changeset/hip-memes-beam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hirasso/restore-scroll": patch
3+
---
4+
5+
Check types during unit tests

.changeset/mean-baboons-leave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hirasso/restore-scroll": patch
3+
---
4+
5+
Resolve `<body>` to the root scrolling element

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Make sure the PR fulfills as many of the following requirements as possible
1818

1919
- [ ] The PR is submitted to the `main` branch
2020
- [ ] The code was formatted before pushing (`pnpm run format`)
21-
- [ ] All tests are passing (`pnpm run test`)
2221
- [ ] New or updated tests are included
2322
- [ ] The documentation was updated as required
2423

.github/workflows/unit-tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@ jobs:
3434
- name: Build
3535
run: pnpm run build
3636

37+
- name: Typecheck
38+
run: pnpm run typecheck
39+
3740
- name: Run tests
3841
run: pnpm run test:unit

.lintstagedrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export default {
2-
"**/*.{js,ts,mjs}": ["prettier --write"],
2+
"**/*.{js,ts,mjs}": ["pnpm run format"],
33
"**/*.astro": ["astro check"],
44
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"test:unit:watch": "vitest --config ./tests/unit/vitest.config.ts",
4444
"test:e2e": "pnpm exec playwright test --config ./tests/e2e/config.playwright.ts",
4545
"test:e2e:dev": "PLAYWRIGHT_ENV=dev pnpm run test:e2e --ui",
46-
"test:e2e:install": "pnpm exec playwright install --with-deps"
46+
"test:e2e:install": "pnpm exec playwright install --with-deps",
47+
"typecheck": "tsc --noEmit"
4748
},
4849
"devDependencies": {
4950
"@changesets/cli": "^2.28.1",

src/env.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/helpers.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export function createContainerSelector(
149149
/**
150150
* Check if an element is a root element (<html> or <body>)
151151
*/
152-
export function isRootElement(element: Element): boolean {
152+
export function isRootElement(element: unknown): boolean {
153153
return (
154154
element instanceof HTMLHtmlElement || element instanceof HTMLBodyElement
155155
);
@@ -206,8 +206,9 @@ export function commitScrollState(state: ScrollState) {
206206
* Resolve a target
207207
*/
208208
export function resolveTarget(target: Target | null): Element | null {
209-
/** The window */
210-
if (target === window) {
209+
if (!target) return null;
210+
211+
if (target === window || isRootElement(target)) {
211212
return document.scrollingElement ?? document.documentElement;
212213
}
213214

tests/unit/tests/resolveTarget.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ describe("resolveTarget", () => {
1717
expect(resolveTarget(window)).toEqual(document.documentElement);
1818
});
1919

20+
it("resolves the <body> to the document root", () => {
21+
expect(resolveTarget(document.body)).toEqual(document.documentElement);
22+
});
23+
2024
it("Returns either an element or nothing", () => {
2125
const existing = document.querySelector(".foo");
2226
const missing = document.querySelector(".waldo");

0 commit comments

Comments
 (0)