Buttons are essential elements in your application's user interface that users can click to trigger events. This documentation will help you understand how to use button components effectively in your Reflex application.
A basic button component is created using the rc.button
method:
rc.button("Click Me!")
You can change the size of a button by setting the size prop to one of the following
values: xs
,sm
,md
, or lg
.
rc.button_group(
rc.button(
"Example", bg="lightblue", color="black", size="sm"
),
rc.button(
"Example", bg="orange", color="white", size="md"
),
rc.button("Example", color_scheme="red", size="lg"),
)
Customize the appearance of buttons by adjusting their color scheme through the color_scheme prop. You have the flexibility to choose from a range of color scales provided by your design system, such as whiteAlpha, blackAlpha, gray, red, blue, or even utilize your own custom color scale.
rc.button_group(
rc.button(
"White Alpha",
color_scheme="whiteAlpha",
min_width="unset",
),
rc.button(
"Black Alpha",
color_scheme="blackAlpha",
min_width="unset",
),
rc.button(
"Gray", color_scheme="gray", min_width="unset"
),
rc.button("Red", color_scheme="red", min_width="unset"),
rc.button(
"Orange", color_scheme="orange", min_width="unset"
),
rc.button(
"Yellow", color_scheme="yellow", min_width="unset"
),
rc.button(
"Green", color_scheme="green", min_width="unset"
),
rc.button(
"Teal", color_scheme="teal", min_width="unset"
),
rc.button(
"Blue", color_scheme="blue", min_width="unset"
),
rc.button(
"Cyan", color_scheme="cyan", min_width="unset"
),
rc.button(
"Purple", color_scheme="purple", min_width="unset"
),
rc.button(
"Pink", color_scheme="pink", min_width="unset"
),
rc.button(
"LinkedIn",
color_scheme="linkedin",
min_width="unset",
),
rc.button(
"Facebook",
color_scheme="facebook",
min_width="unset",
),
rc.button(
"Messenger",
color_scheme="messenger",
min_width="unset",
),
rc.button(
"WhatsApp",
color_scheme="whatsapp",
min_width="unset",
),
rc.button(
"Twitter", color_scheme="twitter", min_width="unset"
),
rc.button(
"Telegram",
color_scheme="telegram",
min_width="unset",
),
width="100%",
)
You can customize the visual style of your buttons using the variant prop. Here are the available button variants:
ghost
: A button with a transparent background and visible text.outline
: A button with no background color but with a border.solid
: The default button style with a solid background color.link
: A button that resembles a text link.unstyled
: A button with no specific styling.
rc.button_group(
rc.button("Ghost Button", variant="ghost"),
rc.button("Outline Button", variant="outline"),
rc.button("Solid Button", variant="solid"),
rc.button("Link Button", variant="link"),
rc.button("Unstyled Button", variant="unstyled"),
)
Make buttons inactive by setting the is_disabled
prop to True
.
rc.button("Inactive button", is_disabled=True)
To indicate a loading state for a button after it's clicked, you can use the following properties:
is_loading
: Set this property toTrue
to display a loading spinner.loading_text
: Optionally, you can provide loading text to display alongside the spinner.spinner_placement
: You can specify the placement of the spinner element, which is 'start' by default.
rc.button(
"Random button",
is_loading=True,
loading_text="Loading...",
spinner_placement="start",
)
You can define what happens when a button is clicked using the on_click
event argument.
For example, to change a value in your application state when a button is clicked:
In the code above, The value of text_value
is changed through the set_text_value
event handler upon clicking the button.
Reflex provides a default setter event_handler for every base var which can be accessed by prefixing the base var with the set_
keyword.
Here’s another example that creates two buttons to increase and decrease a count value:
In this example, we have a ButtonState
state class that maintains a count base var.
When the "Increment" button is clicked, it triggers the ButtonState.increment
event handler, and when the "Decrement"
button is clicked, it triggers the ButtonState.decrement
event handler.
Buttons in Reflex can trigger special events and server-side actions,
allowing you to create dynamic and interactive user experiences.
You can bind these events to buttons using the on_click
prop.
For a comprehensive list of
available special events and server-side actions, please refer to the
Special Events Documentation for detailed information and usage examples.
In your Reflex application, you can group buttons effectively using the Stack
component and
the ButtonGroup
component. Each of these options offers unique capabilities to help you structure
and style your buttons.
The Stack
component allows you to stack buttons both vertically and horizontally, providing a flexible
layout for your button arrangements.
With the stack
component, you can easily create both vertical and horizontal button arrangements.
The ButtonGroup
component is designed specifically for grouping buttons. It allows you to:
- Set the
size
andvariant
of all buttons within it. - Add
spacing
between the buttons. - Flush the buttons together by removing the border radius of their children as needed.
# The `ButtonGroup` component stacks buttons horizontally, whereas the `Stack` component allows stacking buttons both vertically and horizontally.
rc.Button
The Button component is used to trigger an event or event, such as submitting a form, opening a dialog, canceling an event, or performing a delete operation.