import {computed, iTransitionEvent, reactive, StyleProperties, useStyleCache, watch} from "@peryl/react-compose"; import {delay} from "@peryl/utils/delay"; export function useCollapseStyles( { isShow, active, inactive }: { isShow: () => boolean, active: () => StyleProperties, inactive: () => StyleProperties, } ) { const state = (() => { const show = isShow(); return reactive({ isMount: show, styles: show ? active() : inactive(), onTransitionEnd: undefined as undefined | ((e: iTransitionEvent) => void) }); })(); watch(() => isShow(), async show => { if (show) { if (!state.isMount) { state.isMount = true; await delay(23); } state.styles = active(); state.onTransitionEnd = undefined; } else { if (!state.isMount) { return; } await delay(23); state.styles = inactive(); state.onTransitionEnd = () => { state.isMount = false; state.onTransitionEnd = undefined; }; } }); const styles = useStyleCache(styles => { Object.assign(styles, state.styles); }); return reactive({ styles, show: computed(() => state.isMount), onTransitionEnd: (e: iTransitionEvent) => state.onTransitionEnd?.(e) }); }