Skip to content

@react-aria/slider: allow direction to be passed as a parameter in useSlider and useSliderThumb #2315

@jeanpan

Description

@jeanpan

🙋 Feature Request

Currently, useSlider and useSliderThumb use useLocale to get direction. I'd like to request a direction (or dir) parameter to be able pass in useSlider and useSliderThumb so that we don't need to reply on useLocale to get the direction for LTR/RTL.

🤔 Expected Behavior

If the direction parameter is provided, it override the direction of useLocale.

😯 Current Behavior

Current behavior is that we must use <I18nProvider> to specify locale for the direction.

💁 Possible Solution

Add an optional dir parameter in useSlider and useSliderThumb.

/**
 * Provides behavior and accessibility for a thumb of a slider component.
 *
 * @param opts Options for this Slider thumb.
 * @param state Slider state, created via `useSliderState`.
 * @param dir Direction for this Slider thumb.
 */
export function useSliderThumb(
  opts: SliderThumbOptions,
  state: SliderState,
  dir?: Direction
): SliderThumbAria {
  const { index, isRequired, isDisabled, validationState, trackRef, inputRef } =
    opts;

  const isVertical = opts.orientation === "vertical";

  let { direction } = useLocale();

  direction = dir ?? direction;
  ...
}

Same for useSlider. So we can use our theme direction.

const theme = useTheme();
const { thumbProps, inputProps } = useSliderThumb(
   {
     index,
     trackRef,
     inputRef
   },
   state,
   theme.direction
);

🔦 Context

Our system is using ThemeProvider with theme direction directly, it's difficult to convert direction to locale and use <I18nProvider>, so we can't make the useSlider and useSliderThumb work correctly for RTL. It would be great that if they have direction parameter to be more flexible to use.

I am not sure if there is any other way to make them work correctly for RTL, except using <I18nProvider>. Please let me know if there is a better way to do that.

If this proposed change makes sense to you guys, I am happy to prepare the pull request. Thank you. 🙇‍♀️

Metadata

Metadata

Assignees

No one assigned

    Labels

    waitingWaiting on Issue Author

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions