How to use the react-dnd.useDrag function in react-dnd

To help you get started, we’ve selected a few react-dnd examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github MichelleWillaQu / ObjectViewGallery-Art-Showcase / static / gallery.js View on Github external
function TwoDMedia(props) {
  // A mutable ref object that is initialized to null and will persist for the
  // entire lifetime of the component
  const reference = useRef(null);

  const [{isDragging}, drag] = useDrag({
    item: {id: props.id,
           name: props.name,
           order: props.order, 
           type: ItemTypes.TWOD,
           url: props.url},
    canDrag: () => props.editMode,
    collect: (monitor) => ({
      isDragging: !!monitor.isDragging()
    })
  });

  const [, drop] = useDrop({
    accept: [ItemTypes.TWOD, ItemTypes.OBJ, ItemTypes.GLTF],
    canDrop: () => props.editMode,
    hover(item) {
      if (item.order !== props.order){
github OHIF / Viewers / platform / ui / src / components / studyBrowser / Thumbnail.js View on Github external
displaySetInstanceUid,
    imageId,
    imageSrc,
    instanceNumber,
    numImageFrames,
    seriesDescription,
    seriesNumber,
    stackPercentComplete,
    studyInstanceUid,
    onClick,
    onDoubleClick,
    onMouseDown,
    supportsDrag,
  } = props;

  const [collectedProps, drag, dragPreview] = useDrag({
    // `droppedItem` in `dropTarget`
    // The only data it will have access to
    item: {
      studyInstanceUid,
      displaySetInstanceUid,
      type: 'thumbnail', // Has to match `dropTarget`'s type
    },
    canDrag: function(monitor) {
      return supportsDrag;
    },
  });

  const hasImage = imageSrc || imageId;
  const hasAltText = altImageText !== undefined;

  return (
github jamstack-cms / jamstack-cms / src / components / pageTemplates / heroTemplate.js View on Github external
return
      }
      // Dragging upwards
      if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
        return
      }
      // Time to actually perform the action
      moveCard(dragIndex, hoverIndex)
      // Note: we're mutating the monitor item here!
      // Generally it's better to avoid mutations,
      // but it's good here for the sake of performance
      // to avoid expensive index searches.
      item.index = hoverIndex
    }
  })
  const [{ isDragging }, drag] = useDrag({
    item: { type: 'COMPONENT', id, index },
    collect: monitor => ({
      isDragging: monitor.isDragging(),
    }),
  })
  const opacity = isDragging ? 0 : 1
  drag(drop(ref))
  return (
    <div style="{{opacity}}">
      </div>
github mozilla / Spoke / src / ui / hierarchy / HierarchyPanelContainer.js View on Github external
e => {
      onChangeName(node, e.target.value);
    },
    [node, onChangeName]
  );

  const onSubmitNodeName = useCallback(
    e => {
      onRenameSubmit(node, e.target.value);
    },
    [onRenameSubmit, node]
  );

  const renaming = renamingNode && renamingNode.id === node.id;

  const [_dragProps, drag, preview] = useDrag({
    item: { type: ItemTypes.Node },
    begin() {
      const multiple = editor.selected.length > 1;
      return { type: ItemTypes.Node, multiple, value: multiple ? editor.selected : editor.selected[0] };
    },
    canDrag() {
      return !editor.selected.some(selectedObj => !selectedObj.parent);
    },
    collect: monitor => ({
      isDragging: !!monitor.isDragging()
    })
  });

  useEffect(() => {
    preview(getEmptyImage(), { captureDraggingState: true });
  }, [preview]);
