Close
Angular React Web Components Blazor Angular
Open Source

Angular Tabs (タブ) コンポーネントの概要

Ignite UI for Angular Tabs は、関連するコンテンツを 1 つのタブ付きビューで整理およびグループ化することを主な目的とするフル機能のユーザー インターフェース コンポーネントです。これにより、スペースを節約し、コンテンツをよりわかりやすくします。アニメーション、テンプレート化、カスタマイズ オプションなどのさまざまな機能がパックされています。

Angular のタブは、多くのコンテンツを含む Web ページをカテゴリに分類し、簡潔かつスペース効率の良い方法で表示する必要がある場合に非常に便利です。

Ignite UI for Angular igx-tabs コンポーネントは、同様なデータ セットの体系化や切り替えに使用します。igx-tab-item のラッパーとして機能し、それぞれデータのコンテナーおよびタブ ヘッダーを表します。Angular Tabs コンポーネントは、タブを上側に配置して複数のタブ項目がある場合にスクロールを許可します。

Angular Tabs の例

これは、一度に 1 つのビューしか表示できない別のタブ内に 1 つのタブがある Angular のネストされたタブの基本的な例です。Angular でネストされたタブを使用して、より適切で構造化された方法で情報を表示できます。


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

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

ng add igniteui-angular

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

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

// app.module.ts

...
import { IgxTabsModule } from 'igniteui-angular/tabs';
// import { IgxTabsModule } from '@infragistics/igniteui-angular'; for licensed package

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

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

// home.component.ts

import { IGX_TABS_DIRECTIVES } from 'igniteui-angular/tabs';
// import { IGX_TABS_DIRECTIVES } from '@infragistics/igniteui-angular'; for licensed package

@Component({
    selector: 'app-home',
    template: `
    <igx-tabs>
      <igx-tab-item>
        <igx-tab-header>
          <span igxTabHeaderLabel>Tab 1</span>
        </igx-tab-header>
        <igx-tab-content>
          This is Tab 1 content.
        </igx-tab-content>
      </igx-tab-item>
      <igx-tab-item>
        <igx-tab-header>
          <span igxTabHeaderLabel>Tab 2</span>
        </igx-tab-header>
        <igx-tab-content>
          This is Tab 2 content.
        </igx-tab-content>
      </igx-tab-item>
    </igx-tabs>
    `,
    styleUrls: ['home.component.scss'],
    standalone: true,
    imports: [IGX_TABS_DIRECTIVES]
    /* or imports: [IgxTabsComponent, IgxTabItemComponent, IgxTabHeaderComponent, IgxTabContentComponent, IgxTabHeaderLabelDirective] */
})
export class HomeComponent {}

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

Angular Tabs の使用

igx-tab-header にコンテンツを提供することにより、Angular Tabs ヘッダーを設定します。タブの名前を設定するには、igxTabHeaderLabel ディレクティブを使用してスパンを追加するだけです。タブ項目のコンテンツとして表示されるコンテンツは、igx-tab-content タグの間に追加する必要があります。

<igx-tabs>
  <igx-tab-item>
    <igx-tab-header>
      <span igxTabHeaderLabel>Tab 1</span>
    </igx-tab-header>
    <igx-tab-content>
      This is Tab 1 content.
    </igx-tab-content>
  </igx-tab-item>
  <igx-tab-item>
    <igx-tab-header>
      <span igxTabHeaderLabel>Tab 2</span>
    </igx-tab-header>
    <igx-tab-content>
      This is Tab 2 content.
    </igx-tab-content>
  </igx-tab-item>
  <igx-tab-item>
    <igx-tab-header>
      <span igxTabHeaderLabel>Tab 3</span>
    </igx-tab-header>
    <igx-tab-content>
      This is Tab 3 content.
    </igx-tab-content>
  </igx-tab-item>
  <igx-tab-item>
    <igx-tab-header>
      <span igxTabHeaderLabel>Tab 4</span>
    </igx-tab-header>
    <igx-tab-content>
      This is Tab 4 content.
    </igx-tab-content>
  </igx-tab-item>
  <igx-tab-item>
    <igx-tab-header>
      <span igxTabHeaderLabel>Tab 5</span>
    </igx-tab-header>
    <igx-tab-content>
      This is Tab 5 content.
    </igx-tab-content>
  </igx-tab-item>
</igx-tabs>

サンプルを構成した後、結果は以下のようになります。

Angular Tabs の配置

IgxTabs tabAlignment 入力プロパティは、タブの配置と配置の方法を制御します。start、center、end、justify の 4 つの異なる値を受け入れます。

  • Start (デフォルト): タブ ヘッダーの幅はコンテンツ (ラベル、アイコン、両方) によって異なり、すべてのタブに同じパディングがあります。最初のタブは、タブ コンテナーの左側に配置されます。
  • Center: タブ ヘッダーの幅はコンテンツによって異なり、タブ コンテナーの中央を占めます。スペースが足りない場合、スクロール ボタンが表示されます。
  • End (デフォルト): タブ ヘッダーの幅はコンテンツによって異なり、すべてのタブに同じパディングがあります。最後のタブは、タブ コンテナーの右側に配置されます。
  • Justify: すべてのタブ ヘッダーは幅が等しく、タブ コンテナーに完全に適合します。スペースが足りない場合、スクロール ボタンが表示されます。

以下のサンプルは、tabAlignment プロパティ値を切り替えるときにタブがどのように整列されるかを示しています。

Sample below demonstrates how tabs get aligned when switching between tabAlignment property values.

Angular Tabs のカスタマイズ

タブに igxTabHeaderIcon ディレクティブを追加します。igx-tabs コントロールはマテリアル デザイン アイコンと互換性があるため、アプリケーションにアイコンを簡単に追加できます。

これまでアプリケーションで igx-icon を使用したことがない場合は、続行する前に必ず app.module.tsIgxIconModule をインポートしてください。

はじめに Material Icons をメイン アプリケーション フォルダーの ‘styles.scss’ ファイルにインポートします。次に、igx-tab-header の子として、igxTabHeaderIcon ディレクティブが設定された igx-icon を追加します。

// styles.scss

...
@import url('https://fonts.googleapis.com/icon?family=Material+Icons');
...
<igx-tabs>
  <igx-tab-item>
    <igx-tab-header>
        <igx-icon igxTabHeaderIcon>library_music</igx-icon>
        <span igxTabHeaderLabel>Albums</span>
    </igx-tab-header>
    <igx-tab-content>
        Albums
    </igx-tab-content>
  </igx-tab-item>
  <igx-tab-item>
    <igx-tab-header>
        <igx-icon igxTabHeaderIcon>favorite</igx-icon>
        <span igxTabHeaderLabel>Favorite</span>
    </igx-tab-header>
    <igx-tab-content>
        Favorite
    </igx-tab-content>
  </igx-tab-item>
  <igx-tab-item>
    <igx-tab-header>
        <igx-icon igxTabHeaderIcon>info</igx-icon>
        <span igxTabHeaderLabel>Details</span>
    </igx-tab-header>
    <igx-tab-content>
        Details
    </igx-tab-content>
  </igx-tab-item>
</igx-tabs>

サンプルを正しく構成した場合、タブは以下の例のようになります。


ラベルとアイコンを設定するだけでは不十分な場合は、igx-tab-header タグの間に独自のカスタム コンテンツを直接提供することもできます。

<igx-tabs>
  <igx-tab-item>
    <igx-tab-header>
      <!-- your custom tab content goes here -->
      <div>
        <img src="https://static.infragistics.com/marketing/Website/products/ignite-ui-landing/ignite-ui-logo.svg"
             width="80px" height="40px">
      </div>
    </igx-tab-header>
    <igx-tab-content>
      <h1>IgniteUI Rocks!</h1>
    </igx-tab-content>
  </igx-tab-item>
</igx-tabs>

igxPrefix および igxSuffix ディレクティブを使用するだけで、独自のカスタム タブ ヘッダーのプレフィックスとサフィックスを追加することもできます。以下のサンプルは、カスタム ヘッダー コンテンツとプレフィックス/サフィックスを含むタブを追加する方法を示しています。


