p-togglebutton with icons only incorrectly renders nbsp as label
Describe the bug
When a p-togglebutton has both onIcon and offIcon specified but uses no labels (i.e. onLabel='' offLabel='') the result is a nbsp being rendered in the label span yielding an off-center icon and too much space to the side.
Pull Request Link
No response
Reason for not contributing a PR
- [ ] Lack of time
- [ ] Unsure how to implement the fix/feature
- [ ] Difficulty understanding the codebase
- [x] Other
Other Reason
Process is broken... Due to commitlint config cannot create a PR without a refs clause but can't create a bug report without a PR (circular dependency!)
I actually have a fix and will commit and create PR once I have the ref of this ticket.
Reproducer
https://stackblitz.com/edit/github-nenaefqt
Environment
Windows 11 Angular 21 PrimeNg 21
Most prominent in themes with "hard" button edges (e.g. Nora)
Angular version
21.2.5
PrimeNG version
v21
Node version
22.17.1
Browser(s)
No response
Steps to reproduce the behavior
Create a component with a p-toggleButton WITH icons but with empty labels, e.g.:
<p-toggleButton onIcon="pi pi-user" offIcon="pi pi-user" onLabel="" offLabel="" />
Inspect the result - the icon appears slightly left of center and additional space appears to the right of the icon.
Inspecting the rendered elements I can see that a label span was created containing an nbsp like so:
<span class="p-togglebutton-label" data-pc-section="label"> </span>
The nbsp combined with the container's gap property is causing visible whitespace and icon misalignment.
Expected behavior
There should be no whitespace - probably no <span> at all - rendered for an empty label.
Here is how I solved via CSS:
<p-toggleButton class="icon-only-toggle" onIcon="pi pi-user" offIcon="pi pi-user" onLabel="" offLabel=""/>
CSS:
.icon-only-toggle .p-togglebutton-label { display: none; }
That's a great work-around until the underlying problem is fixed.