@base-ui-components/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
53 lines (51 loc) • 1.32 kB
JavaScript
'use client';
import * as React from 'react';
import { mergeReactProps } from '../../utils/mergeReactProps.js';
function getRangeStyles(orientation, offset, leap) {
if (orientation === 'vertical') {
return {
bottom: `${offset}%`,
height: `${leap}%`,
width: 'inherit'
};
}
return {
insetInlineStart: `${offset}%`,
width: `${leap}%`,
height: 'inherit'
};
}
/**
*/
export function useSliderIndicator(parameters) {
const {
orientation,
percentageValues
} = parameters;
let internalStyles;
if (percentageValues.length > 1) {
const trackOffset = percentageValues[0];
const trackLeap = percentageValues[percentageValues.length - 1] - trackOffset;
internalStyles = getRangeStyles(orientation, trackOffset, trackLeap);
} else if (orientation === 'vertical') {
internalStyles = {
bottom: 0,
height: `${percentageValues[0]}%`,
width: 'inherit'
};
} else {
internalStyles = {
insetInlineStart: 0,
width: `${percentageValues[0]}%`,
height: 'inherit'
};
}
const getRootProps = React.useCallback((externalProps = {}) => {
return mergeReactProps(externalProps, {
style: internalStyles
});
}, [internalStyles]);
return React.useMemo(() => ({
getRootProps
}), [getRootProps]);
}