Skip to content

Commit

Permalink
fix: ensure aria attributes based on state
Browse files Browse the repository at this point in the history
  • Loading branch information
Westbrook committed May 4, 2020
1 parent 6761148 commit 6ee43de
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
8 changes: 7 additions & 1 deletion __snapshots__/Switch.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
#### `loads`

```html
<input id="input" tabindex="0" type="checkbox" />
<input
aria-checked="false"
id="input"
role="switch"
tabindex="0"
type="checkbox"
/>
<span id="switch"></span>
<label for="input" id="label">
<slot></slot>
Expand Down
7 changes: 6 additions & 1 deletion packages/checkbox/src/checkbox-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class CheckboxBase extends Focusable {
public quiet = false;

@query('#input')
private inputElement!: HTMLInputElement;
protected inputElement!: HTMLInputElement;

public get focusElement(): HTMLElement {
return this.inputElement;
Expand All @@ -52,11 +52,16 @@ export class CheckboxBase extends Focusable {
this.dispatchEvent(changeEvent);
}

protected get ariaCheckedState(): 'true' | 'false' | 'mixed' {
return this.checked ? 'true' : 'false';
}

protected render(): TemplateResult {
return html`
<input
id="input"
type="checkbox"
aria-checked=${this.ariaCheckedState}
.checked=${this.checked}
@change=${this.handleChange}
/>
Expand Down
23 changes: 22 additions & 1 deletion packages/checkbox/src/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import { CSSResultArray, TemplateResult, html, property } from 'lit-element';
import {
CSSResultArray,
TemplateResult,
html,
property,
PropertyValues,
} from 'lit-element';
import { CheckboxBase } from './checkbox-base.js';
import '@spectrum-web-components/icon';
import {
Expand All @@ -37,6 +43,10 @@ export class Checkbox extends CheckboxBase {
];
}

protected get ariaCheckedState(): 'true' | 'false' | 'mixed' {
return this.indeterminate ? 'mixed' : super.ariaCheckedState;
}

protected render(): TemplateResult {
return html`
<label id="root">
Expand All @@ -53,4 +63,15 @@ export class Checkbox extends CheckboxBase {
</label>
`;
}

protected firstUpdated(changes: PropertyValues): void {
super.firstUpdated(changes);
if (changes.has('invalid')) {
if (this.invalid) {
this.inputElement.setAttribute('aria-invalid', 'true');
} else {
this.inputElement.removeAttribute('aria-invalid');
}
}
}
}
12 changes: 11 additions & 1 deletion packages/switch/src/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import { CSSResultArray, TemplateResult, html } from 'lit-element';
import {
CSSResultArray,
TemplateResult,
html,
PropertyValues,
} from 'lit-element';
import { CheckboxBase } from '@spectrum-web-components/checkbox/lib/checkbox-base.js';
import switchStyles from './switch.css.js';
import legacyStyles from './switch-legacy.css.js';
Expand All @@ -32,4 +37,9 @@ export class Switch extends CheckboxBase {
<label id="label" for="input"><slot></slot></label>
`;
}

protected firstUpdated(changes: PropertyValues): void {
super.firstUpdated(changes);
this.inputElement.setAttribute('role', 'switch');
}
}

0 comments on commit 6ee43de

Please sign in to comment.