Description
Bug report
Current Behavior
Several components (such as the DropdownMenu
component) use the useDirection
hook.
If this hook does not receive a dir
prop, the hook reads the element's computed styles by pulling the direction
property from the element's computed style.
This can be expensive. As none of the elements provide a default for this prop, if used on an element that might changes very often for whatever reason, this will cause the hook to run on every frame. This was happening to me: see a before and after before I went source-code spelunking.
Before (with <Dropdown.Root>
): Video Link
After (with <Dropdown.Root dir="ltr">
): Video Link
Expected behavior
If no property is provided, the hook should stick with its first result rather than repeating the expensive job of getting the element's computed styles.
Reproducible example
Extremely contrived
https://codesandbox.io/s/nervous-jackson-plg87?file=/src/App.tsx
Suggested solution
At the risk of being a touch imperialist, I'd expect ltr
to be the default for the dir
prop.
If you do want to read the element itself to get the direction, then I'd expect that the hook did not refresh when the element changes. Specifically, I'd suggest that element
be dropped from this dependency array.
Otherwise, if you want to foreground the ltr
or rtl
choice, make it a required prop.
If none of the above are possible, I'd expect to see this flagged as a potential performance issue when leaving it undefined for components that may change often.
Your environment
Software | Name(s) | Version |
---|---|---|
Radix Package(s) | @radix-ui/react-dropdown | 0.0.22 |
React | n/a | Latest / see sandbox |
Browser | ` | |
Assistive tech | ` | |
Node | n/a | ` |
npm/yarn | ` | |
Operating System | ` |