Skip to content

Commit

Permalink
chore: rm resize-observer-polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Jan 23, 2025
1 parent 6774b1e commit 1768bc9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rc-resize-observer",
"version": "1.4.3",
"name": "@rc-component/resize-observer",
"version": "1.0.0-0",
"description": "Resize observer for React",
"keywords": [
"react",
Expand Down Expand Up @@ -32,7 +32,7 @@
"deploy": "UMI_ENV=gh npm run build && gh-pages -d dist",
"gh-pages": "npm run deploy",
"prepare": "dumi setup",
"prepublishOnly": "npm run compile && np --no-cleanup --yolo --no-publish",
"prepublishOnly": "npm run compile && rc-np",
"postpublish": "npm run gh-pages",
"lint": "eslint src/ --ext .ts,.tsx,.jsx,.js,.md",
"prettier": "prettier --write \"**/*.{ts,tsx,js,jsx,json,md}\"",
Expand All @@ -41,13 +41,11 @@
"now-build": "npm run build"
},
"dependencies": {
"@babel/runtime": "^7.20.7",
"classnames": "^2.2.1",
"@rc-component/util": "^1.2.0",
"resize-observer-polyfill": "^1.5.1"
"@rc-component/util": "^1.2.0"
},
"devDependencies": {
"@rc-component/father-plugin": "^1.0.0",
"@rc-component/father-plugin": "^2.0.2",
"@testing-library/react": "^12.1.5",
"@types/jest": "^29.5.10",
"@types/react-dom": "^18.0.11",
Expand All @@ -63,7 +61,7 @@
"gh-pages": "^6.1.0",
"glob": "^7.1.6",
"less": "^4.1.3",
"np": "^9.0.0",
"@rc-component/np": "^1.0.0",
"prettier": "^3.2.5",
"pretty-quick": "^4.0.0",
"rc-test": "^7.0.15",
Expand Down
17 changes: 11 additions & 6 deletions src/utils/observerUtil.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import ResizeObserver from 'resize-observer-polyfill';

export type ResizeListener = (element: Element) => void;

// =============================== Const ===============================
Expand All @@ -12,8 +10,15 @@ function onResize(entities: ResizeObserverEntry[]) {
});
}

// Note: ResizeObserver polyfill not support option to measure border-box resize
const resizeObserver = new ResizeObserver(onResize);
// Delay create ResizeObserver since it's not supported in server side
let observer: ResizeObserver;

function ensureResizeObserver() {
if (!observer) {
observer = new ResizeObserver(onResize);
}
return observer;
}

// Dev env only
export const _el = process.env.NODE_ENV !== 'production' ? elementListeners : null; // eslint-disable-line
Expand All @@ -23,7 +28,7 @@ export const _rs = process.env.NODE_ENV !== 'production' ? onResize : null; // e
export function observe(element: Element, callback: ResizeListener) {
if (!elementListeners.has(element)) {
elementListeners.set(element, new Set());
resizeObserver.observe(element);
ensureResizeObserver().observe(element);
}

elementListeners.get(element).add(callback);
Expand All @@ -33,7 +38,7 @@ export function unobserve(element: Element, callback: ResizeListener) {
if (elementListeners.has(element)) {
elementListeners.get(element).delete(callback);
if (!elementListeners.get(element).size) {
resizeObserver.unobserve(element);
ensureResizeObserver().unobserve(element);
elementListeners.delete(element);
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ Object.assign(Enzyme.ReactWrapper.prototype, {
onResize([{ target }]);
},
});

// Mock for ResizeObserver
global.ResizeObserver = class ResizeObserver {
constructor(callback) {
this.callback = callback;
}

observe(target) {
this.callback([{ target }]);
}

unobserve() {}
};

0 comments on commit 1768bc9

Please sign in to comment.