diff --git a/frontend/src/ts/hooks/useRefWithUtils.ts b/frontend/src/ts/hooks/useRefWithUtils.ts new file mode 100644 index 000000000..32fc52327 --- /dev/null +++ b/frontend/src/ts/hooks/useRefWithUtils.ts @@ -0,0 +1,17 @@ +import { Accessor } from "solid-js"; +import { ElementWithUtils } from "../utils/dom"; + +export function useRefWithUtils(): [ + ref: (el: T) => void, + element: Accessor | undefined>, +] { + let elementWithUtils: ElementWithUtils | undefined; + + const ref = (el: T): void => { + elementWithUtils = new ElementWithUtils(el); + }; + + const element = (): ElementWithUtils | undefined => elementWithUtils; + + return [ref, element]; +} diff --git a/frontend/src/ts/test/live-counter.tsx b/frontend/src/ts/test/live-counter.tsx index cce40f4c3..4a99f2264 100644 --- a/frontend/src/ts/test/live-counter.tsx +++ b/frontend/src/ts/test/live-counter.tsx @@ -1,7 +1,6 @@ import { Accessor, JSXElement } from "solid-js"; -import { ElementWithUtils } from "../utils/dom"; -import { isFocused } from "./focus"; import { useVisibilityAnimation } from "../hooks/useVisibilityAnimation"; +import { useRefWithUtils } from "../hooks/useRefWithUtils"; export function LiveCounter(props: { value: Accessor; @@ -11,16 +10,16 @@ export function LiveCounter(props: { }>; class?: string; }): JSXElement { - let ref: ElementWithUtils | undefined; + const [ref, element] = useRefWithUtils(); useVisibilityAnimation({ - element: () => ref, - visible: () => props.visible().value && isFocused(), + element, + visible: () => props.visible().value, animate: () => props.visible().withAnimation, }); return ( -
(ref = new ElementWithUtils(el))} class={props.class}> +
{props.value()}
); diff --git a/frontend/src/ts/test/live-states.tsx b/frontend/src/ts/test/live-states.tsx index c9b832568..a11b0e3e2 100644 --- a/frontend/src/ts/test/live-states.tsx +++ b/frontend/src/ts/test/live-states.tsx @@ -7,6 +7,7 @@ import { getLiveBurstStyle, getLiveSpeedStyle, } from "../signals/config"; +import { isFocused } from "./focus"; const [getWpm, setLiveStatWpm] = createSignal("0"); const [getAcc, setLiveStatAcc] = createSignal("100%"); @@ -17,6 +18,13 @@ const [statsVisible, setStatsVisible] = createSignal({ withAnimation: true, }); +const getStatsVisible = () => { + return { + value: statsVisible().value && isFocused(), + withAnimation: statsVisible().withAnimation, + }; +}; + export { setLiveStatWpm, setLiveStatAcc, setLiveStatBurst, setStatsVisible }; export function mountLiveCounters(): void { @@ -27,17 +35,17 @@ export function mountLiveCounters(): void { (getLiveSpeedStyle() === "text" ? getWpm() : "")} - visible={statsVisible} + visible={getStatsVisible} /> (getLiveAccStyle() === "text" ? getAcc() : "")} - visible={statsVisible} + visible={getStatsVisible} /> (getLiveBurstStyle() === "text" ? getBurst() : "")} - visible={statsVisible} + visible={getStatsVisible} /> ), @@ -51,17 +59,17 @@ export function mountLiveCounters(): void { (getLiveSpeedStyle() === "mini" ? getWpm() : "")} - visible={statsVisible} + visible={getStatsVisible} /> (getLiveAccStyle() === "mini" ? getAcc() : "")} - visible={statsVisible} + visible={getStatsVisible} /> (getLiveBurstStyle() === "mini" ? getBurst() : "")} - visible={statsVisible} + visible={getStatsVisible} /> ),