Skip to content

[Shadow] activeElement behavior seems to break encapsulation #358

@bicknellr

Description

@bicknellr

It seems strange for a ShadowRoot's activeElement to point to elements outside of that ShadowRoot. In particular, the spec currently seems to allow a ShadowRoot's activeElement to point not only to elements within the ShadowRoot but also elements in the tree that the ShadowRoot's host participates in (and possibly even further outwards).

I think it would be more reasonable if activeElement of a ShadowRoot could only point to an element contained within that ShadowRoot (or be null if none was focused). Additionally, if the 'deepest' focused node (i.e. the focused, UA-implemented element without a ShadowRoot) was nested further within descendant ShadowRoots, any given ShadowRoot's activeElement along that chain would point to the most shallow host of that chain contained within that ShadowRoot.

One implication of this approach is that determining focused descendants of the ShadowRoot's host means checking activeElement of the Document / ShadowRoot containing that host. This situation seems to break encapsulation by implying that the code controlling the ShadowRoot / component needs to look at its surrounding tree. However, I would argue that it's preferable as descendants of the ShadowRoot's host (i.e. those outside of the ShadowRoot) should be the responsibility of the code controlling the tree in which those descendants (and the host) participate. Also, there's precedent for using a tree's root to determine focus within that tree given Document's activeElement property already works this way.

An option for providing the ShadowRoot with some insight into what non-shadow descendants of its host might be focused would be to have activeElement point at a slot when an element distributed to that slot (or descendant of) is focused. (Maybe even give slots activeElement that point to the focused non-shadow descendant? They seem a lot like ShadowRoot hosts with no descendants anyways.) I don't think this would really be necessary though, given that you could just check if the activeElement of the host's containing Document / ShadowRoot is contained within the host, but it might make working with focus of non-shadow tree descendants within a component easier.

In general, I think this would make the scope of responsibility of code watching focus of a component's nodes more closely mirror shadow tree boundaries.

(Also, I get the feeling that a ShadowRoot's encapsulation mode would somehow be relevant to this but I haven't been able to figure out anything about encapsulation modes from the spec other than that a ShadowRoot has one and it's either 'open' or 'closed'.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions