StyleSheet
The 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
.
StyleSheet
Creates and attaches a new StyleSheet instance to the DOM.
Kind: global class
Properties
Name | Type | Description |
---|---|---|
classes | Object |
An object mapping the original class names to the generated unique class names. Use this to reference the generated class names in your components. |
styles | Object |
The original styles object provided to the instance. |
uid | String |
A unique identifier for the StyleSheet instance, generated using this.generateUid . |
attributes | Object |
The attributes object, derived from options.attributes , to be added to the <style> element. |
renderers | Array |
The array of renderer functions or method names used to process the styles object. |
el | HTMLElement |
A reference to the <style> element in the DOM. This is created when the instance is attached to the DOM. |
String
String
String
String
Boolean
StyleSheet
StyleSheet
string
The 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 |
{} |
Optional configuration options for the StyleSheet instance. |
[options.prefix] | String |
'fun' |
A prefix used for generating unique identifiers and data attributes. |
[options.generateUid] | function |
A custom function to generate the unique identifier for the StyleSheet instance. | |
[options.generateClassName] | function |
A custom function to generate unique class names for scoped styles. | |
[options.attributes] | Object |
An object containing attributes to be added to the <style> element in the DOM. |
|
[options.renderers] | Array |
['parseStyles', 'renderStyles'] |
An array of renderer functions or method names. The renderers are composed in sequence, where the first receives the styles object, and the last outputs the final CSS string. Strings or functions will be automatically bound to this . |
[options.shouldAttachToDOM] | function |
A 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>;
}
String
Generate a stable unique identifier.
May be overridden by options.generateUid
.
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
Apply 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.
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.
Boolean
Check 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.
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: ' '
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: false
Properties
Name | Type | Description |
---|---|---|
debug | Boolean |
The debug flag. If true, the styles will be formatted with indentation and new lines. |
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.
Destroy all instances in the registry and remove them from it and from the DOM.
Kind: static method of StyleSheet
StyleSheet
The 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>;
StyleSheet
Creates 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>;