1

I am using the React Beautiful DND library to drag items(square divs) between columns or reordered in the same column. I followed their Egghead video tutorial to change the background color of the div as it's being dragged. When it gets dropped, it switches back to the default color of all other items. I want it to slowly fade(like 1 second maybe) to the default color after it is dropped.

Here is my current code styling for the div as it's being dragged and dropped. I added the transition line, that but is not doing anything.

const MyOrder = styled.div`
    background-color: ${(props) =>
        props.isDragging ? '#4FB740' : '#193DF4'};
    transition: background-color 1s ease;
`;

I have tried adding this code to the onDragEnd event:

        setDroppedOrderID(theOrder.orderNumber);
        setTimeout(() => {
          setDroppedOrderID('');
        }, 2000);

And I made the order div that gets dragged look like this:

<MyOrder
        id={orderNumber}
        className={`order size-${size} ${
            droppedOrderID === orderNumber ? ' dropped' : ''
        } ${palletLoc === 'OUTSIDE' ? 'outside' : ''}`}

But it is buggy if someone tries to drag the same item in less than the 2 second time interval.

dmikester1
  • 1,374
  • 11
  • 55
  • 113

1 Answers1

1

You can actually style the drop and do animation

See working demo & full code here in the codesandbox

You need to use isDropAnimating property from the snapshot to check if animation is being done so that you can conditionally return the original style.

code snippet

const OpportunityContainer = styled.div`
  border: 1px solid #ddd;
  border-radius: 0.3rem;
  background: #fff;
  padding: 1rem;
  margin-bottom: 0.8rem;
  transition: background-color 5s ease;
  background-color: ${props => (props.isDragging ? "#4FB740" : "#193DF4")};
`;
function getStyle(style, snapshot) {
  if (!snapshot.isDropAnimating) {
    return style;
  }

  // patching the existing style
  return {
    ...style,
    transition: `all 3s ease`,
    backgroundColor: "blue"
  };
}
const Opportunity = React.memo(({ index, id, title }) => {
  return (
    <Draggable draggableId={id} index={index}>
      {(provided, snapshot) => (
        <OpportunityContainer
          ref={provided.innerRef}
          {...provided.draggableProps}
          {...provided.dragHandleProps}
          isDragging={snapshot.isDragging && !snapshot.isDropAnimating}
          style={getStyle(provided.draggableProps.style, snapshot)}
        >
          {title}
        </OpportunityContainer>
      )}
    </Draggable>
  );
});

export default Opportunity;

Note - Make sure to read this note in the library documentation. isDragging will be true until animation/fade-out is completed. Therefore try to provide less duration for your animation (eg: 1 second or less than 1 second)

gdh
  • 13,114
  • 2
  • 16
  • 28
  • Wow, this is impressive! Quick follow-up question, is it possible to have it stay green for say 2 seconds, then fade to blue? – dmikester1 Jun 16 '20 at 04:18
  • yes - you can add `transitionDelay: '2s' ` ... i have updated the codesandbox...refresh the browser and check. – gdh Jun 16 '20 at 04:25
  • I just noticed something wonky. After you drag an item somewhere, that item is no longer able to be dragged. Any ideas why? – dmikester1 Jun 16 '20 at 14:55
  • i see ... ok - i will take a look and respond – gdh Jun 16 '20 at 15:12
  • 1
    ok - just saw the note on inconsistency in the library [see here](https://github.com/atlassian/react-beautiful-dnd/blob/master/docs/guides/drop-animation.md#skipping-the-drop-animation) ... after dropping, if you gently touch/move other element , you will be able to move the dropped item again ... its a library issue... ...it's pretty late at night here... in my morning, I will find a way to overcome this inconsistency and make it work for you – gdh Jun 16 '20 at 15:50
  • gdh - were you able to make any headway on that last issue? – dmikester1 Jun 18 '20 at 16:45
  • 1
    hi - sorry for delayed response - i was busy .... actually, it was tricky to fix but fix is simple.... In `getStyle` method, just add `all` to the `transition` ...basically you need to complete all transitions .. not just `background` ... the codesandbox is updated just refresh the it – gdh Jun 20 '20 at 06:36