Skip to content

Commit e0167fa

Browse files
committed
Rough swap of popperjs with floating-ui
#872
1 parent 76dc64b commit e0167fa

File tree

4 files changed

+66
-34
lines changed

4 files changed

+66
-34
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ module.exports = function(grunt) {
8888
files: {
8989
'dist/js/stacks.js': [
9090
'node_modules/stimulus/dist/stimulus.umd.js',
91-
'node_modules/@popperjs/core/dist/umd/popper.js',
91+
'node_modules/@floating-ui/core/dist/floating-ui.core.js',
9292
'build/lib/ts/stacks.js',
9393
'build/lib/ts/controllers/**/*.js',
9494
'build/lib/ts/finalize.js'

lib/ts/controllers/s-popover.ts

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Stacks {
44

55
export abstract class BasePopoverController extends StacksController {
66
// @ts-ignore
7-
private popper!: Popper;
7+
private floatingUI!: FloatingUICore;
88

99
protected popoverElement!: HTMLElement;
1010

@@ -71,7 +71,7 @@ namespace Stacks {
7171
this.validate();
7272
if (this.isVisible) {
7373
// just call initialize here, not show. This keeps already visible popovers from adding/firing document events
74-
this.initializePopper();
74+
this.initializeFloatingUI();
7575
} else if (this.data.get("auto-show") === "true") {
7676
this.show(null);
7777
}
@@ -84,9 +84,9 @@ namespace Stacks {
8484
*/
8585
disconnect() {
8686
this.hide();
87-
if (this.popper) {
88-
this.popper.destroy();
89-
delete this.popper;
87+
if (this.floatingUI) {
88+
this.floatingUI.destroy();
89+
delete this.floatingUI;
9090
}
9191
super.disconnect();
9292
}
@@ -110,8 +110,8 @@ namespace Stacks {
110110
dispatcher: dispatcherElement
111111
}).defaultPrevented) { return; }
112112

113-
if (!this.popper) {
114-
this.initializePopper();
113+
if (!this.floatingUI) {
114+
this.initializeFloatingUI();
115115
}
116116

117117
this.popoverElement!.classList.add("is-visible");
@@ -136,10 +136,10 @@ namespace Stacks {
136136

137137
this.popoverElement.classList.remove("is-visible");
138138

139-
if (this.popper) {
139+
if (this.floatingUI) {
140140
// completely destroy the popper on hide; this is in line with Popper.js's performance recommendations
141-
this.popper.destroy();
142-
delete this.popper;
141+
this.floatingUI.destroy();
142+
delete this.floatingUI;
143143
}
144144

145145
// on first interaction, hide-on-outside-click with value "after-dismissal" reverts to the default behavior
@@ -180,25 +180,29 @@ namespace Stacks {
180180
/**
181181
* Initializes the Popper for this instance
182182
*/
183-
private initializePopper() {
183+
private initializeFloatingUI() {
184184
// @ts-ignore
185-
this.popper = Popper.createPopper(this.referenceElement, this.popoverElement, {
186-
placement: this.data.get("placement") || "bottom",
187-
modifiers: [
188-
{
189-
name: "offset",
190-
options: {
191-
offset: [0, 10], // The entire popover should be 10px away from the element
192-
}
193-
},
194-
{
195-
name: "arrow",
196-
options: {
197-
element: ".s-popover--arrow"
198-
},
199-
},
200-
]
201-
});
185+
// this.floatingUI = FloatingUICore.computePosition(this.refrenceElement, this.popoverElement, {
186+
// // @ts-ignore
187+
// middleware: [ FloatingUICore.offset(), FloatingUICore.flip(), FloatingUICore.shift() ]
188+
// });
189+
// this.floatingUI = FloatingUICore.createPopper(this.referenceElement, this.popoverElement, {
190+
// placement: this.data.get("placement") || "bottom",
191+
// modifiers: [
192+
// {
193+
// name: "offset",
194+
// options: {
195+
// offset: [0, 10], // The entire popover should be 10px away from the element
196+
// }
197+
// },
198+
// {
199+
// name: "arrow",
200+
// options: {
201+
// element: ".s-popover--arrow"
202+
// },
203+
// },
204+
// ]
205+
// });
202206
}
203207

204208
/**
@@ -262,8 +266,8 @@ namespace Stacks {
262266
* Schedules the popover to update on the next animation frame if visible
263267
*/
264268
protected scheduleUpdate() {
265-
if (this.popper && this.isVisible) {
266-
this.popper.update();
269+
if (this.floatingUI && this.isVisible) {
270+
this.floatingUI.update();
267271
}
268272
}
269273
}

package-lock.json

Lines changed: 30 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"license": "MIT",
2121
"dependencies": {
22-
"@popperjs/core": "^2.11.3",
22+
"@floating-ui/dom": "^0.4.1",
2323
"stimulus": "^2.0.0"
2424
},
2525
"devDependencies": {

0 commit comments

Comments
 (0)