github Heigvd / Wegas / wegas-app / src / main / webapp / 2 / src / Editor / Components / Page / ComponentPalette.tsx View on Github external
function ComponentElement({ componentName }: ComponentElementProps) {
  const [, drag] = useDrag({
    item: {
      componentName: componentName,
      type: dndComponnent,
    },
  });
  const component = usePageComponentStore(s =&gt; s[componentName]);
  return (
    <div>
      {component ? (
        
      ) : (</div>
github strapi / strapi / packages / strapi-plugin-content-manager / admin / src / components / SelectMany / ListItem.js View on Github external
function ListItem({
  data,
  findRelation,
  mainField,
  moveRelation,
  nextSearch,
  onRemove,
  targetModel,
}) {
  const to = `/plugins/${pluginId}/${targetModel}/${data.id}?redirectUrl=${nextSearch}`;

  const originalIndex = findRelation(data.id).index;
  const [{ isDragging }, drag, preview] = useDrag({
    item: {
      type: ItemTypes.RELATION,
      id: data.id,
      originalIndex,
      data,
      mainField,
    },
    collect: monitor => ({
      isDragging: monitor.isDragging(),
    }),
  });
  const [, drop] = useDrop({
    accept: ItemTypes.RELATION,
    canDrop: () => false,
    hover({ id: draggedId }) {
      if (draggedId !== data.id) {
github mozilla / Spoke / src / ui / assets / AssetGrid.js View on Github external
iconComponent={item.iconComponent}
        onClick={onClickItem}
        data-tip={item.id}
        data-for={tooltipId}
        data-effect="solid"
        label={item.label}
        {...rest}
      /&gt;
    );
  } else {
    content = (
      
    );
  }

  const [_dragProps, drag, preview] = useDrag({
    item: { type: item.type },
    begin() {
      return { type: item.type, multiple: false, value: item };
    }
  });

  useEffect(() =&gt; {
    preview(getEmptyImage(), { captureDraggingState: true });
  }, [preview]);

  return (
    <div>
      
        {content}
      
    </div>
github mikey1384 / twinkle-network / source / containers / VideoPage / QuestionsBuilder / QuestionsListItem.js View on Github external
export default function QuestionsListItem({
  item: listItem,
  onMove,
  questionId
}) {
  const Draggable = useRef(null);
  const [{ opacity }, drag] = useDrag({
    item: { type: ItemTypes.LIST_ITEM, questionId: questionId },
    collect: monitor => ({
      opacity: monitor.isDragging() ? 0 : 1
    })
  });
  const [, drop] = useDrop({
    accept: ItemTypes.LIST_ITEM,
    hover(item) {
      if (!Draggable.current) {
        return;
      }
      if (item.questionId === questionId) {
        return;
      }
      onMove({ sourceId: item.questionId, targetId: questionId });
    }
github lytc / react-sortly / packages / react-sortly / src / Item.tsx View on Github external
function Item(props: ItemProps) {
  const { setDragMonitor, setConnectedDragSource, setInitialDepth } = React.useContext(context);
  const wasHoveredRef = React.useRef(false);
  const connectedDragRef = React.useRef();
  const connectedDropRef = React.useRef();
  const { 
    type, index, id, depth, data, children, onHoverEnd, onHoverBegin, isClosestDragging 
  } = props;
  const t = typeof type === 'function' ? type() : type;
  const [{ isDragging }, dndDrag, preview] = useDrag({
    item: { 
      id, 
      type: t,
      data 
    },
    collect: (monitor) =&gt; ({
      isDragging: monitor.isDragging(),
    }),
    isDragging: (monitor) =&gt; id === monitor.getItem().id,
    begin: (monitor) =&gt; {
      setInitialDepth(depth);
      setDragMonitor(monitor);
      setConnectedDragSource(connectedDragRef);
    },
    end: () =&gt; {
      setInitialDepth(undefined);
github mikey1384 / twinkle-network / source / components / SortableThumb.js View on Github external
export default function SortableThumb({ id, onMove, video }) {
  const [onTitleHover, setOnTitleHover] = useState(false);
  const Draggable = useRef(null);
  const ThumbLabelRef = useRef(null);
  const [{ isDragging }, drag] = useDrag({
    item: { type: ItemTypes.THUMB, id },
    isDragging: monitor => !!monitor.isDragging()
  });
  const [, drop] = useDrop({
    accept: ItemTypes.THUMB,
    hover(item, monitor) {
      if (!Draggable.current) {
        return;
      }
      if (item.id !== id) {
        onMove({ sourceId: item.id, targetId: id });
      }
    }
  });

  return (