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
24 changes: 18 additions & 6 deletions src/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ interface PopupState {
status: PopupStatus;
prevVisible: boolean;
alignClassName: string;

/** Record for CSSMotion is working or not */
inMotion: boolean;
}

interface AlignRefType {
Expand All @@ -96,6 +99,8 @@ class Popup extends Component<PopupProps, PopupState> {
status: null,
prevVisible: null, // eslint-disable-line react/no-unused-state
alignClassName: null,

inMotion: false,
};

public popupRef = React.createRef<HTMLDivElement>();
Expand All @@ -108,7 +113,7 @@ class Popup extends Component<PopupProps, PopupState> {

static getDerivedStateFromProps(
{ visible, ...props }: PopupProps,
{ prevVisible, status }: PopupState,
{ prevVisible, status, inMotion }: PopupState,
) {
const newState: Partial<PopupState> = { prevVisible: visible, status };

Expand All @@ -117,12 +122,11 @@ class Popup extends Component<PopupProps, PopupState> {
if (prevVisible === null && visible === false) {
// Init render should always be stable
newState.status = 'stable';
newState.inMotion = false;
} else if (visible !== prevVisible) {
if (
visible ||
(supportMotion(mergedMotion) &&
['motion', 'AfterMotion', 'stable'].includes(status))
) {
newState.inMotion = false;

if (visible || (supportMotion(mergedMotion) && inMotion)) {
newState.status = null;
} else {
newState.status = 'stable';
Expand Down Expand Up @@ -322,6 +326,14 @@ class Popup extends Component<PopupProps, PopupState> {
mergedMotionVisible = false;
}

// Update trigger to tell if is in motion
['onEnterStart', 'onAppearStart', 'onLeaveStart'].forEach(event => {
mergedMotion[event] = (...args) => {
mergedMotion?.[event]?.(...args);
this.setState({ inMotion: true });
};
});

// ================== Align ==================
const mergedAlignDisabled =
!visible ||
Expand Down
18 changes: 13 additions & 5 deletions tests/popup.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ describe('Popup', () => {
const props = { visible: false };
const state = { prevVisible: null, status: 'something' };

expect(Popup.getDerivedStateFromProps(props, state).status).toBe('stable');
expect(Popup.getDerivedStateFromProps(props, state).status).toBe(
'stable',
);
});

it('does not change when visible is unchanged', () => {
const props = { visible: true };
const state = { prevVisible: true, status: 'something' };

expect(Popup.getDerivedStateFromProps(props, state).status).toBe('something');
expect(Popup.getDerivedStateFromProps(props, state).status).toBe(
'something',
);
});

it('returns null when visible is changed to true', () => {
Expand All @@ -41,7 +45,9 @@ describe('Popup', () => {
const props = { visible: false };
const state = { prevVisible: true, status: 'something' };

expect(Popup.getDerivedStateFromProps(props, state).status).toBe('stable');
expect(Popup.getDerivedStateFromProps(props, state).status).toBe(
'stable',
);
});

it('returns null when visible is changed to false and motion is started', () => {
Expand All @@ -51,7 +57,7 @@ describe('Popup', () => {
motionName: 'enter',
},
};
const state = { prevVisible: true, status: 'motion' };
const state = { prevVisible: true, status: 'motion', inMotion: true };

expect(Popup.getDerivedStateFromProps(props, state).status).toBe(null);
});
Expand All @@ -65,7 +71,9 @@ describe('Popup', () => {
};
const state = { prevVisible: true, status: 'beforeMotion' };

expect(Popup.getDerivedStateFromProps(props, state).status).toBe('stable');
expect(Popup.getDerivedStateFromProps(props, state).status).toBe(
'stable',
);
});
});

Expand Down