Skip to content

Commit

Permalink
feat(popover): fix transition
Browse files Browse the repository at this point in the history
fix popover transition, change Transition to CSSTransition

BREAKING CHANGE: fix popover transition, change Transition to CSSTransition
  • Loading branch information
dmitrsavk committed Jan 22, 2021
1 parent 439148d commit f8564bc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 56 deletions.
2 changes: 1 addition & 1 deletion packages/popover/src/Component.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { name, version } from '../package.json';
const handleButtonRef = node => {
setButtonElement(node);
};
const transitionTimeout = number('transition.timeout (ms)', 0);
const transitionTimeout = number('transition.timeout (ms)', 300);
return (
<div
style={{
Expand Down
99 changes: 50 additions & 49 deletions packages/popover/src/Component.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, useEffect, useCallback, CSSProperties, MutableRefObject } from 'react';
import cn from 'classnames';
import { Transition } from 'react-transition-group';
import { TransitionProps } from 'react-transition-group/Transition';
import { CSSTransition } from 'react-transition-group';
import { CSSTransitionProps } from 'react-transition-group/CSSTransition';
import { usePopper } from 'react-popper';
import { BasePlacement, VariationPlacement, Obj } from '@popperjs/core';

Expand Down Expand Up @@ -68,10 +68,14 @@ export type PopoverProps = {
getPortalContainer?: () => HTMLElement;

/**
* Transition props, прокидываются в компонент Transition.
* См. https://reactcommunity.org/react-transition-group/transition
* CSSTransitionProps, прокидываются в компонент CSSTransitionProps.
*/
transition?: Partial<TransitionProps>;
transition?: CSSTransitionProps;

/**
* Выставляет кастомное свойство transition-duration
*/
transitionDuration?: CSSProperties['transitionDuration'];

/**
* Рендерит компонент, обернутый в Transition
Expand All @@ -94,7 +98,7 @@ const TRANSITION_DURATION = 300;
export const Popover: React.FC<PopoverProps> = ({
children,
getPortalContainer,
transition = {},
transition = { timeout: TRANSITION_DURATION },
anchorElement,
offset = [0, 0],
withArrow = false,
Expand All @@ -106,6 +110,7 @@ export const Popover: React.FC<PopoverProps> = ({
open,
dataTestId,
update,
transitionDuration = `${transition.timeout}ms`,
}) => {
const [referenceElement, setReferenceElement] = useState<RefElement>(anchorElement);
const [popperElement, setPopperElement] = useState<RefElement>(null);
Expand Down Expand Up @@ -151,53 +156,49 @@ export const Popover: React.FC<PopoverProps> = ({
}
}, [updatePopper, update]);

const renderPortal = (showContent: boolean, className?: string, style?: CSSProperties) => (
const renderContent = (style?: CSSProperties) => {
return (
<div
ref={setPopperElement}
className={cn(styles.component, popperClassName)}
style={{
...popperStyles.popper,
...style,
}}
data-test-id={dataTestId}
{...attributes.popper}
>
{children}
{withArrow && (
<div
ref={setArrowElement}
style={popperStyles.arrow}
className={cn(styles.arrow, arrowClassName)}
/>
)}
</div>
);
};

return (
<Portal getPortalContainer={getPortalContainer}>
{showContent && (
<div
ref={setPopperElement}
className={cn(styles.component, className, popperClassName)}
style={{
...popperStyles.popper,
...style,
{withTransition ? (
<CSSTransition
unmountOnExit={true}
classNames={{
enter: styles.enter,
enterActive: styles.enterActive,
exit: styles.exit,
exitActive: styles.exitActive,
}}
data-test-id={dataTestId}
{...attributes.popper}
{...transition}
in={open}
>
{children}
{withArrow && (
<div
ref={setArrowElement}
style={popperStyles.arrow}
className={cn(styles.arrow, arrowClassName)}
/>
)}
</div>
{renderContent({ transitionDuration })}
</CSSTransition>
) : (
open && renderContent()
)}
</Portal>
);

if (withTransition) {
const timeout = transition.timeout === undefined ? TRANSITION_DURATION : transition.timeout;

const transitionProps = {
mountOnEnter: true,
unmountOnExit: true,
...transition,
in: open,
timeout,
};

return (
<Transition {...transitionProps}>
{status =>
renderPortal(true, cn(styles[status], status), {
transitionDuration: `${timeout}ms`,
})
}
</Transition>
);
}

return renderPortal(open);
};
2 changes: 1 addition & 1 deletion packages/popover/src/__snapshots__/Component.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Object {
alfa-portal-container=""
>
<div
class="component entered entered"
class="component"
data-popper-escaped="true"
data-popper-placement="left"
data-popper-reference-hidden="true"
Expand Down
10 changes: 5 additions & 5 deletions packages/popover/src/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@
}
}

.entering {
.enter {
opacity: 0;
}

.entered {
.enterActive {
opacity: 1;
}

.exiting {
opacity: 0;
.exit {
opacity: 1;
}

.exited {
.exitActive {
opacity: 0;
}

0 comments on commit f8564bc

Please sign in to comment.