refactor(ui): new component library (#1740)
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
<span
|
||||
[class]="'app-rating ' + ratingClass()"
|
||||
[style.--app-rating-color]="color()"
|
||||
[style.--app-rating-size]="size()"
|
||||
[attr.aria-label]="ratingAriaLabel()"
|
||||
role="img">
|
||||
@for (fill of starFills(); track $index) {
|
||||
<span class="app-rating-star" [style.--app-rating-star-fill]="fill">
|
||||
<svg class="app-rating-star-empty" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" focusable="false">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M11.48 3.499a.562.562 0 0 1 1.04 0l2.125 5.111a.563.563 0 0 0 .475.345l5.518.442c.499.04.701.663.321.988l-4.204 3.602a.563.563 0 0 0-.182.557l1.285 5.385a.562.562 0 0 1-.84.61l-4.725-2.885a.562.562 0 0 0-.586 0L6.982 20.54a.562.562 0 0 1-.84-.61l1.285-5.386a.562.562 0 0 0-.182-.557l-4.204-3.602a.562.562 0 0 1 .321-.988l5.518-.442a.563.563 0 0 0 .475-.345L11.48 3.5Z" />
|
||||
</svg>
|
||||
<svg class="app-rating-star-filled" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
|
||||
<path fill-rule="evenodd" d="M10.788 3.21c.448-1.077 1.976-1.077 2.424 0l2.082 5.006 5.404.434c1.164.093 1.636 1.545.749 2.305l-4.117 3.527 1.257 5.273c.271 1.136-.964 2.033-1.96 1.425L12 18.354 7.373 21.18c-.996.608-2.231-.29-1.96-1.425l1.257-5.273-4.117-3.527c-.887-.76-.415-2.212.749-2.305l5.404-.434 2.082-5.005Z" clip-rule="evenodd" />
|
||||
</svg>
|
||||
</span>
|
||||
}
|
||||
</span>
|
||||
@@ -1,64 +0,0 @@
|
||||
:host {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
line-height: 1;
|
||||
|
||||
--app-rating-color: var(--p-rating-icon-active-color, var(--p-primary-color));
|
||||
--app-rating-empty-color: var(--p-rating-icon-color, var(--p-text-muted-color));
|
||||
--app-rating-gap: var(--p-rating-gap, 0.2188rem);
|
||||
--app-rating-size: 18px;
|
||||
}
|
||||
|
||||
.app-rating {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: var(--app-rating-gap);
|
||||
line-height: 1;
|
||||
|
||||
&.app-rating-excellent {
|
||||
--app-rating-color: rgb(34 197 94);
|
||||
}
|
||||
|
||||
&.app-rating-good {
|
||||
--app-rating-color: rgb(52 211 153);
|
||||
}
|
||||
|
||||
&.app-rating-mixed {
|
||||
--app-rating-color: rgb(234 179 8);
|
||||
}
|
||||
|
||||
&.app-rating-low {
|
||||
--app-rating-color: rgb(249 115 22);
|
||||
}
|
||||
|
||||
&.app-rating-poor {
|
||||
--app-rating-color: rgb(239 68 68);
|
||||
}
|
||||
}
|
||||
|
||||
.app-rating-star {
|
||||
--app-rating-star-fill: 0%;
|
||||
|
||||
position: relative;
|
||||
display: inline-flex;
|
||||
width: var(--app-rating-size);
|
||||
height: var(--app-rating-size);
|
||||
line-height: 0;
|
||||
}
|
||||
|
||||
.app-rating-star svg {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.app-rating-star-empty {
|
||||
color: var(--app-rating-empty-color);
|
||||
}
|
||||
|
||||
.app-rating-star-filled {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
fill: var(--app-rating-color);
|
||||
clip-path: inset(0 calc(100% - var(--app-rating-star-fill)) 0 0);
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
import {ChangeDetectionStrategy, Component, computed, input} from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'app-rating',
|
||||
standalone: true,
|
||||
templateUrl: './rating.component.html',
|
||||
styleUrls: ['./rating.component.scss'],
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
})
|
||||
export class RatingComponent {
|
||||
value = input<number | null>(null);
|
||||
color = input<string | null>(null);
|
||||
max = input(5);
|
||||
size = input('18px');
|
||||
ariaLabel = input<string | null>(null);
|
||||
|
||||
protected readonly ratingClass = computed(() => this.getRatingClass(this.value()));
|
||||
|
||||
protected readonly ratingAriaLabel = computed(() => {
|
||||
const label = this.ariaLabel();
|
||||
if (label) return label;
|
||||
|
||||
const rating = this.value();
|
||||
return rating === null ? null : `${rating.toFixed(1)} / ${this.max()}`;
|
||||
});
|
||||
|
||||
protected readonly starFills = computed(() => {
|
||||
const rating = this.value();
|
||||
return Array.from({length: this.max()}, (_, index) => this.getStarFill(rating, index));
|
||||
});
|
||||
|
||||
private getStarFill(rating: number | null, starIndex: number): string {
|
||||
if (rating === null) return '0%';
|
||||
const fill = Math.max(0, Math.min(1, rating - starIndex));
|
||||
return `${Math.round(fill * 100)}%`;
|
||||
}
|
||||
|
||||
private getRatingClass(rating: number | null): string {
|
||||
if (rating === null) return 'app-rating-unrated';
|
||||
if (rating >= 4.5) return 'app-rating-excellent';
|
||||
if (rating >= 4) return 'app-rating-good';
|
||||
if (rating >= 3.5) return 'app-rating-mixed';
|
||||
if (rating >= 2.5) return 'app-rating-low';
|
||||
return 'app-rating-poor';
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
@if (tabs().length) {
|
||||
<div ngTabs class="app-tabs-root">
|
||||
<div
|
||||
ngTabList
|
||||
class="app-tabs"
|
||||
[class.is-measured]="indicatorVisible()"
|
||||
[class.is-animated]="indicatorAnimated()"
|
||||
[(selectedTab)]="selectedTabId"
|
||||
[attr.aria-label]="ariaLabel()"
|
||||
>
|
||||
<span
|
||||
class="app-tabs-indicator"
|
||||
aria-hidden="true"
|
||||
[style.transform]="'translateX(' + indicatorLeft() + 'px)'"
|
||||
[style.width.px]="indicatorWidth()"
|
||||
></span>
|
||||
@for (tab of tabs(); track tab.id) {
|
||||
<button
|
||||
ngTab
|
||||
type="button"
|
||||
[id]="tab.id"
|
||||
[value]="tab.id"
|
||||
class="app-tab"
|
||||
>
|
||||
@if (tab.icon) {
|
||||
<i [class]="'app-tab-icon ' + tab.icon" aria-hidden="true"></i>
|
||||
}
|
||||
<span class="app-tab-label">{{ tab.label }}</span>
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
import {
|
||||
afterRenderEffect,
|
||||
ChangeDetectionStrategy,
|
||||
Component,
|
||||
DestroyRef,
|
||||
computed,
|
||||
inject,
|
||||
input,
|
||||
model,
|
||||
signal,
|
||||
viewChildren,
|
||||
} from '@angular/core';
|
||||
import {Tab as NgTab, TabList, Tabs} from '@angular/aria/tabs';
|
||||
|
||||
export interface TabItem {
|
||||
id: string;
|
||||
label: string;
|
||||
icon?: string;
|
||||
}
|
||||
|
||||
export type TabsVariant = 'underline' | 'segmented';
|
||||
export type TabsPlacement = 'inline' | 'below';
|
||||
|
||||
@Component({
|
||||
selector: 'app-tabs',
|
||||
standalone: true,
|
||||
imports: [Tabs, TabList, NgTab],
|
||||
templateUrl: './app-tabs.component.html',
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
host: {
|
||||
'[class.variant-underline]': 'variant() === "underline"',
|
||||
'[class.variant-segmented]': 'variant() === "segmented"',
|
||||
'[class.placement-inline]': 'placement() === "inline"',
|
||||
'[class.placement-below]': 'placement() === "below"',
|
||||
},
|
||||
})
|
||||
export class AppTabsComponent {
|
||||
readonly tabs = input.required<TabItem[]>();
|
||||
readonly variant = input<TabsVariant>('underline');
|
||||
readonly placement = input<TabsPlacement>('below');
|
||||
readonly ariaLabel = input.required<string>();
|
||||
readonly selectedTabId = model<string | undefined>(undefined);
|
||||
|
||||
private readonly destroyRef = inject(DestroyRef);
|
||||
private readonly tabDirectives = viewChildren(NgTab);
|
||||
|
||||
protected readonly activeTabId = computed(() => this.selectedTabId() ?? this.tabs()[0]?.id);
|
||||
protected readonly indicatorLeft = signal(0);
|
||||
protected readonly indicatorWidth = signal(0);
|
||||
protected readonly indicatorVisible = signal(false);
|
||||
protected readonly indicatorAnimated = signal(false);
|
||||
|
||||
private pendingAnimationFrame: number | null = null;
|
||||
private initialRevealScheduled = false;
|
||||
|
||||
constructor() {
|
||||
let resizeObserver: ResizeObserver | null = null;
|
||||
|
||||
afterRenderEffect(() => {
|
||||
this.tabs();
|
||||
this.variant();
|
||||
this.placement();
|
||||
this.activeTabId();
|
||||
this.scheduleMeasure();
|
||||
|
||||
const tabs = this.tabDirectives();
|
||||
if (tabs.length && typeof ResizeObserver !== 'undefined') {
|
||||
resizeObserver ??= new ResizeObserver(() => this.scheduleMeasure());
|
||||
resizeObserver.disconnect();
|
||||
for (const tab of tabs) {
|
||||
resizeObserver.observe(tab.element);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.destroyRef.onDestroy(() => {
|
||||
resizeObserver?.disconnect();
|
||||
if (this.pendingAnimationFrame !== null) {
|
||||
cancelAnimationFrame(this.pendingAnimationFrame);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private measure(): void {
|
||||
const activeTabId = this.activeTabId();
|
||||
const active = this.tabDirectives().find(tab => tab.value() === activeTabId);
|
||||
if (!active) {
|
||||
this.indicatorVisible.set(false);
|
||||
return;
|
||||
}
|
||||
this.indicatorLeft.set(active.element.offsetLeft);
|
||||
this.indicatorWidth.set(active.element.offsetWidth);
|
||||
if (!this.indicatorVisible()) {
|
||||
this.scheduleInitialReveal();
|
||||
return;
|
||||
}
|
||||
this.indicatorAnimated.set(true);
|
||||
}
|
||||
|
||||
private scheduleMeasure(): void {
|
||||
if (this.pendingAnimationFrame !== null) {
|
||||
cancelAnimationFrame(this.pendingAnimationFrame);
|
||||
}
|
||||
|
||||
this.pendingAnimationFrame = requestAnimationFrame(() => {
|
||||
this.pendingAnimationFrame = null;
|
||||
this.measure();
|
||||
});
|
||||
}
|
||||
|
||||
private scheduleInitialReveal(): void {
|
||||
if (this.initialRevealScheduled) return;
|
||||
this.initialRevealScheduled = true;
|
||||
|
||||
void document.fonts.ready.then(() => {
|
||||
if (this.pendingAnimationFrame !== null) {
|
||||
cancelAnimationFrame(this.pendingAnimationFrame);
|
||||
}
|
||||
this.pendingAnimationFrame = requestAnimationFrame(() => {
|
||||
this.measure();
|
||||
this.indicatorVisible.set(true);
|
||||
this.pendingAnimationFrame = requestAnimationFrame(() => {
|
||||
this.pendingAnimationFrame = null;
|
||||
this.indicatorAnimated.set(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user