Skip to content

Instantly share code, notes, and snippets.

@hansoksendahl
Last active June 28, 2019 21:56
Show Gist options
  • Save hansoksendahl/2fe6b96220048bb8360a4bfef48bc95f to your computer and use it in GitHub Desktop.
Save hansoksendahl/2fe6b96220048bb8360a4bfef48bc95f to your computer and use it in GitHub Desktop.
Hooks
Copyright 2019 Hans Oksendahl
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import {useEffect, useState} from 'react';
export default function useRect() {
const [length, setLength] = useState(0);
const [node, setRef] = useState(null);
useEffect(
() => {
if (node !== null) {
const segments = node.attributes.d.value.split(/(?=[mM])/);
const path = document.createElementNS('http://www.w3.org/2000/svg','path');
let max = 0;
for (let d of segments) {
path.setAttributeNS(null, 'd', d);
max = Math.max(max, path.getTotalLength());
}
setLength(max);
}
},
[node]
);
return { length, ref: setRef };
}
import {useEffect, useState} from 'react';
function getRect(node) {
const rect = node.getBoundingClientRect();
const offsetX = window.pageXOffset || window.scrollX;
const offsetY = window.pageYOffset || window.scrollY;
return {
top: rect.top + offsetY,
right: rect.left + offsetX,
bottom: rect.bottom + offsetY,
left: rect.left + offsetX,
width: rect.width,
height: rect.height,
}
}
export default function useRect() {
const initialState = {
top: 0,
right: 0,
bottom: 0,
left: 0,
width: 0,
height: 0,
};
const [rect, setRect] = useState(initialState);
const [node, setRef] = useState(null);
useEffect(
() => {
if (node !== null) {
let isActive = false;
setRect(getRect(node));
function resizeHandler() {
if (!isActive) {
window.requestAnimationFrame(() => {
setRect(getRect(node));
isActive = false;
});
isActive = true;
}
}
window.addEventListener('resize', resizeHandler);
return () => {
window.removeEventListener('resize', resizeHandler);
}
}
},
[node]
);
return { rect, ref: setRef };
}
import {useEffect, useState} from 'react';
export default function useScroll() {
const [pos, setPos] = useState({ x: 0, y: 0 });
useEffect(() => {
let isActive = false;
function scrollHandler() {
if (!isActive) {
window.requestAnimationFrame(() => {
setPos({
x: (window.pageXOffset || window.scrollX),
y: (window.pageYOffset || window.scrollY)
});
isActive = false;
});
isActive = true;
}
}
window.addEventListener('scroll', scrollHandler);
return () => {
window.removeEventListener('scroll', scrollHandler);
}
});
return pos;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment