How do you handle component CSS and Interactivity? #534
-
One of the features React, Svelte, and other framework provide is localization of CSS and JS interaction. My ideal situation would be that a file would have all the css (scoped to the component) js (same) for a specific component. Images I guess could be in a folder named the same as the component but that might again be out of the phlex scope. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
One option is to use CSS frameworks like UnoCSS or Tailwind and JavaScript frameworks like Stimulus or Alpine that allow you to do most of this in your HTML. Another option is to use the Evil Martians approach, where you keep your CSS and JavaScript in the same folder as the component. They wrote about how they do this with ViewComponent components, but it’s all applicable to Phlex. https://evilmartians.com/chronicles/viewcomponent-in-the-wild-supercharging-your-components Others have built extensions on Phlex to support CSS Modules. I think @joelmoss has been working on something like this. It's not something I see Phlex addressing directly in the near future. One thing I am keeping an eye on is declarative shadow DOM. https://developer.chrome.com/en/articles/declarative-shadow-dom/ DSD allows you to declare custom elements with shadow DOM in pure HTML. Once this has better browser support, I will explore some kind of Another thing worth mentioning is new CSS features like container queries and CSS nesting make writing CSS for components much easier. |
Beta Was this translation helpful? Give feedback.
-
In case it helps anyone else, currently Here's a redacted example of a recent component to illustrate class DataGridComponent < ApplicationComponent
def view_template
article do
header do
yield if block_given?
end
table class: :striped do
# table markup here
end
script do
<<~JS
(function() {
const article = document.currentScript.parentNode
console.log(article)
// add extra interactivity here
})()
JS
end
style do
<<~CSS
@scope {
:scope {
/* article styles go here */
table {
/* fancy table styles go here */
}
}
}
CSS
end
end
end
end |
Beta Was this translation helpful? Give feedback.
One option is to use CSS frameworks like UnoCSS or Tailwind and JavaScript frameworks like Stimulus or Alpine that allow you to do most of this in your HTML.
Another option is to use the Evil Martians approach, where you keep your CSS and JavaScript in the same folder as the component. They wrote about how they do this with ViewComponent components, but it’s all applicable to Phlex. https://evilmartians.com/chronicles/viewcomponent-in-the-wild-supercharging-your-components
Others have built extensions on Phlex to support CSS Modules. I think @joelmoss has been working on something like this.
It's not something I see Phlex addressing directly in the near future. One thing I am keeping an e…