Module: NfgUi::Components::Traits::Button

Included in:
Elements::Button
Defined in:
lib/nfg_ui/components/traits/button.rb

Overview

Access to pre-designed Button traits

Constant Summary collapse

TRAITS =
%i[block
close
link
outlined
remote
submit].freeze

Instance Method Summary collapse

Instance Method Details

#block_traitObject



39
40
41
# File 'lib/nfg_ui/components/traits/button.rb', line 39

def block_trait
  options[:block] = true
end

#close_traitObject



43
44
45
46
47
48
49
50
# File 'lib/nfg_ui/components/traits/button.rb', line 43

def close_trait
  self.as = :button
  data[:dismiss] = options.delete(:dismiss)
  options[:theme] = nil
  @css_classes = 'close'
  @body = '×'.html_safe
  assistive_html_attributes.merge!(aria: { label: 'close' })
end

Link trait has some customizations because it outputs a button without button styles (useful for spacing) which makes it feel like the following SHOULD work:

ui.nfg :button, :link, :danger, icon: ‘trash-o’

Where it generates a link themed button that also has a color style.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nfg_ui/components/traits/button.rb', line 21

def link_trait
  colors = NfgUi::Components::Traits::Theme::COLOR_TRAITS

  # Check if we've got any color themes passed through in addition to :link
  if traits.collect { |t| t.in?(colors) }.any? && traits.include?(:link)
    # Grab and remove the theme color from traits so it
    # doesn't override the :link theme
    # and so that we can apply it to the css.
    theme_color = traits.delete((traits & colors).first)

    # Add the text styling to the button's CSS
    options[:class] += " text-#{theme_color}"
  end

  # And finally, set the official theme as a link
  options[:theme] = :link
end

#outlined_traitObject



61
62
63
# File 'lib/nfg_ui/components/traits/button.rb', line 61

def outlined_trait
  options[:outlined] = true
end

#remote_traitObject



52
53
54
# File 'lib/nfg_ui/components/traits/button.rb', line 52

def remote_trait
  options[:remote] = true
end

#submit_traitObject



56
57
58
59
# File 'lib/nfg_ui/components/traits/button.rb', line 56

def submit_trait
  self.as = :button
  options[:type] = 'submit'
end