Skip to content

Commit db1100e

Browse files
committed
Keep focus stable during label activation
Treat the interval between a label press and control activation as one logical focus transaction. Project it into CSS and restore physical focus before keyboard dispatch when a key interrupts the gesture. This preserves selectable label text while focus-derived rings, hints, and bindings keep the same standing. Add browser coverage for labels, shadow controls, and focus transitions.
1 parent bb51604 commit db1100e

17 files changed

Lines changed: 544 additions & 114 deletions

‎plugins/leaf/skills/leaf/CLAUDE.md‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,9 +1674,17 @@ keep that state in an attribute, so one `MutationObserver` over `open` and
16741674
### Standing somewhere
16751675

16761676
Focus is the reader's current place. `focused` follows it through declared
1677-
shadow roots. `markHere` paints one `--here-ring` around the semantic ask or
1678-
control that contains focus. The ring is derived on each paint; it does not
1679-
store the ask walk's position.
1677+
shadow roots. A native label activation may pass through `body` or a focusable
1678+
container between the pointer press and the control's focus; Leaf treats that
1679+
interval as one logical standing without changing DOM focus or preventing label
1680+
text selection.
1681+
`documentFocused` retargets the logical standing to its document host. Painted
1682+
focus readings use one of those two functions; CSS reads the matching `.lf-focus`,
1683+
`.lf-focus-visible`, and `.lf-focus-within` projections. A key ends the pointer
1684+
interval and restores physical focus before dispatch. Code that acts on physical
1685+
focus otherwise reads `document.activeElement` directly. `markHere` paints one
1686+
`--here-ring` around the semantic ask or control that contains focus. The ring
1687+
is derived on each paint; it does not store the ask walk's position.
16801688

16811689
A control that draws the band on itself draws it only where nothing else holds
16821690
that box's one outline. An ask written out around the control wears the band

‎plugins/leaf/skills/leaf/assets/leaf.js‎

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,12 @@ createShadowStage(watchDisclosures);
444444
const {
445445
byCommand,
446446
claimsEsc,
447+
documentFocused,
447448
elementScopes,
448449
focused,
449450
merge,
450451
pruneScopedElements,
452+
recoveredLabelFocus,
451453
scopeRefs,
452454
scopesFor,
453455
} = createScopes({
@@ -456,17 +458,18 @@ const {
456458
});
457459

458460
// Where the reader is standing, painted: the ring on the ask they are in, the mark on the
459-
// passage of the comment they are in, and the line saying what the next press does from
460-
// there. One repaint, because it is one question — every reading is of the focus and the
461-
// open-ask list, and every signal that moves either (a focus move, an answer taken, a
462-
// poll, a widget's own state) moves them all.
461+
// passage of the comment they are in, the focused box's hint, and the line saying what the
462+
// next press does from there. One repaint, because it is one question — every reading is of
463+
// the focus and the open-ask list, and every signal that moves either (a focus move, an
464+
// answer taken, a poll, a widget's own state) moves them all.
463465
//
464466
// Coalesced to a frame: a focus move is a focusout then a focusin, and painting between
465467
// them would flash the scope of nowhere and drop the ring for a frame. Here rather than
466468
// beside the renders it schedules, because the scopes core declares call it as the module
467469
// evaluates, which is before the line has an element to draw into — the frame is what puts
468470
// the first paint after both.
469471
let herePending = false;
472+
let paintInputHints = () => {};
470473
function paintHere() {
471474
if (herePending) return;
472475
herePending = true;
@@ -481,6 +484,7 @@ function paintHere() {
481484
// panel's own render was calling the chip pass.
482485
paintAddresses();
483486
paintCoreControls();
487+
paintInputHints();
484488
renderLine();
485489
});
486490
}
@@ -1376,7 +1380,13 @@ layoutSizes.observe(composer);
13761380
const { showToast } = createNotifications({ liveEl, syncLayout, toastEl });
13771381

13781382
// ---------- text inputs ----------
1379-
const wireInput = createInput({ keys, showToast, spell });
1383+
const { paint: paintInputs, wire: wireInput } = createInput({
1384+
focused,
1385+
keys,
1386+
showToast,
1387+
spell,
1388+
});
1389+
paintInputHints = paintInputs;
13801390

