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

Default type for polymorphic slots #2174

Open
nc163 opened this issue Dec 2, 2024 · 3 comments
Open

Default type for polymorphic slots #2174

nc163 opened this issue Dec 2, 2024 · 3 comments

Comments

@nc163
Copy link

nc163 commented Dec 2, 2024

Feature request

I would like to use polymorphic slots in the following way:

class ListItemComponent < ViewComponent::Base
  renders_one :visual, types: {
    _: AvatarComponent,  # <- default type
    icon: IconComponent,
  }
end
<%=
render ListItemComponent.new do |component|
  component.with_visual() 
  # or component.with_visual_icon
end
%>

Motivation

Currently, We have to call it in the following way:

<%=
render ListItemComponent.new do |component|
  component.with_visual__
end
%>

I would prefer being able to call the default slot type simply as component.with_visual

Additionally, it might depend on how components are structured, but I often use inheritance in my components.
In this case, having a default slot would make it simpler to write:

class Footer < ViewComponent::Base
class Footer::Admin < Footer

...

renders_one :footer, types: {
  _: Footer,
  admin: Footer::Admin
}

...

component.with_footer  # <- For general pages
component.with_footer_admin  # <- For dashboards
@reeganviljoen
Copy link
Collaborator

I like the idea of a default polymorphic slot, since we do now have default slots aswell, I don't like the idea of using _ as ten way to denote a default polymorphic slot though, I think it would be better using the default keyword

All that said, if anyone wants to pick this up it would be great, unless, @joelhawksley, @camertron, @BlakeWilliams, @boardfish have any other ideas or reservations about this

@BlakeWilliams
Copy link
Contributor

I like the idea of a default polymorphic slot, since we do now have default slots aswell, I don't like the idea of using _ as ten way to denote a default polymorphic slot though, I think it would be better using the default keyword

I would avoid _ since that typically means "unused", and default is much more clear about intent/behavior. I'm open to the idea of having a default here, but I don't have strong opinions on the right API yet. I think having a special default could make sense, but I could also see having a default argument (although that might get confusing due to slots having a default implementation).

@boardfish
Copy link
Collaborator

boardfish commented Dec 4, 2024

Yeah, I suppose default_type would help to differentiate from supplying default as a potential polymorphic slot type.

I would also consider how this overlaps with SlotableDefault — I haven't fully formed my thoughts around what the language should be here but it feels as though default as a term could mean a lot of different things in a lot of different contexts.

We could probably avoid having to think about that by changing the interface to slots, but that's a lot of potential upheaval and I'm not sure how many problems it'd solve relative to those it'd create. It occurred to me just now we could do something like:

renders_one :visual, IconComponent,
  with_variants: ->(component) do
    component.or_variant(:avatar, AvatarComponent)
    # you could call or_variant with all the same arguments as renders_one
  end

To have variants as they exist now (ie no "default" variant), you'd just drop the second argument (IconComponent).

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

No branches or pull requests

4 participants