Skip to content

Commit

Permalink
Apply styles to already loaded ha-sidebar and hui-root. Fix #330
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasloven committed Dec 31, 2023
1 parent a7bed4d commit 40fc794
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/helpers/patch_function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ export const patch_object = (obj, patch) => {
}
};

export const patch_prototype = async (cls, patch) => {
export const patch_prototype = async (cls, patch, afterwards?) => {
if (typeof cls === "string") {
await customElements.whenDefined(cls);
cls = customElements.get(cls);
}

return patch_object(cls.prototype, patch);
const patched = patch_object(cls.prototype, patch);
afterwards?.();
return patched;
};

// Decorator for patching a custom-element
export function patch_element(element) {
export function patch_element(element, afterwards?) {
return function patched(constructor) {
patch_prototype(element, constructor);
patch_prototype(element, constructor, afterwards);
};
}
12 changes: 11 additions & 1 deletion src/patch/ha-sidebar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { patch_element } from "../helpers/patch_function";
import { ModdedElement, apply_card_mod } from "../helpers/apply_card_mod";
import { selectTree } from "../helpers/selecttree";

/*
Patch ha-sidebar for theme styling
Expand All @@ -10,7 +11,16 @@ An earlier version of card-mod would also re-run firstUpdated of any existing el
This shouldn't be necessary if card-mod is loaded as a module.
*/

@patch_element("ha-sidebar")
// ha-sidebar may have been used before the patch was applied
const apply = () => {
selectTree(
document,
"home-assistant$home-assistant-main$ha-sidebar",
false
).then((root) => root?.firstUpdated());
};

@patch_element("ha-sidebar", apply)
class SidebarPatch extends ModdedElement {
// @ts-ignore
firstUpdated(_orig, ...args) {
Expand Down
12 changes: 11 additions & 1 deletion src/patch/hui-root.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { patch_element } from "../helpers/patch_function";
import { ModdedElement, apply_card_mod } from "../helpers/apply_card_mod";
import { selectTree } from "../helpers/selecttree";

/*
Patch hui-root for theme styling
Expand All @@ -10,7 +11,16 @@ An earlier version of card-mod would also re-run firstUpdated of any existing el
This shouldn't be necessary if card-mod is loaded as a module.
*/

@patch_element("hui-root")
// hui-root may have been used before the patch was applied
const apply = () => {
selectTree(
document,
"home-assistant$home-assistant-main$partial-panel-resolver ha-panel-lovelace$hui-root",
false
).then((root) => root?.firstUpdated());
};

@patch_element("hui-root", apply)
class HuiRootPatch extends ModdedElement {
firstUpdated(_orig, ...args) {
_orig?.(...args);
Expand Down

0 comments on commit 40fc794

Please sign in to comment.