feat: animation disable when diff > 200

This commit is contained in:
monoid 2023-07-25 10:55:42 +09:00
parent e0b6eab7cf
commit f089a04ef4

View File

@ -1,5 +1,5 @@
import { Button } from "../components/Button.tsx"; import { Button } from "../components/Button.tsx";
import { useEffect, useRef } from "preact/hooks"; import { useEffect, useRef, useLayoutEffect } from "preact/hooks";
import { ComponentChildren } from "preact"; import { ComponentChildren } from "preact";
import { Signal, useSignal } from "@preact/signals"; import { Signal, useSignal } from "@preact/signals";
import { IS_BROWSER } from "$fresh/runtime.ts"; import { IS_BROWSER } from "$fresh/runtime.ts";
@ -91,15 +91,39 @@ interface CorpSimple {
function StockListByDate({prevSet, rows, name}:{prevSet:Set<string>, function StockListByDate({prevSet, rows, name}:{prevSet:Set<string>,
rows:Coperation[], rows:Coperation[],
name: string}){ name: string}){
const lastCount = useRef(rows.length);
const curCount = rows.length;
const parent = useRef<HTMLDivElement>(null); const parent = useRef<HTMLDivElement>(null);
const controller = useRef<{
isEnabled: ()=> boolean,
disable: ()=>void,
enable: ()=> void
}| undefined>();
useEffect(()=>{ useEffect(()=>{
(async ()=>{ (async ()=>{
console.log("animation mount on ",name); console.log("animation mount on ",name);
const {default:autoAnimate} = await import("https://esm.sh/@formkit/auto-animate@0.7.0"); const {default:autoAnimate} = await import("https://esm.sh/@formkit/auto-animate@0.7.0");
parent.current && autoAnimate(parent.current) if (parent.current){
const cntr = autoAnimate(parent.current);
controller.current = cntr;
}
})(); })();
},[parent]); },[parent]);
useLayoutEffect(()=>{
if (controller.current){
if (Math.abs(curCount - lastCount.current) > 200){
console.log('disable animation', curCount, "from", lastCount.current);
controller.current.disable();
}
else {
console.log('enable animation', curCount, "from", lastCount.current);
controller.current.enable();
}
lastCount.current = curCount;
}
}, [parent, rows]);
return <div ref={parent}> return <div ref={parent}>
<h2 class="text-lg">{name}</h2> <h2 class="text-lg">{name}</h2>
{rows.map((row) => { {rows.map((row) => {
@ -109,7 +133,7 @@ name: string}){
key={row.Code} key={row.Code}
class={[ class={[
"bg-white", "bg-white",
firstOccur ? "text-[#ff5454]" : "text-black", firstOccur ? "text-[#ff5454] underline" : "text-black",
].join(" ")} ].join(" ")}
> >
<a href={`https://stockplus.com/m/stocks/KOREA-A${row.Code}`}> <a href={`https://stockplus.com/m/stocks/KOREA-A${row.Code}`}>