It is recommended to always add cursive
or other fallback font at the end of the list, which could be useful for non-Latin (such as CJK) environments, for example browser translation.
Theme
Roughness has a built-in set of colors and supports dark and light themes.
Colors
In dark and light mode, in addition to the foreground and background colors, the theme colors are also different. The built-in color themes use One Light and One Dark color schemes.
Fonts
You can load one or more fonts of your choice by any means and specify the font name(s) with the custom property.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fontsource/cabin-sketch/latin.css">
<style>
:root {
--r-common-font-family: 'Cabin Sketch', cursive;
}
</style>
Customizing Theme
Most components support specific CSS custom properties. You can find them in the documentation related to components, such as Button Styles.
Generally, components use CSS properties with names in the format --R-*
internally, but you should avoid modifying these properties; instead, these properties can be controlled through properties with the same name in lowercase:
/* Render thicker borders for all buttons */
:root {
--r-button-border-width: 2px;
}
Styles
All elements with r-*
classes support the following style variables:
Name | Type | Default Value | Description |
---|---|---|---|
--r-element-font-family |
|
| Font family of the element. |
--r-element-font-size |
|
| Font size of the element. |
--r-element-line-height |
|
| Line height of the element. |
These are only valid for Roughness elements. If you want it to take effect for the entire page, you can define the following styles:
body {
font-family: var(--r-element-font-family);
font-size: var(--r-element-font-size);
line-height: var(--r-element-line-height);
}
The following properties (of colors) are effective for the text content:
Name | Type | Default Value | Description |
---|---|---|---|
--r-element-color |
|
| Color of the element. Usually it will be changed by the See also Color Styles. |
Common style properties are declared under the root node. Changing them will affect all components.
Name | Type | Default Value | Description |
---|---|---|---|
--r-common-font-family |
|
| Font family of components. |
--r-common-font-size |
|
| Font size of components. |
--r-common-small-font-size |
|
| Font size of components with Some browsers (such as Chrome on PC devices) have a minimum font size ( |
--r-common-large-font-size |
|
| Font size of components with |
--r-common-line-height |
|
| Line height of components. |
--r-common-box-padding-block |
|
| Vertical padding of components with rectangular boxes. Such as Button, or cells of Table. |
--r-common-box-padding-inline |
|
| Horizontal padding of components with rectangular boxes. Such as Button, or cells of Table. |
--r-common-overlay-z-index |
|
| Z-order of components at the top of the page. Such as Propover, or dropdown of Select. |
The following properties (of colors) change with dark/light theme changes:
Name | Type | Default Value | Description |
---|---|---|---|
--r-common-color |
|
| Color of foreground content such as text and borders. This is only valid for Roughness components. If you want it to take effect for the entire page, you can define the following styles: css
|
--r-common-background-color |
|
| Color of background content such as backdrop and text stroke. This is only valid for Roughness components. If you want it to take effect for the entire page, you can define the following styles: css
|
--r-common-primary-color |
|
| Color of the key content on the page. Components with |
--r-common-info-color |
|
| Color of auxiliary information on the page. Components with |
--r-common-success-color |
|
| Color of success message. Components with |
--r-common-warning-color |
|
| Color of warning message. Components with |
--r-common-error-color |
|
| Color of error message. Components with |
--r-common-comment-color |
|
| Color of comment message. Components with |
Dark Mode
When the document's root element (usually html
) has a class
of dark
, components will automatically switch to dark mode.
We also provide useDark
function to control dark mode through script:
import { useDark } from 'roughness'
const dark = useDark()
// Toggle dark mode
dark.value = !dark.value
Theming Custom Components
Sometimes you may want customize styles with the theme colors of Roughness components. You can use the Roughness common style properties via CSS var()
:
.my-element {
color: var(--r-common-primary-color);
}
For situations where CSS cannot be used, such as canvas, you can use useColors
to get the colors in current common properties:
import { useColors } from 'roughness'
const colors = useColors()
const context = canvas.getContext('2d')
context.stroke = colors.value.primaryColor
Alternatively, you can directly use colors from light or dark theme:
import { darkColors, lightColors } from 'roughness'
const context = canvas.getContext('2d')
context.stroke = mix(lightColors.primaryColor, darkColors.primaryColor, 0.5)