<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Berkay Sonel</title>
    <description>The latest articles on DEV Community by Berkay Sonel (@berkaysson).</description>
    <link>https://dev.to/berkaysson</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1114669%2F47f70645-447b-49b5-b9c3-634c057db1e4.jpeg</url>
      <title>DEV Community: Berkay Sonel</title>
      <link>https://dev.to/berkaysson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/berkaysson"/>
    <language>en</language>
    <item>
      <title>How to Create Beatiful Snackbar Component with Material UI + React + CSS in sx🤔</title>
      <dc:creator>Berkay Sonel</dc:creator>
      <pubDate>Mon, 10 Jul 2023 14:04:04 +0000</pubDate>
      <link>https://dev.to/berkaysson/how-to-create-beatiful-snackbar-component-with-material-ui-react-css-in-sx-1a67</link>
      <guid>https://dev.to/berkaysson/how-to-create-beatiful-snackbar-component-with-material-ui-react-css-in-sx-1a67</guid>
      <description>&lt;p&gt;&lt;strong&gt;Snackbar components&lt;/strong&gt; play a crucial role in providing feedback and notifications to users in a visually appealing and non-intrusive manner. With the power of Material-UI and React, we can create stunning and customizable snackbars that integrate into our applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites:
&lt;/h2&gt;

&lt;p&gt;To follow along with this tutorial, you should have a basic understanding of React and have Material-UI installed in your project. Make sure you have set up your development environment (I suggest to use create-react-app) before proceeding.&lt;br&gt;
You have to install Material UI components:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm install @mui/material @emotion/react @emotion/styled


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Let's start with a simple MUI Snackbar component:
&lt;/h3&gt;

&lt;p&gt;First, we import the Snackbar component from Material-UI using the following import statement:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import Snackbar from "@mui/material/Snackbar";


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next, we define our functional component, which will return the MUI Snackbar component with the necessary state and functions. The state variable &lt;strong&gt;open&lt;/strong&gt; manages the visibility of the snackbar, and the &lt;strong&gt;handleClose&lt;/strong&gt; function is responsible for closing the snackbar. Here's an example of how the component may look:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import React, { useState } from 'react';
import Snackbar from '@mui/material/Snackbar';