ルーター アウトレット コンテナーとの統合

igx-tabs コンポーネントは、タブ 項目ごとにコンテンツが指定されたタブのリストとして使用することを目的としています。しかし、コンテンツがタブ コンテンツとは別のタブ項目を定義する場合があります。

タブ項目を定義する際にディレクティブを適用することができます。たとえば、この機能を使用して、Angular Router を使用してビュー間のナビゲーションを実現できます。次の例は、igx-tabs コンポーネントを構成して、単一の router-outlet で 3 つのコンポーネントを切り替える方法を示しています。

まず、igx-tabs コンポーネントを保持するメイン コンポーネントと、デモ用のコンテンツを含む 3 つのビュー コンポーネントが必要です。簡単にするために、ビュー コンポーネントには短いテンプレートがあります

import { Component } from '@angular/core';

@Component({
    selector: 'app-tabs-routing',
    styleUrls: ['tabs-routing.component.scss'],
    templateUrl: 'tabs-routing.component.html'
})
export class TabsRoutingComponent { }

@Component({
    template: '<p>Tab 1 Content</p>'
})
export class TabsRoutingView1Component { }

@Component({
    template: '<p>Tab 2 Content</p>'
})
export class TabsRoutingView2Component { }

@Component({
    template: '<p>Tab 3 Content</p>'
})
export class TabsRoutingView3Component { }

次のステップでは、app-routing.module.ts ファイルに適切なナビゲーション マッピングを作成します。

import { RouterModule, Routes } from '@angular/router';

import {
    TabsRoutingComponent,
    TabsRoutingView1Component,
    TabsRoutingView2Component,
    TabsRoutingView3Component
} from './tabs-routing.component';

...

const routes: Routes = [
  {
    path: '',
    pathMatch: 'full',
    redirectTo: '/tabs-routing'
  },
  {
    path: 'tabs-routing',
    component: TabsRoutingComponent,
    children: [
        { path: 'view1', component: TabsRoutingView1Component },
        { path: 'view2', component: TabsRoutingView2Component },
        { path: 'view3', component: TabsRoutingView3Component },
      ]
  }
];

@NgModule({
  exports: [RouterModule],
  imports: [RouterModule.forRoot(appRoutes)]
})
export class AppRoutingModule { }

すべてのナビゲーション ルートがセットアップされたので、igx-tabs コンポーネントを宣言し、ルーティング用に構成する必要があります。ビュー コンポーネントをレンダリングするための router-outlet を必ず追加してください。

<!-- tabs-routing.component.html -->

<igx-tabs #tabs1>
  <igx-tab-item
      routerLinkActive
      #rla1="routerLinkActive"
      [selected]="rla1.isActive"
  >
      <igx-tab-header routerLink="view1">
          <span igxTabHeaderLabel>Tab 1</span>
      </igx-tab-header>
  </igx-tab-item>
  <igx-tab-item
      routerLinkActive
      #rla2="routerLinkActive"
      [selected]="rla2.isActive"
  >
      <igx-tab-header routerLink="view2">
          <span igxTabHeaderLabel>Tab 2</span>
      </igx-tab-header>
  </igx-tab-item>
  <igx-tab-item
      routerLinkActive
      #rla3="routerLinkActive"
      [selected]="rla3.isActive"
  >
      <igx-tab-header routerLink="view3">
          <span igxTabHeaderLabel>Tab 3</span>
      </igx-tab-header>
  </igx-tab-item>
</igx-tabs>

<router-outlet></router-outlet>

上記のコードは、3 つのタブ項目を持つ igx-tabs コンポーネントを作成します。各タブ硬毛区のヘッダーには、ナビゲーションに使用されるルーティング リンクを指定するために使用される RouterLink ディレクティブが適用されています。リンクのいずれかがアクティブになると、RouterLinkActive ディレクティブの isActive プロパティにバインドされるため、対応するタブ項目の selected プロパティが設定されます。このようにして、選択したタブ項目は常に現在の URL パスと同期します。

