1- import { isBrowser , updateClassName } from './utils' ;
2- import { useLayoutEffect , useEffect , useRef } from 'react' ;
3- import { createSingleton } from 'tippy.js' ;
4-
5- export const useIsomorphicLayoutEffect = isBrowser
6- ? useLayoutEffect
7- : useEffect ;
8-
9- export function useUpdateClassName ( component , className , childrenDep ) {
10- useIsomorphicLayoutEffect ( ( ) => {
11- const { tooltip} = component . instance . popperChildren ;
12- if ( className ) {
13- updateClassName ( tooltip , 'add' , className ) ;
14- return ( ) => {
15- updateClassName ( tooltip , 'remove' , className ) ;
16- } ;
17- }
18- } , [ className , childrenDep ] ) ;
19- }
20-
21- export function useInstance ( initialValue ) {
22- // Using refs instead of state as it's recommended to not store imperative
23- // values in state due to memory problems in React(?)
24- const ref = useRef ( ) ;
25-
26- if ( ! ref . current ) {
27- ref . current =
28- typeof initialValue === 'function' ? initialValue ( ) : initialValue ;
29- }
30-
31- return ref . current ;
32- }
1+ import {
2+ useInstance ,
3+ useSingletonCreate ,
4+ useSingletonUpdate ,
5+ useUpdateClassName ,
6+ } from './util-hooks' ;
337
348export function useSingleton ( {
359 className,
3610 plugins,
11+ enabled = true ,
3712 ignoreAttributes = true ,
3813 ...restOfNativeProps
3914} = { } ) {
@@ -48,28 +23,11 @@ export function useSingleton({
4823 ...restOfNativeProps ,
4924 } ;
5025
51- useIsomorphicLayoutEffect ( ( ) => {
52- const { instances} = component ;
53- const instance = createSingleton ( instances , props , plugins ) ;
54-
55- component . instance = instance ;
56-
57- return ( ) => {
58- instance . destroy ( ) ;
59- component . instances = instances . filter ( i => ! i . state . isDestroyed ) ;
60- } ;
61- } , [ component . instances . length ] ) ;
62-
63- useIsomorphicLayoutEffect ( ( ) => {
64- if ( component . renders === 1 ) {
65- component . renders ++ ;
66- return ;
67- }
68-
69- component . instance . setProps ( props ) ;
70- } ) ;
26+ const deps = [ component . instances . length ] ;
7127
72- useUpdateClassName ( component , className , component . instances . length ) ;
28+ useSingletonCreate ( component , props , plugins , enabled , deps ) ;
29+ useSingletonUpdate ( component , props , enabled ) ;
30+ useUpdateClassName ( component , className , deps ) ;
7331
7432 return instance => {
7533 component . instances . push ( instance ) ;
0 commit comments