Stylesheets
Mayu uses Lightning CSS to parse, transform and minify CSS. Lightning CSS automatically compiles many modern CSS syntax features to more compatible output. Read more in their documentation
You can write CSS directly inside components. Class and tag-selectors are scoped to components, so you don’t have to worry about naming.
The stylesheet for each component will become a separate file, and the filename will be based on the content hash.
This means that stylesheets can be cached in the browser very easily, so that thay only will be downloaded when the stylesheet has updated.
Examples
The following example shows how tag-selectors get their class names.
%button Click me
:css
button { color: #f0f; }
This will be transformed into the following Ruby code:
public def render
Mayu::VDOM::H[:button, "Click me", class: styles[:__button]]
end
And generate the following output:
<button class="/app/components/Button_button?abc123">Click me</button>
You can also use class names:
%p.text Hello world
:css
.text { color: #f0f; }
But usually you don’t have to bother with class names. If you keep your components small you can more easily target elements by their names.
:ruby
Button = import("/app/components/Button")
%article
%Button Click me
:css
article {
border: 1px solid #f0f;
}
Button {
background: #0f0;
}
This also works with components, as long as they pass
the class
-prop to to any of their elements.
The following component would, used in the previous example,
get the generated class name for Button
.
%button(class=$class)
%slot
Conditional styles
You can pass symbols, strings, arrays and hashes in the class
-prop.
This works similarly to the classnames
library for JS.
Here is an example of a <li>
-tag that applies the .active
-class
if the active
-prop is truthy.
%li{class: { active: $active }}
%slot
:css
li { opacity: .5; }
.active { opacity: 1; }
Behind the scenes, this uses the styles
-object.
You probably don’t have to use it often,
but it can be really useful sometimes.
styles[:button]
#=> "/app/components/MyComponent.button"
styles[:button, active: false]
#=> "/app/components/MyComponent.button"
styles[:button, active: true]
#=> "/app/components/MyComponent.button /app/components/MyComponent.active"
styles[:button, "foobar"]
#=> "/app/components/MyComponent.button foobar"
Root stylesheet
You can write styles for the root document in ´app/root.css´. This is a good place to set up CSS variables and global overrides for your document. Example