silphius/stories/dom-decorator.jsx

54 lines
1.2 KiB
React
Raw Normal View History

2024-06-10 22:42:30 -05:00
import {useEffect, useRef, useState} from 'react';
2024-07-20 04:32:33 -05:00
import Dom from '@/react/components/dom.jsx';
2024-06-10 22:42:30 -05:00
import {RESOLUTION} from '@/constants.js';
function Decorator({children, style}) {
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
ref={ref}
style={{
backgroundColor: '#1099bb',
2024-06-24 05:00:24 -05:00
flexDirection: 'column',
2024-06-10 22:42:30 -05:00
opacity: 0 === scale ? 0 : 1,
position: 'relative',
2024-06-11 02:24:45 -05:00
height: `calc(${RESOLUTION.y}px * ${scale})`,
2024-06-10 22:42:30 -05:00
width: '100%',
}}
>
<Dom>
<div style={style}>
{children}
</div>
</Dom>
</div>
);
}
export default function(options = {}) {
return function decorate(Decorating) {
return (
<Decorator style={options.style}>
<Decorating />
</Decorator>
);
};
}