Key takeaways:
Material UI is a popular React component library that follows Google's Material Design guidelines to build visually appealing, responsive web apps.
Skeleton component is a visual placeholder that improves user experience during content loading by preventing blank screens.
Skeleton has four variants—text, circular, rectangular, and rounded—for different types of placeholders.
Skeleton components have a default pulsating animation, but you can change it to a wave or disable it entirely using the
animation
prop.Skeleton components can automatically adjust their size based on their parent element, maintaining layout consistency.
Material UI is a popular open-source library that provides pre-designed components and styles for building modern and visually appealing web applications. Material UI follows the guidelines provided by Google’s Material Design resulting in intuitive and well-designed components.
In this Answer, we will look into how we can use Material UI's Skeleton
component in a React application and increase user experience.
Note: Please makes sure to install Material UI version 5 in your systems as we will be using it in the Answer.
Skeleton
componentThe Skeleton
component acts as a visual placeholder while the content is loading on the screen. This helps improve user experience by reducing load time frustration, and ensuring responsiveness and activity while avoiding a blank white screen. To get a clear idea of how a Skeleton
component looks visually, refer to the following illustration:
Skeleton
variantsThe Skeleton
component has four different shape variants that can be used depending on the use case:
text
: It is the default variant that represents a single textual line. Its height can be adjusted using the font size attribute.
circular
: It is usually used when loading thumbnails, icons, or avatars on the web page.
rectangular
: It displays rectangular-shaped placeholders like paragraphs, images, or any rectangular element.
rounded
: It displays rectangular placeholders with rounded corners.
The code to use each variant is shown below:
body { font-family: sans-serif; -webkit-font-smoothing: auto; -moz-font-smoothing: auto; -moz-osx-font-smoothing: grayscale; font-smoothing: auto; text-rendering: optimizeLegibility; font-smooth: always; -webkit-tap-highlight-color: transparent; -webkit-touch-callout: none; } h1 { font-size: 1.5rem; }
In the above output, the first is the rectangular variant, followed by the circular, text, and rounded variants.
Line 4: We import the Skeleton
component from @mui/material/Skeleton
.
Line 10: We define the Skeleton
component with the variant
attribute set to rectangular
and specify its height.
Line 12: We define the Skeleton
component with the variant
attribute set to circular
and specify its width and height.
Line 13: We define the Skeleton
component with the variant
attribute set to text
.
Line 14: We define the Skeleton
component with the variant
attribute set to rounded
.
Skeleton
animationsThe Skeleton
component provides us with pulsating animations by default, but we can change it to a wave animation or just remove it entirely. The coding example for changing animations is given below:
body { font-family: sans-serif; -webkit-font-smoothing: auto; -moz-font-smoothing: auto; -moz-osx-font-smoothing: grayscale; font-smoothing: auto; text-rendering: optimizeLegibility; font-smooth: always; -webkit-tap-highlight-color: transparent; -webkit-touch-callout: none; } h1 { font-size: 1.5rem; }
In the output above, we see that the first Skeleton
placeholder has a wave animation, the circular placeholder’s animation is disabled, and the last two placeholders are, by default, pulsating.
Line 10: We define the Skeleton
component and set the animation
attribute to wave
.
Line 12: We define the Skeleton
component and disable animation by setting the animation
attribute to false
.
Skeleton
automatic resizingThe Skeleton
components can infer the size of the component it is placed in and resize itself. The coding example of Skeleton
components resizing automatically by inferring the dimensions is given below: