ISSUE-008 - Major refactor of the Menu Bar. Utilizing some really fun resize hooks and dropping the ugly route header idea.
This commit is contained in:
1 parent
73ba3b4ddd
commit
f4ccb9b3dd
13 files changed
+86
-83
No files matched your search
@@ -0,0 +1,34 @@
|
||||
import React, { useEffect, 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 },
|
||||
];
|
||||
};
|
||||
Reference in new issue
Block a user