Close
Angular React Web Components Blazor Angular
Open Source

Angular Expansion Panel (展開パネル) コンポーネントの概要

Ignite UI for Angular は、最も便利で使いやすいレイアウト コンポーネントの 1 つである Angular Expansion Panel を提供します。この機能豊富なコンポーネントは、展開/縮小可能な詳細な概要ビューを作成するために使用されます。コンテンツには、Angular Expansion Panel のアニメーション、テキスト、アイコン、ヘッダー、操作バー、およびその他の要素を含めることができます。

Ignite UI Expansion Panel igx-expansion-panel は、軽量な Angular アコーディオン コンポーネントで、縮小または展開のどちらかの状態でレンダリングできます。Angular の Expansion Panel は、マウス クリックまたはキーボード操作によって切り替えることができます。複数の Angular Expansion Panel を組み合わせて Angular アコーディオンにすることもできます。

Angular Expansion Panel の例

Ignite UI Angular を使用して、この単純な Angular Expansion Panel の例を作成しました。サンプルの動作をご確認ください。


Ignite UI for Angular Expansion Panel を使用した作業の開始

Ignite UI for Angular Expansion Panel コンポーネントを使用した作業を開始するには、まず Ignite UI for Angular をインストールする必要があります。既存の Angular アプリケーションで、以下のコマンドを入力します。

ng add igniteui-angular

Ignite UI for Angular については、「はじめに」トピックをご覧ください。

次に、app.module.ts ファイルに IgxExpansionPanelModule をインポートします。

// app.module.ts
...
import { IgxExpansionPanelModule } from 'igniteui-angular/expansion-panel';
// import { IgxExpansionPanelModule } from '@infragistics/igniteui-angular'; for licensed package

@NgModule({
    ...
    imports: [..., IgxExpansionPanelModule],
    ...
})
export class AppModule {}

あるいは、16.0.0 以降、IgxExpansionPanelComponent をスタンドアロンの依存関係としてインポートすることも、IGX_EXPANSION_PANEL_DIRECTIVES トークンを使用してコンポーネントとそのすべてのサポート コンポーネントおよびディレクティブをインポートすることもできます。

// home.component.ts

import { IGX_EXPANSION_PANEL_DIRECTIVES } from 'igniteui-angular/expansion-panel';
// import { IGX_EXPANSION_PANEL_DIRECTIVES } from '@infragistics/igniteui-angular'; for licensed package

@Component({
    selector: 'app-home',
    template: `
    <igx-expansion-panel>
        <igx-expansion-panel-header>
            <igx-expansion-panel-title>
            Golden Retriever
            </igx-expansion-panel-title>
            <igx-expansion-panel-description>
            Medium-large gun dog
            </igx-expansion-panel-description>
        </igx-expansion-panel-header>
        <igx-expansion-panel-body>
            The Golden Retriever is a medium-large gun dog that retrieves shot waterfowl, 
            such as ducks and upland game birds, during hunting and shooting parties. 
            The name "retriever" refers to the breed's ability to retrieve shot game undamaged due to their soft mouth. 
            Golden retrievers have an instinctive love of water, and are easy to train to basic or advanced obedience standards.
        </igx-expansion-panel-body>
    </igx-expansion-panel>
    `,
    styleUrls: ['home.component.scss'],
    standalone: true,
    imports: [IGX_EXPANSION_PANEL_DIRECTIVES]
    /* or imports: [IgxExpansionPanelComponent, IgxExpansionPanelHeaderComponent, IgxExpansionPanelTitleDirective, IgxExpansionPanelDescriptionDirective, IgxExpansionPanelBodyComponent] */
})
export class HomeComponent {}

Ignite UI for Angular Expansion Panel モジュールまたはディレクティブをインポートしたので、igx-expansion-panel コンポーネントの使用を開始できます。

Angular Expansion Panel の使用

以下の表は、Angular Expansion Panel で使用可能なすべてのマークアップ要素を示します。

