devnotes: add section for managing css

This commit is contained in:
thislight 2025-01-16 20:39:41 +08:00
parent b6fbe71a51
commit 8854a3b86a
No known key found for this signature in database
GPG key ID: FCFE5192241CCD4E

View file

@ -84,3 +84,52 @@ But, sometimes you need a redesigned (sometimes better) tool for the generic usa
- *What* this new tool does?
- *How* this tool works?
- Clean up code regularly. Don't keep the unused code forever.
## Managing CSS
Two techniques are still:
- Styled compoenent (solid-styled)
- Native CSS with CSS layering
The second is recommended for massive use. A stylesheet for a component can be placed alongside
the component's file. The stylesheet must use the same name as the component's file name, but replace the extension with
`.css`. Say there is a component file "PreviewCard.tsx", the corresponding stylesheet is "PreviewCard.css". They are imported
by the component file, so the side effect will be applied by the bundler.
The speicifc component uses a root class to scope the rulesets' scope. This convention allows the component's style can be influenced
by the other stylesheets. It works because Tutu is an end-user application, we gain the control of all stylesheets in the app (kind of).
Styled component is still existing for its own good: decalrable and scoped by randomized names.
Though styled component, using attributes for scoping, may not be as performant as the techniques with CSS class names;
It's still provided in the Tutu's code infrastructure for its ease (it even provides a bridge to use js variable in css!).
The following is an example of the recommended usage of solid-styled:
```tsx
// An example of using solid-styled
import { css } from "solid-styled";
import { createSignal } from "solid-js";
const Component = () => {
const [width, setWidth] = createSignal(100);
css`
.root {
width: ${width()}%;
}
`
return <div class="root"></div>
};
```
Native CSS is always recommended. When developing new component, you can use styled component first, and migrate
to native css slowly.
Before v2.0.0, there are CSS modules in use, but they are removed:
- Duplicated loads
- Unaware of order (failed composing)
- Not-ready for hot reload
In short, CSS module does not works well if the stylesheet will be accessed from more than one component.