routerLink ディレクティブは、直接 igx-tab-item ではなく、igx-tab-header に設定されていることに注意してください。

スタイル設定

Tabs テーマのプロパティ マップ

プライマリ プロパティを変更すると、関連するすべての依存プロパティが自動的に更新され、変更が反映されます。

Primary PropertyDependent PropertyDescription
$item-background$item-active-backgroundThe color used for the active/focused tab background.
$item-text-colorThe color used for the tab text color.
$item-icon-colorThe color used for the tab icon.
$item-hover-backgroundThe background used for the tabs on hover.
$indicator-colorThe color used for the active tab indicator.
$button-backgroundThe color used for the button background.
$button-hover-backgroundThe color used for the button background on hover.
$item-active-background$item-active-icon-colorThe color used for the active tab icon.
$item-active-colorThe color used for the active tabs text.
$tab-ripple-colorThe color used for the button background.
$item-text-color$item-hover-colorThe text color used for the tabs on hover if no $item-hover-background is provided
$item-icon-colorThe color used for the tab icon if no $item-background is provided
$item-active-colorThe color used for the active tabs text if no $item-active-background is provided
$indicator-colorThe color used for the active tab indicator if no $item-background is provided
$item-icon-color$item-hover-icon-colorThe color used for the tab icon on hover.
$item-active-icon-colorThe color used for the active tab icon.
$indicator-colorThe color used for the active tab indicator.
$button-background$button-hover-backgroundThe color used for the button background on hover.
$button-colorThe color used for the button icon/text color.
$button-color$button-disabled-colorThe color used for the disabled button icon/text.
$button-ripple-colorThe color used for the button background on hover.
$button-hover-background$button-hover-colorThe color used for the button icon/text color on hover.
Primary PropertyDependent PropertyDescription
$item-background$item-active-backgroundThe color used for the active/focused tab background.
$item-text-colorThe color used for the tab text color.
$item-icon-colorThe color used for the tab icon.
$item-hover-backgroundThe background used for the tabs on hover.
$indicator-colorThe color used for the active tab indicator.
$button-backgroundThe color used for the button background.
$button-hover-backgroundThe color used for the button background on hover.
$item-active-background$item-active-icon-colorThe color used for the active tab icon.
$item-active-colorThe color used for the active tabs text.
$tab-ripple-colorThe ripple color for the tab interaction.
$item-text-color$item-hover-colorThe text color used for the tabs on hover if no $item-hover-background is provided
$item-icon-colorThe color used for the tab icon if no $item-background is provided
$item-active-colorThe color used for the active tabs text if no $item-active-background is provided
$indicator-colorThe color used for the active tab indicator if no $item-background is provided
$item-icon-color$item-hover-icon-colorThe color used for the tab icon on hover.
$item-active-icon-colorThe color used for the active tab icon.
$indicator-colorThe color used for the active tab indicator.
$button-background$button-hover-backgroundThe color used for the button background on hover.
$button-colorThe color used for the button icon/text color.
$button-color$button-disabled-colorThe color used for the disabled button icon/text.
$button-ripple-colorThe color used for the button background on hover.
$button-hover-background$button-hover-colorThe color used for the button icon/text color on hover.
Primary PropertyDependent PropertyDescription
$item-background$item-active-backgroundThe color used for the active/focused tab background.
$item-text-colorThe color used for the tab text color.
$item-icon-colorThe color used for the tab icon.
$item-hover-backgroundThe background used for the tabs on hover.
$indicator-colorThe color used for the active tab indicator.
$button-backgroundThe color used for the button background.
$button-hover-backgroundThe color used for the button background on hover.
$border-colorThe border color of the tabs.
$item-active-background$item-active-icon-colorThe color used for the active tab icon.
$item-active-colorThe color used for the active tabs text.
$tab-ripple-colorThe color used for the button background.
$item-text-color$item-hover-colorThe text color used for the tabs on hover if no $item-hover-background is provided
$item-icon-colorThe color used for the tab icon if no $item-background is provided
$button-colorThe color used for the button icon/text color if no $button-background is provided (non-material)
$item-icon-color$item-hover-icon-colorThe color used for the tab icon on hover if no $item-hover-background is provided
$item-text-colorThe color used for the tab text color if no $item-background is provided
$button-background$button-hover-backgroundThe color used for the button background on hover.
$button-colorThe color used for the button icon/text color.
$button-color$button-hover-colorThe color used for the button icon/text color on hover if no $button-background is provided
$button-disabled-colorThe color used for the disabled button icon/text.
$button-ripple-colorThe color used for the button background on hover.
$button-hover-background$button-hover-colorThe color used for the button icon/text color on hover.
Primary PropertyDependent PropertyDescription
$item-background$item-active-backgroundThe color used for the active/focused tab background.
$item-text-colorThe color used for the tab text color.
$item-icon-colorThe color used for the tab icon.
$item-hover-backgroundThe background used for the tabs on hover.
$indicator-colorThe color used for the active tab indicator.
$button-backgroundThe color used for the button background.
$button-hover-backgroundThe color used for the button background on hover.
$item-active-background$item-active-icon-colorThe color used for the active tab icon.
$item-active-colorThe color used for the active tabs text.
$tab-ripple-colorThe color used for the button background.
$item-text-color$item-hover-colorThe text color used for the tabs on hover if no $item-hover-background is provided
$item-icon-colorThe color used for the tab icon if no $item-background is provided
$item-active-colorThe color used for the active tabs text if no $item-active-background is provided
$indicator-colorThe color used for the active tab indicator if no $item-background is provided
$item-icon-color$item-hover-icon-colorThe color used for the tab icon on hover.
$item-active-icon-colorThe color used for the active tab icon.
$indicator-colorThe color used for the active tab indicator.
$button-background$button-hover-backgroundThe color used for the button background on hover.
$button-colorThe color used for the button icon/text color.
$button-color$button-disabled-colorThe color used for the disabled button icon/text.
$button-ripple-colorThe color used for the button background on hover.
$button-hover-background$button-hover-colorThe color used for the button icon/text color on hover.

