base-ui icon indicating copy to clipboard operation
base-ui copied to clipboard

Should `DirectionProvider` set `dir` attribute on portalled elements (e.g. `Tooltip.Popup`)?

Open benface opened this issue 1 year ago • 5 comments

Feature request

Summary

So if a part of my app is rendered in a right-to-left language, I should wrap that part with <DirectionProvider direction="rtl">, right? I would kind of assume that this handles setting the dir attribute of not just the element that DirectionProvider renders, but also of all the Base UI components in DirectionProvider's React tree, especially those that get rendered in a portal and likely end up outside that element in the actual DOM (tooltips, popovers, menus, etc.), making them not actually right-to-left.

Examples in other libraries

Not sure.

Motivation

Not having to manually track the current direction to set dir on some components that could be deeply nested would be great.

benface avatar Oct 22 '25 20:10 benface

Radix exports a DirectionProvider which takes a dir prop https://www.radix-ui.com/primitives/docs/utilities/direction-provider

Ant Design exports a ConfigProvider which takes the dir prop https://ant.design/components/config-provider

Meanwhile in React-aria the direction is embedded into their internationalization system https://react-spectrum.adobe.com/react-aria/useLocale.html https://react-spectrum.adobe.com/react-aria/I18nProvider.html

eg.

import { I18nProvider, useLocale } from 'react-aria-components';

function App() {
  let { locale, direction } = useLocale();
  
  return (
    <html lang={locale} dir={direction}>
      {/* your app here */}
    </html>
  );
}

<I18nProvider locale="ar-AE">
  <App />
</I18nProvider>

Raghuboi avatar Oct 27 '25 14:10 Raghuboi

I could contribute to this

Raghuboi avatar Oct 27 '25 14:10 Raghuboi

I did notice this when testing RTL a while back, it is a bit annoying to have to manually set this on every nested positioner level: https://github.com/mui/base-ui/blob/master/docs/src/app/(private)/experiments/rtl.tsx#L32

However, setting the dir attribute is all or nothing though, and we decided to not set it to separate the HTML/CSS concerns from configuring the behavior (RA/Ariakit do not set dir either)

A slightly less annoying way I guess would be for your wrapping component e.g. MyMenu.Root to accept a dir/direction prop and forward it via a custom context to MyMenu.Positioner (or Trigger`), so you're just tracking the roots and not the nested parts 🤔

mj12albert avatar Oct 27 '25 18:10 mj12albert

I did notice this when testing RTL a while back, it is a bit annoying to have to manually set this on every nested positioner level: https://github.com/mui/base-ui/blob/master/docs/src/app/(private)/experiments/rtl.tsx#L32

However, setting the dir attribute is all or nothing though, and we decided to not set it to separate the HTML/CSS concerns from configuring the behavior (RA/Ariakit do not set dir either)

A slightly less annoying way I guess would be for your wrapping component e.g. MyMenu.Root to accept a dir/direction prop and forward it via a custom context to MyMenu.Positioner (or Trigger`), so you're just tracking the roots and not the nested parts 🤔

I think wrapping the component could be an intermediary solution for now, but maybe the API reference can have some more clarity

Raghuboi avatar Oct 27 '25 18:10 Raghuboi

This fix will be available in the next npm release of Base UI.

In the meantime, you can try it out on our Canary release channel:

npm i https://pkg.pr.new/@base-ui-components/react@3082

github-actions[bot] avatar Nov 04 '25 05:11 github-actions[bot]