mirror of
https://github.com/bitwarden/browser
synced 2025-12-14 15:23:33 +00:00
Move to libs
This commit is contained in:
58
libs/components/src/badge/badge.directive.ts
Normal file
58
libs/components/src/badge/badge.directive.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { Directive, ElementRef, HostBinding, Input } from "@angular/core";
|
||||
|
||||
type BadgeTypes = "primary" | "secondary" | "success" | "danger" | "warning" | "info";
|
||||
|
||||
const styles: Record<BadgeTypes, string[]> = {
|
||||
primary: ["tw-bg-primary-500"],
|
||||
secondary: ["tw-bg-text-muted"],
|
||||
success: ["tw-bg-success-500"],
|
||||
danger: ["tw-bg-danger-500"],
|
||||
warning: ["tw-bg-warning-500"],
|
||||
info: ["tw-bg-info-500"],
|
||||
};
|
||||
|
||||
const hoverStyles: Record<BadgeTypes, string[]> = {
|
||||
primary: ["hover:tw-bg-primary-700"],
|
||||
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[bitBadge], a[bitBadge], button[bitBadge]",
|
||||
})
|
||||
export class BadgeDirective {
|
||||
@HostBinding("class") get classList() {
|
||||
return [
|
||||
"tw-inline-block",
|
||||
"tw-py-1",
|
||||
"tw-px-1.5",
|
||||
"tw-font-bold",
|
||||
"tw-leading-none",
|
||||
"tw-text-center",
|
||||
"!tw-text-contrast",
|
||||
"tw-rounded",
|
||||
"tw-border-none",
|
||||
"tw-box-border",
|
||||
"tw-whitespace-no-wrap",
|
||||
"tw-text-xs",
|
||||
"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.hasHoverEffects ? hoverStyles[this.badgeType] : []);
|
||||
}
|
||||
|
||||
@Input() badgeType: BadgeTypes = "primary";
|
||||
|
||||
private hasHoverEffects = false;
|
||||
|
||||
constructor(el: ElementRef<Element>) {
|
||||
this.hasHoverEffects = el?.nativeElement?.nodeName != "SPAN";
|
||||
}
|
||||
}
|
||||
11
libs/components/src/badge/badge.module.ts
Normal file
11
libs/components/src/badge/badge.module.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { CommonModule } from "@angular/common";
|
||||
import { NgModule } from "@angular/core";
|
||||
|
||||
import { BadgeDirective } from "./badge.directive";
|
||||
|
||||
@NgModule({
|
||||
imports: [CommonModule],
|
||||
exports: [BadgeDirective],
|
||||
declarations: [BadgeDirective],
|
||||
})
|
||||
export class BadgeModule {}
|
||||
56
libs/components/src/badge/badge.stories.ts
Normal file
56
libs/components/src/badge/badge.stories.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { Meta, Story } from "@storybook/angular";
|
||||
|
||||
import { BadgeDirective } from "./badge.directive";
|
||||
|
||||
export default {
|
||||
title: "Jslib/Badge",
|
||||
component: BadgeDirective,
|
||||
args: {
|
||||
badgeType: "primary",
|
||||
},
|
||||
parameters: {
|
||||
design: {
|
||||
type: "figma",
|
||||
url: "https://www.figma.com/file/f32LSg3jaegICkMu7rPARm/Tailwind-Component-Library-Update?node-id=1881%3A16956",
|
||||
},
|
||||
},
|
||||
} as Meta;
|
||||
|
||||
const Template: Story<BadgeDirective> = (args: BadgeDirective) => ({
|
||||
props: args,
|
||||
template: `
|
||||
<span class="tw-text-main">Span </span><span bitBadge [badgeType]="badgeType">Badge</span>
|
||||
<br><br>
|
||||
<span class="tw-text-main">Link </span><a href="#" bitBadge [badgeType]="badgeType">Badge</a>
|
||||
<br><br>
|
||||
<span class="tw-text-main">Button </span><button bitBadge [badgeType]="badgeType">Badge</button>
|
||||
`,
|
||||
});
|
||||
|
||||
export const Primary = Template.bind({});
|
||||
Primary.args = {};
|
||||
|
||||
export const Secondary = Template.bind({});
|
||||
Secondary.args = {
|
||||
badgeType: "secondary",
|
||||
};
|
||||
|
||||
export const Success = Template.bind({});
|
||||
Success.args = {
|
||||
badgeType: "success",
|
||||
};
|
||||
|
||||
export const Danger = Template.bind({});
|
||||
Danger.args = {
|
||||
badgeType: "danger",
|
||||
};
|
||||
|
||||
export const Warning = Template.bind({});
|
||||
Warning.args = {
|
||||
badgeType: "warning",
|
||||
};
|
||||
|
||||
export const Info = Template.bind({});
|
||||
Info.args = {
|
||||
badgeType: "info",
|
||||
};
|
||||
2
libs/components/src/badge/index.ts
Normal file
2
libs/components/src/badge/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./badge.directive";
|
||||
export * from "./badge.module";
|
||||
Reference in New Issue
Block a user