Focus

Focus scope primitives for trapping and restoring Roblox selection.

Stable direction
@lattice-ui/focus

Part of the stable direction toward v1.0.

What It Is For

Use FocusScope when a section of the UI needs to trap GuiService.SelectedObject and optionally restore focus when that scope deactivates.

It is a foundation package for overlays and composite interaction regions rather than a styled control on its own.

TSX Example Preview

Preview the first lines below, or expand to inspect the full source file.

focus.tsx
TSX Preview
import { FocusScope } from "@lattice-ui/focus";
import React from "@rbxts/react";

export function FocusExample() {
	const [active, setActive] = React.useState(true);

...

Install

Global CLI command: lattice add focus

Monorepo local script

Use your package manager wrapper when running the local lattice command.

pnpm lattice add focus

npm package: @lattice-ui/focus

Peer dependencies

  • @rbxts/react
  • @rbxts/react-roblox

Public Exports

  • FocusScope

State Model

  • FocusScope tracks whether it is active, whether focus should be trapped, and whether prior focus should be restored on teardown.
  • Nested trapped scopes are stack-based, so only the top-most active scope redirects outside selections.
  • The scope model is built around Roblox selection focus, not browser-like DOM focus graphs.

Key API

FocusScope

Use active, trapped, restoreFocus, and asChild to determine how the scope wraps and controls its descendants.

Composition Patterns

Modal or menu internals

Wrap overlay content in FocusScope when leaving the selection region would create invalid or confusing interaction.

Temporary keyboard islands

Use an active trapped scope for short-lived panels that should return the user to the previous selection when they close.

Cautions / Limits

  • Current focus management is based on GuiService.SelectedObject; it does not rewrite NextSelection* relationships for you.
  • Use it for focus ownership, not as a general-purpose navigation system.

Decision Guides