Skip to content

Instantly share code, notes, and snippets.

@runspired
Created October 5, 2016 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save runspired/3c61d69a9039af5f916bb44e26d3bb6e to your computer and use it in GitHub Desktop.
Save runspired/3c61d69a9039af5f916bb44e26d3bb6e to your computer and use it in GitHub Desktop.
Accessibility Labels
import Ember from 'ember';
const {
Component,
computed,
get,
guidFor
} = Ember;
export default Component.extend({
tagName: '',
computed,
targetId: computed(function() {
return guidFor(this);
}),
actions: {
register(child) {
this.set('targetId', get(child, 'id'));
}
}
});
import Ember from 'ember';
const {
Component,
run
} = Ember;
export default Component.extend({
tagName: 'input',
register: null,
didInsertElement() {
run.next(() => {
this.sendAction('register', this);
});
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
<h1>Setting Accessibility Id</h1>
<br>
<h3>Scenario 1: target is an element with a known ID</h3>
{{#a11y-label targetId="foo"}}
<input id="foo">
{{/a11y-label}}
<h3>Scenario 2: target is an element with an unknown ID</h3>
<p>Note: for this to be true, the id would need to be programmatically generated by something else deeper in scope, a situation we should be avoiding anyway. The implementation details here are the same as in scneario 4.</p>
<h3>Scenario 3: target is an element without an ID</h3>
{{#a11y-label as |targetId|}}
<input id="{{targetId}}">
{{/a11y-label}}
<h3>Scenario 4: target is a component's element</h3>
{{#a11y-label as |_ register|}}
{{my-input register}}
{{/a11y-label}}
<br>
{{outlet}}
<br>
<br>
<label for="{{targetId}}">{{text}}</label>
{{yield targetId (action "register")}}
{
"version": "0.10.5",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.8.0",
"ember-data": "2.8.0",
"ember-template-compiler": "2.8.0",
"ember-testing": "2.8.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment