create-huenei-template 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/dist/templates/frontend/angular-base/package.json +0 -1
- package/dist/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/package.json +1 -1
- package/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/templates/frontend/angular-base/package.json +0 -1
- package/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/templates/frontend/angular-base/package-lock.json +0 -13321
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
# Angular Directive Patterns
|
|
2
|
+
|
|
3
|
+
## Table of Contents
|
|
4
|
+
- [DOM Manipulation](#dom-manipulation)
|
|
5
|
+
- [Form Directives](#form-directives)
|
|
6
|
+
- [Intersection Observer](#intersection-observer)
|
|
7
|
+
- [Resize Observer](#resize-observer)
|
|
8
|
+
- [Drag and Drop](#drag-and-drop)
|
|
9
|
+
- [Permission Directive](#permission-directive)
|
|
10
|
+
|
|
11
|
+
## DOM Manipulation
|
|
12
|
+
|
|
13
|
+
### Auto-Focus Directive
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
@Directive({
|
|
17
|
+
selector: '[appAutoFocus]',
|
|
18
|
+
})
|
|
19
|
+
export class AutoFocus {
|
|
20
|
+
private el = inject(ElementRef<HTMLElement>);
|
|
21
|
+
|
|
22
|
+
enabled = input(true, { alias: 'appAutoFocus', transform: booleanAttribute });
|
|
23
|
+
delay = input(0);
|
|
24
|
+
|
|
25
|
+
constructor() {
|
|
26
|
+
afterNextRender(() => {
|
|
27
|
+
if (this.enabled()) {
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
this.el.nativeElement.focus();
|
|
30
|
+
}, this.delay());
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Usage: <input appAutoFocus />
|
|
37
|
+
// Usage: <input [appAutoFocus]="shouldFocus()" [delay]="100" />
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Text Selection Directive
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
@Directive({
|
|
44
|
+
selector: '[appSelectAll]',
|
|
45
|
+
host: {
|
|
46
|
+
'(focus)': 'onFocus()',
|
|
47
|
+
'(click)': 'onClick($event)',
|
|
48
|
+
},
|
|
49
|
+
})
|
|
50
|
+
export class SelectAll {
|
|
51
|
+
private el = inject(ElementRef<HTMLInputElement>);
|
|
52
|
+
|
|
53
|
+
onFocus() {
|
|
54
|
+
// Delay to ensure value is set
|
|
55
|
+
setTimeout(() => this.el.nativeElement.select(), 0);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
onClick(event: MouseEvent) {
|
|
59
|
+
// Select all on first click if not already focused
|
|
60
|
+
if (document.activeElement !== this.el.nativeElement) {
|
|
61
|
+
this.el.nativeElement.select();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Usage: <input appSelectAll value="Select me on focus" />
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Copy to Clipboard
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
@Directive({
|
|
73
|
+
selector: '[appCopyToClipboard]',
|
|
74
|
+
host: {
|
|
75
|
+
'(click)': 'copy()',
|
|
76
|
+
'[style.cursor]': '"pointer"',
|
|
77
|
+
},
|
|
78
|
+
})
|
|
79
|
+
export class CopyToClipboard {
|
|
80
|
+
text = input.required<string>({ alias: 'appCopyToClipboard' });
|
|
81
|
+
|
|
82
|
+
copied = output<void>();
|
|
83
|
+
error = output<Error>();
|
|
84
|
+
|
|
85
|
+
async copy() {
|
|
86
|
+
try {
|
|
87
|
+
await navigator.clipboard.writeText(this.text());
|
|
88
|
+
this.copied.emit();
|
|
89
|
+
} catch (err) {
|
|
90
|
+
this.error.emit(err as Error);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Usage:
|
|
96
|
+
// <button [appCopyToClipboard]="textToCopy" (copied)="showToast('Copied!')">
|
|
97
|
+
// Copy
|
|
98
|
+
// </button>
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Form Directives
|
|
102
|
+
|
|
103
|
+
### Trim Input
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
@Directive({
|
|
107
|
+
selector: 'input[appTrim], textarea[appTrim]',
|
|
108
|
+
host: {
|
|
109
|
+
'(blur)': 'onBlur()',
|
|
110
|
+
},
|
|
111
|
+
})
|
|
112
|
+
export class Trim {
|
|
113
|
+
private el = inject(ElementRef<HTMLInputElement | HTMLTextAreaElement>);
|
|
114
|
+
private ngControl = inject(NgControl, { optional: true, self: true });
|
|
115
|
+
|
|
116
|
+
onBlur() {
|
|
117
|
+
const value = this.el.nativeElement.value;
|
|
118
|
+
const trimmed = value.trim();
|
|
119
|
+
|
|
120
|
+
if (value !== trimmed) {
|
|
121
|
+
this.el.nativeElement.value = trimmed;
|
|
122
|
+
this.ngControl?.control?.setValue(trimmed);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Usage: <input appTrim formControlName="name" />
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Input Mask
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
@Directive({
|
|
134
|
+
selector: '[appMask]',
|
|
135
|
+
host: {
|
|
136
|
+
'(input)': 'onInput($event)',
|
|
137
|
+
'(keydown)': 'onKeydown($event)',
|
|
138
|
+
},
|
|
139
|
+
})
|
|
140
|
+
export class Mask {
|
|
141
|
+
private el = inject(ElementRef<HTMLInputElement>);
|
|
142
|
+
|
|
143
|
+
// Mask pattern: 9 = digit, A = letter, * = any
|
|
144
|
+
mask = input.required<string>({ alias: 'appMask' });
|
|
145
|
+
|
|
146
|
+
onInput(event: InputEvent) {
|
|
147
|
+
const input = this.el.nativeElement;
|
|
148
|
+
const value = input.value;
|
|
149
|
+
const masked = this.applyMask(value);
|
|
150
|
+
|
|
151
|
+
if (value !== masked) {
|
|
152
|
+
input.value = masked;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
onKeydown(event: KeyboardEvent) {
|
|
157
|
+
// Allow navigation keys
|
|
158
|
+
if (['Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', 'Tab'].includes(event.key)) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const input = this.el.nativeElement;
|
|
163
|
+
const position = input.selectionStart ?? 0;
|
|
164
|
+
const maskChar = this.mask()[position];
|
|
165
|
+
|
|
166
|
+
if (!maskChar) {
|
|
167
|
+
event.preventDefault();
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (!this.isValidChar(event.key, maskChar)) {
|
|
172
|
+
event.preventDefault();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private applyMask(value: string): string {
|
|
177
|
+
const mask = this.mask();
|
|
178
|
+
let result = '';
|
|
179
|
+
let valueIndex = 0;
|
|
180
|
+
|
|
181
|
+
for (let i = 0; i < mask.length && valueIndex < value.length; i++) {
|
|
182
|
+
const maskChar = mask[i];
|
|
183
|
+
const inputChar = value[valueIndex];
|
|
184
|
+
|
|
185
|
+
if (maskChar === '9' || maskChar === 'A' || maskChar === '*') {
|
|
186
|
+
if (this.isValidChar(inputChar, maskChar)) {
|
|
187
|
+
result += inputChar;
|
|
188
|
+
valueIndex++;
|
|
189
|
+
} else {
|
|
190
|
+
valueIndex++;
|
|
191
|
+
i--;
|
|
192
|
+
}
|
|
193
|
+
} else {
|
|
194
|
+
result += maskChar;
|
|
195
|
+
if (inputChar === maskChar) {
|
|
196
|
+
valueIndex++;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
private isValidChar(char: string, maskChar: string): boolean {
|
|
205
|
+
switch (maskChar) {
|
|
206
|
+
case '9': return /\d/.test(char);
|
|
207
|
+
case 'A': return /[a-zA-Z]/.test(char);
|
|
208
|
+
case '*': return /[a-zA-Z0-9]/.test(char);
|
|
209
|
+
default: return char === maskChar;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Usage: <input appMask="(999) 999-9999" placeholder="(555) 123-4567" />
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Character Counter
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
@Directive({
|
|
221
|
+
selector: '[appCharCount]',
|
|
222
|
+
})
|
|
223
|
+
export class CharCount {
|
|
224
|
+
private el = inject(ElementRef<HTMLInputElement | HTMLTextAreaElement>);
|
|
225
|
+
|
|
226
|
+
maxLength = input.required<number>({ alias: 'appCharCount' });
|
|
227
|
+
|
|
228
|
+
currentLength = signal(0);
|
|
229
|
+
remaining = computed(() => this.maxLength() - this.currentLength());
|
|
230
|
+
isOverLimit = computed(() => this.remaining() < 0);
|
|
231
|
+
|
|
232
|
+
constructor() {
|
|
233
|
+
effect(() => {
|
|
234
|
+
this.currentLength.set(this.el.nativeElement.value.length);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// Listen for input changes
|
|
238
|
+
afterNextRender(() => {
|
|
239
|
+
this.el.nativeElement.addEventListener('input', () => {
|
|
240
|
+
this.currentLength.set(this.el.nativeElement.value.length);
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Usage with template:
|
|
247
|
+
// <textarea appCharCount="500" #counter="appCharCount"></textarea>
|
|
248
|
+
// <span>{{ counter.remaining() }} characters remaining</span>
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## Intersection Observer
|
|
252
|
+
|
|
253
|
+
### Lazy Load Directive
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
@Directive({
|
|
257
|
+
selector: '[appLazyLoad]',
|
|
258
|
+
})
|
|
259
|
+
export class LazyLoad implements OnDestroy {
|
|
260
|
+
private el = inject(ElementRef<HTMLElement>);
|
|
261
|
+
private observer: IntersectionObserver | null = null;
|
|
262
|
+
|
|
263
|
+
src = input.required<string>({ alias: 'appLazyLoad' });
|
|
264
|
+
placeholder = input('/assets/placeholder.png');
|
|
265
|
+
|
|
266
|
+
loaded = output<void>();
|
|
267
|
+
|
|
268
|
+
constructor() {
|
|
269
|
+
afterNextRender(() => {
|
|
270
|
+
this.setupObserver();
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private setupObserver() {
|
|
275
|
+
this.observer = new IntersectionObserver(
|
|
276
|
+
(entries) => {
|
|
277
|
+
entries.forEach((entry) => {
|
|
278
|
+
if (entry.isIntersecting) {
|
|
279
|
+
this.loadImage();
|
|
280
|
+
this.observer?.disconnect();
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
},
|
|
284
|
+
{ rootMargin: '50px' }
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
this.observer.observe(this.el.nativeElement);
|
|
288
|
+
|
|
289
|
+
// Set placeholder
|
|
290
|
+
if (this.el.nativeElement instanceof HTMLImageElement) {
|
|
291
|
+
this.el.nativeElement.src = this.placeholder();
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
private loadImage() {
|
|
296
|
+
const element = this.el.nativeElement;
|
|
297
|
+
|
|
298
|
+
if (element instanceof HTMLImageElement) {
|
|
299
|
+
element.src = this.src();
|
|
300
|
+
element.onload = () => this.loaded.emit();
|
|
301
|
+
} else {
|
|
302
|
+
element.style.backgroundImage = `url(${this.src()})`;
|
|
303
|
+
this.loaded.emit();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
ngOnDestroy() {
|
|
308
|
+
this.observer?.disconnect();
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// Usage: <img [appLazyLoad]="imageUrl" alt="Lazy loaded image" />
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
### Infinite Scroll
|
|
316
|
+
|
|
317
|
+
```typescript
|
|
318
|
+
@Directive({
|
|
319
|
+
selector: '[appInfiniteScroll]',
|
|
320
|
+
})
|
|
321
|
+
export class InfiniteScroll implements OnDestroy {
|
|
322
|
+
private el = inject(ElementRef<HTMLElement>);
|
|
323
|
+
private observer: IntersectionObserver | null = null;
|
|
324
|
+
|
|
325
|
+
threshold = input(0.1);
|
|
326
|
+
disabled = input(false);
|
|
327
|
+
|
|
328
|
+
scrolled = output<void>();
|
|
329
|
+
|
|
330
|
+
constructor() {
|
|
331
|
+
afterNextRender(() => {
|
|
332
|
+
this.setupObserver();
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
effect(() => {
|
|
336
|
+
if (this.disabled()) {
|
|
337
|
+
this.observer?.disconnect();
|
|
338
|
+
} else {
|
|
339
|
+
this.setupObserver();
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
private setupObserver() {
|
|
345
|
+
this.observer?.disconnect();
|
|
346
|
+
|
|
347
|
+
this.observer = new IntersectionObserver(
|
|
348
|
+
(entries) => {
|
|
349
|
+
if (entries[0].isIntersecting && !this.disabled()) {
|
|
350
|
+
this.scrolled.emit();
|
|
351
|
+
}
|
|
352
|
+
},
|
|
353
|
+
{ threshold: this.threshold() }
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
this.observer.observe(this.el.nativeElement);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
ngOnDestroy() {
|
|
360
|
+
this.observer?.disconnect();
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Usage:
|
|
365
|
+
// <div class="list">
|
|
366
|
+
// @for (item of items(); track item.id) {
|
|
367
|
+
// <div>{{ item.name }}</div>
|
|
368
|
+
// }
|
|
369
|
+
// <div appInfiniteScroll (scrolled)="loadMore()" [disabled]="isLoading()">
|
|
370
|
+
// Loading...
|
|
371
|
+
// </div>
|
|
372
|
+
// </div>
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
## Resize Observer
|
|
376
|
+
|
|
377
|
+
```typescript
|
|
378
|
+
@Directive({
|
|
379
|
+
selector: '[appResize]',
|
|
380
|
+
})
|
|
381
|
+
export class Resize implements OnDestroy {
|
|
382
|
+
private el = inject(ElementRef<HTMLElement>);
|
|
383
|
+
private observer: ResizeObserver | null = null;
|
|
384
|
+
|
|
385
|
+
width = signal(0);
|
|
386
|
+
height = signal(0);
|
|
387
|
+
|
|
388
|
+
resized = output<{ width: number; height: number }>();
|
|
389
|
+
|
|
390
|
+
constructor() {
|
|
391
|
+
afterNextRender(() => {
|
|
392
|
+
this.observer = new ResizeObserver((entries) => {
|
|
393
|
+
const entry = entries[0];
|
|
394
|
+
const { width, height } = entry.contentRect;
|
|
395
|
+
|
|
396
|
+
this.width.set(width);
|
|
397
|
+
this.height.set(height);
|
|
398
|
+
this.resized.emit({ width, height });
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
this.observer.observe(this.el.nativeElement);
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
ngOnDestroy() {
|
|
406
|
+
this.observer?.disconnect();
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// Usage:
|
|
411
|
+
// <div appResize #resize="appResize">
|
|
412
|
+
// Size: {{ resize.width() }}x{{ resize.height() }}
|
|
413
|
+
// </div>
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
## Drag and Drop
|
|
417
|
+
|
|
418
|
+
```typescript
|
|
419
|
+
@Directive({
|
|
420
|
+
selector: '[appDraggable]',
|
|
421
|
+
host: {
|
|
422
|
+
'draggable': 'true',
|
|
423
|
+
'[class.dragging]': 'isDragging()',
|
|
424
|
+
'(dragstart)': 'onDragStart($event)',
|
|
425
|
+
'(dragend)': 'onDragEnd($event)',
|
|
426
|
+
},
|
|
427
|
+
})
|
|
428
|
+
export class Draggable {
|
|
429
|
+
data = input<any>(null, { alias: 'appDraggable' });
|
|
430
|
+
effectAllowed = input<DataTransfer['effectAllowed']>('move');
|
|
431
|
+
|
|
432
|
+
isDragging = signal(false);
|
|
433
|
+
|
|
434
|
+
dragStart = output<DragEvent>();
|
|
435
|
+
dragEnd = output<DragEvent>();
|
|
436
|
+
|
|
437
|
+
onDragStart(event: DragEvent) {
|
|
438
|
+
this.isDragging.set(true);
|
|
439
|
+
|
|
440
|
+
if (event.dataTransfer) {
|
|
441
|
+
event.dataTransfer.effectAllowed = this.effectAllowed();
|
|
442
|
+
event.dataTransfer.setData('application/json', JSON.stringify(this.data()));
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
this.dragStart.emit(event);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
onDragEnd(event: DragEvent) {
|
|
449
|
+
this.isDragging.set(false);
|
|
450
|
+
this.dragEnd.emit(event);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
@Directive({
|
|
455
|
+
selector: '[appDropZone]',
|
|
456
|
+
host: {
|
|
457
|
+
'[class.drag-over]': 'isDragOver()',
|
|
458
|
+
'(dragover)': 'onDragOver($event)',
|
|
459
|
+
'(dragleave)': 'onDragLeave($event)',
|
|
460
|
+
'(drop)': 'onDrop($event)',
|
|
461
|
+
},
|
|
462
|
+
})
|
|
463
|
+
export class DropZone {
|
|
464
|
+
isDragOver = signal(false);
|
|
465
|
+
|
|
466
|
+
dropped = output<any>();
|
|
467
|
+
|
|
468
|
+
onDragOver(event: DragEvent) {
|
|
469
|
+
event.preventDefault();
|
|
470
|
+
this.isDragOver.set(true);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
onDragLeave(event: DragEvent) {
|
|
474
|
+
this.isDragOver.set(false);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
onDrop(event: DragEvent) {
|
|
478
|
+
event.preventDefault();
|
|
479
|
+
this.isDragOver.set(false);
|
|
480
|
+
|
|
481
|
+
const data = event.dataTransfer?.getData('application/json');
|
|
482
|
+
if (data) {
|
|
483
|
+
this.dropped.emit(JSON.parse(data));
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// Usage:
|
|
489
|
+
// <div [appDraggable]="item">Drag me</div>
|
|
490
|
+
// <div appDropZone (dropped)="onItemDropped($event)">Drop here</div>
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
## Permission Directive
|
|
494
|
+
|
|
495
|
+
```typescript
|
|
496
|
+
@Directive({
|
|
497
|
+
selector: '[appHasPermission]',
|
|
498
|
+
})
|
|
499
|
+
export class HasPermission {
|
|
500
|
+
private templateRef = inject(TemplateRef<any>);
|
|
501
|
+
private viewContainer = inject(ViewContainerRef);
|
|
502
|
+
private authService = inject(Auth);
|
|
503
|
+
private hasView = false;
|
|
504
|
+
|
|
505
|
+
permission = input.required<string | string[]>({ alias: 'appHasPermission' });
|
|
506
|
+
mode = input<'any' | 'all'>('any');
|
|
507
|
+
|
|
508
|
+
constructor() {
|
|
509
|
+
effect(() => {
|
|
510
|
+
const hasPermission = this.checkPermission();
|
|
511
|
+
|
|
512
|
+
if (hasPermission && !this.hasView) {
|
|
513
|
+
this.viewContainer.createEmbeddedView(this.templateRef);
|
|
514
|
+
this.hasView = true;
|
|
515
|
+
} else if (!hasPermission && this.hasView) {
|
|
516
|
+
this.viewContainer.clear();
|
|
517
|
+
this.hasView = false;
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
private checkPermission(): boolean {
|
|
523
|
+
const required = this.permission();
|
|
524
|
+
const permissions = Array.isArray(required) ? required : [required];
|
|
525
|
+
const userPermissions = this.authService.permissions();
|
|
526
|
+
|
|
527
|
+
if (this.mode() === 'all') {
|
|
528
|
+
return permissions.every(p => userPermissions.includes(p));
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return permissions.some(p => userPermissions.includes(p));
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// Usage:
|
|
536
|
+
// <button *appHasPermission="'admin'">Admin Only</button>
|
|
537
|
+
// <div *appHasPermission="['edit', 'delete']; mode: 'all'">Edit & Delete</div>
|
|
538
|
+
```
|
|
539
|
+
|
|
540
|
+
## Export Directive Reference
|
|
541
|
+
|
|
542
|
+
```typescript
|
|
543
|
+
@Directive({
|
|
544
|
+
selector: '[appToggle]',
|
|
545
|
+
exportAs: 'appToggle',
|
|
546
|
+
})
|
|
547
|
+
export class Toggle {
|
|
548
|
+
isOpen = signal(false);
|
|
549
|
+
|
|
550
|
+
toggle() {
|
|
551
|
+
this.isOpen.update(v => !v);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
open() {
|
|
555
|
+
this.isOpen.set(true);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
close() {
|
|
559
|
+
this.isOpen.set(false);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// Usage:
|
|
564
|
+
// <div appToggle #toggle="appToggle">
|
|
565
|
+
// <button (click)="toggle.toggle()">Toggle</button>
|
|
566
|
+
// @if (toggle.isOpen()) {
|
|
567
|
+
// <div>Content</div>
|
|
568
|
+
// }
|
|
569
|
+
// </div>
|
|
570
|
+
```
|