Styles
A CAITS app writes its styles in SCSS files, apart from the HTML. Vite compiles them: there is nothing to set up.
Three kinds of stylesheets
| Stylesheet | Where | Imported by | Loaded by |
|---|---|---|---|
| The app's | src/ | The layout | Every page of the layout |
| A page's | Next to the page: src/ | The page | This page only |
| A view's or a component's | In its folder: presentation/ | The view, or the component | Each page that shows it |
A page imports its stylesheet like a module: import "./home-page.scss";. The build makes one CSS file for the layout, and one per page, with the stylesheets of the views it shows.
The app's stylesheet
The stylesheets of the routes
src/routes/_frontend/
+layout.marko import "./style/app.scss";
+page.marko import "./home-page.scss";
home-page.scss @use "values"; then @media (max-width: values.$narrow) { … }
style/
app.scss the fonts, then @use "theme"; @use "elements"; @use "page";
_values.scss the Sass values: $page, $narrow, the mixins
_theme.scss the colors and the fonts, as CSS variables
_elements.scss the HTML elements
_page.scss what every page has- A partial is a file whose name starts with
_. Its CSS goes into the stylesheet that loads it, at the place of its@use: the order of the@useinapp.scssis the order of the CSS. - Every stylesheet of the routes loads the partials of
style/by their name, from any folder:@use "values". - Put the Sass values in a partial that gives no CSS, such as
_values.scss: a page that loads a partial with CSS repeats that CSS. - Use
@use, not@import, which Sass is removing.
Name the classes
Every class is global, so each class of a page, a view or a component starts with its name (the BEM convention):
MessageBoard.scss
.message-board { // the block: the view
&__composer { … } // .message-board__composer: a part of the view
&__message { … }
}The classes that every page uses, such as button, live in the partials of style/. Keyframes are global too: @keyframes message-board-enter. A .module.scss file works as well, with names unique to the file, but TypeScript doesn't check them.
Fonts
Install the fonts with npm, from Fontsource for example, and load them in app.scss. The build copies their files into the app: the pages load nothing from another site.
style/app.scss
@use "@fontsource-variable/geist";
@use "@fontsource/instrument-serif/400-italic.css" as instrument-serif-italic;Sass names a module after its file: when that isn't a valid name (400-italic), give it another with as.
What the rules check
- A stylesheet of the routes is in
style/, next to a page or a layout, or next to a component incomponent/. A view or a component of a context keeps its stylesheet in its own folder. - A stylesheet never goes up a folder (
../), and a context never loads a stylesheet of the routes. The shortcuts (#context/) don't work in a stylesheet: Sass reads#as the start of a fragment.
npm run lint:architecture
src/frontend/messages/presentation/view/message-board/MessageBoard.scss:1 [contexts-without-routes] This stylesheet loads src/routes/_frontend/style/_values.scss, a stylesheet of the routes, but the routes use the contexts, never the other way. Read the values of the theme as CSS variables instead.
1 architecture violation.The unit tests don't compile the styles. The end-to-end tests run on the build, with its CSS.