-
Notifications
You must be signed in to change notification settings - Fork 97
Introduce a useSingleton hook #133
Conversation
|
What benefits does this have over the filtering technique I mentioned in the issue? e.g. <TippySingleton>
<div />
<Tippy content="tooltip">
<button />
</Tippy>
<div />
<Tippy content="tooltip">
<button />
</Tippy>
<div />
</TippySingleton>// If `content` isn't enough (e.g. other children have this prop),
// fallback to them specifying `singleton` as a prop to make it work
// Or is there a better way?
function isTippyChild(child) {
return child.props.content || child.props.singleton;
}Ok - this hook will also work if one of the Seems necessary to add as a feature. |
|
Yeah, basically this allows us to group tippy instances regardless of where they are in our component tree. In our case, I may be able to restructure things such that they’re all children of the same container, but it’s not convenient to do so. Using the hook means it doesn’t matter where your Tippy components live in your app… |
|
I'm just wondering if we should instead add a render props API to I know the whole purpose of hooks was to reduce nesting in component trees, but in this case, I dislike how there would be "two ways" to do things in terms of importing functionality 😖 |
|
The story on the README might be something like: SingletonWraps the
import Tippy, {TippySingleton} from '@tippy.js/react';
// ...
<TippySingleton delay={500}>
<Tippy content="a">
<button />
</Tippy>
<Tippy content="b">
<button />
</Tippy>
</TippySingleton>
import Tippy, {useSingleton} from '@tippy.js/react';
function App() {
const singleton = useSingleton({delay: 500});
return (
<>
<Tippy content="a" singleton={singleton}>
<button />
</Tippy>
<button />
<div>
<Tippy content="b" singleton={singleton}>
<button />
</Tippy>
</div>
</>
);
} |
|
I did consider using Context, would essentially mean converting |
|
I do also think that offering 2 ways is OK, especially right now, since some people like the hooks API and some prefer classes and render props. Personally, if I never have to deal with another render prop, that will make me very happy :) |
|
I think the hook is fine =] As for tests - the |
|
@atomiks Alright, I replicated all the tests in |
|
Thanks a lot! What is |
|
It’s basically there for testing only. I’m using it to access the singleton instance and the array of instances in the test. It’s not ideal, but it feels necessary. |
Yeah you're right 😳 Maybe it should be underscored and highlighted it's only for testing? |
|
Yeah, maybe I’ll rename it to |
|
Actually, I was able to remove that method entirely and get all the instance references I needed with wrapper components. |
|
Edit: nvm made some tweaks myself This looks good now, thanks a lot for this! |
|
Cool, I like your tweaks to the README! |
|
Thank you for all your hard work on Tippy—and especially the fantastic documentation. It's a great library. |
|
Just realized –– does this hook need types? /cc @KubaJastrz |
This addresses #132 — it hasn't been fully tested, but I just wanted to push up what I have for any feedback/discussion. The basic usage is: