Open domenic opened 4 years ago
I think worse than styling would be a flashing input cursor since that implies the page is waiting for input. Keyboard coming up would be a concern but I believe that's gated on user activation so shouldn't be an issue; though I don't know that all browsers gate on that.
Some more details about the current implementation:
document.activeElement
inside a portal always returns <body>
I think worse than styling would be a flashing input cursor since that implies the page is waiting for input
We don't have to show a cursor on everything that's focused, though. See below.
In general my leaning would be toward treating portals the same as other top-level BCs (e.g., window.open()ed tabs, which can have their own independent focused area). It appears we already have handling to not style or show the cursor for such browsing contexts, despite them having independently changing focused area. See the example at https://jsbin.com/luyejohodu/edit?html,output . So if the only downside is styling/cursor related, then that model makes sense to me.
WDYT?
So I fiddled around some more with chrome's focus implementation, and observed a few things:
activeElement
of the document thoughactiveElement
againfocus()
on an element in a window that doesn't have focus, the document's activeElement
is updated, but we do not dispatch a focus event
I'm not sure any of this is speced anywhere (or maybe it is and I misinterpreted the current spec); but if we treated a portal like a window without focus, we would update the activeElement every time we call focus()
in the portal's BC, but never actually dispatch any events. Portal activation would be equivalent to a window getting focus, and we would focus the document's activeElement
(so autofocus
would work). This seems to give it the top-level BC behaviour, and avoids the styling problems as well. WDYT?
Some of this is getting specced in https://github.com/whatwg/html/pull/6172, coincidentally.
Portal activation would be equivalent to a window getting focus, and we would focus the document's activeElement (so autofocus would work). This seems to give it the top-level BC behaviour, and avoids the styling problems as well. WDYT?
This seems perfect to me.
@a4sriniv and I were having a discussion in a separate doc about autofocus, which revealed that we probably should discuss focus in general.
My impression was that, like other top-level browsing contexts, portal browsing contexts would have an independent currently focused area (~
document.activeElement
). So e.g. code inside the portal could useautofocus=""
,element.focus()
, etc., without impacting the embedder browsing context at all.focus
andblur
events would fire inside the portal browsing context, completely independent from the embedder browsing context. And, this currently focused area could be styled differently, e.g. matching:focus
styles from that document.@a4sriniv reports that currently portals are not implemented like that, and (IIUC) instead all attempts to focus something inside a portal browsing context are no-ops. In this model,
document.activeElement
inside a portal would be .... null? Or the<body>
element? Andfocus
andblur
events would never fire, pre-activation.autofocus=""
behavior is undetermined.IMO, having independent focused areas is conceptually clean and simple. However, it does have the potential downside of the styling not matching user expectations. For example, if the portaled content contains a
<input type=text autofocus>
, it could have the blue focus outline, while at the same time something else on the outer page also has the blue focus outline. That'd be a pretty bad user experience, I think.Are there other problems with the independent-areas model?