-
Notifications
You must be signed in to change notification settings - Fork 2
/
getCssVariable.ts
33 lines (31 loc) · 1.03 KB
/
getCssVariable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Utilities
*/
/** Get CSS variable
* @public
*/
export function getCssVariable(
variableName: string,
htmlElement?: HTMLElement
): string {
const element = htmlElement ?? document.documentElement;
const cssStyles = getComputedStyle(element, null);
const cssVal = String(cssStyles.getPropertyValue(variableName)).trim();
return cssVal;
}
/** Get CSS variable as number
* @public
*/
export function getCssVariableAsNumber(
variableName: string,
htmlElement?: HTMLElement
): number {
let cssValNum: number = NaN;
const cssValStr = getCssVariable(variableName, htmlElement);
if (cssValStr) cssValNum = parseFloat(cssValStr);
return cssValNum;
}