Skip to content

Commit

Permalink
Simplify by moving the Fade component one level up
Browse files Browse the repository at this point in the history
  • Loading branch information
fullofcaffeine committed Jun 1, 2024
1 parent 871394c commit 80ccc35
Showing 1 changed file with 21 additions and 31 deletions.
52 changes: 21 additions & 31 deletions packages/components/src/drop-zone/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ import type { WordPressComponentProps } from '../context';
import type { ReactNode } from 'react';

/**
* Handles fade-in/fade-out animation for its children if `reducedMotion` is disabled.
* Relies on the animations being defined in the CSS styles.
* Handles fade-in/fade-out animation for its children if `reducedMotion` is disabled,
* if not, just shows/hides the children without any animation.
*
* @returns {JSX.Element | null} The animated container with children or null.
* Relies on the animations being defined in CSS styles.
*/
const MaybeFade: React.FC< {
show: boolean;
children: ReactNode;
duration?: number;
} > = ( { show, children, duration = 200 } ) => {
} > = ( { show, children } ) => {
const [ shouldRender, setRender ] = useState( show );
const reducedMotion = useReducedMotion();

Expand All @@ -47,7 +47,6 @@ const MaybeFade: React.FC< {
const props = ! reducedMotion
? {
className: show ? 'fade-enter' : 'fade-exit',
style: { animationDuration: `${ duration }ms` },
onAnimationEnd: () => {
if ( ! show ) {
setRender( false );
Expand All @@ -59,30 +58,22 @@ const MaybeFade: React.FC< {
return shouldRender && <div { ...props }>{ children }</div>;
};

function DropIndicator( {
label,
isActive,
}: {
label?: string;
isActive: boolean;
} ) {
function DropIndicator( { label }: { label?: string } ) {
return (
<MaybeFade show={ isActive }>
<div
className="components-drop-zone__content"
style={ { pointerEvents: 'none' } }
>
<div className="components-drop-zone__content-inner">
<Icon
icon={ upload }
className="components-drop-zone__content-icon"
/>
<span className="components-drop-zone__content-text">
{ label ? label : __( 'Drop files to upload' ) }
</span>
</div>
<div
className="components-drop-zone__content"
style={ { pointerEvents: 'none' } }
>
<div className="components-drop-zone__content-inner">
<Icon
icon={ upload }
className="components-drop-zone__content-icon"
/>
<span className="components-drop-zone__content-text">
{ label ? label : __( 'Drop files to upload' ) }
</span>
</div>
</MaybeFade>
</div>
);
}

Expand Down Expand Up @@ -192,10 +183,9 @@ export function DropZoneComponent( {

return (
<div { ...restProps } ref={ ref } className={ classes }>
<DropIndicator
label={ label }
isActive={ isDraggingOverElement || false }
/>
<MaybeFade show={ isDraggingOverElement || false }>
<DropIndicator label={ label } />
</MaybeFade>
</div>
);
}
Expand Down

0 comments on commit 80ccc35

Please sign in to comment.