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

Commit 70d92fa

Browse files
TedSandernshahan
authored andcommitted
Add the ability to tag the content that you would like to focus when an expansion panel opens.
PiperOrigin-RevId: 243308091
1 parent eb4ba2b commit 70d92fa

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

angular_components/lib/material_expansionpanel/material_expansionpanel.dart

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ import 'package:angular_components/utils/disposer/disposer.dart';
4646
/// circumstances as the content will be tabbable and so will be worse for
4747
/// accessibility.
4848
///
49+
/// __Content Reference:__
50+
///
51+
/// - `focusOnOpen` -- Mark a Focusable or DOM element with #focusOnOpen in the
52+
/// content to have that item be focused when the expansion panel opens.
53+
///
4954
@Component(
5055
selector: 'material-expansionpanel',
5156
directives: [
@@ -98,14 +103,35 @@ class MaterialExpansionPanel
98103
: shouldExpandOnLeft = expandOnLeft != null,
99104
forceContentWhenClosed = forceContent != null;
100105

106+
Focusable _focusableItem;
107+
101108
/// Set the auto focus child so that we can focus on it when the panel opens.
102109
///
103110
/// Unfortunately, this only selects the first [AutoFocusDirective] in the
104111
/// contents of the expansion panel, which means that if there is another
105112
/// [AutoFocusDirective] in an <ng-content> that is not the .content, that
106113
/// will get focused instead of the [AutoFocusDirective] inside the .content.
114+
@Deprecated(
115+
'Please tag the element or widget to focus on open with #focusOnOpen')
107116
@ContentChild(AutoFocusDirective)
108-
AutoFocusDirective autoFocusChild;
117+
set autoFocusChild(AutoFocusDirective focus) {
118+
_focusableItem = focus;
119+
}
120+
121+
/// Sets the focus child so that we can focus on it when the panel opens.
122+
@ContentChild('focusOnOpen')
123+
set focusElement(dynamic element) {
124+
if (element is Focusable) {
125+
_focusableItem = element;
126+
} else if (element is ElementRef) {
127+
_focusableItem = RootFocusable(element.nativeElement);
128+
} else {
129+
assert(
130+
element == null,
131+
'Warning expansion panel content has a #focus'
132+
'child which is not an Element, or Focusable');
133+
}
134+
}
109135

110136
HtmlElement _mainPanel;
111137
@ViewChild('mainPanel')
@@ -499,9 +525,9 @@ class MaterialExpansionPanel
499525
_isExpanded.value = expand;
500526
if (byUserAction) _isExpandedChangeByUserAction.add(expand);
501527
_changeDetector.markForCheck();
502-
if (expand && autoFocusChild != null) {
528+
if (expand && _focusableItem != null) {
503529
_domService.scheduleWrite(() {
504-
autoFocusChild.focus();
530+
_focusableItem.focus();
505531
});
506532
}
507533
if (stateWasInitialized) _transitionHeightChange(expand);

examples/material_expansionpanel_example/lib/material_expansionpanel_example.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<div>
1515
<span>Change the name:</span>
16-
<material-input autoFocus [ngModel]="name" (inputKeyPress)="name = $event"></material-input>
16+
<material-input #focusOnOpen [ngModel]="name" (inputKeyPress)="name = $event"></material-input>
1717
</div>
1818
</material-expansionpanel>
1919

@@ -50,6 +50,9 @@
5050
<div value>
5151
Panel headers can also have custom names.
5252
</div>
53+
<div tabIndex="0" #focusOnOpen>
54+
I want to focus this content on open.
55+
</div>
5356
</material-expansionpanel>
5457
</material-expansionpanel-set>
5558
</section>

0 commit comments

Comments
 (0)