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,437 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: angular-directives
|
|
3
|
+
description: Create custom directives in Angular v20+ for DOM manipulation and behavior extension. Use for attribute directives that modify element behavior/appearance, structural directives for portals/overlays, and host directives for composition. Triggers on creating reusable DOM behaviors, extending element functionality, or composing behaviors across components. Note - use native @if/@for/@switch for control flow, not custom structural directives.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Angular Directives
|
|
7
|
+
|
|
8
|
+
Create custom directives for reusable DOM manipulation and behavior in Angular v20+.
|
|
9
|
+
|
|
10
|
+
## Attribute Directives
|
|
11
|
+
|
|
12
|
+
Modify the appearance or behavior of an element:
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { Directive, input, effect, inject, ElementRef } from '@angular/core';
|
|
16
|
+
|
|
17
|
+
@Directive({
|
|
18
|
+
selector: '[appHighlight]',
|
|
19
|
+
})
|
|
20
|
+
export class Highlight {
|
|
21
|
+
private el = inject(ElementRef<HTMLElement>);
|
|
22
|
+
|
|
23
|
+
// Input with alias matching selector
|
|
24
|
+
color = input('yellow', { alias: 'appHighlight' });
|
|
25
|
+
|
|
26
|
+
constructor() {
|
|
27
|
+
effect(() => {
|
|
28
|
+
this.el.nativeElement.style.backgroundColor = this.color();
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Usage: <p appHighlight="lightblue">Highlighted text</p>
|
|
34
|
+
// Usage: <p appHighlight>Default yellow highlight</p>
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Using host Property
|
|
38
|
+
|
|
39
|
+
Prefer `host` over `@HostBinding`/`@HostListener`:
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
@Directive({
|
|
43
|
+
selector: '[appTooltip]',
|
|
44
|
+
host: {
|
|
45
|
+
'(mouseenter)': 'show()',
|
|
46
|
+
'(mouseleave)': 'hide()',
|
|
47
|
+
'[attr.aria-describedby]': 'tooltipId',
|
|
48
|
+
},
|
|
49
|
+
})
|
|
50
|
+
export class Tooltip {
|
|
51
|
+
text = input.required<string>({ alias: 'appTooltip' });
|
|
52
|
+
position = input<'top' | 'bottom' | 'left' | 'right'>('top');
|
|
53
|
+
|
|
54
|
+
tooltipId = `tooltip-${crypto.randomUUID()}`;
|
|
55
|
+
private tooltipEl: HTMLElement | null = null;
|
|
56
|
+
private el = inject(ElementRef<HTMLElement>);
|
|
57
|
+
|
|
58
|
+
show() {
|
|
59
|
+
this.tooltipEl = document.createElement('div');
|
|
60
|
+
this.tooltipEl.id = this.tooltipId;
|
|
61
|
+
this.tooltipEl.className = `tooltip tooltip-${this.position()}`;
|
|
62
|
+
this.tooltipEl.textContent = this.text();
|
|
63
|
+
this.tooltipEl.setAttribute('role', 'tooltip');
|
|
64
|
+
document.body.appendChild(this.tooltipEl);
|
|
65
|
+
this.positionTooltip();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
hide() {
|
|
69
|
+
this.tooltipEl?.remove();
|
|
70
|
+
this.tooltipEl = null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private positionTooltip() {
|
|
74
|
+
// Position logic based on this.position() and this.el
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Usage: <button appTooltip="Click to save" position="bottom">Save</button>
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Class and Style Manipulation
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
@Directive({
|
|
85
|
+
selector: '[appButton]',
|
|
86
|
+
host: {
|
|
87
|
+
'class': 'btn',
|
|
88
|
+
'[class.btn-primary]': 'variant() === "primary"',
|
|
89
|
+
'[class.btn-secondary]': 'variant() === "secondary"',
|
|
90
|
+
'[class.btn-sm]': 'size() === "small"',
|
|
91
|
+
'[class.btn-lg]': 'size() === "large"',
|
|
92
|
+
'[class.disabled]': 'disabled()',
|
|
93
|
+
'[attr.disabled]': 'disabled() || null',
|
|
94
|
+
},
|
|
95
|
+
})
|
|
96
|
+
export class Button {
|
|
97
|
+
variant = input<'primary' | 'secondary'>('primary');
|
|
98
|
+
size = input<'small' | 'medium' | 'large'>('medium');
|
|
99
|
+
disabled = input(false, { transform: booleanAttribute });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Usage: <button appButton variant="primary" size="large">Click</button>
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Event Handling
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
@Directive({
|
|
109
|
+
selector: '[appClickOutside]',
|
|
110
|
+
host: {
|
|
111
|
+
'(document:click)': 'onDocumentClick($event)',
|
|
112
|
+
},
|
|
113
|
+
})
|
|
114
|
+
export class ClickOutside {
|
|
115
|
+
private el = inject(ElementRef<HTMLElement>);
|
|
116
|
+
|
|
117
|
+
clickOutside = output<void>();
|
|
118
|
+
|
|
119
|
+
onDocumentClick(event: MouseEvent) {
|
|
120
|
+
if (!this.el.nativeElement.contains(event.target as Node)) {
|
|
121
|
+
this.clickOutside.emit();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Usage: <div appClickOutside (clickOutside)="closeMenu()">...</div>
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Keyboard Shortcuts
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
@Directive({
|
|
133
|
+
selector: '[appShortcut]',
|
|
134
|
+
host: {
|
|
135
|
+
'(document:keydown)': 'onKeydown($event)',
|
|
136
|
+
},
|
|
137
|
+
})
|
|
138
|
+
export class Shortcut {
|
|
139
|
+
key = input.required<string>({ alias: 'appShortcut' });
|
|
140
|
+
ctrl = input(false, { transform: booleanAttribute });
|
|
141
|
+
shift = input(false, { transform: booleanAttribute });
|
|
142
|
+
alt = input(false, { transform: booleanAttribute });
|
|
143
|
+
|
|
144
|
+
triggered = output<KeyboardEvent>();
|
|
145
|
+
|
|
146
|
+
onKeydown(event: KeyboardEvent) {
|
|
147
|
+
const keyMatch = event.key.toLowerCase() === this.key().toLowerCase();
|
|
148
|
+
const ctrlMatch = this.ctrl() ? event.ctrlKey || event.metaKey : !event.ctrlKey && !event.metaKey;
|
|
149
|
+
const shiftMatch = this.shift() ? event.shiftKey : !event.shiftKey;
|
|
150
|
+
const altMatch = this.alt() ? event.altKey : !event.altKey;
|
|
151
|
+
|
|
152
|
+
if (keyMatch && ctrlMatch && shiftMatch && altMatch) {
|
|
153
|
+
event.preventDefault();
|
|
154
|
+
this.triggered.emit(event);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Usage: <button appShortcut="s" ctrl (triggered)="save()">Save (Ctrl+S)</button>
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Structural Directives
|
|
163
|
+
|
|
164
|
+
Use structural directives for DOM manipulation beyond control flow (portals, overlays, dynamic insertion points). For conditionals and loops, use native `@if`, `@for`, `@switch`.
|
|
165
|
+
|
|
166
|
+
### Portal Directive
|
|
167
|
+
|
|
168
|
+
Render content in a different DOM location:
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import { Directive, inject, TemplateRef, ViewContainerRef, OnInit, OnDestroy, input } from '@angular/core';
|
|
172
|
+
|
|
173
|
+
@Directive({
|
|
174
|
+
selector: '[appPortal]',
|
|
175
|
+
})
|
|
176
|
+
export class Portal implements OnInit, OnDestroy {
|
|
177
|
+
private templateRef = inject(TemplateRef<any>);
|
|
178
|
+
private viewContainerRef = inject(ViewContainerRef);
|
|
179
|
+
private viewRef: EmbeddedViewRef<any> | null = null;
|
|
180
|
+
|
|
181
|
+
// Target container selector or element
|
|
182
|
+
target = input<string | HTMLElement>('body', { alias: 'appPortal' });
|
|
183
|
+
|
|
184
|
+
ngOnInit() {
|
|
185
|
+
const container = this.getContainer();
|
|
186
|
+
if (container) {
|
|
187
|
+
this.viewRef = this.viewContainerRef.createEmbeddedView(this.templateRef);
|
|
188
|
+
this.viewRef.rootNodes.forEach(node => container.appendChild(node));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
ngOnDestroy() {
|
|
193
|
+
this.viewRef?.destroy();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private getContainer(): HTMLElement | null {
|
|
197
|
+
const target = this.target();
|
|
198
|
+
if (typeof target === 'string') {
|
|
199
|
+
return document.querySelector(target);
|
|
200
|
+
}
|
|
201
|
+
return target;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Usage: Render modal at body level
|
|
206
|
+
// <div *appPortal="'body'">
|
|
207
|
+
// <div class="modal">Modal content</div>
|
|
208
|
+
// </div>
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### Lazy Render Directive
|
|
212
|
+
|
|
213
|
+
Defer rendering until condition is met (one-time):
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
@Directive({
|
|
217
|
+
selector: '[appLazyRender]',
|
|
218
|
+
})
|
|
219
|
+
export class LazyRender {
|
|
220
|
+
private templateRef = inject(TemplateRef<any>);
|
|
221
|
+
private viewContainer = inject(ViewContainerRef);
|
|
222
|
+
private rendered = false;
|
|
223
|
+
|
|
224
|
+
condition = input.required<boolean>({ alias: 'appLazyRender' });
|
|
225
|
+
|
|
226
|
+
constructor() {
|
|
227
|
+
effect(() => {
|
|
228
|
+
// Only render once when condition becomes true
|
|
229
|
+
if (this.condition() && !this.rendered) {
|
|
230
|
+
this.viewContainer.createEmbeddedView(this.templateRef);
|
|
231
|
+
this.rendered = true;
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Usage: Render heavy component only when tab is first activated
|
|
238
|
+
// <div *appLazyRender="activeTab() === 'reports'">
|
|
239
|
+
// <app-heavy-reports />
|
|
240
|
+
// </div>
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Template Outlet with Context
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
interface TemplateContext<T> {
|
|
247
|
+
$implicit: T;
|
|
248
|
+
item: T;
|
|
249
|
+
index: number;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
@Directive({
|
|
253
|
+
selector: '[appTemplateOutlet]',
|
|
254
|
+
})
|
|
255
|
+
export class TemplateOutlet<T> {
|
|
256
|
+
private viewContainer = inject(ViewContainerRef);
|
|
257
|
+
private currentView: EmbeddedViewRef<TemplateContext<T>> | null = null;
|
|
258
|
+
|
|
259
|
+
template = input.required<TemplateRef<TemplateContext<T>>>({ alias: 'appTemplateOutlet' });
|
|
260
|
+
context = input.required<T>({ alias: 'appTemplateOutletContext' });
|
|
261
|
+
index = input(0, { alias: 'appTemplateOutletIndex' });
|
|
262
|
+
|
|
263
|
+
constructor() {
|
|
264
|
+
effect(() => {
|
|
265
|
+
const template = this.template();
|
|
266
|
+
const context = this.context();
|
|
267
|
+
const index = this.index();
|
|
268
|
+
|
|
269
|
+
if (this.currentView) {
|
|
270
|
+
this.currentView.context.$implicit = context;
|
|
271
|
+
this.currentView.context.item = context;
|
|
272
|
+
this.currentView.context.index = index;
|
|
273
|
+
this.currentView.markForCheck();
|
|
274
|
+
} else {
|
|
275
|
+
this.currentView = this.viewContainer.createEmbeddedView(template, {
|
|
276
|
+
$implicit: context,
|
|
277
|
+
item: context,
|
|
278
|
+
index,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Usage: Custom list with template
|
|
286
|
+
// <ng-template #itemTemplate let-item let-i="index">
|
|
287
|
+
// <div>{{ i }}: {{ item.name }}</div>
|
|
288
|
+
// </ng-template>
|
|
289
|
+
// <ng-container
|
|
290
|
+
// *appTemplateOutlet="itemTemplate; context: item; index: i"
|
|
291
|
+
// />
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
## Host Directives
|
|
295
|
+
|
|
296
|
+
Compose directives on components or other directives:
|
|
297
|
+
|
|
298
|
+
```typescript
|
|
299
|
+
// Reusable behavior directives
|
|
300
|
+
@Directive({
|
|
301
|
+
selector: '[focusable]',
|
|
302
|
+
host: {
|
|
303
|
+
'tabindex': '0',
|
|
304
|
+
'(focus)': 'onFocus()',
|
|
305
|
+
'(blur)': 'onBlur()',
|
|
306
|
+
'[class.focused]': 'isFocused()',
|
|
307
|
+
},
|
|
308
|
+
})
|
|
309
|
+
export class Focusable {
|
|
310
|
+
isFocused = signal(false);
|
|
311
|
+
|
|
312
|
+
onFocus() { this.isFocused.set(true); }
|
|
313
|
+
onBlur() { this.isFocused.set(false); }
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
@Directive({
|
|
317
|
+
selector: '[disableable]',
|
|
318
|
+
host: {
|
|
319
|
+
'[class.disabled]': 'disabled()',
|
|
320
|
+
'[attr.aria-disabled]': 'disabled()',
|
|
321
|
+
},
|
|
322
|
+
})
|
|
323
|
+
export class Disableable {
|
|
324
|
+
disabled = input(false, { transform: booleanAttribute });
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// Component using host directives
|
|
328
|
+
@Component({
|
|
329
|
+
selector: 'app-custom-button',
|
|
330
|
+
hostDirectives: [
|
|
331
|
+
Focusable,
|
|
332
|
+
{
|
|
333
|
+
directive: Disableable,
|
|
334
|
+
inputs: ['disabled'],
|
|
335
|
+
},
|
|
336
|
+
],
|
|
337
|
+
host: {
|
|
338
|
+
'role': 'button',
|
|
339
|
+
'(click)': 'onClick($event)',
|
|
340
|
+
'(keydown.enter)': 'onClick($event)',
|
|
341
|
+
'(keydown.space)': 'onClick($event)',
|
|
342
|
+
},
|
|
343
|
+
template: `<ng-content />`,
|
|
344
|
+
})
|
|
345
|
+
export class CustomButton {
|
|
346
|
+
private disableable = inject(Disableable);
|
|
347
|
+
|
|
348
|
+
clicked = output<void>();
|
|
349
|
+
|
|
350
|
+
onClick(event: Event) {
|
|
351
|
+
if (!this.disableable.disabled()) {
|
|
352
|
+
this.clicked.emit();
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Usage: <app-custom-button disabled>Click me</app-custom-button>
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
### Exposing Host Directive Outputs
|
|
361
|
+
|
|
362
|
+
```typescript
|
|
363
|
+
@Directive({
|
|
364
|
+
selector: '[hoverable]',
|
|
365
|
+
host: {
|
|
366
|
+
'(mouseenter)': 'onEnter()',
|
|
367
|
+
'(mouseleave)': 'onLeave()',
|
|
368
|
+
'[class.hovered]': 'isHovered()',
|
|
369
|
+
},
|
|
370
|
+
})
|
|
371
|
+
export class Hoverable {
|
|
372
|
+
isHovered = signal(false);
|
|
373
|
+
|
|
374
|
+
hoverChange = output<boolean>();
|
|
375
|
+
|
|
376
|
+
onEnter() {
|
|
377
|
+
this.isHovered.set(true);
|
|
378
|
+
this.hoverChange.emit(true);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
onLeave() {
|
|
382
|
+
this.isHovered.set(false);
|
|
383
|
+
this.hoverChange.emit(false);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
@Component({
|
|
388
|
+
selector: 'app-card',
|
|
389
|
+
hostDirectives: [
|
|
390
|
+
{
|
|
391
|
+
directive: Hoverable,
|
|
392
|
+
outputs: ['hoverChange'],
|
|
393
|
+
},
|
|
394
|
+
],
|
|
395
|
+
template: `<ng-content />`,
|
|
396
|
+
})
|
|
397
|
+
export class Card {}
|
|
398
|
+
|
|
399
|
+
// Usage: <app-card (hoverChange)="onHover($event)">...</app-card>
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
## Directive Composition API
|
|
403
|
+
|
|
404
|
+
Combine multiple behaviors:
|
|
405
|
+
|
|
406
|
+
```typescript
|
|
407
|
+
// Base directives
|
|
408
|
+
@Directive({ selector: '[withRipple]' })
|
|
409
|
+
export class Ripple {
|
|
410
|
+
// Ripple effect implementation
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
@Directive({ selector: '[withElevation]' })
|
|
414
|
+
export class Elevation {
|
|
415
|
+
elevation = input(2);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// Composed component
|
|
419
|
+
@Component({
|
|
420
|
+
selector: 'app-material-button',
|
|
421
|
+
hostDirectives: [
|
|
422
|
+
Ripple,
|
|
423
|
+
{
|
|
424
|
+
directive: Elevation,
|
|
425
|
+
inputs: ['elevation'],
|
|
426
|
+
},
|
|
427
|
+
{
|
|
428
|
+
directive: Disableable,
|
|
429
|
+
inputs: ['disabled'],
|
|
430
|
+
},
|
|
431
|
+
],
|
|
432
|
+
template: `<ng-content />`,
|
|
433
|
+
})
|
|
434
|
+
export class MaterialButton {}
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
For advanced patterns, see [references/directive-patterns.md](references/directive-patterns.md).
|