Skip to content
This repository was archived by the owner on May 20, 2023. It is now read-only.

Commit bc5be5e

Browse files
Googlernshahan
authored andcommitted
Fix aria-pressed state on material-toggle
PiperOrigin-RevId: 189364944
1 parent f71f1bf commit bc5be5e

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/material_toggle/material_toggle.dart

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ import 'package:angular_components/utils/browser/events/events.dart';
3131
directives: const [NgIf],
3232
changeDetection: ChangeDetectionStrategy.OnPush,
3333
visibility: Visibility.local)
34-
class MaterialToggleComponent {
34+
class MaterialToggleComponent implements AfterViewInit {
35+
@ViewChild('toggle')
36+
HtmlElement toggleElement;
37+
38+
@override
39+
void ngAfterViewInit() {
40+
_syncAriaPressed();
41+
}
42+
3543
/// Enables/disables the toggle button.
3644
///
3745
/// `true` is disabled and `false` is enabled.
@@ -42,7 +50,13 @@ class MaterialToggleComponent {
4250
///
4351
/// `true` is ON and `false` is OFF.
4452
@Input()
45-
bool checked = false;
53+
set checked(bool value) {
54+
_checked = value;
55+
_syncAriaPressed();
56+
}
57+
58+
bool get checked => _checked;
59+
bool _checked = false;
4660

4761
final _controller = new StreamController<bool>.broadcast();
4862

@@ -108,4 +122,9 @@ class MaterialToggleComponent {
108122
keyEvent.stopPropagation();
109123
}
110124
}
125+
126+
void _syncAriaPressed() {
127+
if (toggleElement == null) return;
128+
toggleElement.attributes['aria-pressed'] = '$checked';
129+
}
111130
}

lib/material_toggle/material_toggle.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
for details. All rights reserved. Use of this source code is governed by a
44
BSD-style license that can be found in the LICENSE file.
55
-->
6-
<div attr.aria-pressed="{{checked}}"
6+
<div #toggle
77
attr.aria-disabled="{{disabled}}"
88
attr.aria-label="{{ariaLabel}}"
99
class="material-toggle"

0 commit comments

Comments
 (0)