Closed jarcoal closed 1 year ago
Hi @jarcoal, the ref must be defined on the <Menu.Button>
:
const Opener: React.FC<OpenerProps> = React.forwardRef<
HTMLDivElement,
OpenerProps
>(({ children }, ref) => (
<HeadlessMenu.Button as={React.Fragment} ref={ref}>
<children.type {...children.props} />
</HeadlessMenu.Button>
))
Thanks for the reply @ycs77 . I should've mentioned that I tried that and received this Typescript error related to the ref prop:
Type '{ children: ReactElement<any, string | JSXElementConstructor<any>> & ReactNode; as: ExoticComponent<{ children?: ReactNode; }>; ref: ForwardedRef<...>; }' is not assignable to type 'IntrinsicAttributes & Props<ExoticComponent<{ children?: ReactNode; }>, ButtonRenderPropArg, ButtonPropsWeControl>'.
Property 'ref' does not exist on type 'IntrinsicAttributes & Props<ExoticComponent<{ children?: ReactNode; }>, ButtonRenderPropArg, ButtonPropsWeControl>'.ts(2322)
I'm assuming the type HTMLDivElement
needs to be changed to something else, I'm just not sure what it should be...
The button DOM type is HTMLButtonElement
, this will be clear the error:
const Opener: React.FC<OpenerProps> = React.forwardRef<
HTMLButtonElement,
OpenerProps
>(({ children }, ref) => (
<HeadlessMenu.Button as={React.Fragment} ref={ref}>
<children.type {...children.props} />
</HeadlessMenu.Button>
))
Same error:
Type '{ children: ReactElement<any, string | JSXElementConstructor<any>> & ReactNode; as: ExoticComponent<{ children?: ReactNode; }>; ref: ForwardedRef<...>; }' is not assignable to type 'IntrinsicAttributes & Props<ExoticComponent<{ children?: ReactNode; }>, ButtonRenderPropArg, ButtonPropsWeControl>'.
Property 'ref' does not exist on type 'IntrinsicAttributes & Props<ExoticComponent<{ children?: ReactNode; }>, ButtonRenderPropArg, ButtonPropsWeControl>'.ts(2322)
Maybe it's an issue with Headless-UI?
For now I've created a work-around, so I'm all set, just wanted to see if there was a quick-fix that I was missing. Thanks again for your help.
Use Version Use version when question appear:
Describe the question
I'm trying to make a reusable wrapper around the Headless UI's
Menu.Button
component, but for some reason when I use it, Float doesn't seem able to position theMenu.Items
correctly and it just goes to the top left of the screen instead of next to the button itself.Here's what I'm using:
If I change:
to:
it works, but for my use-case I really want to use a Fragment there instead.
Any thoughts on what I could do to use a Fragment but still get the correct positioning? I thought by passing the ref down into the children that would do it, but apparently not.
In case it helps, here's what I have for my
Float
component (again this is inside a custom reusable component):