Skip to content

Commit

Permalink
detect if tooltip rendered in react-select menu and change props base…
Browse files Browse the repository at this point in the history
…d on that
  • Loading branch information
KitoC committed Aug 20, 2024
1 parent 701e83b commit e711c06
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
20 changes: 16 additions & 4 deletions lib/components/Popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import { PropTypes } from "prop-types";
import Box from "../Box";
import {
getFloatingUiRootElement,
getFloatingUiZIndex
getFloatingUiZIndex,
isRenderedInReactSelectMenu
} from "../../utils/floatingUiHelpers";

const DIRECTIONS_MAP = {
Expand Down Expand Up @@ -359,6 +360,7 @@ export default function Popover({
{text}
</StyledPopover>
);
const containsLinks = refs.floating?.current?.querySelectorAll("a").length;

return (
<Box {...props} aria-describedby={context.floatingId}>
Expand All @@ -368,9 +370,19 @@ export default function Popover({
root={getFloatingUiRootElement(context.refs.reference)}
preserveTabOrder={true}
>
<FloatingFocusManager context={context} modal={false}>
{Popover}
</FloatingFocusManager>
{containsLinks ? (
<FloatingFocusManager
context={context}
modal={false}
initialFocus={
isRenderedInReactSelectMenu(context.refs.reference) && -1
}
>
{Popover}
</FloatingFocusManager>
) : (
Popover
)}
</FloatingPortal>
) : (
Popover
Expand Down
15 changes: 14 additions & 1 deletion lib/utils/floatingUiHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const getFloatingUiZIndex = (triggerRef) => {
return 900;
};

const REACT_SELECT_MENU = "react-select-menu";

const isRenderedInReactSelectMenu = (triggerRef) =>
document.getElementById(REACT_SELECT_MENU)?.contains?.(triggerRef.current);

const getFloatingUiRootElement = (triggerRef) => {
const activeModalRef = document.getElementById("modal-overlay");

Expand All @@ -21,7 +26,15 @@ const getFloatingUiRootElement = (triggerRef) => {
return document.getElementById("modal");
}

if (isRenderedInReactSelectMenu(triggerRef)) {
return document.getElementById(REACT_SELECT_MENU).firstChild;
}

return undefined;
};

export { getFloatingUiZIndex, getFloatingUiRootElement };
export {
getFloatingUiZIndex,
getFloatingUiRootElement,
isRenderedInReactSelectMenu
};

0 comments on commit e711c06

Please sign in to comment.