-
Notifications
You must be signed in to change notification settings - Fork 0
/
setCssVar.ts
19 lines (18 loc) · 965 Bytes
/
setCssVar.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { isPlainObject } from '@utopia-utils/share'
/**
* The function sets CSS variables on the root element of a document using the provided variables
* object.
* @param variables - The `variables` parameter is an object that contains key-value pairs representing
* CSS variable names and their corresponding values.
* @param root - The `root` parameter is an optional parameter that represents the root element where
* the CSS variables should be set. By default, it is set to `window?.document?.documentElement`, which
* refers to the root element of the current document.
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/setCssVar.ts
*/
export function setCssVar(variables: Record<string, string | null>, root = window?.document?.documentElement): void {
if (variables && isPlainObject(variables) && root) {
Object.keys(variables).forEach((key) => {
root.style.setProperty(key, variables[key])
})
}
}