|
| 1 | +import { |
| 2 | + booleanAttribute, |
| 3 | + ChangeDetectionStrategy, |
| 4 | + Component, |
| 5 | + effect, |
| 6 | + inject, |
| 7 | + input, |
| 8 | + linkedSignal, |
| 9 | + numberAttribute, |
| 10 | +} from '@angular/core'; |
| 11 | +import { ANIMATED_ICONS_CONFIG } from '../tokens/provider'; |
| 12 | + |
| 13 | +@Component({ |
| 14 | + selector: 'i-gallery-thumbnails', |
| 15 | + template: ` |
| 16 | + <svg |
| 17 | + class="gallery-thumbnails-icon" |
| 18 | + [attr.width]="size()" |
| 19 | + [attr.height]="size()" |
| 20 | + [attr.stroke]="color()" |
| 21 | + [attr.stroke-width]="strokeWidth()" |
| 22 | + [class.animate]="isAnimating()" |
| 23 | + xmlns="http://www.w3.org/2000/svg" |
| 24 | + viewBox="0 0 24 24" |
| 25 | + fill="none" |
| 26 | + stroke-linecap="round" |
| 27 | + stroke-linejoin="round" |
| 28 | + > |
| 29 | + <svg:rect width="18" height="14" x="3" y="3" rx="2" /> |
| 30 | + @for (thumb of thumbs; track thumb; let index = $index) { |
| 31 | + <svg:path class="thumbnail-line" [attr.d]="thumb" [style.--gallery-thumbnails-delay]="index + 1" /> |
| 32 | + } |
| 33 | + </svg> |
| 34 | + `, |
| 35 | + styles: ` |
| 36 | + :host { |
| 37 | + display: inline-block; |
| 38 | + } |
| 39 | + .gallery-thumbnails-icon { |
| 40 | + overflow: visible; |
| 41 | + } |
| 42 | +
|
| 43 | + .thumbnail-line { |
| 44 | + opacity: 1; |
| 45 | + transition: opacity 0.2s ease; |
| 46 | + } |
| 47 | +
|
| 48 | + .gallery-thumbnails-icon.animate .thumbnail-line { |
| 49 | + opacity: 0; |
| 50 | + animation: fadeInSequence 0.3s ease forwards; |
| 51 | + animation-delay: calc(0.1s + var(--gallery-thumbnails-delay) * 0.09s); |
| 52 | + } |
| 53 | +
|
| 54 | + @keyframes fadeInSequence { |
| 55 | + 0% { |
| 56 | + opacity: 0; |
| 57 | + } |
| 58 | + 100% { |
| 59 | + opacity: 1; |
| 60 | + } |
| 61 | + } |
| 62 | + `, |
| 63 | + host: { |
| 64 | + '[class]': 'class()', |
| 65 | + 'aria-label': 'gallery-thumbnails', |
| 66 | + role: 'img', |
| 67 | + '(mouseenter)': 'handleMouseEnter()', |
| 68 | + '(focusin)': 'handleMouseEnter()', |
| 69 | + '(touchstart)': 'handleMouseEnter()', |
| 70 | + }, |
| 71 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 72 | +}) |
| 73 | +export class GalleryThumbnailsIcon { |
| 74 | + #options = inject(ANIMATED_ICONS_CONFIG); |
| 75 | + |
| 76 | + color = input(this.#options?.color ?? 'currentColor'); |
| 77 | + size = input(this.#options?.size ?? 24, { transform: numberAttribute }); |
| 78 | + strokeWidth = input(this.#options?.strokeWidth ?? 2, { transform: numberAttribute }); |
| 79 | + class = input(''); |
| 80 | + animate = input(false, { transform: booleanAttribute }); |
| 81 | + |
| 82 | + protected isAnimating = linkedSignal(() => this.animate()); |
| 83 | + |
| 84 | + #timer: ReturnType<typeof setTimeout> | null = null; |
| 85 | + |
| 86 | + handleMouseEnter(forced = false) { |
| 87 | + if (forced || (!this.animate() && !this.isAnimating())) { |
| 88 | + this.isAnimating.set(true); |
| 89 | + this.#timer = setTimeout(() => this.isAnimating.set(false), 850); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + protected thumbs = ['M4 21h1', 'M9 21h1', 'M14 21h1', 'M19 21h1']; |
| 94 | + |
| 95 | + constructor() { |
| 96 | + effect(() => { |
| 97 | + const animate = this.animate(); |
| 98 | + if (animate) { |
| 99 | + this.handleMouseEnter(true); |
| 100 | + } else { |
| 101 | + if (this.#timer) { |
| 102 | + clearTimeout(this.#timer); |
| 103 | + this.#timer = null; |
| 104 | + } |
| 105 | + } |
| 106 | + }); |
| 107 | + } |
| 108 | +} |
0 commit comments