JavaScript/jQuery 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:
- $(function() {
- $("#tab-panel").dxTabPanel({
- items: [{
- title: "Edit Tab",
- template: function() {
- return "<p>Edit tab's content</p>";
- }
- }, {
- title: "Share Tab",
- template: function() {
- return "<p>Share tab's content</p>";
- }
- }],
- onSelectionChanged: function(e) {
- switchSDAs(e.addedItems[0].title);
- }
- });
- function switchSDAs(tabTitle) {
- // To be implemented
- }
- });
- <html>
- <head>
- <!-- ... -->
- <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
- <!-- DevExtreme resources -->
- <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/26.1.6/css/dx.material.blue.light.css">
- <script type="text/javascript" src="https://cdn3.devexpress.com/jslib/26.1.6/js/dx.all.js"></script>
- <link rel="stylesheet" href="index.css">
- <script type="text/javascript" src="index.js"></script>
- </head>
- <body class="dx-viewport">
- <div id="app-container">
- <div id="tab-panel"></div>
- <div id="action-edit"></div>
- <div id="action-mail"></div>
- <div id="action-copy"></div>
- <div id="action-facebook"></div>
- </div>
- </body>
- </html>
- .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:
- $(function() {
- // TabPanel is configured here
- // ...
- DevExpress.config({
- floatingActionButtonConfig: {
- icon: "share",
- position: {
- my: "right bottom",
- at: "right bottom",
- of: "#app-container",
- offset: "-16 -16"
- }
- }
- });
- const editAction = $("#action-edit").dxSpeedDialAction({
- hint: "Edit",
- icon: "edit",
- onClick: function() {
- showNotification("Edit is clicked")
- }
- }).dxSpeedDialAction("instance");
- const copyAction = $("#action-copy").dxSpeedDialAction({
- hint: "Copy to clipboard",
- icon: "copy",
- visible: false,
- onClick: function() {
- showNotification("Copied to clipboard")
- }
- }).dxSpeedDialAction("instance");
- const mailAction = $("#action-mail").dxSpeedDialAction({
- hint: "Send by email",
- icon: "email",
- visible: false,
- onClick: function() {
- showNotification("Sent by email")
- }
- }).dxSpeedDialAction("instance");
- const facebookAction = $("#action-facebook").dxSpeedDialAction({
- hint: "Share on Facebook",
- icon: "link",
- visible: false,
- onClick: function() {
- showNotification("Shared on Facebook")
- }
- }).dxSpeedDialAction("instance");
- function switchSDAs(tabTitle) {
- if(tabTitle === "Edit Tab") {
- editAction.option("visible", true);
- copyAction.option("visible", false);
- mailAction.option("visible", false);
- facebookAction.option("visible", false);
- }
- if(tabTitle === "Share Tab") {
- editAction.option("visible", false);
- copyAction.option("visible", true);
- mailAction.option("visible", true);
- facebookAction.option("visible", true);
- }
- }
- function showNotification(message) {
- DevExpress.ui.notify({
- message: message,
- position: {
- my: "left bottom",
- at: "left bottom",
- of: "#app-container",
- offset: "16 -16"
- },
- minWidth: null,
- width: function() {
- return $("#app-container").width() * 0.7;
- }
- }, "info", 1000);
- }
- });
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: