Add bridge nav-button controller.

This commit is contained in:
Denis Švara
2026-01-07 09:49:31 +01:00
parent cdc4f2c211
commit 367edfcbac
2 changed files with 54 additions and 0 deletions
@@ -0,0 +1,22 @@
import { BridgeComponent } from "@hotwired/hotwire-native-bridge"
export default class extends BridgeComponent {
static component = "nav-button"
connect() {
super.connect()
if (this.bridgeElement.enabled) {
this.notifyBridgeOfConnect()
}
}
notifyBridgeOfConnect() {
const navButton = this.bridgeElement.getButton()
navButton.displayAsFormSubmitMenu = this.bridgeElement.shouldDisplayAsFormSubmitMenu()
this.send("connect", navButton, () => {
this.element.click()
})
}
}
@@ -11,3 +11,35 @@ BridgeElement.prototype.showOnPlatform = function() {
BridgeElement.prototype.hideOnPlatform = function() {
this.element.classList.add("hide-on-native")
}
BridgeElement.prototype.getButton = function() {
return {
title: this.title,
icon: this.getIcon(),
label: this.getLabel(),
order: this.getOrder()
}
}
BridgeElement.prototype.getIcon = function() {
const name = this.bridgeAttribute("icon-name")
const url = this.bridgeAttribute(`icon-${this.platform}-url`)
if (name || url) {
return { name, url }
}
return null
}
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"
}