Skip to content

Commit

Permalink
feat: derive transition duration without explicit prop
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Oct 22, 2023
1 parent 1512838 commit c43e421
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/app/src/components/stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useTinyStoreStorage } from "@hiogawa/tiny-store/dist/react";
import { TOAST_POSITIONS, type ToastPosition } from "@hiogawa/tiny-toast";
import {
Transition,
TransitionV2,
useLaggedBoolean,
} from "@hiogawa/tiny-transition/dist/react";
import { ANTD_VARS } from "@hiogawa/unocss-preset-antd";
Expand Down Expand Up @@ -510,7 +511,7 @@ export function StorySlide() {
>
<span className="border px-2 py-1">hello from top/right</span>
</Transition>
<Transition
<TransitionV2
appear
show={show}
className="absolute bottom-2 left-2 inline-block duration-500 transform"
Expand All @@ -520,7 +521,7 @@ export function StorySlide() {
leaveTo="translate-x-[-200%]"
>
<span className="border px-2 py-1">hello from bottom/left</span>
</Transition>
</TransitionV2>
</div>
</section>
</div>
Expand Down
3 changes: 2 additions & 1 deletion packages/tiny-transition/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ function onTransitionEnd(el: HTMLElement, callback: () => void) {
};
}

function computeTransitionTimeout(el: HTMLElement): number {
export function computeTransitionTimeout(el: HTMLElement): number {
// this probably also `forceStyle`
const style = getComputedStyle(el);
const [duration, delay] = [
style.transitionDuration,
Expand Down
15 changes: 11 additions & 4 deletions packages/tiny-transition/src/lagged/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
TRANSITION_CALLBACK_TYPES,
type TransitionCallbackProps,
type TransitionCallbackType,
computeTransitionTimeout,
} from "../core";
import { simpleForawrdRef } from "../react";
import {
Expand Down Expand Up @@ -60,7 +61,6 @@ export const TransitionV2 = simpleForawrdRef(function TransitionV2(
show: boolean;
appear?: boolean;
render?: (props: Record<string, any>) => React.ReactNode;
duration: number;
} & TransitionClassProps &
TransitionCallbackProps & {
// choose only common props from `JSX.IntrinsicElements["div"]` to simplify auto-complete
Expand All @@ -87,17 +87,24 @@ export const TransitionV2 = simpleForawrdRef(function TransitionV2(
);

// lagged state
const durationRef = useRef(0);
const state = useLaggedBoolean(props.show, {
duration: props.duration,
get duration() {
return durationRef.current;
},
appear: props.appear,
...stableCallbackPropsWithRef,
});

const mergedRefs = useMergeRefs(ref, elRef, (el: HTMLElement | null) => {
// hacky way to deal with `onEnterFrom`
// since "enterFrom" event comes before we render dom...
if (el) {
// hacky way to deal with `onEnterFrom`
// since "enterFrom" event comes before we render dom...
callbackProps.onEnterFrom?.(el);

// derive timeout duration
// (note that this "force styles" so it must be called after "onEnterFrom")
durationRef.current = computeTransitionTimeout(el);
}
});
const render = props.render ?? defaultRender;
Expand Down

0 comments on commit c43e421

Please sign in to comment.