Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/CSSMotion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,13 @@ export function genCSSMotion(config: CSSMotionConfig) {
return getDOM(nodeRef.current) as HTMLElement;
}

const [status, statusStep, statusStyle, mergedVisible] = useStatus(
const [getStatus, statusStep, statusStyle, mergedVisible] = useStatus(
supportMotion,
visible,
getDomElement,
props,
);
const status = getStatus();

// Record whether content has rendered
// Will return null for un-rendered even when `removeOnLeave={false}`
Expand All @@ -162,10 +163,26 @@ export function genCSSMotion(config: CSSMotionConfig) {
}

// ====================== Refs ======================
React.useImperativeHandle(ref, () => ({
nativeElement: getDomElement(),
inMotion: () => status !== STATUS_NONE,
}));
const refObj = React.useMemo<CSSMotionRef>(() => {
const obj = {} as CSSMotionRef;
Object.defineProperties(obj, {
nativeElement: {
enumerable: true,
get: getDomElement,
},
inMotion: {
enumerable: true,
get: () => {
return () => getStatus() !== STATUS_NONE;
},
},
});
return obj;
}, []);

// We lock `deps` here since function return object
// will repeat trigger ref from `refConfig` -> `null` -> `refConfig`
React.useImperativeHandle(ref, () => refObj, []);

// ===================== Render =====================
let motionChildren: React.ReactNode;
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function useStatus(
onLeaveEnd,
onVisibleChanged,
}: CSSMotionProps,
): [MotionStatus, StepStatus, React.CSSProperties, boolean] {
): [() => MotionStatus, StepStatus, React.CSSProperties, boolean] {
// Used for outer render usage to avoid `visible: false & status: none` to render nothing
const [asyncVisible, setAsyncVisible] = useState<boolean>();
const [getStatus, setStatus] = useSyncState<MotionStatus>(STATUS_NONE);
Expand Down Expand Up @@ -294,5 +294,5 @@ export default function useStatus(
};
}

return [currentStatus, step, mergedStyle, asyncVisible ?? visible];
return [getStatus, step, mergedStyle, asyncVisible ?? visible];
}