タグ名 説明
igx-expansion-panel コンポーネント ホスト - ヘッダーとボディを格納します。
igx-expansion-panel-header コンポーネント ヘッダーを格納します。常に表示されます。タイトル、説明、およびその他の追加のコンテンツを格納します。
igx-expansion-panel-title Expansion Panel のタイトルを設定します。
igx-expansion-panel-description 短い概要を提供するために使用できます。説明は常にタイトルの後に表示されます (タイトルを設定した場合)。
igx-expansion-panel-icon デフォルトの展開/縮小アイコンを変更します。
igx-expansion-panel-body これは展開可能なコンテナーで、パネルの展開時のみ表示されます。

プロパティ バインディングおよびイベント

コンポーネントにロジックを追加して、パネルの現在の状態に応じて igx-expansion-panel-description の表示/非表示を切り替えることができます。 これには、説明をコントロールの collapsed プロパティへバインドします。

// in expansion-panel.component.ts
import { IgxExpansionPanelComponent } from 'igniteui-angular/expansion-panel';
// import { IgxExpansionPanelComponent } from '@infragistics/igniteui-angular'; for licensed package

@Component({...})
export class ExpansionPanelComponent {
    @ViewChild(IgxExpansionPanelComponent, {read: IgxExpansionPanelComponent})
    public panel: IgxExpansionPanelComponent;
}
{/* in expansion-component.component.html */}
<igx-expansion-panel>
    <igx-expansion-panel-header>
        Golden Retriever
        @if (panel.collapsed) {
            <igx-expansion-panel-description>
                Medium-large gun dog
            </igx-expansion-panel-description>
        }
    </igx-expansion-panel-header>
    ...
</igx-expansion-panel>

以下のサンプル コードは、コンポーネントが縮小状態の場合のみ短い説明文を表示する場合の例です。 コンポーネントの状態に応じてより複雑な機能を追加する場合、イベント エミッターにバインドすることもできます。

// in expansion-panel.component.ts

@Component({...})
export class ExpansionPanelComponent {
    ...
    public handleExpansion(args: {event: Event}): void {
        ... // Some functionality
    }
}
{/* in expansion-component.component.html */}
<igx-expansion-panel (onExpanded)="handleExpansion($event)" (contentCollapsed)="handleCollapse($event)"></igx-expansion-panel>

以下は結果です。

コンポーネントのカスタマイズ

ExpansionPanel によってヘッダーを簡単にカスタマイズできます。 ヘッダー アイコンの位置は、igx-expansion-panel-header の iconPosition 入力で設定します。アイコンの位置に指定できるオプションは、left、right、none です。次のコード サンプルは、コンポーネントのボタンを右側に配置するように構成する方法を示しています。

{/* in expansion-component.component.html */}
<igx-expansion-panel>
    <igx-expansion-panel-header [iconPosition]="'right'"></igx-expansion-panel-header>
    ...
</igx-expansion-panel>

iconPosition プロパティは、RTL でも機能します。たとえば、right に表示するよう設定したアイコンは、RTL 設定時にヘッダーの左端に表示されます。

コントロールのトグル状態のデフォルト アイコンをテンプレート化できます。 igx-expansion-panel-icon タグでコンテンツを渡します。

{/* in expansion-component.component.html */}
<igx-expansion-panel>
    <igx-expansion-panel-header [iconPosition]="'right'">
        ...
        <igx-expansion-panel-icon>
            @if (panel.collapsed) {
                <span class="example-icon">Show More</span>
            } @else {
                <span class="example-icon">Show Less</span>
            }
        </igx-expansion-panel-icon>
    </igx-expansion-panel-header>   
    ...
</igx-expansion-panel>

これで Angular Expansion Panel は、パネルが縮小されているときに「Show More」を、完全に展開されると「Show Less」を表示します。

IgxExpansionPanel コントロールを使用すると、あらゆる種類のコンテンツを igx-expansion-panel-body 内に追加できます。IgxGrid、IgxCombo、チャート、さらには他の Expansion Panel もレンダリングできます。

わかりやすくするために、Expansion Panel の本体に基本的なマークアップを追加します。

