Kind: global class
Properties
| Name | Type | Description |
|---|---|---|
| classes | Object |
Object mapping original class names to generated unique class names. |
| styles | Object |
The original styles object provided to the instance. |
| uid | String |
Unique identifier for the StyleSheet instance, generated using this.generateUid. |
| prefix | String |
Prefix for generating unique identifiers. Set via options or subclass. |
| attributes | Object |
Attributes to be added to the <style> element. Set via options or subclass. |
| renderers | Array |
Array of renderer functions or method names used to process the styles object. Set via options or subclass. |
| el | HTMLElement |
Reference to the <style> element in the DOM. Created when the instance is attached to the DOM. |
StringStringStringStringBooleanStyleSheetStyleSheetstringstringThe StyleSheet class is responsible for creating and managing a CSS stylesheet. It takes a styles object and an optional options object as input, processes the styles, and generates a CSS stylesheet that can be attached to the DOM, destroyed, or rendered as a string for server-side rendering.
| Param | Type | Default | Description |
|---|---|---|---|
| styles | Object |
The styles object. This is an object where keys represent CSS selectors and values are style objects. The styles object is processed through the renderers to generate the final CSS string. It is stored in the instance as this.styles. |
|
| [options] | Object |
{} |
Configuration options. The following options are assigned to the instance (this): prefix, generateUid, generateClassName, shouldAttachToDOM, attributes, renderers. |
| [options.prefix] | String |
'fun' |
Prefix for generating unique identifiers and data attributes. |
| [options.generateUid] | function |
Custom function to generate the unique identifier. | |
| [options.generateClassName] | function |
Custom function to generate unique class names. | |
| [options.attributes] | Object |
Attributes to be added to the <style> element. |
|
| [options.renderers] | Array |
['parseStyles', 'renderStyles'] |
Array of renderer functions or method names. Renderers are composed in sequence. Strings or functions are automatically bound to this. |
| [options.shouldAttachToDOM] | function |
Custom function to determine whether the StyleSheet should be added to the DOM. |
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();
// Retrieve the generated classes object from the instance.
const { classes } = instance;
// Use the generated class name in your component.
function Header() {
return <h1 className={classes.root}>Hello World</h1>;
}
StringGenerate a stable unique identifier.
May be overridden by options.generateUid.
Kind: instance method of StyleSheet
Returns: String - The unique identifier.
StringGenerate 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 or by extending the class.
Kind: instance method of StyleSheet
Returns: String - The unique class name.
| Param | Type | Description |
|---|---|---|
| className | String |
The class name. |
| index | Number |
The index of the class name. |
StringApply the renderers to the styles object. It will return a string ready to be added to the style element.
Kind: instance method of StyleSheet
Returns: String - The styles object as a string.
StringRender the StyleSheet as a style element string. Used for server-side rendering.
Kind: instance method of StyleSheet
Returns: String - The instance as a string.
BooleanCheck if the StyleSheet should be added to the DOM.
By default, it returns true if running in a browser environment and no style element
with the same data-fun-uid attribute exists in the DOM.
This prevents duplicate style elements and ensures proper behavior for server-side rendering.
May be overridden by options.shouldAttachToDOM.
Kind: instance method of StyleSheet
Returns: Boolean - True if the StyleSheet should be added to the DOM, false otherwise.
StyleSheetAdd 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.
StyleSheetDestroy 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: ' '
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
Default: __DEV__
Properties
| Name | Type | Description |
|---|---|---|
| debug | Boolean |
The debug flag. If true, the styles will be formatted with indentation and new lines. |
stringRender all instances in the registry as a string, including the style tags. Can be used to insert style tags in an HTML template for server-side rendering.
Kind: static method of StyleSheet
Returns: string - All instances in the registry as a string.
stringRender all instances in the registry as CSS string. Can be used to generate an external CSS file.
Kind: static method of StyleSheet
Returns: string - All instances in the registry rendered as CSS string.
Destroy all instances in the registry and remove them from it and from the DOM.
Kind: static method of StyleSheet
StyleSheetThe createTheme function generates a theme StyleSheet instance with CSS variables
based on the provided themes and options. It supports multiple color schemes,
including light, dark, light dark, and normal.
The themes object defines the styles for these color schemes. Each key in the object
corresponds to a color scheme (light, dark, normal), and its value is an object
containing key-value pairs that will be converted into CSS variables. Nested keys are
concatenated with - to form the variable name. For example, { light : { colors : { primary : 'blue' } } }
generates --fun-colors-primary : blue.
Kind: global function
Returns: StyleSheet - The theme StyleSheet instance. Use classes.root to get the theme class name.
Apply this class to the element you want to theme. The CSS variables will be available for all
its descendants.
| Param | Type | Description |
|---|---|---|
| themes | Object |
An object defining styles for color schemes (light, dark, normal). Each key corresponds to a color scheme, and its value is an object of key-value pairs converted to CSS variables. Nested keys are concatenated with - to form variable names. |
| [options] | Object |
An optional object to customize the theme generation. It includes options for selecting color schemes, customizing CSS variable prefixes, and controlling StyleSheet creation. |
| [options.colorScheme] | String |
Specifies the color scheme(s) to use. Possible values are: light (uses the light theme only), dark (uses the dark theme only), light dark (default, supports both light and dark themes, adapting to system preferences; can override system preference with data-color-scheme set to light or dark), and normal (uses the normal theme only). |
| [options.cssVarsPrefix] | String |
The prefix for the generated CSS variables. Default is fun. For example, a key color in the theme will generate a CSS variable like --fun-color. |
| [options.createStyleSheet] | function |
A function used to create a new StyleSheet instance. By default, it uses the css function. |
| [options.styleSheetOptions] | Object |
Options to pass when creating the StyleSheet instance. Default is system. |
Example
// Create a theme with light and dark color schemes and apply it to the entire page.
const theme = createTheme({
light : {
colorPrimary : 'black',
backgroundLevel1 : 'white'
},
dark : {
colorPrimary : 'white',
backgroundLevel1 : '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-colorPrimary)', // Use the CSS variable generated from the theme.
backgroundColor : 'var(--fun-backgroundLevel1)'
}
});
// Add the `button` class to a button component.
// The button will use the CSS variables defined in the theme for its styles.
// Once the theme is applied, the button will automatically update its styles.
// If the system color scheme changes (e.g., from light to dark), the button will
// dynamically update to reflect the new theme without requiring additional code.
const Button = ({ label }) => <button className={classes.button}>{label}</button>;
StyleSheetCreates and attaches a new StyleSheet instance to the DOM.
Kind: global function
Returns: StyleSheet - The created StyleSheet instance. Use the classes property to access the generated class names.
| Param | Type | Description |
|---|---|---|
| styles | Object |
An object containing CSS rules. Keys represent selectors, and values represent style objects. |
| [options] | Object |
Optional configuration for the StyleSheet instance. Includes options like prefix, renderers, and more. |
Example
// Create styles for a link component.
const { classes } = css({
link : {
color : 'blue',
'&:hover' : {
textDecoration : 'underline'
}
}
});
// Use the generated `link` class in a component.
const Link = ({ label, href }) => <a className={classes.link} href={href}>{label}</a>;