Skip to content

Label clicks not polyfilled #85

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

Closed
bennypowers opened this issue Oct 18, 2022 · 1 comment · Fixed by #86
Closed

Label clicks not polyfilled #85

bennypowers opened this issue Oct 18, 2022 · 1 comment · Fixed by #86

Comments

@bennypowers
Copy link
Contributor

Consider this example switch component

import {html, css, LitElement} from 'lit';
import {customElement, property} from 'lit/decorators.js';

@customElement('x-switch')
export class XSwitch extends LitElement {
  static formAssociated = true;
  
  #internals = this.attachInternals();

  @property({ type: Boolean }) checked = false;

  connectedCallback() {
    super.connectedCallback();
    this.addEventListener('click', this.onClick);
  }
  
  render() {
    return this.checked ? 'x' : 'o';
  }
  
  onClick() {
    this.checked = !this.checked;
  }
}

When it is labeled, clicking the label should toggle the switch:

<x-switch id="x"></x-switch>
<label for="x">LABEL</label>

In Firefox and Chromium (supporting), clicking the label will toggle the switch
However, in epiphany (webkit) clicking the label will not toggle the switch.

The polyfill should add click listeners to labels associated with the control via for

@jpzwarte
Copy link
Contributor

Just ran into this difference as well; the polyfill focuses instead of clicks the element:

    const initLabels = (ref, labels) => {
        if (labels.length) {
            Array.from(labels).forEach(label => label.addEventListener('click', ref.focus.bind(ref)));
            let firstLabelId = labels[0].id;
            if (!labels[0].id) {
                firstLabelId = `${labels[0].htmlFor}_Label`;
                labels[0].id = firstLabelId;
            }
            ref.setAttribute('aria-labelledby', firstLabelId);
        }
    };

Pretty easy to change to ref.click.bind(ref)?

bennypowers added a commit to bennypowers/element-internals-polyfill that referenced this issue Oct 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants