Skip to content

Typescript Library - Custom Colors - CSS Variables #32

Open
@wojtek-viirtue

Description

@wojtek-viirtue

The applyTheme function as referenced below, creates CSS variables on a given target element and scopes everything to the sys token type. However, custom colors are not factored in. Given that the custom colors do account for "color", "on", and "container" (as well as light/dark) is there a convention for how these should be tokenized. Also, is there a reason that the convention would/wouldn't be applied in the applyTheme function ?

export function applyTheme(theme: Theme, options?: {
dark?: boolean,
target?: HTMLElement,
}) {
const target = options?.target || document.body;
const isDark = options?.dark ?? false;
const scheme = isDark ? theme.schemes.dark : theme.schemes.light;
for (const [key, value] of Object.entries(scheme.toJSON())) {
const token = key.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
const color = hexFromArgb(value);
target.style.setProperty(`--md-sys-color-${token}`, color);
}
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions