ionicons
ionicons copied to clipboard
ariaLabel doesn't disable <title> tag on IonIcons 5
Perhaps I'm missing something, but I can't seem to get the
While I don't mind the title tag being there, I do mind the tooltip that browsers show. I do my own tooltips with bootstrap/popper on links containing the icons, so they're just additional clutter and even show wrong information (like "create" when I use that icon to edit something).
ion-icon { pointer-events: none; }
helps with that, but it somehow messes up the z order (showing above the tooltips). .tooltip { z-index: 100000; }
fixes that.
The pointer-events: none;
solution is a good workaround, but it has some downsides (you cannot bind a (click)
event to the element any more). In our case we had to change more code to wrap everything in additional elements. Needs to be fixed in Ionic/Ionicons IMO.
I am sure there is a better way, but this is a clunky workaround I am using:
function repion() {
let ionicons = document.querySelectorAll('ion-icon');
ionicons.forEach(icon => {
let properTitle = icon.getAttribute('title');
if (properTitle) {
icon.shadowRoot.querySelector('.icon-inner svg title').innerHTML = properTitle
}
});
}
@quinlo Thanks, looks good. When/where do you call this function?
you can made a directive and do this: constructor(private el: ElementRef) { } ngOnInit(){ const removeTitle = () => { if(this.el.nativeElement && this.el.nativeElement.shadowRoot && this.el.nativeElement.shadowRoot.querySelector('.icon-inner svg title') ) { this.el.nativeElement.shadowRoot.querySelector('.icon-inner svg title').innerHTML = ''; } else { setTimeout( () => { removeTitle(); }, 500 ); } } removeTitle() }
then, you can use it in your ion icon in this way
I extended the directive so you can provide an alternative title:
import { Directive, ElementRef, Input } from '@angular/core';
@Directive({
selector: '[ioniconRemoveTitle]',
})
export class IoniconsRemoveTitleDirective {
@Input('iconTitle') iconTitle: string;
constructor(private el: ElementRef) {
// Doing nothing.
}
ngOnInit(): void {
const removeTitle = () => {
if (
this.el.nativeElement &&
this.el.nativeElement.shadowRoot &&
this.el.nativeElement.shadowRoot.querySelector('.icon-inner svg title')
) {
this.el.nativeElement.shadowRoot.querySelector('.icon-inner svg title').innerHTML =
this.iconTitle || '';
} else {
setTimeout(() => {
removeTitle();
}, 500);
}
};
removeTitle();
}
}
Use it like this ([iconTitle]
is optional):
<ion-icon name="close-circle" ioniconRemoveTitle [iconTitle]="'New title for close button'"></ion-icon>
+1 for workaround, but I'd like to cast my vote for some built-in functionality to remove and edit the tooltip. There are 2 or 3 other issues looking for the same thing.
I think it makes more sense to populate the <title>
tag at runtime instead of at build time, or just not have it all together.
If there is a [title]
attribute on <ion-icon>
, then don't add <title>
tag to svg, otherwise add <title>
with default English label.
Having it hard coded like this at build time makes it hard to customizing the title that appears on :hover
.
Another use case for this is for internationalization.
If the <title>
tag is pre-populated at build time in English, then all other languages needs to find a work around to remove/replace the title tag or something.
I found a line of code that seems to adds the <title>
of at build but not sure where to do it during runtime.
https://github.com/ionic-team/ionicons/blob/8c2a507fcff2cb5f2edff60357ffd7feb70bb987/scripts/build.ts#L169-L172
Yup, not ideal that this functionality isnt supported out of the box.
Since displaying the icon name on icon hover event is actually often worse than displaying nothing at all (e.g. "Create" is displayed when operation is an Edit), probably better to just disable displaying icon title via pointer-events: none
until ionicons
supports this functionality; that, or go with one of the above hacks.
+1 for a built-in fix for this. As @ariscol mentioned, there are several other issues on this repo talking about the same thing:
https://github.com/ionic-team/ionicons/issues/915 https://github.com/ionic-team/ionicons/issues/1065 https://github.com/ionic-team/ionicons/issues/1059 https://github.com/ionic-team/ionicons/issues/1049 https://github.com/ionic-team/ionicons/issues/949
@liamdebeasi Is this something we can get changed? I'm sure someone here could make a PR with this change. Would that be considered for merging?
@quinlo your solution is awesome, but it didn't work for me. I am stuck at the icon.shadowRoot.querySelector()
function which returns null and undefined
This CSS-only solution fixed it for me and works great and very simple, however, I would love to see this properly implemented in a future version.
https://github.com/ionic-team/ionicons/issues/949#issuecomment-859793631
Hi everyone,
Wanted to provide a quick update. We plan on removing the default <title>
and default aria-label
values in Ionicons. This is a breaking change, so it will need to happen in the next major release of Ionicons.
Just as an idea when would the next major release roll out? Thanks
We do not have a time estimate at the moment, but I will update this thread when I have more to share.
Okay, hope it'll be soon. I'm loving the simplicity of ionicons 😀
I see that ionicons has published 6.1.0-0 to NPM. Trying it out the title tag still shows up causing the tooltip. Is this supposed to be removed in 6.1 and if so is there an ETA on when that will be included? This is what I'm looking forward to most in the next update is removing these tooltips.
Thanks for the report. This has been resolved via https://github.com/ionic-team/ionicons/pull/1166 and a fix will be available in Ionicons 6.1. In this release, the <title>
element inside of each SVG has been removed. Developers can provide a title
to create a tooltip that appears when hovering the icon. They can also provide aria-label
to customize what assistive technology announces when selecting the icon.
Hello @liamdebeasi Will there be an update of the library in ionic-framework soon, or in the next patch version?
Thanks!
Yes, we plan on updating the dependency in an upcoming minor release of Ionic Framework: https://github.com/ionic-team/ionic-framework/pull/26617
Note: This PR only updates the dependency for icons that are used internally (such as the icon for ion-back-button
). Developers consuming Ionicons in their own apps should still manually update their Ionicons dependency.