-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tooltip to strict on checking children #2451
Comments
I suggest we change the check to: if (
!Children.only(children) ||
children?.type === Fragment ||
Children.count(children) > 1
) {
console.error(
'<Tooltip> children needs to be a single ReactElement and not: <Fragment/> | <></>',
);
return null;
} If this fails we could probably rely on the internal error that is thrown |
See your suggestion, but this will break if passing nested Fragments for instance passing:
I suggest we therefore do the check on render, as this is makes us 100% sure of the resulting DOM:
|
What do you mean with nested fragments? <Tooltip content='content'>
<>
<p>hei</p>
</>
</Tooltip> |
I tried doing this with the check we have today, and the tooltip is rendering fine. const Group = forwardRef<HTMLDivElement, { children: React.ReactNode }>(
({ children }, ref) => {
return <div ref={ref}>{children}</div>;
},
);
...
<Tooltip content='content'>
<Group>
<p>hei</p>
</Group>
</Tooltip> EDIT: const Group = forwardRef<HTMLDivElement, { children: React.ReactNode }>(
({ children }, ref) => {
return (
<>
<div ref={ref}>{children}</div>
</>
);
},
); |
Both of these will work fine ☝️ But thats only because Group renders a div. If Group renders only a Fragment, it will not work. Therefore I sugges checking the actually rendered DOM :) |
If the group only renders a fragment the tooltip can't be rendered, so I am still not following the issue here. Could you send a snippet of your group? |
It's also not documented in storybook, since nothing is documented (😓 ), but the component you pass as a child needs to be able to recieve a ref |
Precisely; we want to log an error if the consumer is using Tooltip wrong.
|
Right, so our Tooltip does not support any element that can not receive a ref, because the wrapping element itself is a clone and not a
making it consistent with other components, and allowing:
...if for some reason needing to wrap a tooltip around non-phrasing content? :) |
I am pretty sure this will not work either. Take a look at our article about |
We do not need a ref on the trigger itself then- since we know the trigger will always render a element with this approach, the ref can be placed on |
This component was made before we started using Ideally we would do things react knows about, and to my knowledge, using |
Partly agree; should we provide Though, doing |
Description of the bug
Reported by team in Mattilsynet;
Vi har denne Tooltip varianten
Dette treffer det du la til på Tooltip
Dersom vi bytter
<Group>
med<div>
går det fint, eller wrapper<Group>
med<div>
, men det som er rart er at<Group>
selv returnerer en<div>
😅Suggestion:
Less strict
children
checking in component, but rather warning added in auseEffect
where we can inspect the actual amount of rendered DOM children.The text was updated successfully, but these errors were encountered: