# FocusTrap > A utility component that traps keyboard focus within its children, preventing focus from escaping to elements outside the trap. The FocusTrap component is part of SummitUI, a Blazor component library focused on WCAG-compliant, fully customizable headless components. ## Features - Traps Tab and Shift+Tab navigation within boundaries - Automatic focus on activation - Returns focus on deactivation - Handles edge cases (no focusable elements) - Programmatic focus control methods - Activation/deactivation callbacks ## Installation ```bash dotnet add package SummitUI ``` ## Anatomy Import the component and wrap your content: ```razor @using SummitUI.Components.Utilities
``` ## API Reference ### FocusTrap | Property | Type | Default | Description | |----------|------|---------|-------------| | ChildContent | RenderFragment? | null | Content to render within the trap | | IsActive | bool | true | Whether trap is active | | AutoFocus | bool | true | Auto-focus first element when activated | | ReturnFocus | bool | true | Return focus to previously focused element on deactivation | | OnActivated | EventCallback | - | Callback when trap activates | | OnDeactivated | EventCallback | - | Callback when trap deactivates | ### Public Methods | Method | Returns | Description | |--------|---------|-------------| | FocusFirstAsync() | ValueTask | Manually focus the first focusable element | | FocusLastAsync() | ValueTask | Manually focus the last focusable element | ## Examples ### Basic Dialog ```razor @code { private bool showDialog = false; } @if (showDialog) {
} @code { private void HandleConfirm() { // Handle confirmation showDialog = false; } } ``` ### Controlled Activation Toggle the focus trap on and off. ```razor @code { private bool isTrapActive = false; }

Focus Trap Area

``` ### Programmatic Focus Control Manually focus the first or last element. ```razor @code { private FocusTrap? focusTrap; }
@code { private async Task FocusFirst() { if (focusTrap != null) { await focusTrap.FocusFirstAsync(); } } private async Task FocusLast() { if (focusTrap != null) { await focusTrap.FocusLastAsync(); } } } ``` ## Styling ### CSS Example Common styles for dialogs and overlays used with FocusTrap: ```css .su-dialog-overlay { position: fixed; inset: 0; background: rgb(var(--su-foreground) / 0.5); display: flex; align-items: center; justify-content: center; z-index: 1000; } .su-dialog { background: rgb(var(--su-card)); padding: 24px; border-radius: 8px; border: 1px solid rgb(var(--su-border)); box-shadow: 0 4px 20px rgb(var(--su-foreground) / 0.2); max-width: 400px; width: 100%; } ``` ## Accessibility ### Best Practices - **Always use with modals/dialogs:** Focus trapping is essential for modal accessibility - **Provide an escape route:** Always include a way to close/deactivate the trap (close button, Escape key) - **Use with `role="dialog"`:** Combine with proper ARIA roles for screen readers - **Return focus:** Keep `ReturnFocus` enabled to maintain user context ### Screen Reader Considerations - The focus trap should be used in conjunction with `aria-modal="true"` for dialogs - Include `aria-labelledby` or `aria-label` on the dialog container - Announce the dialog opening/closing to screen readers if needed