11/* eslint-disable @typescript-eslint/no-empty-function */
2- import { createPopper } from '@popperjs/core' ;
3- import type {
4- Options as PopperOptions ,
5- Instance as PopperInstance ,
6- } from '@popperjs/core' ;
2+ import { computePosition , autoUpdate , offset } from '@floating-ui/dom' ;
73import type { PopoverOptions } from './types' ;
84import type { InstanceOptions } from '../../dom/types' ;
95import { PopoverInterface } from './interface' ;
@@ -28,7 +24,7 @@ class Popover implements PopoverInterface {
2824 _targetEl : HTMLElement ;
2925 _triggerEl : HTMLElement ;
3026 _options : PopoverOptions ;
31- _popperInstance : PopperInstance ;
27+ _cleanupAutoUpdate : Function ;
3228 _clickOutsideEventListener : EventListenerOrEventListenerObject ;
3329 _keydownEventListener : EventListenerOrEventListenerObject ;
3430 _visible : boolean ;
@@ -48,7 +44,7 @@ class Popover implements PopoverInterface {
4844 this . _targetEl = targetEl ;
4945 this . _triggerEl = triggerEl ;
5046 this . _options = { ...Default , ...options } ;
51- this . _popperInstance = null ;
47+ this . _cleanupAutoUpdate = null ;
5248 this . _visible = false ;
5349 this . _initialized = false ;
5450 this . init ( ) ;
@@ -63,7 +59,6 @@ class Popover implements PopoverInterface {
6359 init ( ) {
6460 if ( this . _triggerEl && this . _targetEl && ! this . _initialized ) {
6561 this . _setupEventListeners ( ) ;
66- this . _popperInstance = this . _createPopperInstance ( ) ;
6762 this . _initialized = true ;
6863 }
6964 }
@@ -89,10 +84,8 @@ class Popover implements PopoverInterface {
8984 // remove event listeners for click outside
9085 this . _removeClickOutsideListener ( ) ;
9186
92- // destroy the Popper instance if you have one (assuming this._popperInstance is the Popper instance)
93- if ( this . _popperInstance ) {
94- this . _popperInstance . destroy ( ) ;
95- }
87+ // stop FloatingUI auto updates
88+ this ?. _cleanupAutoUpdate ( ) ;
9689
9790 this . _initialized = false ;
9891 }
@@ -133,18 +126,13 @@ class Popover implements PopoverInterface {
133126 } ) ;
134127 }
135128
136- _createPopperInstance ( ) {
137- return createPopper ( this . _triggerEl , this . _targetEl , {
138- placement : this . _options . placement ,
139- modifiers : [
140- {
141- name : 'offset' ,
142- options : {
143- offset : [ 0 , this . _options . offset ] ,
144- } ,
145- } ,
146- ] ,
147- } ) ;
129+ _initializeFloatingUI ( ) {
130+ computePosition ( this . _triggerEl , this . _targetEl , {
131+ placement : this . _options . placement ,
132+ middleware : [ offset ( this . _options . offset ) ]
133+ } ) . then ( ( { x, y } ) => {
134+ Object . assign ( this . _targetEl . style , { left : `${ x } px` , top : `${ y } px` } ) ;
135+ } ) ;
148136 }
149137
150138 _getTriggerEvents ( ) {
@@ -241,24 +229,17 @@ class Popover implements PopoverInterface {
241229 this . _targetEl . classList . remove ( 'opacity-0' , 'invisible' ) ;
242230 this . _targetEl . classList . add ( 'opacity-100' , 'visible' ) ;
243231
232+ // Update its position
233+ this . _initializeFloatingUI ( ) ;
244234 // Enable the event listeners
245- this . _popperInstance . setOptions ( ( options : PopperOptions ) => ( {
246- ...options ,
247- modifiers : [
248- ...options . modifiers ,
249- { name : 'eventListeners' , enabled : true } ,
250- ] ,
251- } ) ) ;
235+ this . _cleanupAutoUpdate = autoUpdate ( this . _triggerEl , this . _targetEl , ( ) => { this . _initializeFloatingUI ( ) } ) ;
252236
253237 // handle click outside
254238 this . _setupClickOutsideListener ( ) ;
255239
256240 // handle esc keydown
257241 this . _setupKeydownListener ( ) ;
258242
259- // Update its position
260- this . _popperInstance . update ( ) ;
261-
262243 // set visibility to true
263244 this . _visible = true ;
264245
@@ -271,13 +252,8 @@ class Popover implements PopoverInterface {
271252 this . _targetEl . classList . add ( 'opacity-0' , 'invisible' ) ;
272253
273254 // Disable the event listeners
274- this . _popperInstance . setOptions ( ( options : PopperOptions ) => ( {
275- ...options ,
276- modifiers : [
277- ...options . modifiers ,
278- { name : 'eventListeners' , enabled : false } ,
279- ] ,
280- } ) ) ;
255+ this . _cleanupAutoUpdate ( ) ;
256+ this . _cleanupAutoUpdate = null ;
281257
282258 // handle click outside
283259 this . _removeClickOutsideListener ( ) ;
0 commit comments