MCPcopy Create free account
hub / github.com/adobe/react-spectrum / SliderBase

Function SliderBase

packages/@react-spectrum/s2/src/Slider.tsx:408–516  ·  view source on GitHub ↗
(
  props: SliderBaseProps<T> & {sliderRef: RefObject<HTMLDivElement | null>; track: ReactNode}
)

Source from the content-addressed store, hash-verified

406});
407
408export function SliderBase<T extends number | number[]>(
409 props: SliderBaseProps<T> & {sliderRef: RefObject<HTMLDivElement | null>; track: ReactNode}
410): ReactNode {
411 let formContext = useContext(FormContext);
412 // oxlint-disable-next-line react/react-compiler
413 props = useFormProps(props);
414 let {
415 label,
416 labelPosition = 'top',
417 labelAlign = 'start',
418 size = 'M',
419 minValue = 0,
420 maxValue = 100,
421 children,
422 track: trackElement,
423 sliderRef,
424 ...ariaProps
425 } = props;
426 // SliderInput is currently the only supported child.
427 let hasInput = !!children;
428
429 // oxlint-disable react/react-compiler
430 return (
431 <AriaSlider
432 {...ariaProps}
433 minValue={minValue}
434 maxValue={maxValue}
435 ref={sliderRef}
436 className={renderProps =>
437 (props.UNSAFE_className || '') +
438 mergeStyles(
439 style(field())({labelPosition, isInForm: !!formContext}),
440 slider({...renderProps, labelPosition, size, isInForm: !!formContext}, props.styles)
441 )
442 }>
443 {({state}) => {
444 let maxLabelLength = 0;
445 // Also measure one step in from each bound: for decimal steps (e.g. 0–10 with step 0.1),
446 // 0.1 and 9.9 are wider than 0 and 10.
447 let minStep = snapValueToStep(minValue + state.step, minValue, maxValue, state.step);
448 let maxStep = snapValueToStep(maxValue - state.step, minValue, maxValue, state.step);
449 let minFormatted = state.getFormattedValue(minValue);
450 let minStepFormatted = state.getFormattedValue(minStep);
451 let maxFormatted = state.getFormattedValue(maxValue);
452 let maxStepFormatted = state.getFormattedValue(maxStep);
453 let minSizer =
454 [...minStepFormatted].length > [...minFormatted].length ? minStepFormatted : minFormatted;
455 let maxSizer =
456 [...maxStepFormatted].length > [...maxFormatted].length ? maxStepFormatted : maxFormatted;
457 switch (state.values.length) {
458 case 1:
459 maxLabelLength = Math.max([...minSizer].length, [...maxSizer].length);
460 break;
461 case 2:
462 maxLabelLength = Math.max(
463 [...state.getFormattedValue([minValue, minStep])].length,
464 [...state.getFormattedValue([maxStep, maxValue])].length
465 );

Callers

nothing calls this directly

Calls 5

useFormPropsFunction · 0.90
mergeStylesFunction · 0.90
fieldFunction · 0.90
snapValueToStepFunction · 0.90
getFormattedValueMethod · 0.80

Tested by

no test coverage detected