File size: 1,184 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import classNames from 'classnames';
import type { FC, PropsWithChildren } from 'react';
import React from 'react';
import { DragPreviewImage, useDrag } from 'react-dnd';

import { dragIcon } from '../../../core/components/Cell/Draggable/useDragHandle';
import { useSetLayoutMode } from '../../../core/components/hooks/displayMode';

import type { CellDrag, InsertNewCell } from '../../../core/types';

const Draggable: FC<
  PropsWithChildren<{
    insert: InsertNewCell;
  }>
> = ({ insert, children }) => {
  const setLayoutMode = useSetLayoutMode();
  const [{ isDragging }, dragRef, preview] = useDrag<
    CellDrag,
    void,
    {
      isDragging: boolean;
    }
  >({
    type: 'cell',
    item: () => {
      setLayoutMode();
      return {
        cell: insert,
      };
    },
    collect: (monitor) => ({
      isDragging: monitor.isDragging(),
    }),
  });
  const classes = classNames(
    { 'react-page-toolbar-draggable-is-dragged': isDragging },
    'react-page-toolbar-draggable'
  );

  return (
    <div className={classes} ref={dragRef}>
      <DragPreviewImage connect={preview} src={dragIcon} />
      {children}
    </div>
  );
};

export default Draggable;