Skip to content

Commit 36a8432

Browse files
committed
fix(resize): uses ResizeObserver if possible
As discussed in sveltejs#4233, ResizeObserver is now widely available (~90% as of 15/01/2021, [source](http://caniuse.com/?search=ResizeObserver)) so we can use it instead of a custom-built solution to listen to resizes. I also needed to add @types/resize-observer-browser because the type definition for `ResizeObserver` hasn't landed in TS yet (see microsoft/TypeScript#28502). Closes sveltejs#4233.
1 parent 8180c5d commit 36a8432

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
"@sveltejs/eslint-config": "github:sveltejs/eslint-config#v5.6.0",
106106
"@types/mocha": "^7.0.0",
107107
"@types/node": "^8.10.53",
108+
"@types/resize-observer-browser": "^0.1.5",
108109
"@typescript-eslint/eslint-plugin": "^4.9.0",
109110
"@typescript-eslint/parser": "^4.9.0",
110111
"acorn": "^7.4.0",

src/compiler/tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"include": ["."],
44

55
"compilerOptions": {
6-
"lib": ["es2017", "webworker"]
6+
"lib": ["es2017", "webworker"],
77

88
// TODO: remove mocha types from the whole project
9-
// "types": ["node", "estree"]
9+
"types": ["node", "estree", "mocha"]
1010
}
1111
}

src/runtime/internal/dom.ts

+13
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,19 @@ export function is_crossorigin() {
260260
}
261261

262262
export function add_resize_listener(node: HTMLElement, fn: () => void) {
263+
if (window.ResizeObserver) {
264+
const observer = new ResizeObserver(entries => {
265+
for (const entry of entries) {
266+
if (entry.target === node) {
267+
fn();
268+
break;
269+
}
270+
}
271+
});
272+
observer.observe(node);
273+
return observer.disconnect;
274+
}
275+
263276
const computed_style = getComputedStyle(node);
264277

265278
if (computed_style.position === 'static') {

src/runtime/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"compilerOptions": {
66
"lib": ["es2015", "dom", "dom.iterable"],
77
"target": "es2015",
8-
"types": [],
8+
"types": ["resize-observer-browser"],
99

1010
"baseUrl": ".",
1111
"paths": {

0 commit comments

Comments
 (0)