Class: RubyNative::Helper::NavbarBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_native/helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ NavbarBuilder

Returns a new instance of NavbarBuilder.



105
106
107
108
# File 'lib/ruby_native/helper.rb', line 105

def initialize(context)
  @context = context
  @items = []
end

Instance Method Details

#button(title = nil, icon: nil, icons: nil, href: nil, click: nil, position: :trailing, selected: false, &block) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ruby_native/helper.rb', line 110

def button(title = nil, icon: nil, icons: nil, href: nil, click: nil, position: :trailing, selected: false, &block)
  resolved = RubyNative::Helper.resolve_icon(icon: icon, icons: icons, platform: @context.try(:native_platform))
  data = { native_button: "" }
  data[:native_title] = title if title
  data[:native_icon] = resolved if resolved
  data[:native_href] = href if href
  data[:native_click] = click if click
  data[:native_position] = position.to_s
  data[:native_selected] = "" if selected

  if block
    menu = NavbarMenuBuilder.new(@context)
    @context.capture(menu, &block)
    @items << @context.tag.div(data: data) { menu.to_html }
  else
    @items << @context.tag.div(data: data)
  end
end

#share_button(url: nil, title: "Share", icon: "square.and.arrow.up", icons: nil, position: :trailing) ⇒ Object

Adds a native share button to the nav bar. Tapping it opens the platform share sheet for ‘url:` (defaults to the current page on the web side) so it works even where embedded web views don’t support ‘navigator.share`. Icons follow the same rules as the other navbar buttons: `icon:` applies to every platform and `icons:` ({ ios:, android: }) overrides per platform.



135
136
137
138
139
140
141
142
143
# File 'lib/ruby_native/helper.rb', line 135

def share_button(url: nil, title: "Share", icon: "square.and.arrow.up", icons: nil, position: :trailing)
  resolved = RubyNative::Helper.resolve_icon(icon: icon, icons: icons, platform: @context.try(:native_platform))
  data = { native_button: "", native_share: "" }
  data[:native_title] = title if title
  data[:native_icon] = resolved if resolved
  data[:native_position] = position.to_s
  data[:native_share_url] = url if url
  @items << @context.tag.div(data: data)
end

#submit_button(title: "Save", click: "[type='submit']") ⇒ Object



145
146
147
148
149
150
151
# File 'lib/ruby_native/helper.rb', line 145

def submit_button(title: "Save", click: "[type='submit']")
  @items << @context.tag.div(data: {
    native_submit_button: "",
    native_title: title,
    native_click: click
  })
end

#to_htmlObject



153
154
155
# File 'lib/ruby_native/helper.rb', line 153

def to_html
  @context.safe_join(@items)
end