Object with names
as keys and corresponding CSS variable values (or undefined
if not present).
The arcade!WithUndefinedValues
type represents an object with values that could be undefined
.
elements!InvalidElementError
if the specified target
wasn't found.
HTML
<style>
:root {
--color-fg: green;
}
</style>
<button
id="example"
style="--color-bg: blue; --gap: 24;"
>
Example
</button>
Get from Element
type Shape = {
"--color-bg": string | undefined;
"--gap": number | undefined;
};
const element = findElement("#example")!;
getCssVars<Shape>(["--color-bg", "--gap"], element);
// { "--color-bg": "blue", "--gap": 24 }
Get from :root
type Shape = {
"--color-fg": string | undefined;
};
getCssVars<Shape>(["--color-fg"]);
// { "--color-fg": "green" }
Builds an object with the keys equal to the CSS variable
names
and the value equal to the corresponding variable value in thetarget
.If no
target
is specified, usesdocumentElement
(i.e.:root
).If the value is found it is coerced to a boolean if
"true"
or"false"
, a number if numeric, or the string value if a string. If not found, the value is excluded from the return value.You will need to perform checks for whether a value is
undefined
in the returned object if some of the entries weren't present. See the code block below for additional details.