13811391
const { landTyping, pageSelection, selectionAnchor, snapSelection } =
13821392
createSelectionCapture({
@@ -1686,7 +1696,7 @@ const hasThreads = () => openThreads().length > 0;
16861696
// control inside it, whose own press is its own. Open and resolved threads both qualify:
16871697
// each has a primary Enter action and x changes the same resolution state in either direction.
16881698
const focusedThread = () => {
1689-
const active = document.activeElement;
1699+
const active = documentFocused();
16901700
return active?.classList?.contains("lf-thread") ? active : null;
16911701
};
16921702
// The item the reader is standing in, which is what a press means when they have pointed
@@ -1717,12 +1727,12 @@ const focusedThread = () => {
17171727
// from one means the page whole. A box that takes letters never arrives here at all: the
17181728
// typing scope claims the letter before the page is asked.
17191729
//
1720-
// `document.activeElement` rather than `focused()`, for the reason askPosition gives: a
1721-
// control staged in a shadow tree retargets to its host, and the host is the place in the
1722-
// document both the chrome guard and the item walk want. standingConversation below wants
1723-
// the other reading, and says so.
1730+
// `documentFocused()` rather than `focused()`, for the reason askPosition gives: a control
1731+
// staged in a shadow tree retargets to its host, and the host is the place in the document
1732+
// both the chrome guard and the item walk want. standingConversation below wants the inner
1733+
// reading, and says so.
17241734
const standingItem = () => {
1725-
const held = document.activeElement;
1735+
const held = documentFocused();
17261736
if (!held || held === document.body || inChrome(held)) return null;
17271737
const working = held.matches?.(ASK_CONTROL) ? standingIn() : null;
17281738
return working ?? itemAt(askPlace(held));
@@ -1896,7 +1906,7 @@ const letGo = () => document.body.focus({ preventScroll: true });
18961906
// Their next Space is then that button rather than the page's scroll. CLAUDE.md's "The
18971907
// reader has to be standing somewhere" holds the rest.
18981908
function rung() {
1899-
const active = document.activeElement;
1909+
const active = documentFocused();
19001910
const holding = Boolean(active) && active !== document.body;
19011911
if (fabAnchorAt())
19021912
return {
@@ -2048,6 +2058,7 @@ const {
20482058
} = createAskView({
20492059
PAGE_PAINT_ATTRIBUTE,
20502060
SCROLL,
2061+
documentFocused,
20512062
announce,
20522063
askEntry,
20532064
askSource,
@@ -2267,12 +2278,12 @@ const COMPOSER = {
22672278
// the scope rather than below it, because a row naming a predicate directly reads the
22682279
// binding as the table is built — the deferring wrapper the branch here used to need was
22692280
// the only thing hiding that.
2270-
const inTheBox = () => panel.contains(document.activeElement);
2281+
const inTheBox = () => panel.contains(documentFocused());
22712282
// The panel thread the reader is in, asked by class because that is the anchors module's
22722283
// question: which logged thread's passage to paint. It is not the box's way out, which
22732284
// climbs further and answers for a seat on the page too — the two readings stayed apart
22742285
// rather than one standing in for the other.
2275-
const focusedThreadOf = () => document.activeElement?.closest?.(".lf-thread");
2286+
const focusedThreadOf = () => documentFocused()?.closest?.(".lf-thread");
22762287
// Where a box hands the reader back to, which is the rung `c` came down. It asked
22772288
// `.lf-thread` and the panel alone, so the two boxes outside the chrome — a conversation
22782289
// seated on the page, and each thread on that seat — had no way out but the page's own
@@ -3018,6 +3029,7 @@ const { availableCommands, executeCommand, readerIn, shadow, stack } = createDis
30183029
keepShown,
30193030
paintHere,
30203031
panel,
3032+
recoveredLabelFocus,
30213033
SCOPES,
30223034
scopesFor,
30233035
setChord,
@@ -3180,13 +3192,16 @@ const unaccountedGesture = () =>
31803192
// have typed — a composition surface is a focused textarea, any holding words, or a
31813193
// widget-built one (data-lf-offer) even empty, because deleting everything is still an
31823194
// edit.
3183-
const midComposition = () =>
3184-
composerOpen ||
3185-
Boolean(fabAnchorAt()) ||
3186-
unaccountedGesture() ||
3187-
(document.activeElement?.tagName === "TEXTAREA" &&
3188-
(document.activeElement.value !== "" ||
3189-
document.activeElement.hasAttribute("data-lf-offer")));
3195+
const midComposition = () => {
3196+
const active = focused();
3197+
return (
3198+
composerOpen ||
3199+
Boolean(fabAnchorAt()) ||
3200+
unaccountedGesture() ||
3201+
(active?.tagName === "TEXTAREA" &&
3202+
(active.value !== "" || active.hasAttribute("data-lf-offer")))
3203+
);
3204+
};
31903205
// Through the chooser's one door, so the chip opens exactly the version it names. At the
31913206
// live root that is an explicit in-place release of the composition hold; on an immutable
31923207
// page it is ordinary version travel.

‎plugins/leaf/skills/leaf/assets/runtime/asks/view.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export function createAskView({
1010
asksPanel,
1111
banner,
1212
blocksOnScreen,
13+
documentFocused,
1314
el,
1415
elementById,
1516
inChrome,
@@ -280,11 +281,11 @@ export function createAskView({
280281
// ask parts from neither list: its question is settled, so there is nothing left there to
281282
// be standing in, and a settled group goes on being named by its own words.
282283
//
283-
// document.activeElement rather than focused(), for the reason askPosition gives: a
284+
// Document focus rather than the inner control, for the reason askPosition gives: a
284285
// control staged in a shadow tree retargets to its host, and the host is the place in the
285286
// document this wants.
286287
function standingIn() {
287-
const held = document.activeElement;
288+
const held = documentFocused();
288289
if (!held || held === document.body) return null;
289290
const place = askPlace(held);
290291
return (
@@ -347,11 +348,11 @@ export function createAskView({
347348
// reading. Every one of them can be absent, and then the first ask is the only answer
348349
// there is.
349350
//
350-
// document.activeElement rather than focused(): a control staged in a shadow tree
351+
// Document focus rather than the inner control: a control staged in a shadow tree
351352
// retargets to its host, which is exactly what this question wants — a place in the
352353
// document to measure the asks against, not the control the register would dispatch to.
353354
function askPosition() {
354-
const held = document.activeElement;
355+
const held = documentFocused();
355356
// The banner stands over the page rather than in it, and its controls are addresses
356357
// the reader holds from wherever they are. The Asks button focuses itself on the way
357358
// to running this, so measuring from it would send every press on it back to the top.

0 commit comments

Comments
 (0)