nice_partials icon indicating copy to clipboard operation
nice_partials copied to clipboard

Customisation with options

Open domchristie opened this issue 3 years ago • 2 comments

Closes #13

This pull request adds a simple customisation enhancement. It allows Nice Partials to provide a default options object to the parent view via np, and made accesible from p.options. This can be modified by the parent view as needed, effectively allowing it to "reach in" and modify the internals of the partial for a given case.

For example, classes can be modified for that occasional case that needs different: in this case, adding rounded-none and removing rounded:

# app/views/posts/index.html.erb
<%= render 'components/card', class: 'rounded-none' do |p| %>
  <% p.options[:class].delete('rounded') %>
  …
<% end %>
# app/views/components/_card.html.erb
<% yield p = np(class: ['card', 'rounded', local_assigns[:class]]) %>
<div class="<%= token_list(p.options[:class]) %>">
  …
</div>

What's happening here?

  1. The card partial sets up the default options hash with class names, merging in those passed in as locals.
  2. The index template passes in rounded-noneas a local, then deletes the rounded class
  3. The card partial sets the class attribute with the now-modified options[:class] using token_list in Rails 6.1

domchristie avatar May 21 '21 18:05 domchristie