Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google material icons loading slowly, flicker from text to icon

Installing Google Material icons using Setup Method 2 self hosting for our React project the ligatures associated with the icon is sometimes displayed before the material icon.

<i class="material-icons">face</i> {/* shows text "face" on site prior to proper material icon load */}

For example the above line would display "face" for a second before showing a face. How can we delay the UI rendering until the file references are fully loaded?

/*material icons file references loaded locally */
    @font-face {
      font-family: 'Material Icons';
      font-style: normal;
      font-weight: 400;
      src: url(../node_modules/material-design-icons/iconfont/MaterialIcons-Regular.eot); /* For IE6-8 */
      src: local('Material Icons'), local('MaterialIcons-Regular'), url(../node_modules/material-design-icons/iconfont/MaterialIcons-Regular.woff2) format('woff2'), url(../node_modules/material-design-icons/iconfont/MaterialIcons-Regular.woff) format('woff'), url(../node_modules/material-design-icons/iconfont/MaterialIcons-Regular.ttf) format('truetype');
    }
like image 906
sammarcow Avatar asked Nov 17 '25 13:11

sammarcow


1 Answers

Create a file called preload.js in your src folder.

document.fonts.load('10pt "Material Icons"').then(function () {
  console.log('Material Icons font has been preloaded.');
});

add the following script tag at the end of the head section in index.html

<script defer src="%PUBLIC_URL%/preload.js"></script>

user in your React component like this

import Icon from '@material-ui/core/Icon';

 function MyComponent() {
   return <Icon>home</Icon>;
 }

I hope it's working

like image 193
Kishor Ahir Avatar answered Nov 20 '25 03:11

Kishor Ahir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!