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 b352016
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions packages/components/src/drop-zone/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { upload, Icon } from '@wordpress/icons';
import { getFilesFromDataTransfer } from '@wordpress/dom';
import {
__experimentalUseDropZone as useDropZone,
useReducedMotion,
// useReducedMotion,
} from '@wordpress/compose';

/**
Expand All @@ -23,18 +23,19 @@ 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();
//const reducedMotion = useReducedMotion();
const reducedMotion = false;

useEffect( () => {
if ( show ) {
Expand All @@ -47,7 +48,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 +59,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 +184,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 b352016

Please sign in to comment.