Table of Contents

Class TwPopoverPickerComponentBase

Namespace
TwBlazor
Assembly
TwBlazor.dll

Shared focus-management and JS-interop plumbing for text-editable "combobox" pickers that open a popover panel on focus (TwDatePicker, TwTimePicker): a Tab focus trap and background inert-ing while the panel is open, an outside-click handler that closes it, and restoring focus to the trigger once it does. Each derived picker still owns its own value parsing/formatting and OnAfterRenderAsync override (their native-vs-custom picker detection differs slightly in what happens afterward), but the open/close mechanics themselves are identical, so they live here once instead of being copy-pasted per picker.

public abstract class TwPopoverPickerComponentBase : TwBlazorTextInputComponentBase, IComponent, IHandleEvent, IHandleAfterRender, ITwComponent, ITwInputComponent, IAsyncDisposable
Inheritance
TwPopoverPickerComponentBase
Implements
Derived
Inherited Members

Constructors

TwPopoverPickerComponentBase()

protected TwPopoverPickerComponentBase()

Fields

FocusReturnToken

Opaque token (captured via JS interop from the element focused just before the panel opened, almost always the trigger textfield) used to restore focus there once the panel closes.

protected string? FocusReturnToken

Field Value

string

InputRoot

Reference to the TwInputRoot component instance, used to access the root DOM element for JS interop.

protected TwInputRoot? InputRoot

Field Value

TwInputRoot

PanelRef

Reference to the popover panel element, used to move focus into it and to trap Tab navigation while it's open.

protected ElementReference PanelRef

Field Value

ElementReference

PendingOpenFocus

Set when the panel opens so the next OnAfterRenderAsync moves focus into it once (TwColorPicker, TwSelect<T>). Pickers whose trigger stays a text-editable combobox (TwDatePicker, TwTimePicker, and their range/date-time variants) don't use this - focus has to stay on the input for typing to work, so users move into the panel explicitly (Tab, a click, or an arrow key) instead.

protected bool PendingOpenFocus

Field Value

bool

Remarks

Not used to gate the Tab focus trap, background inert-ing, or panel positioning - those must re-run on every render while the panel is open rather than only once, since a stray render triggered by unrelated state (e.g. the previous close's own trailing re-render still in flight) could otherwise consume a one-shot flag against a stale PanelRef before the render that actually mounts the new panel gets a chance to run it.

PendingPanelFocus

Set when the panel should take focus on the next render: after it was opened from the keyboard (Arrow Down on the trigger) or from the trigger's icon button. Opening with a click leaves focus in the text field so the user can keep typing.

protected bool PendingPanelFocus

Field Value

bool

UseNativePicker

Indicates whether the browser's native picker UI is being used instead of the custom popover, either because PreferNativePicker was explicitly set or because the client platform (iOS/Android) was detected via JS interop.

protected bool UseNativePicker

Field Value

bool

Properties

JSRuntime

Gets or sets the JavaScript runtime instance used for interop operations.

[Inject]
public IJSRuntime JSRuntime { get; set; }

Property Value

IJSRuntime

PreferNativePicker

Overrides automatic device detection for whether the browser's native picker should be used instead of the custom popover. Leave unset (null) to auto-detect based on the client platform (iOS and Android use the native picker by default).

[Parameter]
public bool? PreferNativePicker { get; set; }

Property Value

bool?

iconExpanded

The aria-expanded value for the trigger's icon button, which opens and closes the panel. Omitted when the browser's native picker is in use, since the button then only moves focus.

protected string? iconExpanded { get; }

Property Value

string

isFocused

Determines whether the popover panel is currently shown.

protected bool isFocused { get; set; }

Property Value

bool

panelId

The id of the popover panel element, referenced by the trigger's aria-controls while the panel is open.

protected string panelId { get; }

Property Value

string

triggerAttributes

Extra ARIA attributes forwarded onto the trigger textfield's rendered <input> so assistive technology knows it opens a popover dialog and whether that dialog is currently open. Omitted when the native browser picker is in use, since no custom dialog will appear.

protected Dictionary<string, object> triggerAttributes { get; }

Property Value

Dictionary<string, object>

triggerInputRef

Reference to the trigger textfield's actual <input> element, supplied by derived pickers (TwDatePicker, TwTimePicker) that render a TwTextfield<T> trigger. Used by OnIconClickAsync() to focus that element directly when the browser's native picker is in use.

protected virtual ElementReference? triggerInputRef { get; }

Property Value

ElementReference?

Methods

ApplyPanelTrapAsync(ElementReference)

Arms the Tab focus trap on panel and makes everything outside it inert. Every picker calls this from its own OnAfterRenderAsync whenever its panel is open.

protected Task ApplyPanelTrapAsync(ElementReference panel)

Parameters

panel ElementReference

Returns

Task

Close()

Closes the picker's popover panel and cleans up JavaScript event handlers.

[JSInvokable("Close")]
public override Task Close()

Returns

Task

Remarks

This method is invoked from JavaScript when a click outside the picker is detected. It sets isFocused to false, unregisters the outside click handler, and triggers a UI refresh.

ClosePanelAfterTextCommitAsync()

Closes the panel after the user committed text typed into the trigger (Enter, or leaving the field).

protected Task ClosePanelAfterTextCommitAsync()

Returns

Task

Remarks

When the panel was open, focus goes back to the trigger. Tab from the trigger moves into the panel, which this call is about to remove, so leaving focus where it was would drop it on the page body. When the panel was already closed, focus has moved on by itself and is left alone.

ClosePanelAsync()

Closes the panel and returns focus to whatever opened it.

protected Task ClosePanelAsync()

Returns

Task

DisposeAsync()

Disposes of the component's resources asynchronously.

public override ValueTask DisposeAsync()

Returns

ValueTask

Remarks

This method ensures that JavaScript event handlers are unregistered and the .NET object reference is disposed, even if the component is removed from the DOM without Close() being called. This prevents memory leaks and orphaned event listeners.

OnAfterRenderAsync(bool)

Lifts the background inert-ing if it is still on after the panel has closed.

protected override Task OnAfterRenderAsync(bool firstRender)

Parameters

firstRender bool

Returns

Task

Remarks

Picking a value closes the panel through ReleasePanelTrapAsync(), which awaits several JS calls. The component re-renders while those are in flight, still with the panel open, and that render's OnAfterRenderAsync arms the trap again after the release has already cleared it. Nothing runs after the panel is gone to undo that, so the rest of the page (inside a dialog, its Save and Cancel buttons) stayed inert and unclickable. The render that removes the panel always follows, so checking here catches it.

OnIconClickAsync()

Handles the trigger's icon button: opens the panel and moves focus into it, or closes the panel when it is already open.

protected Task OnIconClickAsync()

Returns

Task

Remarks

With the browser's native picker there is no panel to open, so the button moves focus to the trigger input instead. It focuses triggerInputRef directly when a derived picker supplies one, rather than scanning the whole InputRoot for its first focusable element, which would find the icon button itself.

OnIconKeyDownAsync(KeyboardEventArgs)

Handles keydown events on the trigger icon so it's operable from the keyboard (Enter and Space act as a click), since it's a <div> rather than a native button.

protected Task OnIconKeyDownAsync(KeyboardEventArgs e)

Parameters

e KeyboardEventArgs

Returns

Task

OnPanelKeyDownAsync(KeyboardEventArgs)

Handles keydown events on the popover panel, closing it (and restoring focus to the trigger) when Escape is pressed.

protected Task OnPanelKeyDownAsync(KeyboardEventArgs e)

Parameters

e KeyboardEventArgs

Returns

Task

OnTriggerClickAsync()

Handles a click on the trigger input, opening the popover panel. Focus stays in the input so the user can still type a value.

protected Task OnTriggerClickAsync()

Returns

Task

OnTriggerKeyDownAsync(KeyboardEventArgs)

Handles keydown events on the trigger input. Arrow Down opens the panel and moves focus into it, and Escape closes an open panel.

protected Task OnTriggerKeyDownAsync(KeyboardEventArgs e)

Parameters

e KeyboardEventArgs

Returns

Task

Remarks

The panel does not open when the input merely receives focus: that would block the rest of the page for anyone tabbing through a form.

OpenPanelAsync()

Opens the popover panel.

protected virtual Task OpenPanelAsync()

Returns

Task

Remarks

Sets isFocused to true and registers an outside click handler to detect clicks outside the component. If the component is readonly, disabled, or the native picker is in use, the custom popover panel will not be shown.

RegisterOutsideClickAsync()

Registers a JavaScript event handler to detect clicks outside the picker component.

protected Task RegisterOutsideClickAsync()

Returns

Task

Remarks

This method is called when the picker is focused. It ensures the handler is only registered once by checking the TwBlazor.TwPopoverPickerComponentBase.registeredOutsideHandler flag.

RegisterPanelScrollBehaviorAsync(ElementReference, bool)

Positions panel against InputRoot and keeps it correct for as long as it stays open (see twPicker.registerScrollReposition): on desktop, a page scroll or window resize closes the panel instead of chasing the trigger around the viewport, since scrolling away from the field being edited reads as the user moving on; on mobile, the panel keeps repositioning itself to stay glued to the trigger instead, since there a page scroll is how touch users reveal more of a panel taller than the viewport. Call once per open from OnAfterRenderAsync, alongside the Tab focus trap and background inert-ing.

protected Task RegisterPanelScrollBehaviorAsync(ElementReference panel, bool matchAnchorWidth = false)

Parameters

panel ElementReference

The popover panel element to position and track.

matchAnchorWidth bool

Whether the panel's width should be set to match InputRoot's trigger width in pixels, rather than keeping its own natural/CSS width - see TwSelect<T>.

Returns

Task

ReleasePanelTrapAsync()

Releases the Tab focus trap, clears background inert-ing, and unregisters the scroll/resize listener that kept the panel glued to its trigger while open (registered, along with the panel's initial position, by each derived picker's own OnAfterRenderAsync via twPicker.registerScrollReposition; safe to call even for a picker that never registered one). Must be called (and awaited) while the panel is still mounted - i.e. before isFocused is set to false - since it needs PanelRef to still resolve to a live DOM node.

protected Task ReleasePanelTrapAsync()

Returns

Task

RestoreFocusAsync()

Restores focus to whatever element was focused (captured via FocusReturnToken) right before the panel opened, typically this component's own trigger textfield. No-ops if no token was captured (e.g. the panel is being closed a second time).

protected Task RestoreFocusAsync()

Returns

Task

UnregisterOutsideClickAsync()

Unregisters the JavaScript outside click handler and disposes of the .NET object reference.

protected Task UnregisterOutsideClickAsync()

Returns

Task

Remarks

This method should be called when the picker is closed to prevent memory leaks and remove event listeners.