Base UI events
Change events such asonOpenChange, onValueChange, and onPressedChange are custom to Base UI. They can be emitted by various different DOM events, effects, or even during rendering.
Base UI event signatures
eventDetails property is passed as a second argument to Base UI event handlers. This enables the event to be customized, and also allows you to conditionally run side effects based on the reason or DOM event that caused the change.
eventDetails object
reasonis used to determine why the change event occurred, which can be useful to conditionally run certain side effects. Most IDEs show the possible string values after typingreason === '.eventis the native DOM event that caused the change.cancelstops the component from changing its internal state.allowPropagationallows the DOM event to propagate in cases where Base UI stops the propagation.isCanceledindicates whether the change event has been canceled.isPropagationAllowedindicates whether the DOM event is allowed to propagate.
Canceling a Base UI event
An event can be canceled with thecancel() method on eventDetails:
Prevent a tooltip from closing when pressing the trigger
Allowing propagation of the DOM event
In most components, pressing the Esc key stops the propagation of the event so parent popups don’t close simultaneously. This can also be customized with theallowPropagation() method:
Allowing propagation of the event
Preventing Base UI from handling a React event
To prevent Base UI from handling a React event likeonClick, you can use the preventBaseUIHandler() method on the event object:
Prevent Base UI's default behavior
Controlling components with state
Change event handlers enable a component to be controlled with your own external state. Components are uncontrolled by default, meaning that they manage their own state internally.Uncontrolled dialog
open or value, and the state’s setter to its corresponding change handler, such as onOpenChange or onValueChange.
For instance, you can open Dialog after a timeout, without a trigger:
Controlled dialog