Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions crates/bevy_ui/src/widget/button.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
use crate::{FocusPolicy, Interaction, Node};
use accesskit::Role;
use bevy_a11y::AccessibilityNode;
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};

/// Marker struct for buttons
/// Unified button component that combines UI layout, interaction handling, focus management,
/// and accessibility support.
///
/// This component automatically includes all necessary components for a functional button
/// through the `#[require]` attribute:
/// - [`Node`]: For UI layout
/// - [`FocusPolicy::Block`]: For focus management
/// - [`Interaction`]: For tracking interaction state
/// - [`AccessibilityNode`]: For screen reader support
///
/// # Interactive Behavior
///
/// To enable full interactive behavior (pressed state, activation events), you need to:
/// 1. Add the [`bevy_ui_widgets::ButtonPlugin`] to your app
/// 2. Listen for [`bevy_ui_widgets::Activate`] events
/// ```
#[derive(Component, Debug, Default, Clone, Copy, PartialEq, Eq, Reflect)]
#[reflect(Component, Default, Debug, PartialEq, Clone)]
#[require(Node, FocusPolicy::Block, Interaction)]
#[require(
Node,
FocusPolicy::Block,
Interaction,
AccessibilityNode(accesskit::Node::new(Role::Button))
)]
pub struct Button;
11 changes: 2 additions & 9 deletions crates/bevy_ui_widgets/src/button.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use accesskit::Role;
use bevy_a11y::AccessibilityNode;
use bevy_app::{App, Plugin};
use bevy_ecs::query::Has;
use bevy_ecs::{
component::Component,
entity::Entity,
observer::On,
query::With,
Expand All @@ -17,12 +14,8 @@ use bevy_ui::{InteractionDisabled, Pressed};

use crate::Activate;

/// Headless button widget. This widget maintains a "pressed" state, which is used to
/// indicate whether the button is currently being pressed by the user. It emits an [`Activate`]
/// event when the button is un-pressed.
#[derive(Component, Default, Debug)]
#[require(AccessibilityNode(accesskit::Node::new(Role::Button)))]
pub struct Button;
// Re-export the Button component from bevy_ui
pub use bevy_ui::widget::Button;

fn button_on_key_event(
mut event: On<FocusedInput<KeyboardInput>>,
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_ui_widgets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
//! We are actively seeking feedback on the design and implementation of this crate, so please
//! file issues or create PRs if you have any comments or suggestions.
//!
//! ## Button Component
//!
//! The [`Button`] component is re-exported from [`bevy_ui::widget::Button`] to provide a unified
//! API. The component itself is defined in `bevy_ui`, while the interactive behavior (plugins and
//! observers) is provided by this crate through [`ButtonPlugin`].
//!
//! ## State Management
//!
//! Most of the widgets use external state management: this means that the widgets do not
Expand Down
Loading