{/* in expansion-panel.component.html */}
...
<igx-expansion-panel-body>
    <div class="example-content">

        <img [src]="imgSource" alt="dog-image">
        The Golden Retriever is a medium-large gun dog that retrieves shot waterfowl, such as ducks and upland game birds, during hunting and shooting parties. The name "retriever" refers to the breed's ability to retrieve shot game undamaged due to their soft mouth. Golden retrievers have an instinctive love of water, and are easy to train to basic or advanced obedience standards.
        <a igxButton="outlined" target="_blank" [href]="readMore">Read more</a>
    
</div>
</igx-expansion-panel-body>
...

以下は上記の変更の結果です。

スタイル設定

Expansion Panel テーマのプロパティ マップ

$header-background プロパティと $body-background プロパティを変更すると、次の依存プロパティが自動的に更新されます。

プライマリ プロパティ 依存プロパティ 説明
$header-background $header-title-color パネル ヘッダーのタイトル テキストの色。
$header-icon-color パネル ヘッダーのアイコンの色。
$header-description-color パネル ヘッダーの説明テキストの色。
$header-focus-background フォーカス時のパネル ヘッダーの背景色。
$disabled-text-color 無効なパネルのテキストの色。
$disabled-description-color 無効なパネル ヘッダーの説明テキストの色。
$body-background $body-color パネル本文のテキストの色。

パレットおよび色

はじめに、後でコンポーネントに渡すカスタム パレットを作成します。

// In real life, this should be in our main sass file so we can share the palette between all components. 
// In our case, it's in the component SCSS file "expansion-styling.component.scss".

@use "igniteui-angular/theming" as *;

// IMPORTANT: Prior to Ignite UI for Angular version 13 use:
// @import '~igniteui-angular/lib/core/styles/themes/index';

// Add your brand colors.
$my-primary-color:#353a4b;
$my-secondary-color: #ffd351;
$my-surface-color: #efefef;

// Create custom palette.
$my-color-palette: palette(
  $primary: $my-primary-color,
  $secondary: $my-secondary-color,
  $surface: $my-surface-color
);

コンポーネント テーマの作成

コンポーネント テーマを作成し、上記のスニペットから $my-color-palette パレットを渡します。

// In expansion-styling.component.scss
// Create expansion panel theme.
$custom-panel-theme: expansion-panel-theme(
  // Styling parameters.
  $header-background: color($my-color-palette, "primary", 700),
  $header-focus-background: color($my-color-palette, "primary", 700),
  $header-title-color: color($my-color-palette, "secondary"),
  $header-icon-color: color($my-color-palette, "secondary"),
  $body-background: color($my-color-palette, "primary", 700),
  $body-color: color($my-color-palette, "secondary", 100),
  $border-radius: .5
);

パレットを作成する代わりに、引数としてカラーを直接 expansion-panel-theme 関数に割り当てることもできます。title-color、icon-color、またはその他の前景プロパティを明示的に指定しない場合は、背景とのコントラストが高い方の黒または白が自動的に割り当てられます。

$custom-panel-theme: expansion-panel-theme(
  $header-background: #353a4b,
  $header-focus-background: #353a4b,
  $header-title-color: #ffd351,
  $header-icon-color: #ffd351,
  ...
);

テーマ エンジンを介したスタイル設定に使用可能なすべてのパラメーターを確認するには、API ヘルプを参照してください。

コンポーネント テーマの適用

コンポーネント テーマを適用するには、tokens ミックスインをインクルードし、$custom-panel-theme マップを渡します。

// In expansion-styling.component.scss
:host {
  @include tokens($custom-panel-theme);
}

Ignite UI テーマ エンジンの使用方法の詳細については、こちらをクリックしてください。

デモ

Tailwind によるスタイル設定

カスタム Tailwind ユーティリティ クラスを使用して Expansion Panel をスタイル設定できます。まず Tailwind を設定してください。

グローバル スタイルシートに Tailwind をインポートした上で、以下のように必要なテーマ ユーティリティを適用します:

@import "tailwindcss";
...
@use 'igniteui-theming/tailwind/utilities/material.css';

