tutu/src/platform/anim.ts

56 lines
1.2 KiB
TypeScript
Raw Normal View History

import {
createContext,
createRenderEffect,
createSignal,
untrack,
useContext,
type Accessor,
type Signal,
} from "solid-js";
2024-07-14 20:28:44 +08:00
2024-08-05 15:33:00 +08:00
export type HeroSource = {
2024-10-09 17:33:27 +08:00
[key: string | symbol | number]: HTMLElement | undefined;
2024-08-05 15:33:00 +08:00
};
2024-07-14 20:28:44 +08:00
const HeroSourceContext = createContext<Signal<HeroSource>>(
/* __@PURE__ */ undefined,
);
2024-07-14 20:28:44 +08:00
2024-08-05 15:33:00 +08:00
export const HeroSourceProvider = HeroSourceContext.Provider;
2024-07-14 20:28:44 +08:00
2024-10-29 15:22:25 +08:00
export function useHeroSource() {
2024-08-05 15:33:00 +08:00
return useContext(HeroSourceContext);
2024-07-14 20:28:44 +08:00
}
2024-08-12 21:55:26 +08:00
/**
* Use hero value for the {@link key}.
2024-10-29 15:22:25 +08:00
*
* Note: the setter here won't set the value of the hero source.
* To set hero source, use {@link useHeroSource} and set the corresponding key.
2024-08-12 21:55:26 +08:00
*/
export function useHeroSignal(
key: string | symbol | number,
2024-10-09 17:33:27 +08:00
): Signal<HTMLElement | undefined> {
const source = useHeroSource();
if (source) {
2024-10-09 17:33:27 +08:00
const [get, set] = createSignal<HTMLElement>();
createRenderEffect(() => {
const value = source[0]();
if (value[key]) {
set(value[key]);
source[1]((x) => {
const cpy = Object.assign({}, x);
delete cpy[key];
return cpy;
});
}
});
return [get, set];
} else {
2024-10-29 15:22:25 +08:00
console.debug("no hero source")
return [() => undefined, () => undefined];
}
}