Hooks

useDraggable

useDraggable() provides drag-to-corner functionality. Used internally by ConsentDialogTrigger, this hook lets you build custom draggable elements that snap to viewport corners.

import { useDraggable } from '@c15t/react';

function DraggableButton() {
  const { corner, isDragging, handlers, dragStyle } = useDraggable({
    defaultPosition: 'bottom-right',
    persistPosition: true,
  });

  return (
    <button
      {...handlers}
      style={{
        ...dragStyle,
        position: 'fixed',
        // Position based on corner
        ...(corner.includes('bottom') ? { bottom: 16 } : { top: 16 }),
        ...(corner.includes('right') ? { right: 16 } : { left: 16 }),
      }}
    >
      {isDragging ? 'Dragging...' : 'Drag me'}
    </button>
  );
}

Options

OptionTypeDefaultDescription
defaultPositionCornerPosition'bottom-right'Initial corner position
persistPositionbooleantrueSave position to localStorage
onPositionChange(position: CornerPosition) => void-Callback on position change

Return Value

PropertyTypeDescription
cornerCornerPositionCurrent corner: 'top-left''top-right''bottom-left''bottom-right'
isDraggingbooleanWhether the element is being dragged
isSnappingbooleanWhether the element is animating to a new corner
wasDragged() => booleanWhether the last interaction was a drag (vs click)
handlersobjectPointer event handlers to spread onto the element
dragStyleCSSPropertiesTransform style for drag offset

Behavior

  • Drag starts on pointer down (left click / single touch)
  • Movement threshold of 5px distinguishes drag from click
  • On pointer up, element snaps to the nearest corner based on drag direction and velocity
  • Position persists to localStorage by default