ViewComponentHelper
is a simple gem designed to enhance the functionality provided by ViewComponent. It offers shortcuts to access generated ViewComponents, making your Rails development experience smoother and more efficient.
Install the gem and add to the application's Gemfile by executing:
$ bundle add view_component_helper
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install view_component_helper
To use ViewComponent, you'll need to follow the example below. By utilizing this gem, you can make calls like the following:
# Before
<%= render(MessageComponent.new(name: "World")) %>
# After
<%= render_vc("messageComponent", name: "World") %>
or
<%= vc("messageComponent", name: "World") %>
For more information on ViewComponents, please refer to the official documentation.
ViewComponentHelper
allows for easier referencing of ViewComponent
classes located under the app/components
directory by providing you with intuitive shortcut methods.
For every ViewComponent
you define within the app/components
directory, ViewComponentHelper
automatically provides a shortcut method to render it.
- For a component located at
app/components/title_component.rb
:
You can utilize the following shortcut:
title_component(title: "test title")
- For a component located at
app/components/button.rb
:
<%= button %>
- For a component located at
app/components/dialog/component.rb
<%= dialog_component do |c| %>
<% c.with_title { "dialog title" } %>
<% c.with_body do %>
<p>this is dialog body</p>
<% end %>
<div>content</div>
<% end %>
Bug reports and pull requests are welcome on GitHub at https://github.com/yapr/view_component_helper.
The gem is available as open source under the terms of the MIT License.