I have the following code below. What I want to do is to change the icons that appear, while retaining the same API (the icon: 'pi pi-check-circle',
argument). In other words, I want zero edits to the code below and a different icon to appear on screen.
Is this possible? Please show me how to do it.
my.component.html
<p-toast position="top-left" class="p-toast-position"/>
my.component.ts
this.messageService.add({
severity: 'success',
detail: 'Message Content',
icon: 'pi pi-check-circle',
sticky: true,
});
I looked here for solutions but all involve customising my own components or inserting my own templates, but those are intrusive and adopting them means I need to more code just to use a different icon.
I found the answer to my question. This solution assumes you use icomoon.io and SVG icons.
icomoon.{woff|svg|ttf|eot}
. You should also see a demo.html
file.Now you have the font ready.
@font-face {
font-family: 'my-font';
font-display: block;
src: url('./icomoon.eot') format('embedded-opentype'), url('./icomoon.woff') format('woff'), url('./icomoon.svg') format('svg'), url('./icomoon.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
.pi {
font-family: 'my-font';
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;
}
.pi-icon-name::before {
content: "<the respective unicode character>"
}
In pi-icon-name
, replace <the respective unicode character>
with the unicode you see on demo.html
. If the unicode is e900
, enter \e900
. Refer to the picture below for more info.
styles.css
You'll then be able to use the same API like below:
my.component.ts
this.messageService.add({
severity: 'success',
detail: 'Message Content',
icon: 'pi pi-icon-name',
sticky: true,
});
If you want to extend instead of replace primeicons, use your own CSS class naming system. I.e. don't name it .pi
, .pi-*
, because it clashes with primeicon's classes etc