Skip to content

Add programmatic focus management - #118

Merged
cowboyd merged 1 commit into
mainfrom
dom-focus
Dec 5, 2026
Merged

cowboyd merged 1 commit into
mainfrom
dom-focus

Conversation

@cowboyd

@cowboyd cowboyd commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds focus(), blur(), activeElement, and focus events. Removing a focused subtree clears focus without firing blur.

Type of change

  • Bug fix
  • Feature
  • Refactor (no behavior change)
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • All tests pass (pnpm test)
  • Files are formatted (pnpm format)
  • I have added/updated tests for my changes (if applicable)
  • I have added a changeset

AI-generated code disclosure

  • This PR includes AI-generated code

@pkg-pr-new

pkg-pr-new Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

commit: 1f0081a

@cowboyd
cowboyd added this pull request to stack #114 December 1, 2026 18:03
@cowboyd
cowboyd marked this pull request as ready for review December 1, 2026 19:14
@cowboyd
cowboyd marked this pull request as draft December 2, 2026 22:22
@cowboyd cowboyd added the input focus, keyboard, editing, mouse, anything coming from the release. label Dec 2, 2026
@cowboyd cowboyd added this to the Alpha-1 milestone Dec 2, 2026
@cowboyd
cowboyd removed this pull request from stack #114 December 2, 2026 22:23
@cowboyd
cowboyd force-pushed the dom-is-connected branch 3 times, most recently from 89c8fa4 to 4b3d8e8 Compare December 3, 2026 19:31
Base automatically changed from dom-is-connected to main December 3, 2026 21:09
@cowboyd
cowboyd force-pushed the dom-focus branch 2 times, most recently from 8863192 to e510812 Compare December 3, 2026 21:25
@cowboyd
cowboyd marked this pull request as ready for review December 3, 2026 21:25
natemoo-re

This comment was marked as outdated.

Comment thread packages/ui/src/dom.ts Resolved
Comment thread packages/ui/src/dom.ts
Comment on lines +1049 to +1102
function setActiveElement(document: Document, element: Element | null): void {
if (element === null) {
focii.delete(document);
} else {
focii.set(document, element);
}
}

function getActiveElement(document: Document): Element | null {
return focii.get(document) ?? null;
}

function transitionActiveElement(document: Document, to: Element | null): void {
const current = getActiveElement(document);
if (to === current) {
return;
}

setActiveElement(document, null);

if (current !== null) {
dispatch(current, {
event: new FocusEvent('blur', {
relatedTarget: to,
}),
isTrusted: true,
});

dispatch(current, {
event: new FocusEvent('focusout', {
relatedTarget: to,
}),
isTrusted: true,
});
}

setActiveElement(document, to);

if (to !== null) {
dispatch(to, {
event: new FocusEvent('focus', {
relatedTarget: current,
}),
isTrusted: true,
});
dispatch(to, {
event: new FocusEvent('focusin', {
relatedTarget: current,
}),
isTrusted: true,
});
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice to wrap this up in a FocusManager state machine? could give us a good extension point for implementing tab behavior

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@natemoo-re How about we do that with this one #127

Then we'll have all the pieces and we can put them together in a separate module.

@dreyfus92 dreyfus92 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome job @cowboyd, left a few comments 😄

Comment thread packages/ui/src/dom.ts Resolved
Comment thread packages/ui/src/dom.ts Resolved
@cowboyd
cowboyd requested a review from natemoo-re December 5, 2026 19:16

@natemoo-re natemoo-re left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@cowboyd
cowboyd merged commit 4878f79 into main Dec 5, 2026
9 checks passed
@cowboyd
cowboyd deleted the dom-focus branch December 5, 2026 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

input focus, keyboard, editing, mouse, anything coming from the release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants