-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
912 additions
and
795 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { setCssVar } from './setCssVar' | ||
|
||
describe('setCssVar', () => { | ||
it('should set CSS variables on the root element of a document using the provided variables object', () => { | ||
const root = document.createElement('div') | ||
const variables = { | ||
'--color': 'red', | ||
'--size': '10px', | ||
} | ||
|
||
setCssVar(variables, root) | ||
|
||
expect(root.style.getPropertyValue('--color')).toBe('red') | ||
expect(root.style.getPropertyValue('--size')).toBe('10px') | ||
}) | ||
|
||
it('should set CSS variables on the root element of a document using the provided variables object', () => { | ||
const variables = { | ||
'--color': 'red', | ||
'--size': '10px', | ||
} | ||
|
||
setCssVar(variables) | ||
|
||
expect(window.document.documentElement.style.getPropertyValue('--color')).toBe('red') | ||
expect(window.document.documentElement.style.getPropertyValue('--size')).toBe('10px') | ||
}) | ||
|
||
it('should not set CSS variables on the root element of a document if the provided variables object is not a plain object', () => { | ||
const root = document.createElement('div') | ||
const variables = null | ||
|
||
// @ts-expect-error for test | ||
setCssVar(variables, root) | ||
|
||
expect(root.style.getPropertyValue('--color')).toBe('') | ||
expect(root.style.getPropertyValue('--size')).toBe('') | ||
}) | ||
|
||
it('should not set CSS variables on the root element of a document if the provided variables object is not a plain object', () => { | ||
const variables = null | ||
|
||
// @ts-expect-error for test | ||
setCssVar(variables) | ||
|
||
expect(document.documentElement.style.getPropertyValue('--color')).toBe('') | ||
expect(document.documentElement.style.getPropertyValue('--size')).toBe('') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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, any>, root = window?.document?.documentElement) { | ||
if (variables && isPlainObject(variables) && root) { | ||
Object.keys(variables).forEach((key) => { | ||
root.style.setProperty(key, variables[key]) | ||
}) | ||
} | ||
} |
Oops, something went wrong.