-
Notifications
You must be signed in to change notification settings - Fork 16
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
Content area options #4
Comments
Somewhat related: looks like combining/merging DOM token lists (and therefore some |
@domchristie I don't think we necessarily need a new option to accomplish the outcome desired above. For example, my first instinct would be to try and accomplish the same thing just using locals, like so:
And the partial would be something like:
In practice this has actually worked pretty well for me. In particular, you have a lot of control within the partial itself what you want to do with the locals that are passed in. You can, for example, merge them into a default version of a hash that's defined in the partial. You can overwrite the default values. You can append to the default values (which works well for utility classes, for example). I've got a bunch of examples of different things that can be accomplished from the Bullet Train code base and other projects I've worked on. I'll try to find a moment to put them into the documentation. Happy to continue discussing, but generally my instinct here is for folks to accomplish as much as possible using the native approach they would have taken with partials in the first place. |
(Please let me know if there is an angle I'm missing here.) |
I'm a bit torn. On the one hand, using locals requires no extra code, and it keeps the library code nice and clean; on the other, we're polluting <%= render 'section', data: { component: 'chat', props: { id: 1 } }, toolbar: { class: 'toolbar--chat', data: { component: 'toolbar', props: { actions: […] }.to_json } } do |p| %>
<%# … %>
<%# … %>
<%# … %>
<%# … %>
<%# … %>
<%# … %>
<% p.content_for :toolbar do %>
<%# … %>
<% end %>
<% end %> versus: <%= render 'section', data: { component: 'chat', props: { id: 1 } } do |p| %>
<%# … %>
<%# … %>
<%# … %>
<%# … %>
<%# … %>
<%# … %>
<% p.content_for :toolbar, class: 'toolbar--chat', data: { component: 'toolbar', props: { actions: […] }.to_json } do %>
<%# … %>
<% end %>
<% end %> Inside the partial, we'll have just as much control over the options since it's just whatever was passed in to Perhaps there's a better API for this that doesn't move away from the elegance of just wrapping |
Yeah, I see your point. I'll keep thinking on this one, too. |
I should add, I'm looking into migrating from a hand-rolled component framework to Nice Partials, and stress-testing it on a layout component. The very shortened code I'm testing looks like: <%= section data: { component: '…' } do |s| %>
<%= s.toolbar class: '…' do %>
<%# … %>
<% end %>
<% end %> Which would ideally convert to something like: <%= render 'section', data: { component: '…' } do |p| %>
<%= p.content_for :toolbar, class: '…' do %>
<%# … %>
<% end %>
<% end %> |
@domchristie Not sure how I didn't grok this the first time I looked at it, but this is actually fantastic. I totally get this now. It's just local locals, but for the named content areas/slots. Do you have a PR by chance? |
No PR atm, but I've had a quick play with callbacks. It still feels Rails-y. This is what it could look like: class ApplicationPartial < NicePartials::Partial
before_capture :set_content_options
def options_for(name)
content_options[name_for(name)] || {}
end
private
def content_options
@content_options ||= {}
end
def set_content_options
content_options[name_for(current_name)] = current_options
end
end |
Obviously it would require overriding |
@domchristie Hey, sorry this took me so long to circle back around on, but I'm convinced you were right about this. For example, we might have a content area on a component that defaults to having padding, but we want to allow downstream component users to specify that the default padding should be disabled, e.g.:
|
No worries! Overriding component styles it something I've been mulling over (and I think is related to: #13). |
When rendering component-like partials, it'd be nice to customise element attributes within the partial, not just the content. For example, let's say we have a
section
partial, with a<header>
element, and we can set its content viap.content_for :header
. What if we want to customise the<header>
element itself in a particular situation? A Rails-y API might look likeWith this API, we'd need to have some kind of name-spaced store for the options passed into
content_for
, and a method to access them. Perhaps something likep.options_for(:name)
:I think this could be accomplished with something like:
This expands the API beyond the current mirrored approach, but thought I'd throw it out there and see what you make of it.
The text was updated successfully, but these errors were encountered: