Vue 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:
- <template>
- <div id="app-container">
- <DxTabPanel
- @selection-changed="switchSDAs">
- <DxItem title="Edit tab">
- <template #default>
- <p>Edit tab's content</p>
- </template>
- </DxItem>
- <DxItem title="Share tab">
- <template #default>
- <p>Share tab's content</p>
- </template>
- </DxItem>
- </DxTabPanel>
- <!-- To be implemented -->
- </div>
- </template>
- <script setup lang="ts">
- import 'devextreme/dist/css/dx.material.blue.light.css';
- import DxTabPanel, { DxItem } from 'devextreme-vue/tab-panel';
- // switchSDAs to be implemented
- </script>
- <style>
- .dx-fa-button-icon, .dx-fa-button-icon-close {
- text-align: center;
- }
- p {
- text-align: center;
- }
- #app-container {
- height: 360px;
- width: 320px;
- border: 1px solid black;
- }
- .dx-tabpanel .dx-tabs-wrapper {
- display: flex;
- flex-flow: row nowrap;
- }
- .dx-tab {
- display: flex;
- flex-flow: row nowrap;
- flex: 1;
- justify-content: center;
- }
- </style>
The following code creates four SpeedDialActions. At launch, only "Edit" appears. The switchSDA function displays or hides actions depending on the active tab:
- <template>
- <div id="app-container">
- <!-- TabPanel is configured here -->
- <DxSpeedDialAction
- hint="Edit"
- icon="edit"
- :visible="currentTab === 'Edit tab'"
- @click="showNotification('Edit is clicked')"
- />
- <DxSpeedDialAction
- hint="Copy to clipboard"
- icon="copy"
- :visible="currentTab === 'Share tab'"
- @click="showNotification('Copied to clipboard')"
- />
- <DxSpeedDialAction
- hint="Send by email"
- icon="email"
- :visible="currentTab === 'Share tab'"
- @click="showNotification('Sent by email')"
- />
- <DxSpeedDialAction
- hint="Share on Facebook"
- icon="link"
- :visible="currentTab === 'Share tab'"
- @click="showNotification('Shared on Facebook')"
- />
- </div>
- </template>
- <script setup lang="ts">
- import { ref } from 'vue';
- import { DxTabPanel, DxItem } from 'devextreme-vue/tab-panel';
- import DxSpeedDialAction from 'devextreme-vue/speed-dial-action';
- import type { DxTabPanelTypes } from 'devextreme-vue/tab-panel';
- import type { GlobalConfig } from 'devextreme/common';
- import type { Properties as ToastProperties } from 'devextreme/ui/toast';
- import notify from 'devextreme/ui/notify';
- import config from 'devextreme/core/config';
- import type { TabItem } from '../types';
- const floatingActionButtonConfig: GlobalConfig = {
- floatingActionButtonConfig: {
- icon: 'share',
- position: {
- my: 'right bottom',
- at: 'right bottom',
- of: '#app-container',
- offset: '-16 -16',
- },
- },
- };
- config(floatingActionButtonConfig);
- const currentTab = ref<string>('Edit tab');
- const switchSDAs = (e: DxTabPanelTypes.SelectionChangedEvent): void => {
- const addedItem = e.addedItems[0] as TabItem;
- currentTab.value = addedItem.title;
- };
- const 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);
- };
- </script>
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: