silphius/app/react-components/dom.jsx

38 lines
864 B
React
Raw Normal View History

2024-06-10 22:42:30 -05:00
import {useEffect, useRef, useState} from 'react';
import {RESOLUTION} from '@/constants.js';
import styles from './dom.module.css';
/**
* Dom autoscales with resolution.
*/
export default function Dom({children}) {
const ref = useRef();
const [scale, setScale] = useState(0);
useEffect(() => {
if (!ref.current) {
return;
}
function onResize() {
const {parentNode} = ref.current;
const {width} = parentNode.getBoundingClientRect();
2024-06-11 02:24:45 -05:00
setScale(width / RESOLUTION.x);
2024-06-10 22:42:30 -05:00
}
window.addEventListener('resize', onResize);
onResize();
return () => {
window.removeEventListener('resize', onResize);
}
}, [ref.current]);
return (
<div className={styles.dom} ref={ref}>
{scale > 0 && (
<style>{`.${styles.dom}{--scale:${scale}}`}</style>
)}
{children}
</div>
);
}