Skip to contents
This page is an adaptation of the related MUI Material UI documentation page.
## Warning: package 'shiny' was built under R version 4.5.2

Floating Action Button

A Floating Action Button (FAB) performs the primary, or most common, action on a screen.

A floating action button appears in front of all screen content, typically as a circular shape with an icon in its center. FABs come in two types: regular, and extended.

Only use a FAB if it is the most suitable way to present a screen’s primary action. Only one component is recommended per screen to represent the most common action.

Basic FAB

CssBaseline(
  Box(
    sx = list('& > :not(style)' = list(m = 1)),
    Fab(
      color = "primary",
      `aria-label` = "add",
      shiny::icon("plus")
    ),
    Fab(
      color = "secondary",
      `aria-label` = "edit",
      shiny::icon("edit")
    ),
    Fab(
      variant = "extended",
      shiny::icon("location-arrow", class = "fa-sm", style = "margin-right: 8px;"),
      "Navigate"
    ),
    Fab(
      disabled = TRUE,
      `aria-label` = "like",
      shiny::icon("heart")
    )
  )
)
JS code
import * as React from 'react';
import Box from '@mui/material/Box';
import Fab from '@mui/material/Fab';
import AddIcon from '@mui/icons-material/Add';
import EditIcon from '@mui/icons-material/Edit';
import FavoriteIcon from '@mui/icons-material/Favorite';
import NavigationIcon from '@mui/icons-material/Navigation';

export default function FloatingActionButtons() {
  return (
    <Box sx={{ '& > :not(style)': { m: 1 } }}>
      <Fab color="primary" aria-label="add">
        <AddIcon />
      </Fab>
      <Fab color="secondary" aria-label="edit">
        <EditIcon />
      </Fab>
      <Fab variant="extended">
        <NavigationIcon sx={{ mr: 1 }} />
        Navigate
      </Fab>
      <Fab disabled aria-label="like">
        <FavoriteIcon />
      </Fab>
    </Box>
  );
}

Size

By default, the size is large. Use the size prop for smaller floating action buttons.

CssBaseline(
  Box(
    sx = list('& > :not(style)' = list(m = 1)),
    Fab(
      size = "small",
      color = "secondary",
      `aria-label` = "add",
      shiny::icon("plus")
    ),
    Fab(
      size = "medium",
      color = "secondary",
      `aria-label` = "add",
      shiny::icon("plus")
    ),
    Fab(
      color = "secondary",
      `aria-label` = "add",
      shiny::icon("plus")
    )
  )
)
JS code
import * as React from 'react';
import Box from '@mui/material/Box';
import Fab from '@mui/material/Fab';
import AddIcon from '@mui/icons-material/Add';

export default function FloatingActionButtonSize() {
  return (
    <Box sx={{ '& > :not(style)': { m: 1 } }}>
      <Fab size="small" color="secondary" aria-label="add">
        <AddIcon />
      </Fab>
      <Fab size="medium" color="secondary" aria-label="add">
        <AddIcon />
      </Fab>
      <Fab color="secondary" aria-label="add">
        <AddIcon />
      </Fab>
    </Box>
  );
}
CssBaseline(
  Box(
    sx = list('& > :not(style)' = list(m = 1)),
    Fab(
      variant = "extended",
      size = "small",
      color = "primary",
      shiny::icon("location-arrow", class = "fa-sm", style = "margin-right: 8px;"),
      "Extended"
    ),
    Fab(
      variant = "extended",
      size = "medium",
      color = "primary",
      shiny::icon("location-arrow", class = "fa-sm", style = "margin-right: 8px;"),
      "Extended"
    ),
    Fab(
      variant = "extended",
      color = "primary",
      shiny::icon("location-arrow", class = "fa-sm", style = "margin-right: 8px;"),
      "Extended"
    )
  )
)
JS code
import * as React from 'react';
import Box from '@mui/material/Box';
import Fab from '@mui/material/Fab';
import NavigationIcon from '@mui/icons-material/Navigation';

export default function FloatingActionButtonExtendedSize() {
  return (
    <Box sx={{ '& > :not(style)': { m: 1 } }}>
      <Fab variant="extended" size="small" color="primary">
        <NavigationIcon sx={{ mr: 1 }} />
        Extended
      </Fab>
      <Fab variant="extended" size="medium" color="primary">
        <NavigationIcon sx={{ mr: 1 }} />
        Extended
      </Fab>
      <Fab variant="extended" color="primary">
        <NavigationIcon sx={{ mr: 1 }} />
        Extended
      </Fab>
    </Box>
  );
}

Animation

The floating action button animates onto the screen as an expanding piece of material, by default.

A floating action button that spans multiple lateral screens (such as tabbed screens) should briefly disappear, then reappear if its action changes.

The Zoom transition can be used to achieve this. Note that since both the exiting and entering animations are triggered at the same time, we use enterDelay to allow the outgoing Floating Action Button’s animation to finish before the new one enters.

library(muiMaterial)
library(shiny)

