summaryrefslogtreecommitdiff
path: root/tests/javascript.js
diff options
context:
space:
mode:
authorTommaso Negri <tommaso@hey.com>2025-12-05 17:50:04 +0100
committerTommaso Negri <tommaso@hey.com>2025-12-05 17:50:04 +0100
commitf9e2bdfd8285107572a356646b0a70d5a03136e7 (patch)
treeacbc8408fbf3cb35940cce8bea59b32cd6dcc435 /tests/javascript.js
parent804891fd726137e024292149bbf67083fd0a422d (diff)
downloadvoltage-f9e2bdfd8285107572a356646b0a70d5a03136e7.tar.gz
voltage-f9e2bdfd8285107572a356646b0a70d5a03136e7.zip
Add test files
Diffstat (limited to 'tests/javascript.js')
-rw-r--r--tests/javascript.js75
1 files changed, 75 insertions, 0 deletions
diff --git a/tests/javascript.js b/tests/javascript.js
new file mode 100644
index 0000000..8a3e8a8
--- /dev/null
+++ b/tests/javascript.js
@@ -0,0 +1,75 @@
+import { Controller } from "@hotwired/stimulus"
+import { computePosition, offset, flip, shift, autoUpdate } from "@floating-ui/dom"
+
+export default class extends Controller {
+ static targets = ["origin", "floatable"]
+
+ static values = {
+ placement: {
+ type: String,
+ default: "bottom-start"
+ },
+ offset: {
+ type: Number,
+ default: 2
+ },
+ padding: {
+ type: Number,
+ default: 16
+ }
+ }
+
+ connect() {
+ this.cleanup = null
+
+ if (this.hasFloatableTarget) {
+ Object.assign(this.floatableTarget.style, {
+ position: "fixed",
+ left: "0",
+ top: "0",
+ })
+ }
+ }
+
+ disconnect() {
+ if (this.cleanup) {
+ this.cleanup()
+ this.cleanup = null
+ }
+ }
+
+ originTargetConnected() {
+ this.arrange()
+ }
+
+ floatableTargetConnected() {
+ this.arrange()
+ }
+
+ arrange() {
+ if (this.hasOriginTarget && this.hasFloatableTarget) {
+ if (this.cleanup) {
+ this.cleanup()
+ this.cleanup = null
+ return
+ }
+
+ this.cleanup = autoUpdate(this.originTarget, this.floatableTarget, () => {
+ computePosition(this.originTarget, this.floatableTarget, {
+ strategy: "fixed",
+ placement: this.placementValue,
+ middleware: [
+ offset(this.offsetValue),
+ flip(),
+ shift({ padding: this.paddingValue }),
+ ]
+ }).then(({x, y }) => {
+ Object.assign(this.floatableTarget.style, {
+ left: `${x}px`,
+ top: `${y}px`,
+ })
+ })
+ })
+ }
+ }
+}