ユーティリティ ファイルには、light テーマと dark テーマの両方のバリエーションが含まれています。

  • light-* クラスはライト テーマ用です。
  • dark-* クラスはダーク テーマ用です。
  • プレフィックスの後にコンポーネント名を追加します (例: light-expansion-panel、dark-expansion-panel)。

これらのクラスを適用すると、動的なテーマの計算が可能になります。そこから、任意のプロパティを使用して、生成された CSS 変数をオーバーライドできます。コロンの後に、有効な CSS カラー形式 (HEX、CSS 変数、RGB など) を指定します。

プロパティの完全なリストは、expansion-panel-theme で確認できます。構文は次のとおりです:

<igx-expansion-panel
  class="!light-expansion-panel
  ![--header-background:#4F6A5A]
  ![--body-background:#A3C7B2]"
>
  ...
</igx-expansion-panel>

ユーティリティ クラスが優先されるようにするには、感嘆符 (!) が必要です。Tailwind はスタイルをレイヤーに適用しますが、これらのスタイルを重要としてマークしないと、コンポーネントのデフォルトのテーマによってオーバーライドされてしまいます。

最終的に、Expansion Panel は次のようになります:

Angular Expansion Panel のアニメーション

特定のアニメーションの使用

コンポーネントの展開や縮小時にデフォルトのアニメーション以外を使用することも可能です。 前述のとおり igxExpansionPanel がすでに app.module.ts にインポートされている場合、カスタム アニメーション設定オブジェクトを作成し、Ignite UI for Angular Expansion Panel で使用するように設定できます。この方法には useAnimation メソッドと使用する特定のアニメーションが必要なため、まずこれらをインポートし、次のようにアニメーション設定を定義します。

// in expansion-panel.component.ts
import { useAnimation } from '@angular/animations';
import { IgxExpansionPanelComponent } from 'igniteui-angular/expansion-panel';
import { slideInLeft, slideOutRight } from 'igniteui-angular/animations';
// import { IgxExpansionPanelComponent, slideInLeft, slideOutRight } from '@infragistics/igniteui-angular'; for licensed package

@Component({...})
export class ExpansionPanelComponent {
    @ViewChild(IgxExpansionPanelComponent, {read: IgxExpansionPanelComponent})
    public panel: IgxExpansionPanelComponent;

    public animationSettingsCustom = {
        closeAnimation: useAnimation(slideOutRight, {
            params: {
                duration: '100ms',
                toPosition: 'translateX(25px)'
            }
        }),
        openAnimation: useAnimation(slideInLeft, {
            params: {
                duration: '500ms',
                fromPosition: 'translateX(-15px)',
                startOpacity: 0.1
            }
        })
    };

    public collapsed() {
        return this.panel && this.panel.collapsed;
    }
}

このように、**ビルトイン アニメーション スイート** の slideInLeft アニメーションと slideOutRight アニメーションを使用して、コンポーネントのコンテンツが左側からより劇的に表示され、コンテンツの縮小時には右側へ消えるようにします。その際、既存のパラメーターの一部を、使用したい特定のパラメーターでオーバーライドします。

このサンプルではユーザー情報を表示しています。ここで重要なのは、次のようにアニメーション設定をコンポーネントに渡すことです: [animationSettings] = "animationSettingsCustom"

{/* in expansion-panel.component.html */}
...
<igx-expansion-panel [animationSettings] = "animationSettingsCustom" class="my-expansion-panel">
    <igx-expansion-panel-header>
        <igx-expansion-panel-title class="sample-title">Angular</igx-expansion-panel-title>
    </igx-expansion-panel-header>
    <igx-expansion-panel-body>
        Angular (commonly referred to as "Angular 2+" or "Angular v2 and above") is a TypeScript-based open-source web application framework led by the Angular Team at Google and by a community of individuals and corporations.
    </igx-expansion-panel-body>
</igx-expansion-panel>
...

以下は結果です。

複数パネルの場合

igxAccordion トピックを参照してください。

API リファレンス

テーマの依存関係