Skip to content

Commit 86fe7aa

Browse files
saraDahanCodeSara Dahanbennypowers
authored
fix(icon): clear displayed icon when icon attribute is removed or empty (#2727)
* fix(rh-icon): clear icon when attribute is removed or empty * chore(icon): fix lint errors * chore(icon): add changeset file * chore(rh-icon): apply requested changes * fix(rh-icon): ensure dynamic icon removal works in SSR and client-side * chore: fix lint's errors * chore: Adding a test to test the behavior in SSR --------- Co-authored-by: Sara Dahan <[email protected]> Co-authored-by: Benny Powers - עם ישראל חי! <[email protected]>
1 parent 5810c46 commit 86fe7aa

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

.changeset/twenty-eggs-create.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@rhds/elements": patch
3+
---
4+
5+
`<rh-icon>`: remove the displayed icon when the `icon` attribute is removed or set to an empty string.
6+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<rh-icon icon="hat"></rh-icon>
2+
<rh-button id="remove">Remove Icon</rh-button>
3+
<script type="module">
4+
import "@rhds/elements/rh-icon/rh-icon.js";
5+
import "@rhds/elements/rh-button/rh-button.js";
6+
const icon = document.querySelector('rh-icon');
7+
const btn = document.getElementById('remove');
8+
9+
btn.addEventListener('click', () => {
10+
icon.removeAttribute('icon');
11+
12+
});
13+
</script>
14+
15+
<rh-icon icon="">test</rh-icon>

elements/rh-icon/rh-icon.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export type IconResolverFunction = (set: string, icon: string) =>
2727
* @param f callback
2828
*/
2929
const ric: typeof globalThis.requestIdleCallback =
30-
globalThis.requestIdleCallback
30+
globalThis.requestIdleCallback
3131
?? globalThis.requestAnimationFrame
3232
?? (async (f: () => void) => Promise.resolve().then(f));
3333

@@ -137,6 +137,9 @@ export class RhIcon extends LitElement {
137137
#getContent() {
138138
if (isServer) {
139139
const { set = 'standard', icon } = this;
140+
if (!icon) {
141+
return '';
142+
}
140143
return globalThis.RH_ICONS.get(set)?.get(icon as never) ?? '';
141144
} else {
142145
return this.content as string ?? '';
@@ -152,6 +155,10 @@ export class RhIcon extends LitElement {
152155
@observes('icon')
153156
@observes('set')
154157
private iconChanged(): void {
158+
if (!this.icon) {
159+
this.content = null;
160+
return;
161+
}
155162
this.#dispatchLoad();
156163
}
157164

0 commit comments

Comments
 (0)