ISSUE-022 - Well. That just works. Awesome.
This commit is contained in:
1 parent
fb48517bac
commit
399b904f07
6 files changed
+20
-86
No files matched your search
+3
-3
@@ -1,6 +1,6 @@
|
||||
import { Outlet } from "react-router-dom";
|
||||
import useMeasure from "react-use-measure";
|
||||
|
||||
import { useDimensions } from "./utils/hooks";
|
||||
import {
|
||||
setDimensions,
|
||||
} from "./features/pageDimensions/pageDimensionsSlice";
|
||||
@@ -14,11 +14,11 @@ function App() {
|
||||
const [appRef, {
|
||||
height: appHeight,
|
||||
width: appWidth,
|
||||
}] = useDimensions<HTMLDivElement>();
|
||||
}] = useMeasure();
|
||||
const [mainMenuRef, {
|
||||
height: mainMenuHeight,
|
||||
width: mainMenuWidth,
|
||||
}] = useDimensions<HTMLDivElement>();
|
||||
}] = useMeasure();
|
||||
|
||||
// Set App dimensions.
|
||||
useEffect(() => {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { useSelector, useDispatch } from "react-redux";
|
||||
import { useTransition, animated } from "@react-spring/web";
|
||||
import { useDrag } from "@use-gesture/react";
|
||||
import useMeasure from "react-use-measure";
|
||||
|
||||
import { useDimensions } from "../../../utils/hooks";
|
||||
import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSelector";
|
||||
import { setPosition } from "../../../features/overlayTool/overlayToolSlice";
|
||||
import { OverlayImage, OverlayImageContainer, OverlayImageSubContainer } from "./styles";
|
||||
@@ -31,8 +31,7 @@ export function OverlayDisplay({
|
||||
const [overlayItemImageRef, {
|
||||
width: overlayItemWidth,
|
||||
height: overlayItemHeight,
|
||||
}] = useDimensions<HTMLImageElement>();
|
||||
console.log(overlayItemWidth, overlayItemHeight);
|
||||
}] = useMeasure();
|
||||
const
|
||||
minX = -bleedover,
|
||||
minY = -bleedover,
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { useEffect } from "react";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
import { Outlet } from "react-router-dom";
|
||||
import useMeasure from "react-use-measure";
|
||||
|
||||
import { mainMenuDimensionsSelector } from "../../features/pageDimensions/pageDimensionsSelector";
|
||||
import { useDimensions } from "../../utils/hooks";
|
||||
import { setDimensions } from "../../features/pageDimensions/pageDimensionsSlice";
|
||||
|
||||
import { FullScreenContainer } from "../Common/FullScreenContainer.styled";
|
||||
@@ -18,7 +18,7 @@ export default function Workspaces() {
|
||||
const [workspaceRef, {
|
||||
height: workspaceHeight,
|
||||
width: workspaceWidth,
|
||||
}] = useDimensions<HTMLDivElement>();
|
||||
}] = useMeasure();
|
||||
|
||||
// Set Workspace dimensions.
|
||||
useEffect(() => {
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
import useResizeObserver from "@react-hook/resize-observer";
|
||||
import React, { useEffect, useLayoutEffect, useRef, useState } from "react";
|
||||
import ResizeObserver from "resize-observer-polyfill";
|
||||
|
||||
export const useDimensions = <T extends HTMLElement>() => {
|
||||
const ref = useRef<T>(null);
|
||||
const [width, setWidth] = useState(0);
|
||||
const [height, setHeight] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
const element = ref.current;
|
||||
if (element == null) return;
|
||||
const resizeObserver = new ResizeObserver(entries => {
|
||||
if (!Array.isArray(entries)) return;
|
||||
if (!entries.length) return;
|
||||
|
||||
const entry = entries[0];
|
||||
const boundingRect = entry.target.getBoundingClientRect();
|
||||
if (width !== boundingRect.width) setWidth(boundingRect.width);
|
||||
if (height !== boundingRect.height) setHeight(boundingRect.height);
|
||||
});
|
||||
resizeObserver.observe(element);
|
||||
|
||||
return () => resizeObserver.unobserve(element);
|
||||
}, [height, width]);
|
||||
|
||||
// Returning correctly with generics apparently requires type def here.
|
||||
return [
|
||||
ref,
|
||||
{ width, height },
|
||||
] as [
|
||||
React.RefObject<T>,
|
||||
{ height: number, width: number },
|
||||
];
|
||||
};
|
||||
|
||||
/**
|
||||
* Proven to be less reliable than using resizeObserver directly.
|
||||
* @returns
|
||||
*/
|
||||
export const useDimensionsAlt1 = <T extends HTMLElement>() => {
|
||||
const ref = useRef<T>(null);
|
||||
const [size, setSize] = useState<DOMRect>();
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const element = ref.current;
|
||||
if (element == null) return;
|
||||
setSize(element.getBoundingClientRect());
|
||||
}, [ref]);
|
||||
|
||||
useResizeObserver(ref, (entry) => setSize(entry.contentRect));
|
||||
|
||||
// Returning correctly with generics apparently requires type def here.
|
||||
return [
|
||||
ref,
|
||||
size ?? { width: 0, height: 0 },
|
||||
] as [
|
||||
React.RefObject<T>,
|
||||
DOMRect,
|
||||
];
|
||||
};
|
||||
|
||||
/**
|
||||
* Based off the code demo for useResizeObserver.
|
||||
* https://github.com/jaredLunde/react-hook/tree/master/packages/resize-observer
|
||||
* @param target React Ref
|
||||
* @returns
|
||||
*/
|
||||
export const useDimensionsAlt2 = <T extends HTMLElement>(target: React.RefObject<T>) => {
|
||||
const [size, setSize] = useState<DOMRect>();
|
||||
|
||||
useLayoutEffect(() => {
|
||||
setSize(target.current?.getBoundingClientRect());
|
||||
}, [target]);
|
||||
|
||||
useResizeObserver(target, entry => setSize(entry.contentRect));
|
||||
return size ?? { width: 0, height: 0, x: 0, y: 0 };
|
||||
};
|
||||
Reference in new issue
Block a user