I am a bit lost in css selsctors right now. Iām trying to configure a visual feedback for clicking / tapping a mushroom-template-card.
The easiest way would be to use an :active pseudoclass:
type: custom:mushroom-template-card
primary: Test-Button
icon: mdi:gesture-tap
card_mod:
style:
.: |
ha-card {
transition: background-color 0.2s ease;
}
ha-card:active {
background-color: rgba(0, 122, 255, 0.2);
}
This works perfectly fine on a desktop, when clicking the element with a mouse.
But: the color change is effectively invisible on touch devices, because ātappingā makes an element only active for a fraction of a second.
So, plan B would be to replace this :active effect with a real css animation, but I canāt figure out, how to reliably trigger a css animation by clicking/tapping the element.
The only āworkaroundā I found so far, is to inject the animation class in a tap_action with browser_mod.javascript:
type: custom:mushroom-template-card
primary: Beispiel
icon: mdi:lightbulb
tap_action:
action: perform-action
perform_action: browser_mod.javascript
data:
code: |
(() => {
try {
const container = document
.querySelector("body > home-assistant")?.shadowRoot
.querySelector("home-assistant-main")?.shadowRoot
.querySelector("ha-drawer > partial-panel-resolver > ha-panel-lovelace")?.shadowRoot
.querySelector("hui-root")?.shadowRoot
.querySelector("#view > hui-view > hui-sections-view")?.shadowRoot
.querySelector("div > ha-sortable > div > div > hui-section > hui-grid-section")?.shadowRoot
.querySelector("ha-sortable > div > div:nth-child(4) > hui-card > mushroom-template-card")?.shadowRoot
.querySelector("ha-card > mushroom-card > mushroom-state-item")?.shadowRoot
.querySelector("div");
if (container) {
container.classList.add("blink-on-click");
setTimeout(() => container.classList.remove("blink-on-click"), 600);
} else {
console.warn("Container nicht gefunden.");
}
} catch (e) {
console.error("Fehler beim Triggern der Animation:", e);
}
})();
card_mod:
style:
mushroom-state-item$: |
@keyframes flash-bg {
0% { background-color: rgba(255, 255, 0, 0.6); }
100% { background-color: transparent; }
}
.blink-on-click {
animation: flash-bg 0.6s ease-out;
}
But apart from this javascript solution being quite cumbersome in general, I even couldnāt figure out, how to reliably select the element other than with an absolute DOM path. Thus, this is only working as long as the element stays in exactly the right place in the DOM. It even fails in edit mode because of the element then being located in a different DOM structure.
Thus, this isnāt even a viable workaround, but a proof-of-concept, that the javascript solution is working at all.
Is there any other (elegant?) solution to this problem?
And yes: I know, I could change the state of some entity in the backend for a short time with a script triggered by the tap_action, but it seems even more cumbersome to me, needing to maintain a HA entity just for a āblinkā effect. Especially If I want to have this effect on a lot of mushroom-cards and donāt want to bother other Dashboard users with my blink actions.