Skip to content

Instantly share code, notes, and snippets.

@MelSumner
Created May 19, 2022 21:10
Show Gist options
  • Save MelSumner/c663a8a477cabc9b39caa61777e04deb to your computer and use it in GitHub Desktop.
Save MelSumner/c663a8a477cabc9b39caa61777e04deb to your computer and use it in GitHub Desktop.

Accessible Toggle in Ember.js

In https://joshcollinsworth.com/blog/accessible-toggle-buttons, there is a toggle button component in various frameworks. This is the Ember equivalent of those examples.

In app/components/toggle-button.hbs:

<button aria-pressed="{{if this.isPressed "true" "false"}}" {{on "click" this.toggleButton}}>
  {{@buttonText}}
  <span aria-hidden="true" class="icon">▼</span>
</button>

In app/components/toggle-button.js:

import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';

export default class ToggleButtonComponent extends Component {
  @tracked isPressed = false;

  @action
  toggleButton() {
    this.isPressed = !this.isPressed;
  }
}

Invoked in a page template:

<ToggleButton @buttonText="Toggle Text" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment