StyleSheet
The createTheme
function creates a theme StyleSheet instance.
It supports light, dark, and system color schemes.
StyleSheet
Creates a new StyleSheet instance and attaches it to the DOM.
Kind: global class
Properties
Name | Type | Description |
---|---|---|
classes | Object |
The classes object. An object with keys as your original class names and values as the generated unique class names. It will be generated by the instance. Use it to get the class name to use in your components. |
styles | Object |
The styles object. The original styles object. See styles . |
uid | Number |
The unique identifier used to generate class names. It will be incremented on each generated class name. |
id | String |
The unique identifier for the stylesheet. It will be used as the style element id. It will be generated by this.generateId or can be set on options.attributes.id . |
attributes | Object |
See options.attributes . |
renderers | Array |
See options.renderers . |
el | HTMLElement |
The style element. A reference to the style element in the DOM. It will be created when the instance is attached. |
String
String
String
StyleSheet
StyleSheet
string
The StyleSheet class receives at the constructor a styles object and an options
object and generate a css StyleSheet.
The StyleSheet can be attached to the DOM, destroyed or rendered as string for
server-side rendering.
Param | Type | Description |
---|---|---|
styles | Object |
The styles object. An object with keys as selectors and values as style objects. This object will pass trough the renderers and generate the css string. It will be added to the instance as this.styles . |
options | Object |
The options object. options.idPrefix , options.generateClassName , options.generateId , options.attributes and options.renderers will be added to the instance. |
options.idPrefix | String |
The generated id prefix. By default, it's StyleSheet.prefix . |
options.generateId | function |
The function to generate ids. This id will be used as the <style> element id. |
options.generateClassName | function |
The function to generate class names. This class name will be used to generate the unique class names for scoped styles. |
options.attributes | Object |
The attributes object. This attributes will be added to the <style> element. |
options.renderers | Array |
The array of renderers. Renderers are functions that transform style objects into CSS strings. When composed, the first renderer receives the styles object, and the final one outputs the resulting CSS string. Elements in the renderers array can be either functions or strings that reference methods of the StyleSheet instance. These methods will be bound to the instance before they are invoked. By default, StyleSheet are rendered using the built-in renderers: ['parseStyles', 'renderStyles'] . |
Example
// Create a new StyleSheet instance with a styles object.
const instance = new StyleSheet({
root : {
color : 'black'
}
});
// Attach the StyleSheet instance to the DOM.
instance.attach();
// Get classes object from the instance.
const { classes } = instance;
// Use the classes object to get the class name and use it in your component.
function Header = () => <h1 className={classes.root}>Hello World</h1>;
String
Generate a unique identifier. Used for the style element id.
May be overridden by options.generateId
.
Kind: instance method of StyleSheet
Returns: String
- The unique identifier.
String
Generate a unique class name.
Transform local selectors that are classes to unique class names
to be used as class names in the styles object.
May be overridden by options.generateClassName
.
Kind: instance method of StyleSheet
Returns: String
- The unique class name.
Param | Type | Description |
---|---|---|
className | String |
The class name. |
String
Render the StyleSheet as a style element string. Used for server-side rendering.
Kind: instance method of StyleSheet
Returns: String
- The instance as a string.
StyleSheet
Add the instance to the registry and if we are in the browser, attach it to the DOM.
Kind: instance method of StyleSheet
Returns: StyleSheet
- The instance.
StyleSheet
Destroy the instance and remove it from the registry and from the DOM, if it's present.
Kind: instance method of StyleSheet
Returns: StyleSheet
- The instance.
Kind: static property of StyleSheet
Default: fun
Properties
Name | Type | Description |
---|---|---|
prefix | String |
The class prefix. Used to generate unique class names. |
Kind: static property of StyleSheet
Default: 4 spaces
Properties
Name | Type | Description |
---|---|---|
indent | String |
The indent string. Used to format text when debug is enabled. |
Kind: static property of StyleSheet
Properties
Name | Type | Description |
---|---|---|
registry | Array |
The registry array. StyleSheet instances will be added to this array. |
Kind: static property of StyleSheet
Properties
Name | Type | Description |
---|---|---|
uid | Number |
The unique identifier counter. It will be incremented for each generated id. |
Kind: static property of StyleSheet
Default: false
If true, the CSS will be formatted with new lines and indents.
Properties
Name | Type | Description |
---|---|---|
debug | Boolean |
The debug flag. |
string
Render all instances in the registry as a string.
Kind: static method of StyleSheet
Returns: string
- All instances in the registry as a string.
Attach all instances in the registry to the DOM.
Kind: static method of StyleSheet
Destroy all instances in the registry and remove them from it and from the DOM.
Kind: static method of StyleSheet
StyleSheet
The createTheme
function creates a theme StyleSheet instance.
It supports light, dark, and system color schemes.
Kind: global function
Returns: StyleSheet
- The theme StyleSheet instance. Use classes.root
to get the theme class name.
Apply it to the element you want to theme. CSS variables will be available for all its descendants.
Param | Type | Description |
---|---|---|
themes | Object |
An object containing light and dark themes: { light, dark } . Each theme object will be converted to CSS variables available under the root class of the theme StyleSheet instance. For example: { backgroundLevel1: 'black' } will be converted to --fun-backgroundLevel1 . You can add the root class to the root element of your component to theme a single component, or to the body element to theme the entire page. |
options | Object |
An options object. |
options.colorScheme | String |
The color scheme. Possible values are light , dark , and system . If light or dark is set, the theme will be fixed to that color scheme, and only the necessary CSS variables will be generated. The CSS color-scheme property will be set to that value. If system is set, the theme will be generated for both light and dark color schemes, and by default, it will follow the system color scheme. The CSS color-scheme property will be set to light or dark accordingly. To override the system color scheme, set the data-color-scheme attribute to light or dark on a parent element. |
options.cssVarsPrefix | String |
The CSS variables prefix. Default is fun . |
options.createStyleSheet | function |
A function used to create a new StyleSheet instance. By default, it uses the css function. |
options.styleSheetOptions | Object |
The options object to be used when creating the StyleSheet instance. Default is system . |
Example
// Create a default theme and apply it to the entire page.
const theme = createTheme({
light : {
color : 'black',
backgroundColor : 'white'
},
dark : {
color : 'white',
backgroundColor : 'black'
}
});
// Add the `root` class (the theme class) to the body element.
// This will apply the theme to the entire page.
document.body.classList.add(theme.classes.root);
// Add some styles using the theme CSS variables.
const { classes } = css({
button : {
color : 'var(--fun-color)',
backgroundColor : 'var(--fun-backgroundColor)'
}
});
// Add the `button` class to a button component.
// You can use the variables in your styles even before the theme is applied or created.
// Your component will update when the theme is applied.
// If the system color scheme changes, the button will update automatically.
const Button = ({ label }) => <button className={classes.button}>{label}</button>;
StyleSheet
Creates a new StyleSheet instance and attaches it to the DOM.
Kind: global function
Returns: StyleSheet
- The StyleSheet instance.
Param | Type | Description |
---|---|---|
styles | Object |
The CSS rules. |
Example
// Create some styles for a link component.
const { classes } = css({
link : {
color : 'blue',
'&:hover' : {
textDecoration : 'underline'
}
}
});
// Create a link component. Add the `link` class to it.
const Link = ({ label, href }) => <a className={classes.link} href={href}>{label}</a>;