module NfgUi::Components::Utilities::Patches::IntegratedSlatAction
Terminology: SlatAction - is the equivalent of a dropdown menu item SlatActions - is the parent / wrapping component of the SlatAction(s)
and… Integrated Slat Action essentially replaces a dropdown toggle & dropdown menu with either a text link or a button component.
Usage: When you are creating a slat that does not need a menu but has a singular action link For example: when the only option you have on a slat is to delete that item.
This module allows you to pass in options that will automatically generate a slat action for you … without having to manually add a slat_action to your code.
Example usage:
ui.nfg :slat_actions, menu: false, icon: 'trash', body: 'Delete', href: '#nogo'¶ ↑
The above will automatically generate a slat action so you do not need to add a slat action in the slat_actions (note: plural slat_actions)
This is a monkeypatch while we re-evaluate the Slat component suite so as not to break existing code implementation on our apps.
Public Instance Methods
Passes in the rails confirm option to the slat action component. Rails example: link_to 'Delete', …, confirm: 'Are you sure?'
# File lib/nfg_ui/components/utilities/patches/integrated_slat_action.rb, line 50 def confirm options.fetch(:confirm, nil) end
Passes the :disable_with option, when present, to the integrated slat action component – this adds the rails disable_with
Rails example: link_to 'Delete', …, disable_with
: 'Deleting…'
# File lib/nfg_ui/components/utilities/patches/integrated_slat_action.rb, line 57 def disable_with options.fetch(:disable_with, nil) end
Sets a fallback for href so that integrated action items are still correctly styled
# File lib/nfg_ui/components/utilities/patches/integrated_slat_action.rb, line 64 def href super || '#' end
Passes in the rails confirm option to the slat action component. Rails example: link_to 'Update', …, method: :patch
# File lib/nfg_ui/components/utilities/patches/integrated_slat_action.rb, line 70 def method options.fetch(:method, nil) end
Allow for outline settings to be added Assumes true if `button: true` per design system style guide expectations.
# File lib/nfg_ui/components/utilities/patches/integrated_slat_action.rb, line 77 def outlined options[:outlined] || button end
Passes in the rails :remote option to the slat action component Rails example: link_to 'Get Started', remote: true
# File lib/nfg_ui/components/utilities/patches/integrated_slat_action.rb, line 83 def remote options.fetch(:remote, nil) end
Render the component!
# File lib/nfg_ui/components/utilities/patches/integrated_slat_action.rb, line 88 def render_integrated_slat_action button ? render_button : render_link end
Passes the standard `:theme` option to the integrated Slat Action
When the slat action component is a `button` (`button: true`): The integrated slat action will default to the `:secondary` theme per the design system style guide.
The default `:secondary` theme can be manually overridden by passing in a `:theme` trait or `:theme` option to the parent SlatActions component.
# File lib/nfg_ui/components/utilities/patches/integrated_slat_action.rb, line 100 def theme options.fetch(:theme, nil) end
Private Instance Methods
Determines if the slat action component is a delete link Evaluated by examining the :method option
This is used to determine where to inject the `delete` HTML data attribute (data-method) and value ('delete') in the integrated slat action's HTML options
# File lib/nfg_ui/components/utilities/patches/integrated_slat_action.rb, line 123 def delete_link? @delete_link ||= options.fetch(:method, nil) == :delete end
Given the complexity of the data-attributes, this isolates them into their own method for ease of following what's going on.
Essentially, since the :delete method is actually converted to a data-attribute in rails, we have to manually merge the `data-method='delete'` into the data attributes hash
This brings the whole data-attributes suite together and optionally merges in the data-method='delete' if this is a delete button/link.
# File lib/nfg_ui/components/utilities/patches/integrated_slat_action.rb, line 158 def integrated_slat_action_html_data_attributes mergable_delete_link = delete_link? ? { method: :delete } : {} { disable_with: disable_with, confirm: confirm }.merge(mergable_delete_link) end
The HTML options that are passed into the manually created slat action component For instances where you are bypassing the manual slat action component by leveraging the
# File lib/nfg_ui/components/utilities/patches/integrated_slat_action.rb, line 167 def integrated_slat_action_html_options { href: href, class: integrated_slat_action_link_css_classes, method: send(:method), remote: remote, data: integrated_slat_action_html_data_attributes } end
Supplies the SlatActions with the appropriate css classes for the action link when the SlatActions is used to bypass writing a manual slat action component This is complicated, and thus… This is why we are creating a patching module – so that we can later rip this stuff out.
# File lib/nfg_ui/components/utilities/patches/integrated_slat_action.rb, line 111 def integrated_slat_action_link_css_classes [ ("text-#{theme}" if theme), 'd-block' ].join(' ').squish end
These options are removed from attributes because they are picked up and placed on the SlatActions and not on the integrated *slat action component*
NfgUi::Components::Utilities::Iconable#non_html_attribute_options
# File lib/nfg_ui/components/utilities/patches/integrated_slat_action.rb, line 178 def non_html_attribute_options super.push(:button, :confirm, :disable_with, :href, :method, :remote, :theme) end
When `:button` is `false`, a link is rendered
# File lib/nfg_ui/components/utilities/patches/integrated_slat_action.rb, line 188 def render_link content_tag(:a, **integrated_slat_action_html_options) do if icon NfgUi::Components::Foundations::Icon.new({ traits: [icon], text: (block_given? ? yield : body), theme: theme }, view_context).render else (block_given? ? yield : body) end end end