react-native-sortables
Version:
Powerful Sortable Components for Flexible Content Reordering in React Native
66 lines (62 loc) • 2.11 kB
JavaScript
;
import React from "react";
import { useCallback, useEffect } from 'react';
import { View } from 'react-native';
import { GestureDetector } from 'react-native-gesture-handler';
import { runOnUI, useAnimatedRef } from 'react-native-reanimated';
import { useCustomHandleContext, useIsInPortalOutlet, useItemContext } from '../../providers';
import { error } from '../../utils';
/** Props for the Sortable Handle component */
import { jsx as _jsx } from "react/jsx-runtime";
export default function CustomHandle(props) {
// The item is teleported when it is rendered within the PortalOutlet
// component
const isTeleported = useIsInPortalOutlet();
// In case of teleported handle items, we want to render just the
// handle component without any functionality
return isTeleported ? /*#__PURE__*/_jsx(View, {
children: props.children
}) : /*#__PURE__*/_jsx(CustomHandleComponent, {
...props
});
}
function CustomHandleComponent({
children,
mode = 'draggable'
}) {
const customHandleContext = useCustomHandleContext();
if (!customHandleContext) {
throw error('Please add a `customHandle` property on the Sortable component to use a custom handle component.');
}
const {
gesture,
isActive,
itemKey
} = useItemContext();
const handleRef = useAnimatedRef();
const {
registerHandle,
updateActiveHandleMeasurements
} = customHandleContext;
const dragEnabled = mode === 'draggable';
useEffect(() => {
return registerHandle(itemKey, handleRef, mode === 'fixed-order');
}, [handleRef, itemKey, registerHandle, mode]);
const onLayout = useCallback(() => {
'worklet';
if (isActive.value) {
updateActiveHandleMeasurements(itemKey);
}
}, [itemKey, isActive, updateActiveHandleMeasurements]);
return /*#__PURE__*/_jsx(GestureDetector, {
gesture: gesture.enabled(dragEnabled),
userSelect: "none",
children: /*#__PURE__*/_jsx(View, {
collapsable: false,
ref: handleRef,
onLayout: runOnUI(onLayout),
children: children
})
});
}
//# sourceMappingURL=CustomHandle.js.map