ui <- TabContext.shinyInput(
  inputId = "TabContextExample", # with observer in server
  value = "value1",
  Box(
    sx = list(borderBottom = 1, borderColor = 'divider'),
    TabList.shinyInput(
      inputId = "tabListExample",
      value = "value1",
      Tab(label="Item One", value = "value1"),
      Tab(label="Item Two", value = "value2"),
      Tab(label="Item Three", value = "value3")
    )
  ),
  TabPanel.shinyInput(
    inputId = "tab1", 
    value = "value1",
    Typography("Item 1"),
    Box(
      sx = list(
        height = 200,
        transform = "translateZ(0px)",
        flexGrow = 1
      ),
      Zoom(
        `in` = TRUE,
        timeout = list(enter = 500),
        style = list(
          transitionDelay = "500ms"
        ),
        unmountOnExit = TRUE,
        Fab(
          sx = list(position = "absolute", bottom = 16, right = 16),
          color = "secondary",
          `aria-label` = "edit",
          shiny::icon("edit")
        )
      )
    )
  ),
  TabPanel.shinyInput(
    inputId = "tab2", 
    value = "value2",
    Typography("Item 2"),
    Box(
      sx = list(
        height = 200,
        transform = "translateZ(0px)",
        flexGrow = 1
      ),
      Zoom(
        `in` = TRUE,
        timeout = list(enter = 500),
        style = list(
          transitionDelay = "500ms"
        ),
        unmountOnExit = TRUE,
        Fab(
          sx = list(position = "absolute", bottom = 16, right = 16),
          color = "secondary",
          `aria-label` = "edit",
          shiny::icon("edit")
        )
      )
    )
  ),
  TabPanel.shinyInput(
    inputId = "tab3", 
    value = "value3",
    Typography("Item 3"),
    Box(
      sx = list(
        height = 200,
        transform = "translateZ(0px)",
        flexGrow = 1
      ),
      Zoom(
        `in` = TRUE,
        timeout = list(enter = 500),
        style = list(
          transitionDelay = "500ms"
        ),
        unmountOnExit = TRUE,
        Fab(
          sx = list(position = "absolute", bottom = 16, right = 16),
          color = "inherit",
          `aria-label` = "zoom",
          shiny::icon("plus")
        )
      )
    )
  )
)

server <- function(input, output, session) {
  observe({
    updateTabContext.shinyInput(
      inputId = "TabContextExample", 
      value = input$tabListExample
    )
  })
}

shinyApp(ui, server)
JS code
import * as React from 'react';
import PropTypes from 'prop-types';
import { useTheme } from '@mui/material/styles';
import AppBar from '@mui/material/AppBar';
import Tabs from '@mui/material/Tabs';
import Tab from '@mui/material/Tab';
import Box from '@mui/material/Box';
import Fab from '@mui/material/Fab';
import AddIcon from '@mui/icons-material/Add';
import EditIcon from '@mui/icons-material/Edit';
import UpIcon from '@mui/icons-material/KeyboardArrowUp';
import { green } from '@mui/material/colors';
import Zoom from '@mui/material/Zoom';

function TabPanel(props) {
  const { children, value, index, ...other } = props;

  return (
    <div
      role="tabpanel"
      hidden={value !== index}
      id={`action-tabpanel-${index}`}
      aria-labelledby={`action-tab-${index}`}
      {...other}
    >
      {value === index && <Box sx={{ p: 3 }}>{children}</Box>}
    </div>
  );
}

TabPanel.propTypes = {
  children: PropTypes.node,
  index: PropTypes.number.isRequired,
  value: PropTypes.number.isRequired,
};

function a11yProps(index) {
  return {
    id: `action-tab-${index}`,
    'aria-controls': `action-tabpanel-${index}`,
  };
}

const fabStyle = {
  position: 'absolute',
  bottom: 16,
  right: 16,
};

const fabGreenStyle = {
  color: 'common.white',
  bgcolor: green[500],
  '&:hover': {
    bgcolor: green[600],
  },
};

export default function FloatingActionButtonZoom() {
  const theme = useTheme();
  const [value, setValue] = React.useState(0);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  const transitionDuration = {
    enter: theme.transitions.duration.enteringScreen,
    exit: theme.transitions.duration.leavingScreen,
  };

  const fabs = [
    {
      color: 'primary',
      sx: fabStyle,
      icon: <AddIcon />,
      label: 'Add',
    },
    {
      color: 'secondary',
      sx: fabStyle,
      icon: <EditIcon />,
      label: 'Edit',
    },
    {
      color: 'inherit',
      sx: { ...fabStyle, ...fabGreenStyle },
      icon: <UpIcon />,
      label: 'Expand',
    },
  ];

  return (
    <Box
      sx={{
        bgcolor: 'background.paper',
        width: 500,
        position: 'relative',
        minHeight: 200,
      }}
    >
      <AppBar position="static" color="default">
        <Tabs
          value={value}
          onChange={handleChange}
          indicatorColor="primary"
          textColor="primary"
          variant="fullWidth"
          aria-label="action tabs example"
        >
          <Tab label="Item One" {...a11yProps(0)} />
          <Tab label="Item Two" {...a11yProps(1)} />
          <Tab label="Item Three" {...a11yProps(2)} />
        </Tabs>
      </AppBar>
      {fabs.map((fab, index) => (
        <Zoom
          key={fab.color}
          in={value === index}
          timeout={transitionDuration}
          style={{
            transitionDelay: `${value === index ? transitionDuration.exit : 0}ms`,
          }}
          unmountOnExit
        >
          <Fab sx={fab.sx} aria-label={fab.label} color={fab.color}>
            {fab.icon}
          </Fab>
        </Zoom>
      ))}
    </Box>
  );
}