angular-directives
Angular v20以降でDOM操作や機能拡張のためのカスタムディレクティブを作成します。要素の動作・見た目を変える属性ディレクティブ、ポータルやオーバーレイ向けの構造ディレクティブ、コンポジション用のホストディレクティブに活用してください。再利用可能なDOM動作の実装や、要素機能の拡張、コンポーネント間での動作合成が必要な場面で使用します。なお、制御フローには@if/@for/@switchをネイティブで使用し、カスタム構造ディレクティブは使用しないでください。
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.
SKILL.md 本文
Angular ディレクティブ
Angular v20+ でカスタムディレクティブを作成し、再利用可能な DOM 操作と動作を実現します。
属性ディレクティブ
要素の外観または動作を変更します:
import { Directive, input, effect, inject, ElementRef } from '@angular/core';
@Directive({
selector: '[appHighlight]',
})
export class Highlight {
private el = inject(ElementRef<HTMLElement>);
// セレクターと一致するエイリアスを持つ入力
color = input('yellow', { alias: 'appHighlight' });
constructor() {
effect(() => {
this.el.nativeElement.style.backgroundColor = this.color();
});
}
}
// 使用例: <p appHighlight="lightblue">Highlighted text</p>
// 使用例: <p appHighlight>Default yellow highlight</p>
host プロパティの使用
@HostBinding/@HostListener ではなく host を推奨します:
@Directive({
selector: '[appTooltip]',
host: {
'(mouseenter)': 'show()',
'(mouseleave)': 'hide()',
'[attr.aria-describedby]': 'tooltipId',
},
})
export class Tooltip {
text = input.required<string>({ alias: 'appTooltip' });
position = input<'top' | 'bottom' | 'left' | 'right'>('top');
tooltipId = `tooltip-${crypto.randomUUID()}`;
private tooltipEl: HTMLElement | null = null;
private el = inject(ElementRef<HTMLElement>);
show() {
this.tooltipEl = document.createElement('div');
this.tooltipEl.id = this.tooltipId;
this.tooltipEl.className = `tooltip tooltip-${this.position()}`;
this.tooltipEl.textContent = this.text();
this.tooltipEl.setAttribute('role', 'tooltip');
document.body.appendChild(this.tooltipEl);
this.positionTooltip();
}
hide() {
this.tooltipEl?.remove();
this.tooltipEl = null;
}
private positionTooltip() {
// Position logic based on this.position() and this.el
}
}
// 使用例: <button appTooltip="Click to save" position="bottom">Save</button>
クラスとスタイルの操作
@Directive({
selector: '[appButton]',
host: {
'class': 'btn',
'[class.btn-primary]': 'variant() === "primary"',
'[class.btn-secondary]': 'variant() === "secondary"',
'[class.btn-sm]': 'size() === "small"',
'[class.btn-lg]': 'size() === "large"',
'[class.disabled]': 'disabled()',
'[attr.disabled]': 'disabled() || null',
},
})
export class Button {
variant = input<'primary' | 'secondary'>('primary');
size = input<'small' | 'medium' | 'large'>('medium');
disabled = input(false, { transform: booleanAttribute });
}
// 使用例: <button appButton variant="primary" size="large">Click</button>
イベント処理
@Directive({
selector: '[appClickOutside]',
host: {
'(document:click)': 'onDocumentClick($event)',
},
})
export class ClickOutside {
private el = inject(ElementRef<HTMLElement>);
clickOutside = output<void>();
onDocumentClick(event: MouseEvent) {
if (!this.el.nativeElement.contains(event.target as Node)) {
this.clickOutside.emit();
}
}
}
// 使用例: <div appClickOutside (clickOutside)="closeMenu()">...</div>
キーボードショートカット
@Directive({
selector: '[appShortcut]',
host: {
'(document:keydown)': 'onKeydown($event)',
},
})
export class Shortcut {
key = input.required<string>({ alias: 'appShortcut' });
ctrl = input(false, { transform: booleanAttribute });
shift = input(false, { transform: booleanAttribute });
alt = input(false, { transform: booleanAttribute });
triggered = output<KeyboardEvent>();
onKeydown(event: KeyboardEvent) {
const keyMatch = event.key.toLowerCase() === this.key().toLowerCase();
const ctrlMatch = this.ctrl() ? event.ctrlKey || event.metaKey : !event.ctrlKey && !event.metaKey;
const shiftMatch = this.shift() ? event.shiftKey : !event.shiftKey;
const altMatch = this.alt() ? event.altKey : !event.altKey;
if (keyMatch && ctrlMatch && shiftMatch && altMatch) {
event.preventDefault();
this.triggered.emit(event);
}
}
}
// 使用例: <button appShortcut="s" ctrl (triggered)="save()">Save (Ctrl+S)</button>
構造ディレクティブ
制御フロー以外の DOM 操作 (ポータル、オーバーレイ、動的挿入ポイント) に構造ディレクティブを使用します。条件分岐とループには、ネイティブの @if、@for、@switch を使用してください。
ポータルディレクティブ
異なる DOM 位置にコンテンツをレンダリングします:
import { Directive, inject, TemplateRef, ViewContainerRef, OnInit, OnDestroy, input } from '@angular/core';
@Directive({
selector: '[appPortal]',
})
export class Portal implements OnInit, OnDestroy {
private templateRef = inject(TemplateRef<any>);
private viewContainerRef = inject(ViewContainerRef);
private viewRef: EmbeddedViewRef<any> | null = null;
// ターゲットコンテナセレクターまたは要素
target = input<string | HTMLElement>('body', { alias: 'appPortal' });
ngOnInit() {
const container = this.getContainer();
if (container) {
this.viewRef = this.viewContainerRef.createEmbeddedView(this.templateRef);
this.viewRef.rootNodes.forEach(node => container.appendChild(node));
}
}
ngOnDestroy() {
this.viewRef?.destroy();
}
private getContainer(): HTMLElement | null {
const target = this.target();
if (typeof target === 'string') {
return document.querySelector(target);
}
return target;
}
}
// 使用例: モーダルを body レベルでレンダリング
// <div *appPortal="'body'">
// <div class="modal">Modal content</div>
// </div>
遅延レンダリングディレクティブ
条件が満たされるまでレンダリングを遅延させます (1 回限り):
@Directive({
selector: '[appLazyRender]',
})
export class LazyRender {
private templateRef = inject(TemplateRef<any>);
private viewContainer = inject(ViewContainerRef);
private rendered = false;
condition = input.required<boolean>({ alias: 'appLazyRender' });
constructor() {
effect(() => {
// 条件が true になった時点で1回のみレンダリング
if (this.condition() && !this.rendered) {
this.viewContainer.createEmbeddedView(this.templateRef);
this.rendered = true;
}
});
}
}
// 使用例: タブが最初にアクティブになった時点でのみ重いコンポーネントをレンダリング
// <div *appLazyRender="activeTab() === 'reports'">
// <app-heavy-reports />
// </div>
コンテキスト付きテンプレートアウトレット
interface TemplateContext<T> {
$implicit: T;
item: T;
index: number;
}
@Directive({
selector: '[appTemplateOutlet]',
})
export class TemplateOutlet<T> {
private viewContainer = inject(ViewContainerRef);
private currentView: EmbeddedViewRef<TemplateContext<T>> | null = null;
template = input.required<TemplateRef<TemplateContext<T>>>({ alias: 'appTemplateOutlet' });
context = input.required<T>({ alias: 'appTemplateOutletContext' });
index = input(0, { alias: 'appTemplateOutletIndex' });
constructor() {
effect(() => {
const template = this.template();
const context = this.context();
const index = this.index();
if (this.currentView) {
this.currentView.context.$implicit = context;
this.currentView.context.item = context;
this.currentView.context.index = index;
this.currentView.markForCheck();
} else {
this.currentView = this.viewContainer.createEmbeddedView(template, {
$implicit: context,
item: context,
index,
});
}
});
}
}
// 使用例: カスタムリストとテンプレート
// <ng-template #itemTemplate let-item let-i="index">
// <div>{{ i }}: {{ item.name }}</div>
// </ng-template>
// <ng-container
// *appTemplateOutlet="itemTemplate; context: item; index: i"
// />
ホストディレクティブ
コンポーネントまたは他のディレクティブ上で動作を構成します:
// 再利用可能な動作ディレクティブ
@Directive({
selector: '[focusable]',
host: {
'tabindex': '0',
'(focus)': 'onFocus()',
'(blur)': 'onBlur()',
'[class.focused]': 'isFocused()',
},
})
export class Focusable {
isFocused = signal(false);
onFocus() { this.isFocused.set(true); }
onBlur() { this.isFocused.set(false); }
}
@Directive({
selector: '[disableable]',
host: {
'[class.disabled]': 'disabled()',
'[attr.aria-disabled]': 'disabled()',
},
})
export class Disableable {
disabled = input(false, { transform: booleanAttribute });
}
// ホストディレクティブを使用するコンポーネント
@Component({
selector: 'app-custom-button',
hostDirectives: [
Focusable,
{
directive: Disableable,
inputs: ['disabled'],
},
],
host: {
'role': 'button',
'(click)': 'onClick($event)',
'(keydown.enter)': 'onClick($event)',
'(keydown.space)': 'onClick($event)',
},
template: `<ng-content />`,
})
export class CustomButton {
private disableable = inject(Disableable);
clicked = output<void>();
onClick(event: Event) {
if (!this.disableable.disabled()) {
this.clicked.emit();
}
}
}
// 使用例: <app-custom-button disabled>Click me</app-custom-button>
ホストディレクティブの出力を公開
@Directive({
selector: '[hoverable]',
host: {
'(mouseenter)': 'onEnter()',
'(mouseleave)': 'onLeave()',
'[class.hovered]': 'isHovered()',
},
})
export class Hoverable {
isHovered = signal(false);
hoverChange = output<boolean>();
onEnter() {
this.isHovered.set(true);
this.hoverChange.emit(true);
}
onLeave() {
this.isHovered.set(false);
this.hoverChange.emit(false);
}
}
@Component({
selector: 'app-card',
hostDirectives: [
{
directive: Hoverable,
outputs: ['hoverChange'],
},
],
template: `<ng-content />`,
})
export class Card {}
// 使用例: <app-card (hoverChange)="onHover($event)">...</app-card>
ディレクティブ構成 API
複数の動作を組み合わせます:
// ベースディレクティブ
@Directive({ selector: '[withRipple]' })
export class Ripple {
// Ripple effect implementation
}
@Directive({ selector: '[withElevation]' })
export class Elevation {
elevation = input(2);
}
// 構成されたコンポーネント
@Component({
selector: 'app-material-button',
hostDirectives: [
Ripple,
{
directive: Elevation,
inputs: ['elevation'],
},
{
directive: Disableable,
inputs: ['disabled'],
},
],
template: `<ng-content />`,
})
export class MaterialButton {}
高度なパターンについては、references/directive-patterns.md を参照してください。
ライセンス: MIT(寛容ライセンスのため全文を引用しています) · 原本リポジトリ
詳細情報
- 作者
- analogjs
- ライセンス
- MIT
- 最終更新
- 不明
Source: https://github.com/analogjs/angular-skills / ライセンス: MIT
関連スキル
newsblur-cli
ターミナルからNewsBlurを管理できます。フィードの閲覧、ストーリーの検索、記事の保存・共有、インテリジェンス分類器の学習、新しいフィードの発見、ワークフローの自動化がNewsBlur CLIで実現します。ユーザーがNewsBlurアカウントを操作したい場合、フィードの確認、購読管理、またはニュース読み込みに関するスクリプト構築時に活用してください。
caveman-compress
自然言語のメモリファイル(CLAUDE.md、todos、preferences)を「原始人形式」に圧縮し、入力トークンを削減します。技術的な内容、コード、URL、構造はすべて保持したまま圧縮します。圧縮版が元のファイルを上書きし、人間が読める形のバックアップはFILE.original.mdとして保存されます。トリガー:/caveman-compress FILEPATH または「compress memory file」
find-skills
日本語の意図から Agent Skills を発見する。「楽天SEOのスキル探して」「PDFを処理したい」「データ分析を自動化したい」などの日本語リクエストに対応。Claude Code (CLI)、Codex、Gemini CLI、claude.ai (Web) いずれでも動作。日本最大の Agent Skills データベース「Agent Skills by ALSEL」(11,000件超、全件日本語化、ダウンロード可能スキル8,600件超) から、ユーザーの意図に合うスキルを推薦・インストール案内する。
planning-and-task-breakdown
仕事を順序立てたタスクに分割します。仕様書や要件が明確にあり、実装可能なタスクに分解する必要がある場合に利用してください。タスクが大きすぎて着手しづらい場合、スコープを見積もる必要がある場合、または並列で作業を進められる場合に活用できます。
docx
このスキルは、ユーザーがWord文書(.docxファイル)を作成、読み込み、編集、操作したいときに使用します。以下の場合に実行してください:「Word文書」「.docx」などの記述、または目次・見出し・ページ番号・レターヘッドなどのフォーマットを含む専門的な文書の作成リクエスト。また、.docxファイルのコンテンツ抽出・再編成、文書への画像挿入・置換、Word形式での検索置換、変更履歴やコメント機能の使用、コンテンツを整形したWord文書への変換の場合も対象です。ユーザーが「レポート」「メモ」「手紙」「テンプレート」などの成果物をWord形式または.docxファイルで求める場合はこのスキルを使用してください。PDF、スプレッドシート、Google Docs、文書作成と無関係なコーディングタスクには使用しないでください。
idea-refine
アイデアを反復的に改善します。構造化された発散的思考と収束的思考を通じて、アイデアを洗練させることができます。「idea-refine」または「ideate」を使用してトリガーします。