mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-02-21 07:14:11 +00:00
Rename to ts (#92)
Rename Remove ts-nocheck, fix errors Update state when graph cleared via UI (#88) Convert to ts, fix imports Co-authored-by: pythongosssss <125205205+pythongosssss@users.noreply.github.com>
This commit is contained in:
57
src/scripts/ui/utils.ts
Normal file
57
src/scripts/ui/utils.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
export type ClassList = string | string[] | Record<string, boolean>;
|
||||
|
||||
export function applyClasses(
|
||||
element: HTMLElement,
|
||||
classList: ClassList,
|
||||
...requiredClasses: string[]
|
||||
) {
|
||||
classList ??= "";
|
||||
|
||||
let str: string;
|
||||
if (typeof classList === "string") {
|
||||
str = classList;
|
||||
} else if (classList instanceof Array) {
|
||||
str = classList.join(" ");
|
||||
} else {
|
||||
str = Object.entries(classList).reduce((p, c) => {
|
||||
if (c[1]) {
|
||||
p += (p.length ? " " : "") + c[0];
|
||||
}
|
||||
return p;
|
||||
}, "");
|
||||
}
|
||||
element.className = str;
|
||||
if (requiredClasses) {
|
||||
element.classList.add(...requiredClasses);
|
||||
}
|
||||
}
|
||||
|
||||
export function toggleElement(
|
||||
element: HTMLElement,
|
||||
{
|
||||
onHide,
|
||||
onShow,
|
||||
}: {
|
||||
onHide?: (el: HTMLElement) => void;
|
||||
onShow?: (el: HTMLElement, value) => void;
|
||||
} = {}
|
||||
) {
|
||||
let placeholder: HTMLElement | Comment;
|
||||
let hidden: boolean;
|
||||
return (value) => {
|
||||
if (value) {
|
||||
if (hidden) {
|
||||
hidden = false;
|
||||
placeholder.replaceWith(element);
|
||||
}
|
||||
onShow?.(element, value);
|
||||
} else {
|
||||
if (!placeholder) {
|
||||
placeholder = document.createComment("");
|
||||
}
|
||||
hidden = true;
|
||||
element.replaceWith(placeholder);
|
||||
onHide?.(element);
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user