Skip to content

Commit

Permalink
use use-resize-observer package
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuripetusko committed Apr 13, 2024
1 parent fe74944 commit de4a753
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
3 changes: 2 additions & 1 deletion packages/rmrk-2d-renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"isomorphic-dompurify": "^2.2.0",
"lucide-react": "^0.303.0",
"pixi.js": "^7.2.4",
"use-image": "^1.1.1"
"use-image": "^1.1.1",
"use-resize-observer": "^9.1.0"
},
"devDependencies": {
"@changesets/changelog-github": "^0.5.0",
Expand Down
31 changes: 5 additions & 26 deletions packages/rmrk-2d-renderer/src/lib/rmrk-2d-renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,7 @@ import { css, cx } from 'styled-system/css';
import useImage from 'use-image';
import { Skeleton } from '../ui/skeleton.js';
import { INHERIT_RENDER_CONTEXT } from './consts.js';

const useObserveElementDimensions = (ref?: React.RefObject<HTMLDivElement>) => {
const [width, setWidth] = useState(0);
const [height, setHeight] = useState(0);
useEffect(() => {
const resizeObserver = new ResizeObserver((event) => {
// Depending on the layout, you may need to swap inlineSize with blockSize
// https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry/contentBoxSize
setWidth(event[0]?.contentBoxSize[0]?.inlineSize || 0);
setHeight(event[0]?.contentBoxSize[0]?.blockSize || 0);
});

if (ref?.current) {
resizeObserver.observe(ref.current);
}
}, [ref?.current]);

const isLoading = width === 0 || height === 0;

return { width, height, isLoading };
};
import useResizeObserver from 'use-resize-observer';

const useGetResourceDimensions = () => {
const [w, setW] = useState(0);
Expand All @@ -58,11 +38,10 @@ const useGetResourceDimensions = () => {
};

const useGetCanvasStateDimensions = (ref?: React.RefObject<HTMLDivElement>) => {
const {
width,
height,
isLoading: isLoadingParentDimensions,
} = useObserveElementDimensions(ref);
const { width = 0, height = 0 } = useResizeObserver<HTMLDivElement>({ ref });

const isLoadingParentDimensions = width === 0 || height === 0;

const {
onResourceLoad,
isLoading: isLoadingResourceDimensions,
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit de4a753

Please sign in to comment.