React Floating Action Button - Getting Started
The Floating Action Button (FAB) sits above all screen content and triggers a primary action. FAB either executes a single action or expands to display two to five related actions (speed dial).
In DevExtreme, the FAB acts as a container for SpeedDialAction components.
This tutorial demonstrates how to create single and multi-action FABs, and how to update action sets during screen transitions.
Single Action
A single-action FAB triggers the main screen action:
Add one SpeedDialAction to the page and specify its onClick and icon properties. You can also define a hint.
Set the FAB's position property in GlobalConfig.
The code example is available in the following GitHub repository:
Multiple Actions (Speed Dial)
A FAB can open a menu with multiple related actions (speed dial).
To create a FAB that triggers a speed dial, place multiple SpeedDialAction components on the page. Assign a unique icon and onClick event handler to each action. FAB sorts actions by index.
Define general FAB options in the floatingActionButtonConfig object. Specify position, max speed dial actions, icon, closeIcon, and other properties.
See the code example in the following GitHub repository:
Handle Screen Transitions
In the following example, each screen has a distinct FAB, as each FAB contains only actions for that screen. The DevExtreme TabPanel emulates screen switching:
The following code includes the TabPanel configuration and an empty switchSDA function. Implement switchSDA to control action visibility:
- import React from 'react';
- import 'devextreme/dist/css/dx.material.blue.light.css';
- import './App.css';
- import TabPanel, { Item } from 'devextreme-react/tab-panel';
- function App(): JSX.Element {
- // switchSDAs to be implemented
- return (
- <div id="app-container">
- <<TabPanel onSelectionChanged={switchSDAs}>
- <Item title="Edit tab">
- <p>Edit tab's content</p>
- </Item>
- <Item title="Share tab">
- <p>Share tab's content</p>
- </Item>
- </TabPanel>
- {/* To be implemented */}
- </div>
- );
- }
- export default App;
- .dx-fa-button-icon, .dx-fa-button-icon-close {
- text-align: center;
- }
- p {
- text-align: center;
- }
- #app-container {
- height: 360px;
- width: 320px;
- }
- .dx-tabpanel .dx-tabs-wrapper {
- display: flex;
- flex-flow: row nowrap;
- }
- .dx-tab {
- display: flex;
- flex-flow: row nowrap;
- flex: 1;
- justify-content: center;
- }
The following code creates four SpeedDialActions. At launch, only "Edit" appears. The switchSDA function displays or hides actions depending on the active tab:
- // ...
- import { useCallback, useState } from 'react';
- import TabPanel, { Item } from 'devextreme-react/tab-panel';
- import SpeedDialAction from 'devextreme-react/speed-dial-action';
- import config from 'devextreme/core/config';
- import type { GlobalConfig } from 'devextreme/common';
- import notify from 'devextreme/ui/notify';
- import type { TabPanelTypes } from 'devextreme-react/tab-panel';
- import type { Properties as ToastProperties } from 'devextreme/ui/toast';
- const globalConfig: GlobalConfig = {
- floatingActionButtonConfig: {
- icon: 'share',
- position: {
- my: 'right bottom',
- at: 'right bottom',
- of: '#app-container',
- offset: '-16 -16',
- },
- },
- };
- config(globalConfig);
- function showNotification(message: string): void {
- const options: ToastProperties = {
- message,
- position: {
- my: 'left bottom',
- at: 'left bottom',
- of: '#app-container',
- offset: '16 -16',
- },
- width: 320 * 0.7,
- minWidth: 0,
- };
- notify(options, 'info', 1000);
- }
- function App(): JSX.Element {
- const [currentTab, setCurrentTab] = useState<string>('Edit tab');
- const switchSDAs = useCallback(
- (e: TabPanelTypes.SelectionChangedEvent): void => {
- setCurrentTab(e.addedItems[0].title);
- },
- [],
- );
- const handleEditClick = useCallback((): void => {
- showNotification('Edit is clicked');
- }, []);
- const handleCopyClick = useCallback((): void => {
- showNotification('Copied to clipboard');
- }, []);
- const handleMailClick = useCallback((): void => {
- showNotification('Sent by email');
- }, []);
- const handleSocialClick = useCallback((): void => {
- showNotification('Shared on Social Media');
- }, []);
- return (
- <div id="app-container">
- <!-- TabPanel configuration -->
- <SpeedDialAction
- hint="Edit"
- icon="edit"
- visible={currentTab === 'Edit tab'}
- onClick={handleEditClick}
- />
- <SpeedDialAction
- hint="Copy to clipboard"
- icon="copy"
- visible={currentTab === 'Share tab'}
- onClick={handleCopyClick}
- />
- <SpeedDialAction
- hint="Send by email"
- icon="email"
- visible={currentTab === 'Share tab'}
- onClick={handleMailClick}
- />
- <SpeedDialAction
- hint="Share on Social Media"
- icon="link"
- visible={currentTab === 'Share tab'}
- onClick={handleSocialClick}
- />
- </div>
- );
- }
- export default App;
You can find the full code in the following GitHub repository:
For more information on the Floating Action Button's functionality, explore the following resources: