angulartypescriptprimeng

How does PrimeNG use classes to display icons instead of HTML tags?


I was reading through the GitHub project and noticed that icons extend from a BaseIcon class, which in turn extends from a BaseComponent. From what I saw, you can use the selector, for example, <AngleUpIcon>, to display the SVG of that icon. However, I would like to understand how PrimeNG manages to display icons using classes, for example:

<i class="pi pi-user" style="font-size: 2.5rem"></i>

Find a clear explanation of how PrimeNG achieves the functionality of displaying icons using classes instead of HTML tags, possibly through custom components or other mechanisms.


Solution

  • https://github.com/primefaces/primeicons/blob/master/primeicons.css — this is the explanation you're looking for.

    Basically, they use PrimeIcons, which are organized as a font. .pi CSS class specifies that it should use this font, and adds some rendering hints:

    .pi {
        font-family: 'primeicons';
        speak: none;
        font-style: normal;
        font-weight: normal;
        font-variant: normal;
        text-transform: none;
        line-height: 1;
        display: inline-block;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }
    

    While the specific class, e.g. .pi-user, simply specifies which symbol to render before your empty element:

    .pi-user:before {
        content: "\e939";
    }
    

    This char code corresponds to the necessary icon in the font.

    Obviously, having the whole icon set as a single file reduces the number of HTTP requests, which is the primary reason to organize it that way.