Skip to content

Commit

Permalink
fix(icon): fix HostBinding classes for onPush Host (#49)
Browse files Browse the repository at this point in the history
The classes of the Hostbinding where not set correctly for onPush Hosts.
The size was not displayed correctly.
Now the classes of the icon are set and detected by the host correctly.

close #46
  • Loading branch information
elite-benni authored Nov 14, 2023
1 parent cef38d4 commit 17627da
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
3 changes: 2 additions & 1 deletion libs/ui/icon/helm/src/lib/hlm-icon.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Input } from '@angular/core';
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { By } from '@angular/platform-browser';
import { NgIconComponent, provideIcons } from '@ng-icons/core';
import { radixCheck } from '@ng-icons/radix-icons';
Expand All @@ -8,6 +8,7 @@ import { HlmIconComponent } from './hlm-icon.component';
@Component({
selector: 'hlm-mock',
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [HlmIconComponent],
providers: [provideIcons({ radixCheck })],
template: `<hlm-icon class="test" ngIconClass="test2" name="radixCheck" [size]="size" color="red" strokeWidth="2" />`,
Expand Down
22 changes: 11 additions & 11 deletions libs/ui/icon/helm/src/lib/hlm-icon.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
ChangeDetectionStrategy,
Component,
computed,
effect,
HostBinding,
Input,
signal,
Expand Down Expand Up @@ -35,11 +34,6 @@ const iconVariants = cva('inline-flex', {

export type IconSize = DefinedSizes | string;

const generateClasses = (size: IconSize, userCls: ClassValue) => {
const variant = isDefinedSize(size) ? size : 'none';
return hlm(iconVariants({ variant }), userCls);
};

const isDefinedSize = (size: IconSize): size is DefinedSizes => {
return DEFINED_SIZES.includes(size as DefinedSizes);
};
Expand Down Expand Up @@ -70,39 +64,45 @@ export class HlmIconComponent {
@Input({ required: true })
set name(value: IconName | string) {
this._name.set(value);
this.cls = this.generateClasses();
}

@Input()
set size(value: IconSize) {
this._size.set(value);
this.cls = this.generateClasses();
}

@Input()
set color(value: string | undefined) {
this._color.set(value);
this.cls = this.generateClasses();
}

@Input()
set strokeWidth(value: string | number | undefined) {
this._strokeWidth.set(value);
this.cls = this.generateClasses();
}

@Input()
set ngIconClass(cls: ClassValue) {
this.ngIconCls.set(cls);
this.cls = this.generateClasses();
}

@Input()
set class(cls: ClassValue) {
this.userCls.set(cls);
this.cls = this.generateClasses();
}

@HostBinding('class')
protected cls = generateClasses(this._size(), this.userCls());
protected cls = this.generateClasses();

constructor() {
effect(() => {
this.cls = generateClasses(this._size(), this.userCls());
});
private generateClasses() {
const size: IconSize = this._size();
const variant = isDefinedSize(size) ? size : 'none';
return hlm(iconVariants({ variant }), this.userCls());
}
}

1 comment on commit 17627da

@vercel
Copy link

@vercel vercel bot commented on 17627da Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

spartan – ./

spartan-goetzrobin.vercel.app
spartan-git-main-goetzrobin.vercel.app
www.spartan.ng
spartan.ng

Please sign in to comment.