Prefixes
Last updated
Last updated
You may have noticed that we are using prefixes from the previous code exemples.
The prefix is composed of two letters and followed by a dash : .pr-class-name
.
For instance, we will use the letter g
that stand for Garden, and tell us that the class come from the Garden design system. Another reason to use this source ID is that if anybody chose to use the same naming conventions that are described in this documentation, we need a differentiator.
This will make our two cohabiting code source collision proof.
lets' review our architecture :
All the elements within the layout to the utilities layers will have class names. Each of witch will take the second letter as a class type identifier in the prefix :
So for instance, if you are creating a layout class for a drop down, you should call your block entity .gl-drop-down
. You can read it "garden layout drop down".
There is also two other types of prefixes :
The JS hooks are selectors that are used only by javascript in order to select elements.
Those classes should not have any styles, and be used only in the markup as a mean to select one or more elements.
They can be IDs or classes depending on wether or not you need to select one or multiple elements.
State classes are classes that are generally added via javascript. It is important to not mistake modifiers and states. A modifier is a variation of an entity where a state is a behavior that may change during the user's experience.
Note that state classes should never be used alone, but always in association with their related entity.
State classes should use an Auxiliary verbs as prefix.
Let's see some exemples of state classes used with their related entity.
Themes classes :
Themes classes are used to create multiple themes. This is not very often that you should use theme and they are only meant to create aesthetic variations of your design like a dark mode.
Do not use themes only has a mean to override the Garden library in your project.
We will see how it should be done later in.
To create themes you need to override your main theme using a theme classe.
Scope classes :
Scope classes are used for markups you don't have control over. (only)
For exemple, if you have a WYSIWYG editor that output markup with classes that you don't have control over, use a Scope to style them :
Or a date-picker library :
From this typing naming conventions, you can :
Know immediately where the classe is declared
All theses conventions for the classes names can be parsed , that mean that we will be able to build a custom linter tool that understand every types of classes, apply specific rules to them, and redirect you to the specific doc when warning you. Awesome right ?
Side note: Some time ago I started to develop a PostCSS / Stylelint library for that purpose. Maybe I will found enough time to finish it, and if any body want to help me, he is welcome :)
For more informations, you can and should definitely read this awesome article by Harry Roberts