Can we rely on the private @_parent?
#510
Replies: 2 comments 1 reply
|
Hey @ianterrell, the I think this use-case makes a lot of sense. I would definitely consider caching the theme at each layer with def theme
if defined?(@theme)
@theme
else
@theme = @_parent&.theme
end
endI would also skip the class attribute entirely. You can specify a theme with a single-line method definition like this instead. def theme = :fooIf |
For sure! That was silly of me. :)
Definitely on the I didn't dig into it, but it seemed that the layout was outside of this rendering tree; I was hoping to put a theme in the layout at first. Danke! |
Uh oh!
There was an error while loading. Please reload this page.
I've been playing with Phlex and enjoying it! It's really great to see innovation in this space.
One thing I'm trying to do is theme components. With a lot of CSS that is possible fairly easily with wrapping classes, but I'm using Tailwind with a custom version of something like
class_variantsto pick which classes to use amongst various styles. One component, many configurable styles — sometimes grouped in an overarching theme.I wondered if I could inspect the view hierarchy, and digging through the source I noticed a helpful looking
@_parentin the private part of the API...https://github.com/joeldrapper/phlex/blob/0481870c9267139dec91a3245d2dc95084e71700/lib/phlex/sgml.rb#L47
With that I can set up something that will look for a theme along the rendered view hierarchy via ActiveSupport's
class_attribute:And then use it something like this:
Now views can specify a theme and all the components get rendered with the appropriate styles since they can traverse up the hierarchy.
It may be a terrible idea to be doing this, but it seems to work and completely change the look of a page with just a line! I like that part of it.
Does anyone have any thoughts? Is the view render tree parentage something that could be moved into a supported public API?
All reactions