UNPKG

@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.

67 lines (65 loc) 1.96 kB
'use client'; import * as React from 'react'; import { MeterRootContext } from "./MeterRootContext.js"; import { formatNumber } from "../../utils/formatNumber.js"; import { useRenderElement } from "../../utils/useRenderElement.js"; import { jsx as _jsx } from "react/jsx-runtime"; function formatValue(value, locale, format) { if (!format) { return formatNumber(value / 100, locale, { style: 'percent' }); } return formatNumber(value, locale, format); } /** * Groups all parts of the meter and provides the value for screen readers. * Renders a `<div>` element. * * Documentation: [Base UI Meter](https://base-ui.com/react/components/meter) */ export const MeterRoot = /*#__PURE__*/React.forwardRef(function MeterRoot(componentProps, forwardedRef) { const { format, getAriaValueText, locale, max = 100, min = 0, value: valueProp, render, className, ...elementProps } = componentProps; const [labelId, setLabelId] = React.useState(); const formattedValue = formatValue(valueProp, locale, format); let ariaValuetext = `${valueProp}%`; if (getAriaValueText) { ariaValuetext = getAriaValueText(formattedValue, valueProp); } else if (format) { ariaValuetext = formattedValue; } const defaultProps = { 'aria-labelledby': labelId, 'aria-valuemax': max, 'aria-valuemin': min, 'aria-valuenow': valueProp, 'aria-valuetext': ariaValuetext, role: 'meter' }; const contextValue = React.useMemo(() => ({ formattedValue, max, min, setLabelId, value: valueProp }), [formattedValue, max, min, setLabelId, valueProp]); const element = useRenderElement('div', componentProps, { ref: forwardedRef, props: [defaultProps, elementProps] }); return /*#__PURE__*/_jsx(MeterRootContext.Provider, { value: contextValue, children: element }); }); if (process.env.NODE_ENV !== "production") MeterRoot.displayName = "MeterRoot";