Fab

The Floating Action Button (FAB) is a dynamic button that stays visible and provides access to a primary action throughout the user's journey in the application. It is a powerful UI element that adds a touch of elegance and convenience to the user experience.
Installation
Spec Doc
This is an illustration of Fab component.
size
placement
isHovered
isDisabled
isPressed
<Box
h={360}
w={320}
bg="$backgroundLight50"
$dark-bg="$backgroundDark900"
borderRadius="$md"
>
<Fab
size="md"
placement="bottom right"
isHovered={false}
isDisabled={false}
isPressed={false}
>
<FabIcon as={AddIcon} className="mr-1" />
<FabLabel>Quick start</FabLabel>
</Fab>
</Box>

Installation

Step 1: Install the following dependencies:

npm i @gluestack-ui/fab

Step 2: Copy and paste the following code into your project.

import { createFab } from "@gluestack-ui/fab"
import { Platform, Text, View } from "react-native"
import { Pressable } from "react-native"
import {
tva,
withStyleContext,
withStyleContextAndStates,
useStyleContext,
} from "@gluestack-ui/nativewind-utils"
import React from "react"
import { cssInterop } from "nativewind"
export const UIFab = createFab({
// @ts-ignore
Root:
Platform.OS === "web"
? withStyleContext(Pressable)
: withStyleContextAndStates(Pressable),
Label: Text,
Icon: View,
})
// @ts-ignore
cssInterop(UIFab, { className: "style" })
cssInterop(UIFab.Label, { className: "style" })
cssInterop(UIFab.Icon, { className: "style" })
const fabStyle = tva({
base: "group/fab bg-primary-500 rounded-full z-20 p-4 flex-row items-center justify-center absolute hover:bg-primary-600 active:bg-primary-700 disabled:opacity-40 disabled:pointer-events-all disabled:cursor-not-allowed data-[focus=true]:outline-2 data-[focus=true]:outline-primary-700 data-[focus=true]:outline-solid",
variants: {
size: {
sm: "px-2.5 py-2.5 text-sm",
md: "px-3 py-3 text-md",
lg: "px-4 py-4 text-lg",
},
placement: {
"top right": "top-4 right-4",
"top left": "top-4 left-4",
"bottom right": "bottom-4 right-4",
"bottom left": "bottom-4 left-4",
"top center": "top-4 self-center",
"bottom center": "bottom-4 self-center",
},
},
})
const fabLabelStyle = tva({
base: "text-typography-50 font-normal font-body tracking-md text-left mx-2",
variants: {
isTruncated: {
true: "",
},
bold: {
true: "font-bold",
},
underline: {
true: "underline",
},
strikeThrough: {
true: "line-through",
},
size: {
"2xs": "text-2xs",
xs: "text-xs",
sm: "text-sm",
md: "text-md",
lg: "text-lg",
xl: "text-xl",
"2xl": "text-2xl",
"3xl": "text-3xl",
"4xl": "text-4xl",
"5xl": "text-5xl",
"6xl": "text-6xl",
},
sub: {
true: "text-xs",
},
italic: {
true: "italic",
},
highlight: {
true: "bg-yellow-500",
},
},
parentVariants: {
size: {
sm: "text-sm",
md: "text-md",
lg: "text-lg",
},
},
defaultVariants: {
size: "md",
},
})
const fabIconStyle = tva({
base: "text-typography-50 group-hover/fab:text-typography-0 group-active/fab:text-typography-0",
variants: {
size: {
"2xs": "h-3 w-3",
xs: "h-3.5 w-3.5",
sm: "h-4 w-4",
md: "w-4 h-4",
lg: "h-5 w-5",
xl: "h-6 w-6",
},
},
defaultVariants: {
size: "md",
},
})
const Fab = React.forwardRef(
(
{ size = "md", placement = "bottom right", className, ...props }: any,
ref: any
) => {
return (
<UIFab
ref={ref}
className={fabStyle({ size, placement, class: className })}
context={{ size }}
/>
)
}
)
const FabLabel = React.forwardRef(
(
{
size = "md",
isTruncated = false,
bold = false,
underline = false,
strikeThrough = false,
className,
...props
}: any,
ref: any
) => {
const { size: parentSize } = useStyleContext()
return (
<UIFab.Label
ref={ref}
{...props}
className={fabLabelStyle({
parentVariants: {
size: parentSize,
},
size,
isTruncated,
bold,
underline,
strikeThrough,
class: className,
})}
/>
)
}
)
const FabIcon = React.forwardRef(
({ size = "md", className, as: AsComp, ...props }: any, ref: any) => {
const { size: parentSize } = useStyleContext()
if (AsComp) {
return (
<AsComp
ref={ref}
{...props}
className={fabIconStyle({
parentVariants: {
size: parentSize,
},
size,
class: className,
})}
/>
)
}
return (
<UIFab.Icon
ref={ref}
{...props}
className={fabIconStyle({
parentVariants: {
size: parentSize,
},
size,
class: className,
})}
/>
)
}
)
Fab.displayName = "Fab"
FabLabel.displayName = "FabLabel"
FabIcon.displayName = "FabIcon"
export { Fab, FabLabel, FabIcon }

Step 3: Update the import paths to match your project setup.

API Reference

To use this component in your project, include the following import statement in your file.
import { Fab } from "@gluestack-ui/themed"
export default () => (
<Fab>
<FabLabel />
<FabIcon />
</Fab>
)

Fab

Contains all fab related layout style props and actions. It inherits all the properties of React Native's Pressable component.
Prop
Type
Default
Description
placement
"top left" | "top right" | "bottom left" | "bottom right" | "top center" | "bottom center"
"bottom right"
Placement of the Fab
isHovered
bool
false
To manually set hover to the fab.
isPressed
bool
false
To manually set pressable state to the fab.
isFocused
bool
false
To manually set focused state to the fab.
isDisabled
bool
false
To manually set disable to the fab.

FabLabel

Contains all text related layout style props and actions. It inherits all the properties of React Native's Text component.

FabIcon

Contains all Icon related layout style props and actions. It inherits all the properties of Gluestack Style's AsForwarder component.

Features

  • Keyboard support for actions.
  • Support for hover, focus and active states.
  • Option to add your styles or use the default styles.

Accessibility

We have outlined the various features that ensure the Fab component is accessible to all users, including those with disabilities. These features help ensure that your application is inclusive and meets accessibility standards.Adheres to the WAI-ARIA design pattern

Keyboard

  • Tab: Moves focus to the next focusable element.
  • Shift + Tab: Moves focus to the previous focusable element.
  • Enter: Triggers the fab's action.

Screen Reader

  • VoiceOver: When the fab is focused, the screen reader will announce the fab's label and its current state.

Focus Management

  • The onFocus and onBlur props manage focus states and provide visual cues to users. This is especially important for users who rely on keyboard navigation.

Dependencies

The following are the lists of all the libraries and packages the component relies on. This information will help you ensure that your project has all the necessary dependencies installed to use the component.
  • @gluestack-ui/utils
  • @react-native-aria/focus
  • @react-native-aria/interactions

Props

Fab component is created using Pressable component from react-native. It extends all the props supported by React Native Pressable.
Edit this page on GitHub