7 changed files with 90 additions and 39 deletions
@ -1,39 +1,68 @@ |
|||||||
import { Component, Input } from "@angular/core"; |
import { Directive, ElementRef, HostBinding, Input, OnChanges, OnInit } from "@angular/core"; |
||||||
|
|
||||||
type BadgeTypes = "primary" | "secondary" | "success" | "danger" | "warning" | "info"; |
type BadgeTypes = "primary" | "secondary" | "success" | "danger" | "warning" | "info"; |
||||||
|
|
||||||
const styles: Record<BadgeTypes, string[]> = { |
const styles: Record<BadgeTypes, string[]> = { |
||||||
primary: ["tw-bg-primary-500", "hover:tw-bg-primary-700"], |
primary: ["tw-bg-primary-500"], |
||||||
secondary: ["tw-bg-secondary-500", "hover:tw-bg-secondary-700"], |
secondary: ["tw-bg-text-muted"], |
||||||
success: ["tw-bg-success-500", "hover:tw-bg-success-700"], |
success: ["tw-bg-success-500"], |
||||||
danger: ["tw-bg-danger-500", "hover:tw-bg-danger-700"], |
danger: ["tw-bg-danger-500"], |
||||||
warning: ["tw-bg-warning-500", "hover:tw-bg-warning-700"], |
warning: ["tw-bg-warning-500"], |
||||||
info: ["tw-bg-info-500", "hover:tw-bg-info-700"], |
info: ["tw-bg-info-500"], |
||||||
}; |
}; |
||||||
|
|
||||||
@Component({ |
const hoverStyles: Record<BadgeTypes, string[]> = { |
||||||
selector: "bit-badge", |
primary: ["hover:tw-bg-primary-700"], |
||||||
template: `<span [ngClass]="classes"><ng-content></ng-content></span>`, |
secondary: ["hover:tw-bg-secondary-700"], |
||||||
|
success: ["hover:tw-bg-success-700"], |
||||||
|
danger: ["hover:tw-bg-danger-700"], |
||||||
|
warning: ["hover:tw-bg-warning-700"], |
||||||
|
info: ["hover:tw-bg-info-700"], |
||||||
|
}; |
||||||
|
|
||||||
|
@Directive({ |
||||||
|
selector: "span[bit-badge], a[bit-badge], button[bit-badge]", |
||||||
}) |
}) |
||||||
export class BadgeComponent { |
export class BadgeComponent implements OnInit, OnChanges { |
||||||
@Input() |
@HostBinding("class") @Input("class") classList = ""; |
||||||
type: BadgeTypes = "primary"; |
|
||||||
|
@Input() badgeType: BadgeTypes = "primary"; |
||||||
|
|
||||||
|
private isSpan = false; |
||||||
|
|
||||||
|
constructor(private el: ElementRef<Element>) { |
||||||
|
this.isSpan = el?.nativeElement?.nodeName == "SPAN"; |
||||||
|
} |
||||||
|
|
||||||
|
ngOnInit(): void { |
||||||
|
this.classList = this.classes.join(" "); |
||||||
|
} |
||||||
|
|
||||||
|
ngOnChanges() { |
||||||
|
this.ngOnInit(); |
||||||
|
} |
||||||
|
|
||||||
get classes() { |
get classes() { |
||||||
return [ |
return [ |
||||||
"tw-inline-block", |
"tw-inline-block", |
||||||
"tw-py-0.5", |
"tw-py-1", |
||||||
"tw-px-1", |
"tw-px-1.5", |
||||||
"tw-font-bold", |
"tw-font-bold", |
||||||
"tw-leading-none", |
"tw-leading-none", |
||||||
"tw-text-center", |
"tw-text-center", |
||||||
"tw-text-contrast", |
"!tw-text-contrast", |
||||||
"tw-align-baseline", |
|
||||||
"tw-rounded", |
"tw-rounded", |
||||||
"tw-border-collapse", |
"tw-border-none", |
||||||
"tw-box-border", |
"tw-box-border", |
||||||
"tw-whitespace-no-wrap", |
"tw-whitespace-no-wrap", |
||||||
"tw-text-xs", |
"tw-text-xs", |
||||||
].concat(styles[this.type]); |
"hover:tw-no-underline", |
||||||
|
"focus:tw-outline-none", |
||||||
|
"focus:tw-ring", |
||||||
|
"focus:tw-ring-offset-2", |
||||||
|
"focus:tw-ring-primary-700", |
||||||
|
] |
||||||
|
.concat(styles[this.badgeType]) |
||||||
|
.concat(this.isSpan ? [] : hoverStyles[this.badgeType]); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue