Repository navigation
Expose the resolved side of Popper-based content to JS
#4169
Replies: 1 comment
|
I don't think there's a supported JS API for this yet. Even re-declaring What works today without relying on private props is reading import * as React from 'react';
type Side = 'top' | 'right' | 'bottom' | 'left';
export function usePlacedSide(ref: React.RefObject<HTMLElement | null>) {
const subscribe = React.useCallback(
(onChange: () => void) => {
const content = ref.current?.closest('[data-side]');
if (!content) return () => {};
const observer = new MutationObserver(onChange);
observer.observe(content, { attributes: true, attributeFilter: ['data-side'] });
return () => observer.disconnect();
},
[ref],
);
return React.useSyncExternalStore(
subscribe,
() => (ref.current?.closest('[data-side]')?.getAttribute('data-side') ?? undefined) as Side | undefined,
() => undefined,
);
}Then inside the content: function Options() {
const ref = React.useRef<HTMLDivElement>(null);
const side = usePlacedSide(ref);
const searchRow = <SearchRow key="search" />;
const list = <OptionList key="list" />;
return <div ref={ref}>{side === 'top' ? [list, searchRow] : [searchRow, list]}</div>;
}Since the DOM order actually changes, ArrowUp/ArrowDown and Tab follow it. The reason it subscribes instead of just reading the attribute during render: when I checked this with A |
Uh oh!
There was an error while loading. Please reload this page.
Feature request
Overview
There is no public way to read the resolved
sideof Popper-based content in JS.data-sidecovers styling, but not logic that depends on the placement.PopperContentProps.onPlacedfires exactly when the placement settles, but the wrappers omit it from their types (popover.tsx:399,tooltip.tsx:488,hover-card.tsx:234,menu.tsx:357), and Select names itSelectPopperPrivateProps(select.tsx:611) — so it is clearly meant to stay internal. At runtime it still arrives via the props spread (popover.tsx:502), whichmeans re-declaring the prop locally works, but that relies on behaviour you never promised.
Two ways I could see this exposed:
onPlacedSideChange?: (side: Side) => voidusePopperSide()hookThe hook would suit my case better, since the value is usually consumed by a child of the content rather than by the content element itself.
Examples in other libraries
Floating UI, which Popper wraps, returns the resolved
placementas a value fromuseFloating()— Popper destructures it itself inpopper.tsx:228. Popper.js exposes the same throughusePopper()'sstate.placement. In both cases the placement is available to the consumer, not only as a DOM attribute.Who does this impact? Who is this for?
Anyone whose content has to be arranged differently depending on the side it opened on. My case is a dropdown anchored to an input, where the search / "create entry" row should stay next to that input: below the options when the content opens upwards, above them otherwise.
CSS
orderis not sufficient there, because keyboard navigation follows the DOM order. Reordering only visually makes ArrowUp/ArrowDown and Tab move in the wrong direction, so the content has to be rendered in a different order — which needs the side as a value.Additional context
If there is a supported way to do this that I have missed, I would be glad to be pointed at it.
@radix-ui/react-popover1.1.23,@radix-ui/react-popper1.3.7, React 19All reactions