Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make ref stable #62

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.useState<CSSMotionRef>(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥不用 useMemo 大佬?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以的,改着改着就不是原来的样子了。我改成 useMemo

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];
}