Skip to content

Nested horizontal and vertical in same list #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
"README.md"
],
"main": "lib/index.js",
"bin": "",
"man": "",
"repository": {
"type": "git",
"url": "https://github.com/jagaapple/react-sortful.git"
Expand All @@ -48,7 +46,7 @@
},
"config": {},
"dependencies": {
"react-use-gesture": "^7.0.4"
"react-use-gesture": "^7.0.15"
},
"devDependencies": {
"@babel/core": "^7.8.6",
Expand Down
55 changes: 39 additions & 16 deletions src/drag-handle.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,64 @@ import * as React from "react";
import { useGesture } from "react-use-gesture";

import { ItemContext } from "./item";
import { UseGestureConfig, UseGestureEvent } from "react-use-gesture/dist/types";

type Props = {
className?: string;
children: React.ReactNode;
/**
* Specify a react-use-gesture config object to use for specifying the drag.
*/
useGestureConfig?: UseGestureConfig;
/**
* If you want to do something on click or on tap, specify this option.
*/
onTap?: (event: UseGestureEvent) => void;
};

export const DragHandleComponent = (props: Props) => {
const itemContext = React.useContext(ItemContext);

const useGestureConfig: UseGestureConfig = { ...(props.useGestureConfig || {}) };
if (props.onTap) {
useGestureConfig.drag = useGestureConfig.drag || {};
useGestureConfig.drag.filterTaps = true;
}

// Checks `props.children` has one React node.
React.useEffect(() => {
React.Children.only(props.children);
}, [props.children]);

const draggableBinder = useGesture({
onDragStart: (state: any) => {
if (itemContext.isLocked) return;
const draggableBinder = useGesture(
{
onDragStart: (state: any) => {
if (itemContext.isLocked) return;

const event: React.SyntheticEvent = state.event;
event.persist();
event.stopPropagation();
const event: React.SyntheticEvent = state.event;
event?.persist?.();
event?.stopPropagation?.();

itemContext.dragHandlers.onDragStart();
},
onDrag: ({ down, movement }) => {
if (itemContext.isLocked) return;
itemContext.dragHandlers.onDragStart();
},
onDrag: ({ down, movement, tap, event }) => {
if (tap && props.onTap) {
props.onTap(event as any);
return;
}

itemContext.dragHandlers.onDrag(down, movement);
},
onDragEnd: () => {
if (itemContext.isLocked) return;
if (itemContext.isLocked) return;

itemContext.dragHandlers.onDragEnd();
itemContext.dragHandlers.onDrag(down, movement);
},
onDragEnd: () => {
if (itemContext.isLocked) return;

itemContext.dragHandlers.onDragEnd();
},
},
});
useGestureConfig,
);

return (
<div className={props.className} {...draggableBinder()}>
Expand Down
Loading