To fix focusability and focus navigation order issue (W3C 25473, Chromium 380445), introduce delegatesFocus
flag under shadow root, to address some shortcomings that
the default focus navigation behavior (defined in the Shadow DOM spec) has.
- Fix tab navigation ordering issue when shadow host has
tabindex
attribute - Changes focusing behavior when mouse click or
focus()
on shadow host or within its shadow root to make it easier to create complex component like<input type="date">
:focus
on shadow host matches when inner element is focused.
delegatesFocus
is a read-only boolean property on a shadow root, which indicates focus activity (tab navigation, mouse click, focus()) on its shadow host will be delegated to its shadow. This attribute is set via ShadowRootInit
dictionary when createShadowRoot
is called. This attribute is immutable during the lifetime of its associated shadow root.
partial interface ShadowRoot {
readonly boolean delegatesFocus;
}
partial dictionary ShadowRootInit {
boolean delegatesFocus;
}
If a shadow host delegates focus (its shadow root was created with delegatesFocus=true
), the following behavior is expected.
-
TAB navigation order
If tabindex is 0 or positive and the containing shadow tree has any focusable element, forward tab navigation will skip the host and forwards the focus to the first focusable element. For backward tab navigation, the host is skipped after its first focusable element. In the case oftabindex="-1"
, the whole shadow tree is skipped for the tab navigation. -
focus()
method behavior
Invoking focus() method on the host element will delegate the focus the first focusable element in its shadow tree. This applies recursively for nested shadow trees. If the shadow root doesn’t contain any focusable element, the host itself gets focus. -
autofocus
attribute
If autofocus attribute is specified, the focus is forwarded like focus() method when the page load finishes. -
Response to mouse click
If focusable area within the shadow tree is clicked, the element gets focus. If non-focusable area within the shadow tree is clicked, the focus moves to the first focusable element in its shadow tree, as if the host getsfocus()
ed. -
CSS
:focus
pseudo-class
A selector like#host:focus
matches when focus is in any of its descendant shadow tree.
- When a web author wants to create his/her own one with multiple focusable fields using combination of Shadow DOM and Custom Elements
You see <date-input>
<input type=date>
fields. The former is built with web components (as a polyme element), the latter is native implementation.
- Read the full document for more complete documentation.
- Follow crbug/496005 for Blink implementation.