Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Toggle]: Fix toggles not toggling #25966

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions ui/webui/resources/cr_elements/cr_toggle/cr_toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import '//resources/brave/leo.bundle.js'

import {CrLitElement, css} from '//resources/lit/v3_0/lit.rollup.js';
import { CrLitElement, css } from '//resources/lit/v3_0/lit.rollup.js';
import { getHtml } from './cr_toggle.html.js';

export interface CrToggleElement {
Expand All @@ -32,6 +32,7 @@ export class CrToggleElement extends CrLitElement {
checked: {
type: Boolean,
reflect: true,
notify: true,
fallaciousreasoning marked this conversation as resolved.
Show resolved Hide resolved
},

disabled: {
Expand All @@ -44,11 +45,26 @@ export class CrToggleElement extends CrLitElement {
checked: boolean = false;
disabled: boolean = false;

override firstUpdated(){
this.addEventListener('click', e => {
// Prevent |click| event from bubbling. It can cause parents of this
// elements to erroneously re-toggle this control.
e.stopPropagation();
e.preventDefault();
})
}

// The Nala event looks a bit different to the Chromium one, so we need to
// convert it.
onChange_(e: { checked: boolean }) {
async onChange_(e: { checked: boolean }) {
this.checked = e.checked
this.dispatchEvent(new CustomEvent('change', { bubbles: true, composed: true, detail: e.checked }))

// Yield, so that 'checked-changed' (originating from `notify: 'true'`) fire
// before the 'change' event below, which guarantees that any Polymer parent
// with 2-way bindings on the `checked` attribute are updated first.
await this.updateComplete

this.dispatchEvent(new CustomEvent('change', { bubbles: true, composed: true, detail: this.checked }))
}
}

Expand Down
Loading