refactor(ui): new component library (#1740)
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import { booleanAttribute, ChangeDetectionStrategy, Component, computed, input, output } from '@angular/core';
|
||||
import { cn } from '../cn';
|
||||
import { SPINNER_RING } from '../spinner/app-spinner.variants';
|
||||
import { buttonVariants, type ButtonSize, type ButtonTone, type ButtonVariant } from './app-button.variants';
|
||||
|
||||
@Component({
|
||||
selector: 'app-button',
|
||||
standalone: true,
|
||||
host: {
|
||||
class: 'inline-block align-middle',
|
||||
'[class.w-full]': 'fluid()',
|
||||
},
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
template: `
|
||||
<button
|
||||
[attr.id]="buttonId() || null"
|
||||
[class]="buttonClass()"
|
||||
[disabled]="disabled() || loading()"
|
||||
[type]="type()"
|
||||
[attr.name]="name() || null"
|
||||
[attr.value]="value() ?? null"
|
||||
[attr.form]="form() || null"
|
||||
[attr.title]="title() || null"
|
||||
[attr.tabindex]="tabIndex()"
|
||||
[attr.aria-label]="ariaLabel() || null"
|
||||
[attr.aria-busy]="loading() ? 'true' : null"
|
||||
[attr.aria-controls]="ariaControls() || null"
|
||||
[attr.aria-expanded]="ariaExpanded()"
|
||||
[attr.aria-haspopup]="ariaHasPopup()"
|
||||
[attr.aria-pressed]="ariaPressed()"
|
||||
(click)="clicked.emit($event)">
|
||||
@if (loading()) {
|
||||
<span [class]="loadingRingClass" aria-hidden="true"></span>
|
||||
} @else if (icon() && iconPos() === 'left') {
|
||||
<i [class]="iconClass()" aria-hidden="true"></i>
|
||||
}
|
||||
@if (label()) {
|
||||
<span class="leading-none">{{ label() }}</span>
|
||||
}
|
||||
<ng-content />
|
||||
@if (!loading() && icon() && iconPos() === 'right') {
|
||||
<i [class]="iconClass()" aria-hidden="true"></i>
|
||||
}
|
||||
</button>
|
||||
`,
|
||||
})
|
||||
export class AppButtonComponent {
|
||||
readonly tone = input<ButtonTone>('neutral');
|
||||
readonly variant = input<ButtonVariant>('soft');
|
||||
readonly size = input<ButtonSize>('md');
|
||||
readonly iconOnly = input(false, { transform: booleanAttribute });
|
||||
readonly rounded = input(false, { transform: booleanAttribute });
|
||||
readonly fluid = input(false, { transform: booleanAttribute });
|
||||
readonly styleClass = input('');
|
||||
readonly buttonId = input('');
|
||||
readonly name = input('');
|
||||
readonly value = input<string | number | null>(null);
|
||||
readonly form = input('');
|
||||
readonly title = input('');
|
||||
readonly tabIndex = input<number | null>(null);
|
||||
readonly label = input('');
|
||||
readonly ariaLabel = input('');
|
||||
readonly ariaControls = input('');
|
||||
readonly ariaExpanded = input<boolean | string | null>(null);
|
||||
readonly ariaHasPopup = input<boolean | 'false' | 'true' | 'menu' | 'listbox' | 'tree' | 'grid' | 'dialog' | null>(null);
|
||||
readonly ariaPressed = input<boolean | 'false' | 'true' | 'mixed' | null>(null);
|
||||
readonly icon = input('');
|
||||
readonly iconPos = input<'left' | 'right'>('left');
|
||||
readonly loading = input(false, { transform: booleanAttribute });
|
||||
readonly disabled = input(false, { transform: booleanAttribute });
|
||||
readonly type = input<'button' | 'submit' | 'reset'>('button');
|
||||
|
||||
readonly clicked = output<MouseEvent>();
|
||||
|
||||
protected readonly buttonClass = computed(() =>
|
||||
cn(
|
||||
buttonVariants({
|
||||
tone: this.tone(),
|
||||
variant: this.variant(),
|
||||
size: this.size(),
|
||||
iconOnly: this.iconOnly(),
|
||||
rounded: this.rounded(),
|
||||
fluid: this.fluid(),
|
||||
}),
|
||||
this.styleClass(),
|
||||
),
|
||||
);
|
||||
protected readonly iconClass = computed(() => cn(this.icon(), 'shrink-0 m-0 leading-none'));
|
||||
protected readonly loadingRingClass = cn(SPINNER_RING, 'size-[1em] border-2 shrink-0');
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import { cva } from 'class-variance-authority';
|
||||
import { neutralControlBorderClass, neutralSurfaceHoverClass } from '../control.styles';
|
||||
|
||||
export type ButtonTone = 'neutral' | 'primary' | 'danger';
|
||||
export type ButtonVariant = 'soft' | 'solid' | 'ghost';
|
||||
export type ButtonSize = 'sm' | 'md' | 'lg';
|
||||
|
||||
export const buttonVariants = cva(
|
||||
'inline-flex shrink-0 cursor-pointer select-none items-center justify-center ' +
|
||||
'rounded-md border leading-none whitespace-nowrap font-medium ' +
|
||||
'transition-[background-color,border-color,color] duration-150 active:translate-y-px ' +
|
||||
'focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary ' +
|
||||
'disabled:pointer-events-none disabled:opacity-50',
|
||||
{
|
||||
variants: {
|
||||
tone: { neutral: '', primary: '', danger: '' },
|
||||
variant: { soft: '', solid: '', ghost: '' },
|
||||
size: {
|
||||
sm: 'h-8 px-3 gap-1.5 text-xs',
|
||||
md: 'h-9 px-3 gap-2 text-sm',
|
||||
lg: 'h-10 px-4 gap-2 text-sm',
|
||||
},
|
||||
iconOnly: { true: '', false: '' },
|
||||
rounded: { true: 'rounded-full', false: '' },
|
||||
fluid: { true: 'w-full', false: '' },
|
||||
},
|
||||
compoundVariants: [
|
||||
{
|
||||
tone: 'neutral',
|
||||
variant: ['soft', 'solid'],
|
||||
class: `bg-card text-text-strong ${neutralControlBorderClass} shadow-control ${neutralSurfaceHoverClass} hover:border-text/15`,
|
||||
},
|
||||
{
|
||||
tone: 'primary',
|
||||
variant: 'soft',
|
||||
class:
|
||||
'bg-primary/10 text-primary-text border-primary/15 shadow-control hover:bg-primary/20 dark:hover:bg-primary/15 hover:border-primary/30 dark:border-primary/30 dark:hover:border-primary/45',
|
||||
},
|
||||
{
|
||||
tone: 'primary',
|
||||
variant: 'solid',
|
||||
class: 'bg-primary text-primary-contrast border-transparent font-semibold shadow-control hover:bg-primary-hover',
|
||||
},
|
||||
{
|
||||
tone: 'danger',
|
||||
variant: 'soft',
|
||||
class:
|
||||
'bg-danger/10 text-danger border-danger/15 shadow-control hover:bg-danger/20 dark:hover:bg-danger/15 hover:border-danger/30 dark:border-danger/30 dark:hover:border-danger/45',
|
||||
},
|
||||
{
|
||||
tone: 'danger',
|
||||
variant: 'solid',
|
||||
class: 'bg-danger text-white border-transparent font-semibold shadow-control hover:bg-danger-hover',
|
||||
},
|
||||
{
|
||||
tone: 'neutral',
|
||||
variant: 'ghost',
|
||||
class: `bg-transparent border-transparent text-text-secondary ${neutralSurfaceHoverClass} hover:text-text-strong`,
|
||||
},
|
||||
{ tone: 'primary', variant: 'ghost', class: 'bg-transparent border-transparent text-primary hover:bg-primary/10' },
|
||||
{ tone: 'danger', variant: 'ghost', class: 'bg-transparent border-transparent text-danger hover:bg-danger/10' },
|
||||
{ iconOnly: true, size: 'sm', class: 'w-8 px-0 gap-0' },
|
||||
{ iconOnly: true, size: 'md', class: 'w-9 px-0 gap-0' },
|
||||
{ iconOnly: true, size: 'lg', class: 'w-10 px-0 gap-0' },
|
||||
],
|
||||
defaultVariants: { tone: 'neutral', variant: 'soft', size: 'md', iconOnly: false, rounded: false, fluid: false },
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user