タブのスタイル設定は、すべてのテーマ関数とコンポーネント ミックスインが存在するテーマ モジュールをインポートする必要があります。

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

// 重要: Ignite UI for Angular 13 より前のバージョンは、次を使用してください。
// @import '~igniteui-angular/lib/core/styles/themes/index';

最もシンプルな方法として、tabs-theme を拡張する新しいテーマを作成します。$item-background$item-active-color などの少数のベース パラメーターを指定することで、最小限の労力でタブのスタイルを設定できます。テーマは、さまざまなインタラクション状態に必要なすべての背景の色と前景の色を自動的に生成します。

追加のパラメーターをオーバーライドして、外観をさらに微調整することもできます。

$dark-tabs: tabs-theme(
  $item-background: #292826,
  $item-active-color: #F4D45C,
);

上記のようにカラーの値をハードコーディングする代わりに、palette および color 関数を使用してカラーに関してより高い柔軟性を実現することができます。使い方の詳細についてはパレットのトピックをご覧ください。

次に、tabs-theme を拡張する新しいテーマを作成し、タブグループのスタイルを設定できるさまざまなプロパティを受け取ります。

タブのコンテンツの一部として使用されるコンポーネントをスタイルするには、それぞれのコンポーネントに固有の追加テーマを作成する必要があります。

最後にコンポーネントのテーマを含めます

:host {
  @include tokens($dark-tabs);
}

デモ

Tailwind によるスタイル設定

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

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

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

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

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

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

プロパティの完全なリストは IgxTabs テーマ で確認できます。構文は次のとおりです:

<igx-tabs
  class="!light-tabs
  ![--item-background:#011627]
  ![--item-active-icon-color:#FF8040]
  ![--item-active-color:#FF8040]">
  ...
</igx-tabs>

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

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


API リファレンス

テーマの依存関係

その他のリソース


コミュニティに参加して新しいアイデアをご提案ください。