Skip to content

Commit

Permalink
[Tooltip] useAfterExitAnimation (#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks authored Dec 6, 2024
1 parent de3622e commit 72baa4d
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/react/src/dialog/root/useDialogRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { type InteractionType } from '../../utils/useEnhancedClickHandler';
import type { RequiredExcept, GenericHTMLProps } from '../../utils/types';
import { useOpenInteractionType } from '../../utils/useOpenInteractionType';
import { mergeReactProps } from '../../utils/mergeReactProps';
import { useAfterExitAnimation } from '../../utils/useAfterCloseAnimation';
import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation';

export function useDialogRoot(parameters: useDialogRoot.Parameters): useDialogRoot.ReturnValue {
const {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/menu/root/useMenuRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useTransitionStatus } from '../../utils/useTransitionStatus';
import { useEventCallback } from '../../utils/useEventCallback';
import { useControlled } from '../../utils/useControlled';
import { TYPEAHEAD_RESET_MS } from '../../utils/constants';
import { useAfterExitAnimation } from '../../utils/useAfterCloseAnimation';
import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation';

const EMPTY_ARRAY: never[] = [];

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/popover/root/usePopoverRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
translateOpenChangeReason,
type OpenChangeReason,
} from '../../utils/translateOpenChangeReason';
import { useAfterExitAnimation } from '../../utils/useAfterCloseAnimation';
import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation';

export function usePopoverRoot(params: usePopoverRoot.Parameters): usePopoverRoot.ReturnValue {
const {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/preview-card/root/usePreviewCardRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
translateOpenChangeReason,
type OpenChangeReason,
} from '../../utils/translateOpenChangeReason';
import { useAfterExitAnimation } from '../../utils/useAfterCloseAnimation';
import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation';

export function usePreviewCardRoot(
params: usePreviewCardRoot.Parameters,
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/select/root/useSelectRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useEventCallback } from '../../utils/useEventCallback';
import { warn } from '../../utils/warn';
import type { SelectRootContext } from './SelectRootContext';
import type { SelectIndexContext } from './SelectIndexContext';
import { useAfterExitAnimation } from '../../utils/useAfterCloseAnimation';
import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation';

export function useSelectRoot<T>(params: useSelectRoot.Parameters<T>): useSelectRoot.ReturnValue {
const {
Expand Down
34 changes: 10 additions & 24 deletions packages/react/src/tooltip/root/useTooltipRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,21 @@ import {
} from '@floating-ui/react';
import { useControlled } from '../../utils/useControlled';
import { useTransitionStatus } from '../../utils/useTransitionStatus';
import { useAnimationsFinished } from '../../utils/useAnimationsFinished';
import { useEventCallback } from '../../utils/useEventCallback';
import { OPEN_DELAY } from '../utils/constants';
import type { TransitionStatus } from '../../utils/useTransitionStatus';
import type { GenericHTMLProps } from '../../utils/types';
import { useLatestRef } from '../../utils/useLatestRef';
import {
translateOpenChangeReason,
type OpenChangeReason,
} from '../../utils/translateOpenChangeReason';
import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation';

export function useTooltipRoot(params: useTooltipRoot.Parameters): useTooltipRoot.ReturnValue {
const {
open: externalOpen,
onOpenChange: onOpenChangeProp = () => {},
defaultOpen = false,
keepMounted = false,
hoverable = true,
animated = true,
trackCursorAxis = 'none',
Expand Down Expand Up @@ -63,11 +61,16 @@ export function useTooltipRoot(params: useTooltipRoot.Parameters): useTooltipRoo
[onOpenChange, setOpenUnwrapped],
);

const { mounted, setMounted, transitionStatus } = useTransitionStatus(open, animated);
const { mounted, setMounted, transitionStatus } = useTransitionStatus(open);

const runOnceAnimationsFinish = useAnimationsFinished(popupRef);

const openRef = useLatestRef(open);
useAfterExitAnimation({
open,
animated,
animatedElementRef: popupRef,
onFinished() {
setMounted(false);
},
});

const context = useFloatingRootContext({
elements: { reference: triggerElement, floating: positionerElement },
Expand Down Expand Up @@ -95,18 +98,6 @@ export function useTooltipRoot(params: useTooltipRoot.Parameters): useTooltipRoo
} else if (reasonValue === 'hover') {
setInstantTypeState(undefined);
}

if (!keepMounted && !openValue) {
if (animated) {
runOnceAnimationsFinish(() => {
if (!openRef.current) {
setMounted(false);
}
});
} else {
setMounted(false);
}
}
},
});

Expand Down Expand Up @@ -220,11 +211,6 @@ export namespace useTooltipRoot {
* @default 0
*/
closeDelay?: number;
/**
* Whether the tooltip popup element stays mounted in the DOM when closed.
* @default false
*/
keepMounted?: boolean;
}

export interface ReturnValue {
Expand Down

0 comments on commit 72baa4d

Please sign in to comment.