const MySnackbar = () =&amp;gt; {
  const [open, setOpen] = useState(false);

  const handleClose = (event, reason) =&amp;gt; {
   // this condition will prevent dissapering Snackbar when clicking away
    if (reason === 'clickaway') {
      return; 
    }

    setOpen(false);
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;button onClick={()=&amp;gt;setOpen(true)}&amp;gt;Activate Snackbar&amp;lt;/button&amp;gt;
      &amp;lt;Snackbar 
       open={open} 
       onClose={handleClose} 
       autoHideDuration={4000}
       message="Hello World!"
      /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default MySnackbar;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So, let's break it down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We have the &lt;strong&gt;&lt;em&gt;open&lt;/em&gt;&lt;/strong&gt; state variable that manages the &lt;strong&gt;&lt;em&gt;open&lt;/em&gt;&lt;/strong&gt; prop of the Snackbar component. By updating the &lt;strong&gt;&lt;em&gt;open&lt;/em&gt;&lt;/strong&gt; state, we control whether the Snackbar is visible or not.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The button there for just to make &lt;strong&gt;&lt;em&gt;open&lt;/em&gt;&lt;/strong&gt; state true so we can see our Snackbar.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;&lt;em&gt;onClose&lt;/em&gt;&lt;/strong&gt; prop of the Snackbar component is handled by the &lt;strong&gt;&lt;em&gt;handleClose&lt;/em&gt;&lt;/strong&gt;function. This function takes two arguments: event and reason. When the Snackbar is closed, we check the reason. If the reason is &lt;em&gt;'clickaway'&lt;/em&gt;, indicating that the user clicked away from the Snackbar, we don't close it. This prevents the Snackbar from disappearing unintentionally.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To determine when the Snackbar should automatically close, we use the &lt;strong&gt;&lt;em&gt;autoHideDuration&lt;/em&gt;&lt;/strong&gt; prop. This prop defines the duration, in milliseconds, for which the Snackbar will be displayed before automatically closing. In our example, the Snackbar will be shown for 4000 milliseconds (4 seconds) before it closes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lastly, we have the &lt;strong&gt;&lt;em&gt;message&lt;/em&gt;&lt;/strong&gt; prop, which sets the text content that will be displayed in the Snackbar.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's look our Snackbar:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhnxn5cuo8z4tmq7dl8d0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhnxn5cuo8z4tmq7dl8d0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is not the best looking Snackbar 🙄, but we can change it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Styling Snackbar
&lt;/h3&gt;

&lt;p&gt;We have the ability to customize the style of its elements using the sx prop. However, the interesting part lies in our desire to specifically style the SnackbarContent component. SnackbarContent is a component provided by Material-UI that is used as the content container within the Snackbar component.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;Snackbar 
       open={open} 
       onClose={handleClose} 
       autoHideDuration={4000}
       message="Hello World!"
       ContentProps={{
        sx:{
          //Content styles goes here
        }
       }}
/&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;ContentProps&lt;/em&gt;&lt;/strong&gt; is props that applied to SnackbarContent and &lt;strong&gt;&lt;em&gt;sx&lt;/em&gt;&lt;/strong&gt; is the "lets you work with a superset of CSS that packages all of the style functions exposed in @mui/system"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's start with a simple style change:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;Snackbar 
       open={open} 
       onClose={handleClose} 
       autoHideDuration={4000}
       message="Hello World!"
       ContentProps={{
        sx:{
          border: "1px solid black",
          borderRadius: "40px",
          color: "black",
          bgcolor: "lightgreen",
          fontWeight: "bold",
        }
       }}
/&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So, we add border, change colors and font weight.&lt;/p&gt;

&lt;p&gt;Our new Snackbar should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0fx24mcr1caoc4380rkg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0fx24mcr1caoc4380rkg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's sure better, but there is always things to improve&lt;/p&gt;

&lt;p&gt;Let's focus the text alignment, I really like centered texts in Snackbar. But adding &lt;code&gt;textAlign:"center"&lt;/code&gt; in sx props directly will not work, Why ?&lt;br&gt;
It's because we are only styling the SnackbarContent component. SnackbarContent also contains additional containers, such as the &lt;a href="https://mui.com/material-ui/api/snackbar-content/#css" rel="noopener noreferrer"&gt;message and action containers&lt;/a&gt;. To align the text properly, we need to override the styles of the message container in ContentProps, to make this happen we can use a syntax like below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;Snackbar 
       open={open} 
       onClose={handleClose} 
       autoHideDuration={4000}
       message="Hello World!"
       ContentProps={{
        sx:{
          border: "1px solid black",
          borderRadius: "40px",
          color: "black",
          bgcolor: "lightgreen",
          fontWeight: "bold",
          textAlign: "center",
          // centering our message
          width:"100%",
          "&amp;amp; .MuiSnackbarContent-message":{
            width:"inherit",
            textAlign: "center",
          }
        }
       }}
/&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;What are we doing here?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First we add width prop to our SnackbarContent styles, which is &lt;code&gt;width:"100%"&lt;/code&gt;, We should add this because we want to fill all the spaces of Snackbar with SnckbarContent&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then we select the message wrapper: &lt;code&gt;"&amp;amp; .MuiSnackbarContent-message":{}&lt;/code&gt;, this is how we select elements in Material UI. The "&amp;amp;" symbol is used as a reference to the current component or container and  ".MuiSnackbarContent-message" portion of the selector targets the specific class name associated with the message element within the SnackbarContent component.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next we add, &lt;code&gt;width:"inherit",textAlign: "center",&lt;/code&gt; inside of message container we select above.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally we have a centered Snackbar :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzewwrieo2girxbw50kzb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzewwrieo2girxbw50kzb.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is better right 😌&lt;/p&gt;

&lt;p&gt;You can add additional styles to further customize the SnackbarContent component according to your preferences.&lt;/p&gt;

&lt;p&gt;I suggest you to also add a &lt;a href="http://reactcommunity.org/react-transition-group/transition" rel="noopener noreferrer"&gt;Transition Property&lt;/a&gt; or change position of Snackbar easiliy with &lt;a href="https://mui.com/material-ui/react-snackbar/#positioned-snackbars" rel="noopener noreferrer"&gt;anchorOrigin&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/amazing-shirley-962ykp?file=/src/App.js" rel="noopener noreferrer"&gt;Code Sandbox link&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>ui</category>
      <category>css</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
