Skip to content

Commit

Permalink
feat: add style transform function parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Sv443 committed May 22, 2024
1 parent f8a1924 commit 3ad6e3e
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@ export function onInteraction<TElem extends HTMLElement>(elem: TElem, listener:
* Adds a style element to the DOM at runtime.
* @param css The CSS stylesheet to add
* @param ref A reference string to identify the style element - defaults to a random 5-character string
* @param transform A function to transform the CSS before adding it to the DOM
*/
export function addStyle(css: string, ref?: string) {
export function addStyle(css: string, ref?: string, transform: (css: string) => string = (c) => c) {
if(!domLoaded)
throw new Error("DOM has not finished loading yet");
const elem = addGlobalStyle(css);
const elem = addGlobalStyle(transform(css));
elem.id = `bytm-global-style-${ref ?? randomId(5, 36)}`;
return elem;
}
Expand All @@ -197,10 +198,10 @@ export function currentMediaType(): "video" | "song" {
}

/** Adds a global style element with the contents of the specified CSS resource */
export async function addStyleFromResource(key: ResourceKey & `css-${string}`) {
export async function addStyleFromResource(key: ResourceKey & `css-${string}`, transform: (css: string) => string = (c) => c) {
const css = await fetchCss(key);
if(css) {
addStyle(css, key.slice(4));
addStyle(transform(css), key.slice(4));
return true;
}
return false;
Expand Down

0 comments on commit 3ad6e3e

Please sign in to comment.