Skip to content

Customisation with options#14

Open
domchristie wants to merge 1 commit into
bullet-train-co:mainfrom
domchristie:options
Open

Customisation with options#14
domchristie wants to merge 1 commit into
bullet-train-co:mainfrom
domchristie:options

Conversation

@domchristie

@domchristie domchristie commented May 21, 2021

Copy link
Copy Markdown
Contributor

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

@andrewculver

Copy link
Copy Markdown
Contributor

@domchristie OK, sat with this a bit and I think this gets merged. Give me a little bit to find some time to really play with it. I have a couple ideas for syntactic sugar here that might be nice.

@domchristie

Copy link
Copy Markdown
Contributor Author

Cool. I'm still having a play with it myself (in combination with #9). I'll hopefully be able to detail a real use case soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Modifying HTML attributes within a Nice Partial

2 participants