@@ -9,11 +9,11 @@ export type ComputeConfig = Omit<ComputePositionConfig, "platform"> & {
99 onComputed ?: ( computed : ComputePositionReturn ) => void
1010 /**
1111 * false: Don't initialize autoUpdate;
12- * object: Initialization with its own parameters ;
13- * undefined: Standard autoUpdate values from the documentation ;
14- * @default undefined
12+ * true: Standard autoUpdate values from the documentation ;
13+ * object: All as in the autoUpdate documentation. Your parameters are added to the default ones ;
14+ * @default true
1515 */
16- autoUpdate ?: false | Partial < Options >
16+ autoUpdate ?: boolean | Partial < Options >
1717} ;
1818export type UpdatePosition = ( contentOptions ?: Omit < ComputeConfig , 'autoUpdate' > ) => void ;
1919export type ReferenceAction = ( node : HTMLElement ) => void ;
@@ -23,11 +23,18 @@ export type ArrowOptions = { padding?: Padding, element: Writable<HTMLElement> }
2323export function createFloatingActions ( initOptions ?: ComputeConfig ) : [ ReferenceAction , ContentAction , UpdatePosition ] {
2424 let referenceElement : ReferenceElement ;
2525 let floatingElement : FloatingElement ;
26+ const defaultOptions :Partial < ComputeConfig > = {
27+ autoUpdate : true
28+ }
2629 let options : ComputeConfig | undefined = initOptions ;
2730
31+ const getOptions = ( mixin ?:object ) :ComputeConfig => {
32+ return { ...defaultOptions , ...( initOptions || { } ) , ...( mixin || { } ) }
33+ }
34+
2835 const updatePosition :UpdatePosition = ( updateOptions ) => {
2936 if ( referenceElement && floatingElement ) {
30- options = { ... initOptions , ... updateOptions } ;
37+ options = getOptions ( updateOptions ) ;
3138 computePosition ( referenceElement , floatingElement , options )
3239 . then ( v => {
3340 Object . assign ( floatingElement . style , {
@@ -48,15 +55,29 @@ export function createFloatingActions(initOptions?: ComputeConfig): [ReferenceAc
4855 const contentAction : ContentAction = ( node , contentOptions ) => {
4956 let autoUpdateDestroy :ReturnType < typeof _autoUpdate > | undefined
5057 floatingElement = node ;
51- options = { ...initOptions , ...contentOptions } ;
52- updatePosition ( ) ;
53- if ( options . autoUpdate !== false ) {
54- autoUpdateDestroy = _autoUpdate ( referenceElement , floatingElement , ( ) => updatePosition ( ) , options . autoUpdate ) ;
58+ options = getOptions ( contentOptions ) ;
59+ updatePosition ( contentOptions ) ;
60+ const destroyAutoUpdate = ( ) => {
61+ if ( autoUpdateDestroy ) {
62+ autoUpdateDestroy ( )
63+ autoUpdateDestroy = undefined
64+ }
65+ }
66+ const initAutoUpdate = ( { autoUpdate} = options || { } ) :typeof autoUpdateDestroy => {
67+ destroyAutoUpdate ( )
68+ if ( autoUpdate !== false ) {
69+ return _autoUpdate ( referenceElement , floatingElement , ( ) => updatePosition ( options ) , ( autoUpdate === true ? { } : autoUpdate ) ) ;
70+ }
71+ return
5572 }
73+ autoUpdateDestroy = initAutoUpdate ( )
5674 return {
57- update : updatePosition ,
75+ update ( contentOptions :Parameters < typeof contentAction > [ 1 ] ) {
76+ updatePosition ( contentOptions )
77+ autoUpdateDestroy = initAutoUpdate ( contentOptions )
78+ } ,
5879 destroy ( ) {
59- autoUpdateDestroy && autoUpdateDestroy ( )
80+ destroyAutoUpdate ( )
6081 }
6182 }
6283 }
0 commit comments