@scrolia/react-native
Version:
A headless scrollbar component
69 lines (67 loc) • 1.69 kB
JavaScript
import { useScrollCore } from "../../contexts/scrollcore.mjs";
import { setLengthFn } from "./functions/length.mjs";
import { handleScrollFn } from "./functions/scroll.mjs";
/** Content handler. */
const createContentHandler = (axis, options) => {
const { props } = options ?? {};
const { options: { disabled, animated, plugins }, [axis]: { hvTrack, hvThumb, total, view, viewOffset, scrollbarLength, setScrollbarLength, scrollbarOffset, setScrollbarOffset } } = useScrollCore();
const setLength = setLengthFn({
axis,
disabled,
animated,
plugins,
hvTrack,
hvThumb,
total,
view,
viewOffset,
scrollbarLength,
setScrollbarLength
});
const onLayout = (event) => {
if (!disabled) {
if (axis === "x") view.current = event.nativeEvent.layout.width;
else view.current = event.nativeEvent.layout.height;
setLength();
}
props?.onLayout?.(event);
};
const onContentSizeChange = (width, height) => {
if (!disabled) {
if (axis === "x") total.current = width;
else total.current = height;
setLength();
}
props?.onContentSizeChange?.(width, height);
};
const onScroll = (event) => {
if (!disabled) handleScrollFn({
axis,
event,
disabled,
animated,
plugins,
hvTrack,
hvThumb,
total,
view,
viewOffset,
scrollbarOffset,
setScrollbarOffset
});
props?.onScroll?.(event);
};
return {
onLayout,
onContentSizeChange,
onScroll
};
};
const useContentXHandler = (options) => {
return createContentHandler("x", options);
};
const useContentYHandler = (options) => {
return createContentHandler("y", options);
};
export { useContentXHandler, useContentYHandler };
//# sourceMappingURL=index.mjs.map