Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customisation with options #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

domchristie
Copy link
Contributor

@domchristie domchristie commented May 21, 2021

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
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
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