Skip to content

Snackbar

Overview

The Snackbar component provides brief, informative messages to users, appearing at the bottom or top of the screen. It is ideal for communicating non-intrusive updates, such as confirming an action or showing temporary feedback.

snackbar gif

Properties

namedescriptiontypedefault
contentRequired. The main text displayed on the Snackbar.string-
actionTitle for the action button, displayed to the right of the content.string-
offsetDistance from the bottom of the screennumber64
durationHow long the Snackbar remains visible (in ms).number2000
showCloseIconWhether to display a close icon on the Snackbar.booleanfalse
closeIconSizeThe size of the close icon.number20
closeIconColorColor of the close icon.ColorValue-
animationDurationDuration of the show/hide animation (in milliseconds).number500
actionStyleCustom styles for the action button text.TextStyle-
contentStyleCustom styles for the content text.TextStyle-
onActionPressCallback function triggered when the action button is pressed.() => void-

API Methods

The SnackbarRef interface provides the following methods:

namedescription
show()Displays the Snackbar on the screen.
dismiss()Dismisses the Snackbar immediately.

Usage

Follow these steps to use the Snackbar component:

  1. Create a ref for the Snackbar using the SnackbarRef interface.
  2. Pass the ref as a prop to the Snackbar component.
  3. Use the show() method from ref.current to display the Snackbar, or use the dismiss() method to hide it.

Code example:

typescript
import React, { useRef } from 'react';
import { Snackbar, type SnackbarRef } from '@computools/react-native-material-components';

const MyComponent = () => {
  const snackbarRef = useRef<SnackbarRef>(null);

  const handleActionPress = () => {
    console.log('Action button pressed');
  };

  const showSnackbar = () => {
    snackbarRef.current?.show();
  };

  const hideSnackbar = () => {
    snackbarRef.current?.dismiss();
  };

  return (
    <>
      <TextButton title="Show Snackbar" onPress={showSnackbar} />
      <Snackbar
        ref={snackbarRef}
        content="This is a Snackbar message!"
        action="Dismiss"
        onActionPress={hideSnackbar}
        duration={3000}
        showCloseIcon
      />
    </>
  );
};