@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.
32 lines (31 loc) • 858 B
JavaScript
'use client';
import * as React from 'react';
import { mergeReactProps } from '../../utils/mergeReactProps.js';
function valueToPercent(value, min, max) {
return (value - min) * 100 / (max - min);
}
function useProgressIndicator(parameters) {
const {
max = 100,
min = 0,
value
} = parameters;
const percentageValue = Number.isFinite(value) && value !== null ? valueToPercent(value, min, max) : null;
const getStyles = React.useCallback(() => {
if (!percentageValue) {
return {};
}
return {
insetInlineStart: 0,
height: 'inherit',
width: `${percentageValue}%`
};
}, [percentageValue]);
const getRootProps = React.useCallback((externalProps = {}) => mergeReactProps(externalProps, {
style: getStyles()
}), [getStyles]);
return {
getRootProps
};
}
export { useProgressIndicator };