Repository navigation
Prevent components (with side-effects) from being rendered within hidden collection tree #10944
|
Hey, I'm integrating TanStack Hotkeys in our application for shortcut handling. If a modal / popover is open, we want to prevent shortcuts registered outside to be activated. TanStack provides exclusive scopes for that, which can be rendered as part of the modal / overlay. For popovers (excl. non-modal popovers) we can wrap children within a provider: The issue is that collection components like We could use Is there a better way to handle this, or is making Regards, |
Replies: 2 comments 5 replies
|
You can keep this in the popover wrapper without passing import { useId, useState, type ReactNode } from 'react';
import { Popover, type PopoverProps } from 'react-aria-components';
import { HotkeysProvider } from '@tanstack/react-hotkeys';
type ScopedPopoverProps = Omit<PopoverProps, 'children'> & {
children: ReactNode;
};
function ScopedPopover({ children, ...props }: ScopedPopoverProps) {
const scope = useId();
const [host, setHost] = useState<HTMLElement | null>(null);
return (
<Popover {...props} ref={setHost}>
{({ isExiting }) => (
<HotkeysProvider
scope={scope}
exclusive
enabled={host !== null && !isExiting}
>
{children}
</HotkeysProvider>
)}
</Popover>
);
}Use this wrapper for the popovers that should take over the shortcuts. In the hidden pass, the ListBox can still supply its items, but the provider doesn't activate its scope or enable its hotkeys. When the visible host attaches, only the I tested the Select case in jsdom with |
I would say that this isn't necessarily the answer, we need to make this easier to address. I'm not sure what that looks like yet, whether it's exposing
useIsHiddenpublicly or something else. Can you open an Issue for this?Maybe the answer is just better documentation around it, but I hope we can do something to make it easier than that. I'd love if people didn't need to know about or think about the hidden collection tree