@attio/react-native-bottom-sheet-toolbox
Version:
27 lines (25 loc) • 941 B
JavaScript
;
import * as React from "react";
import { PixelRatio } from "react-native";
import { useSharedValue } from "react-native-reanimated";
function roundAndReplace(sv, v) {
const roundedV = PixelRatio.roundToNearestPixel(v);
if (roundedV !== sv.get()) sv.set(roundedV);
}
/**
* A helper hook for providing shared values for onLayout measurements.
*/
export function useLayoutSharedValue(defaultHeight, defaultY) {
const heightSharedValue = useSharedValue(typeof defaultHeight === "number" ? defaultHeight : null);
const ySharedValue = useSharedValue(typeof defaultY === "number" ? defaultY : null);
const onLayout = React.useCallback(evt => {
roundAndReplace(heightSharedValue, evt.nativeEvent.layout.height);
roundAndReplace(ySharedValue, evt.nativeEvent.layout.y);
}, []);
return {
height: heightSharedValue,
y: ySharedValue,
onLayout
};
}
//# sourceMappingURL=use-layout-shared-value.js.map