refactor: ui

This commit is contained in:
cha0s 2022-03-24 17:28:50 -05:00
parent 2c6ab4ccc3
commit 6bcdd322a1
7 changed files with 50 additions and 63 deletions

View File

@ -12,7 +12,7 @@ import {
import {UserRequired} from '@flecks/user/local'; import {UserRequired} from '@flecks/user/local';
import '@humus/app/scss/index.scss'; import '@humus/app/scss/index.scss';
import Ui from '../valent-ui'; import Ui from './ui';
import Login from './login'; import Login from './login';
import Overview from './overview'; import Overview from './overview';
import Play from './play'; import Play from './play';

View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -0,0 +1,47 @@
import {
PropTypes,
React,
useContext,
useEffect,
useRef,
} from '@flecks/react';
import Context from './context';
import styles from './index.module.scss';
export {Context};
const Ui = ({children}) => {
const [width, height] = useContext(Context);
const $ui = useRef();
useEffect(() => {
if (!$ui.current) {
return undefined;
}
const onResize = () => {
const {innerWidth, innerHeight} = window;
const ratio = innerWidth / innerHeight;
const targetRatio = width / height;
$ui.current.style.setProperty(
'--scale',
ratio <= targetRatio ? innerWidth / width : innerHeight / height,
);
};
window.addEventListener('resize', onResize);
onResize();
return () => {
window.removeEventListener('resize', onResize);
};
}, [$ui, height, width]);
return (
<div className={styles.ui} ref={$ui} style={{height: `${height}px`, width: `${width}px`}}>
{children}
</div>
);
};
Ui.propTypes = {
children: PropTypes.node.isRequired,
};
export default Ui;

View File

@ -1,4 +1,4 @@
.valent-ui { .ui {
display: inline-block; display: inline-block;
left: 50%; left: 50%;
overflow: hidden; overflow: hidden;

View File

@ -1,60 +0,0 @@
import './index.scss';
import {
PropTypes,
React,
useContext,
useEffect,
useRef,
} from '@flecks/react';
import Context from './context';
export {Context};
const ValentUi = ({children}) => {
const [width, height] = useContext(Context);
const $ui = useRef();
useEffect(() => {
if (!$ui.current) {
return undefined;
}
const onResize = () => {
const RATIO = width / height;
const {innerWidth, innerHeight} = window;
const ratio = innerWidth / innerHeight;
let scale;
if (ratio < RATIO) {
scale = innerWidth / width;
}
else if (ratio > RATIO) {
scale = innerHeight / height;
}
else {
scale = innerWidth / width;
}
$ui.current.style.setProperty('--scale', scale);
};
window.addEventListener('resize', onResize);
onResize();
return () => window.removeEventListener('resize', onResize);
}, [$ui, height, width]);
return (
<div
className="valent-ui"
ref={$ui}
style={{
height: `${height}px`,
width: `${width}px`,
}}
>
{children}
</div>
);
};
ValentUi.propTypes = {
children: PropTypes.node.isRequired,
};
export default ValentUi;

View File

@ -2,7 +2,7 @@ import {Hooks} from '@flecks/core';
import App from './components/app'; import App from './components/app';
import {selfEntity} from './state'; import {selfEntity} from './state';
import Ui, {Context} from './components/valent-ui'; import Ui, {Context} from './components/app/ui';
export * from './hooks'; export * from './hooks';
export * from './state'; export * from './state';