From 76eaf326c0d7dc24df9ec8931ea73819455c58e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20=C5=A0vara?= Date: Wed, 7 Jan 2026 17:54:52 +0100 Subject: [PATCH] Add bridge buttons controller. It sends all buttons in one connect payload. --- app/assets/stylesheets/native.css | 6 +-- .../controllers/bridge/buttons_controller.js | 38 +++++++++++++++++++ .../bridge/initializers/bridge_element.js | 11 +----- 3 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 app/javascript/bridge/controllers/bridge/buttons_controller.js diff --git a/app/assets/stylesheets/native.css b/app/assets/stylesheets/native.css index 9a60026a7..afa72fc1f 100644 --- a/app/assets/stylesheets/native.css +++ b/app/assets/stylesheets/native.css @@ -52,12 +52,12 @@ } } -[data-bridge-components~=nav-button] { - [data-controller~=bridge--nav-button]:not(summary) { +[data-bridge-components~=buttons] { + [data-bridge--buttons-target~=button]:not(summary) { display: none; } - summary[data-controller~=bridge--nav-button] { + summary[data-bridge--buttons-target~=button] { overflow: hidden; width: 0; height: 0; diff --git a/app/javascript/bridge/controllers/bridge/buttons_controller.js b/app/javascript/bridge/controllers/bridge/buttons_controller.js new file mode 100644 index 000000000..7cafc93fe --- /dev/null +++ b/app/javascript/bridge/controllers/bridge/buttons_controller.js @@ -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() + } +} \ No newline at end of file diff --git a/app/javascript/bridge/initializers/bridge_element.js b/app/javascript/bridge/initializers/bridge_element.js index 7787f8b8a..471ab1aaa 100644 --- a/app/javascript/bridge/initializers/bridge_element.js +++ b/app/javascript/bridge/initializers/bridge_element.js @@ -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" }