Add bridge buttons controller.

It sends all buttons in one connect payload.
This commit is contained in:
Denis Švara
2026-01-07 17:54:52 +01:00
parent dc9c544408
commit 76eaf326c0
3 changed files with 42 additions and 13 deletions
@@ -0,0 +1,38 @@
import { BridgeComponent } from "@hotwired/hotwire-native-bridge"
import { BridgeElement } from "@hotwired/hotwire-native-bridge"
export default class extends BridgeComponent {
static component = "buttons"
static targets = [ "button" ]
connect() {
super.connect()
if (!this.hasButtonTarget) return
this.notifyBridgeOfConnect()
}
disconnect() {
super.disconnect()
this.notifyBridgeOfDisconnect()
}
notifyBridgeOfConnect() {
const buttons = this.buttonTargets.map((target, index) => {
const element = new BridgeElement(target)
return { ...element.getButton(), index }
})
this.send("connect", { buttons }, message => {
this.activateButton(message)
})
}
notifyBridgeOfDisconnect() {
this.send("disconnect")
}
activateButton(message) {
const selectedIndex = message.data.selectedIndex
this.buttonTargets[selectedIndex].click()
}
}
@@ -16,8 +16,7 @@ BridgeElement.prototype.getButton = function() {
return {
title: this.title,
icon: this.getIcon(),
label: this.getLabel(),
order: this.getOrder()
label: this.getLabel()
}
}
@@ -36,14 +35,6 @@ BridgeElement.prototype.getLabel = function() {
return this.bridgeAttribute("label")
}
BridgeElement.prototype.getOrder = function() {
return this.bridgeAttribute("order") || "1"
}
BridgeElement.prototype.shouldDisplayAsFormSubmitMenu = function() {
return this.bridgeAttribute("display-as-form-submit-menu") === "true"
}
BridgeElement.prototype.displaysNavButtonMenu = function() {
return this.bridgeAttribute("displays-nav-button-menu") === "true"
}