Skip to content

Commit

Permalink
šŸ› Fix issue where switching tabs would transition colors on browser bā€¦
Browse files Browse the repository at this point in the history
ā€¦uttons
  • Loading branch information
kierandrewett committed Dec 29, 2023
1 parent 81f2caa commit 71dbcb0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
6 changes: 6 additions & 0 deletions components/widgets/content/browser-button.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

.browser-button {
--button-disabled-opacity: 0.65;

transition: none;
}

.browser-button:is(:hover, :active, :focus, :focus-visible, [was-hover], [was-focus]) {
transition: var(--button-transition);
}

.browser-button:disabled {
Expand Down
51 changes: 50 additions & 1 deletion components/widgets/content/browser-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,37 @@ class BrowserButton extends BrowserContextualMixin(HTMLButtonElement) {
this.setAttribute("mode", newMode);
}

/**
* Toggles a transitioning psuedo class on the button
* @param {string} name
*/
_toggleTransitionPsuedoClass(name) {
this.toggleAttribute(`was-${name}`, true);

this.addEventListener(
"transitionend",
() => {
this.removeAttribute(`was-${name}`);
},
{ once: true }
);
}

/**
* Handles internal browser button events
* @param {Event} event
*/
_handleBrowserButtonEvent(event) {
switch (event.type) {
case "mouseleave":
this._toggleTransitionPsuedoClass("hover");
break;
case "focusout":
this._toggleTransitionPsuedoClass("focus");
break;
}
}

connectedCallback() {
this.classList.add("browser-button");
this.classList.toggle(
Expand All @@ -156,9 +187,27 @@ class BrowserButton extends BrowserContextualMixin(HTMLButtonElement) {
this.elements.label
)
);

this.addEventListener(
"mouseleave",
this._handleBrowserButtonEvent.bind(this)
);
this.addEventListener(
"focusout",
this._handleBrowserButtonEvent.bind(this)
);
}

disconnectedCallback() {}
disconnectedCallback() {
this.removeEventListener(
"mouseleave",
this._handleBrowserButtonEvent.bind(this)
);
this.removeEventListener(
"focusout",
this._handleBrowserButtonEvent.bind(this)
);
}
}

customElements.define("browser-button", BrowserButton);

0 comments on commit 71dbcb0

Please sign in to comment.