react-use-gesture#useDrag TypeScript Examples
The following examples show how to use
react-use-gesture#useDrag.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.tsx From hubble-ui with Apache License 2.0 | 6 votes |
Component = function DragPanelComponent(props: Props) {
const bind = useDrag(e => {
const dy = e.delta[1];
props.onResize && props.onResize(dy);
});
return (
<animated.div {...bind()} className={css.dragPanel}>
<div className={css.left}>
<FlowsTableColumnsSelector
visibleColumns={props.flowsTableVisibleColumns}
onToggleColumn={props.onToggleFlowsTableColumn}
/>
</div>
<div className={css.center} />
<div className={css.right} />
</animated.div>
);
}
Example #2
Source File: PageContent.tsx From tailchat with GNU General Public License v3.0 | 6 votes |
PageGestureWrapper: React.FC = React.memo((props) => {
const { setShowSidebar } = useSidebarContext();
const bind = useDrag(
(state) => {
const { swipe } = state;
const swipeX = swipe[0];
if (swipeX > 0) {
setShowSidebar(true);
} else if (swipeX < 0) {
setShowSidebar(false);
}
},
{
axis: 'x',
swipeDistance: 5,
}
);
return <PageContentRoot {...bind()}>{props.children}</PageContentRoot>;
})
Example #3
Source File: array-supercontrol.tsx From utopia with MIT License | 5 votes |
export function useArraySuperControl<T>(
values: ReadonlyArray<T>,
onSubmitValue: (newValue: ReadonlyArray<T>, transient?: boolean | undefined) => void,
rowHeight: number,
reversed: boolean = false,
) {
const localDraggingIndex = React.useRef(values.map((_, index) => index))
const [springs, setSprings] = useSprings(
values.length,
getStyleForArrayDragItem(localDraggingIndex.current, reversed, rowHeight),
[values],
)
React.useEffect(() => {
localDraggingIndex.current = values.map((_, index) => index)
setSprings(getStyleForArrayDragItem(localDraggingIndex.current, reversed, rowHeight))
}, [values, setSprings, reversed, rowHeight])
const bind = useDrag(({ args: [currentlyDraggingOriginalIndex], down, movement: [, y] }) => {
const currentlyDraggingTemporaryIndex = localDraggingIndex.current.indexOf(
currentlyDraggingOriginalIndex,
)
const newCurrentlyDraggingTemporaryIndex = clamp(
Math.round((currentlyDraggingOriginalIndex * rowHeight + (reversed ? -y : y)) / rowHeight),
0,
values.length - 1,
)
localDraggingIndex.current = move(
currentlyDraggingTemporaryIndex,
newCurrentlyDraggingTemporaryIndex,
localDraggingIndex.current,
)
setSprings(
getStyleForArrayDragItem(localDraggingIndex.current, reversed, rowHeight, down, {
currentlyDraggingOriginalIndex: currentlyDraggingOriginalIndex as number,
currentlyDraggingTemporaryIndex,
y,
}),
)
// Ensure to only change the ordering if it has actually changed.
if (!down && currentlyDraggingOriginalIndex !== newCurrentlyDraggingTemporaryIndex) {
const newValue = move(
currentlyDraggingOriginalIndex as number,
newCurrentlyDraggingTemporaryIndex,
[...values],
)
onSubmitValue(newValue)
}
})
return {
bind,
springs,
}
}
Example #4
Source File: DragableContainer.tsx From react-planet with MIT License | 4 votes |
export function DragableContainer(props: Props) {
const {
children,
on,
dragable,
dragRadius,
bounceRadius,
open,
bounceOnOpen,
bounceOnClose,
bounceDirection,
} = props;
if (on) {
const [{ x, y, cursor }, set] = useSpring(() => ({
x: 0,
y: 0,
config: { tension: 400, friction: 7, precision: 0.1 },
cursor: "pointer",
}));
const classes = useStyles(props);
React.useEffect(() => {
if ((open && bounceOnOpen) || (!open && bounceOnClose)) {
const bRadius = bounceRadius ? bounceRadius : DEFAULT_BOUNCE_RADIUS;
let x = bRadius;
let y = bRadius;
switch (bounceDirection) {
case "LEFT":
x = -bRadius;
y = 0;
break;
case "RIGHT":
x = bRadius;
y = 0;
break;
case "TOP":
x = 0;
y = -bRadius;
break;
case "BOTTOM":
x = 0;
y = bRadius;
break;
}
set({ x, y });
setTimeout(() => set({ x: 0, y: 0 }), 100);
}
}, [open]);
// Creates a drag gesture
const bind = useDrag(({ down, movement: [dX, dY] }) => {
if (dragable) {
const rMax = dragRadius ? dragRadius : DEFAULT_DRAG_RADIUS;
const r = Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2));
// Find point along radius with rMax length
// See: https://math.stackexchange.com/q/1630886
if (r > rMax) {
dX *= rMax / r;
dY *= rMax / r;
}
set({
x: down ? dX : 0,
y: down ? dY : 0,
immediate: down,
cursor: down ? "grabbing" : "pointer",
});
}
});
return (
<div className={classes.root}>
<animated.div
{...bind()}
className={classes.dragable}
style={{ cursor: cursor, left: x, top: y }}
>
{children}
</animated.div>
</div>
);
} else {
return <>{children}</>;
}
}