Idea
It would be nice to have a page, that renders colors, font sizes etc. to have something like a living styleguide, not only a component library.
This should happen completely by parsing CSS files to avoid any additional work for developers.
See https://holidaypirates.github.io/nucleus/demo e.g.
Implementation
What to include in the styleguide
- colors
- fonts
- font-family
- font-size
- font-weight
- line-height
- text-transform
- letter-spacing
- spacings
How to parse
Custom properties need to be defined in :root {}
and need to start with a prefix like
--color-
--font-family-
--font-size-
--font-weight-
--line-height-
--text-transform-
--letter-spacing-
--spacing-
to be included in the styleguide. For later, customizing these via regexes might be an option, but for a first implementation, this is sufficient.
Everything after the prefix is used as the name, so primary
would be the name for the color --color-primary
.
Open questions
How to deal with media queries
Three options:
- Using the original custom properties
When using the original custom properties, we would simply render some elements and apply the custom properties. When doing that, the user would e.g. have to resize the window, change contrast settings of the OS etc.
This makes the implementation very easy, but it does not give a full overview of the different custom properties. Especially e.g. colors, which can depend on some OS settings (prefers-contrast: high
), would require manual changes by the user to see all variants.
- Displaying the custom properties for each media query as defined
This allows the user to see all variants by listing all media queries like an accordion and inside showing the custom properties, which are defined for that media query. This is easy to implement, but it does not show the user which custom properties are actually used for each media query (as it basically just mirrors the code which requires some mental effort).
- Displaying the custom properties for each media query as computed
This allows the user to see all variants at one glance, but it makes the implementation way more difficult.
Media queries would be listed like an accordion and in each section, we would show the custom properties that would be active when this media query is active. The custom properties would not be used directly, but instead their parsed value.
The difficult part is that custom properties do not exclusively belong to one media query, but can be used by multiple media queries. This is especially complicated when showing fonts, which is a combination of multiple properties. For example, this
:root {
--font-family-body: Helvetica;
--line-height: 1.4;
}
@media (max-width: 32em) {
:root {
--font-size-body: 1.6rem;
}
}
@media (min-width: 32.03125em) {
:root {
--line-height: 1.6;
}
}
@media (min-width: 32.03125em) and (max-width: 64em) {
:root {
--font-size-body: 1.8rem;
}
}
@media (min-width: 64.03125em) {
:root {
--font-size-body: 2rem;
}
}
would have to be displayed like this:
default:
body:
font-family: Helvetica
line-height: 1.4
(max-width: 32em):
body:
font-family: Helvetica
line-height: 1.4
font-size: 1.6rem
(min-width: 32.03125em) and (max-width: 64em):
body:
font-family: Helvetica
line-height: 1.6
font-size: 1.8rem
(min-width: 64.03125em):
body:
font-family: Helvetica
line-height: 1.6
font-size: 2rem
Additionally it could for whatever reason happen that e.g. screen
is used for just one media query. This would have to be considered as well.
Currently tending to solution 2.
How to deal with colors that are e.g. only for backgrounds
Depending on the design system, some colors might only be used for text and some might only be used for backgrounds, borders etc.
When scanning the CSS files for custom properties that start with --color-
, there is no way to know if it should be used for text or backgrounds or both.
Idea:
Custom properties can start with --color-text-
(or simply --color-text
: in that case text
would be the name and it would be clear that it is for text), --color-bg-
, --color-border-etc.. That way it would be clear what it is used for. What happens though if there are also custom properties, that only start with
--color-`? Should they be displayed for text, background, border etc.?
What to do with custom properties that have a custom property as value
Example:
:root {
--color-black: #000;
--color-mediumGrey: #999;
--color-text: var(--color-mediumGrey);
}
@media (prefers-contrast: high) {
--color-text: var(--color-black);
}
Should the custom property --color-text
be ignored as its values have actually already been display before?
If we display it, should it be labelled by the defined value (--color-mediumGrey
and --color-black
) or the computed values (#999
and #000
)?
If we display it, should we use the actual custom property or the computed value to display the color? Probably the computed value, as this would be in sync with how we would most likely deal with media queries.