You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow overriding slot and content methods on a view component with the ability to call super to reference the default implementation. This allows for some nice customization on how to set content or slot content, and follows a pattern seen in ActiveRecord and other gems.
I am happy to work on this feature given some time.
Motivation
In some cases, I want to support developers defining content that will be used in a slot multiple ways. For example, a modal component might have a simple string title, or it might be richer html
= render Modal.new(title: "oh no")
= render Modal.new do |modal|
modal.with_title do
... more html with images, links
Allowing both forms retains flexibility when its needed but simplicity when it's not, reducing the barrier for other devs to use this component.
This would work if I could override title and title? like so:
This is similar to what you can do with ActiveRecord attribute methods, as the attribute setters and getters are defined on a module that is included my the class. For example,
However, in Slottable, we define the methods with_foo directly on the component itself, which means if we override title as I attempted above, there is no super. In my specific example:
If we defined the slot and content methods on an included module, similar to ActiveRecord and Mongoid, the override would be possible, and at no cost to existing functionality or simplicity to ViewComponent users.
The text was updated successfully, but these errors were encountered:
ozydingo
changed the title
Define slot methods ina module so they can be override and references with super
Define slot methods in a module so they can be override and references with superMay 16, 2024
Feature request
Allow overriding slot and content methods on a view component with the ability to call
super
to reference the default implementation. This allows for some nice customization on how to set content or slot content, and follows a pattern seen in ActiveRecord and other gems.I am happy to work on this feature given some time.
Motivation
In some cases, I want to support developers defining content that will be used in a slot multiple ways. For example, a modal component might have a simple string title, or it might be richer html
= render Modal.new(title: "oh no")
= render Modal.new do |modal| modal.with_title do ... more html with images, links
Allowing both forms retains flexibility when its needed but simplicity when it's not, reducing the barrier for other devs to use this component.
This would work if I could override
title
andtitle?
like so:This is similar to what you can do with ActiveRecord attribute methods, as the attribute setters and getters are defined on a module that is included my the class. For example,
The mongoid gem works similarly:
However, in Slottable, we define the methods
with_foo
directly on the component itself, which means if we overridetitle
as I attempted above, there is nosuper
. In my specific example:If we defined the slot and content methods on an included module, similar to ActiveRecord and Mongoid, the override would be possible, and at no cost to existing functionality or simplicity to ViewComponent users.
The text was updated successfully, but these errors were encountered: