@scrolia/react-native
Version:
A headless scrollbar component
157 lines (155 loc) • 4.3 kB
JavaScript
const require_runtime = require('../../../_virtual/_rolldown/runtime.js');
const require_contexts_scrollcore = require('../../../contexts/scrollcore.js');
const require_plugin = require('../../../functions/plugin.js');
let react_native = require("react-native");
let react = require("react");
react = require_runtime.__toESM(react);
const onPanResponderGrant = ({ axis, event, state, startPos, isDrag, disabled, animated, plugins, hvTrack, hvThumb, total, view, viewOffset }) => {
event.preventDefault();
const pointerOffset = axis === "x" ? state.x0 : state.y0;
startPos.current = {
viewOffset: viewOffset.current,
pointerOffset
};
for (const plugin of plugins) {
if (!plugin.onDragStart) continue;
require_plugin.tryPlugin(plugin, plugin.onDragStart, {
axis,
isDisabled: disabled,
isAnimated: animated,
isDefined: hvTrack && hvThumb,
total: total.current,
view: view.current,
viewOffset: viewOffset.current,
pointerOffset
});
}
isDrag.current = true;
};
const onPanResponderMove = ({ axis, state, startPos, isDrag, disabled, animated, plugins, contentType, contentRef, hvTrack, hvThumb, total, view, viewOffset }) => {
if (!isDrag.current || !contentRef.current) return void 0;
const _startPos = startPos.current;
const delta = axis === "x" ? state.dx : state.dy;
const ratio = view.current / total.current;
const scrollTo = _startPos.viewOffset + delta / ratio;
let result;
for (const plugin of plugins) {
if (!plugin.onDragMove) continue;
result = require_plugin.tryPlugin(plugin, plugin.onDragMove, {
axis,
isDisabled: disabled,
isAnimated: animated,
isDefined: hvTrack && hvThumb,
total: total.current,
view: view.current,
viewOffset: viewOffset.current,
pointerOffset: _startPos.pointerOffset + (axis === "x" ? state.dx : state.dy),
viewOffsetInit: _startPos.viewOffset,
pointerOffsetInit: _startPos.pointerOffset,
delta,
ratio,
scrollTo: result?.scrollTo ?? scrollTo
}) ?? result;
}
let final;
if (result?.scrollTo) final = result.scrollTo;
else final = scrollTo;
switch (contentType.current) {
case "scrollview":
contentRef.current.scrollTo({
...axis === "x" ? { x: final } : { y: final },
animated: false
});
break;
case "flatlist":
contentRef.current.scrollToOffset({
offset: final,
animated: false
});
break;
}
};
const onPanResponderRelease = ({ axis, state, startPos, isDrag, disabled, animated, plugins, hvTrack, hvThumb, total, view, viewOffset }) => {
const _startPos = startPos.current;
for (const plugin of plugins) {
if (!plugin.onDragEnd) continue;
require_plugin.tryPlugin(plugin, plugin.onDragEnd, {
axis,
isDisabled: disabled,
isAnimated: animated,
isDefined: hvTrack && hvThumb,
total: total.current,
view: view.current,
viewOffset: viewOffset.current,
pointerOffset: axis === "x" ? state.x0 : state.y0,
viewOffsetInit: _startPos.viewOffset,
pointerOffsetInit: _startPos.pointerOffset
});
}
isDrag.current = false;
};
const createPanResponder = (axis) => {
const { options: { disabled, animated, plugins }, [axis]: { contentType, contentRef, hvTrack, hvThumb, total, view, viewOffset } } = require_contexts_scrollcore.useScrollCore();
const isDrag = react.useRef(false);
const startPos = react.useRef({
viewOffset: 0,
pointerOffset: 0
});
return react_native.PanResponder.create({
onStartShouldSetPanResponder: () => true,
onMoveShouldSetPanResponder: () => true,
onPanResponderGrant: (event, state) => {
onPanResponderGrant({
axis,
event,
state,
startPos,
isDrag,
disabled,
animated,
plugins,
hvTrack,
hvThumb,
total,
view,
viewOffset
});
},
onPanResponderMove: (_, state) => {
onPanResponderMove({
axis,
state,
startPos,
isDrag,
disabled,
animated,
plugins,
contentType,
contentRef,
hvTrack,
hvThumb,
total,
view,
viewOffset
});
},
onPanResponderRelease: (_, state) => {
onPanResponderRelease({
axis,
state,
startPos,
isDrag,
disabled,
animated,
plugins,
hvTrack,
hvThumb,
total,
view,
viewOffset
});
}
});
};
exports.createPanResponder = createPanResponder;
//# sourceMappingURL=pan.js.map