Slots #656
stephannv
started this conversation in
Component architecture
Slots
#656
Replies: 1 comment
-
I created Phlex::Slotable: class MyComponent < Phlex::HTML
include Phlex::Slotable
# Generic slot
slot :content
# Component slot
slot :header, Header
# Slot collection
slot :item, Item, collection: true
# Lambda slot
slot :footer, ->(**html_options, &content) { footer(**html_options, &content) }
# Polymorphic slot
slot :avatar, types: { icon: Icon, image: Image }
def template
render content_slot if content_slot?
render header_slot if header_slot?
render item_slots.each { |s| render s } if item_slots?
render footer_slot if footer_slot?
render avatar_slot if avatar_slot?
end
end
MyComponent.new.call do |c|
c.with_content { "Generic slot" }
c.with_header(size: :lg) { "Component slot" }
c.with_item(href: "/a") { "Item A" }
c.with_item(href: "/a") { "Item A" }
c.with_footer(class: "bg-red") { "Lambda slot" }
if image_url
c.with_image_avatar(src: image_url)
else
c.with_icon_avatar(name: :user)
end
end The code powering this is here: https://github.com/stephannv/phlex-slotable/blob/main/lib/phlex/slotable.rb I'm welcoming suggestions =). If you think this can be useful for phlex repo/org, let me know. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I was thinking if a Slot API for Phlex is a wanted feature. References:
Phlex deferred render is powerful and phlexible, but sometimes we want a rigid structure for our components and we end up writing the same code structure multiple times. So I created a custom module to register slot and I was thinking if it could fit on Phlex or it should be a separated gem.
I was experimenting with some ideas and I could do something like this:
Using this component:
Things I didn't think yet:
slot :button, ->(&content) { render(ButtonComponent.new(color: primary, size: :lg), &content) }
slot :avatar, icon: IconComponent, image: ImageComponent
)If this is a wanted feature, I could open a PR to discuss the implementation.
Beta Was this translation helpful? Give feedback.
All reactions