Migrating from ion-slides to Swiper.js
ion-slidesを探していますか?ion-slidesは v6.0.0 で非推奨となり、v7.0.0 で削除されました。Swiper.js ライブラリを直接使用することをお勧めします。移行プロセスは以下に詳しく説明されています。
モダンなタッチスライダーコンポーネントが必要な場合は、Swiper.jsをおすすめします。Swiper 9 では、Angular コンポーネントの代わりとしてSwiper Elementが導入されました。このガイドでは、Ionic Framework アプリケーションに Swiper Element を設定する方法について説明します。また、ion-slidesから Swiper Element へ移行する際に必要な情報についても説明します。
Getting Started
まず、Ionic の最新バージョンにアップデートしてください。
npm install @ionic/angular@latest
それが完了したら、プロジェクト内で Swiper の依存関係をインストールしてください。
npm install swiper@latest
次に、CUSTOM_ELEMENTS_SCHEMAを追加する必要があります。これにより、Angular に、Custom Element を使用することを伝えます。この処理は、app.module.tsファイルで行うことも、Swiper を使用するコンポーネントのモジュールファイルで行うこともできます。
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
@NgModule({
schemas: [..., CUSTOM_ELEMENTS_SCHEMA]
});
...
最後に、Swiper の Custom Element をグローバルに登録するために、register関数を呼び出す必要があります。この処理は一度だけ行うべきですので、app.component.tsファイルにそのコードを記述してください。
import { register } from 'swiper/element/bundle';
register();
@Component({
...
})
...
その後は、ion-slides要素をswiper-containerに、ion-slide要素をswiper-slideに置き換えるだけです。ちなみに、これらの Custom Element をインポートする必要はありません。registerを呼び出すことで、Angular が自動的にこれらの要素を認識します。
<swiper-container>
<swiper-slide>Slide 1</swiper-slide>
<swiper-slide>Slide 2</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
</swiper-container>
Bundled vs. Core Versions
通常は swiper/element/bundle から register 関数をインポートします。これにより Swiper のバンドル版が使われ、各機能に必要なモジュールとスタイルシートがすべて自動的に含まれます。
追加モジュールを自動的に含まないコア版を使いたい場合は、Swiper のコアバージョンおよびモジュールのドキュメントを参照してください。この移行ガイドの残りは、あなたがバンドル版を使用していることを前提としています。
Swiping with Style
CSS を移行するには、まず selector を更新して、新しい Custom Element をターゲットにするようにしてください。
| ion-slides Selector | Swiper Selector |
|---|---|
ion-slides | swiper-container |
ion-slide | swiper-slide |
もし、ion-slidesに含まれる CSS custom properties を使用している場合、以下は Swiper で使用される対応するプロパティのリストです。
ion-slides CSS property | swiper-container CSS property |
|---|---|
--bullet-background | --swiper-pagination-bullet-inactive-color |
--bullet-background-active | --swiper-pagination-color |
--progress-bar-background | --swiper-pagination-progressbar-bg-color |
--progress-bar-background-active | --swiper-pagination-color |
--scroll-bar-background | --swiper-scrollbar-bg-color |
--scroll-bar-background-active | --swiper-scrollbar-drag-bg-color |
追加のカスタム CSS では、Swiper Element が Shadow DOM カプセル化を使用しているため、スタイルを Shadow DOM スコープに注入する必要があります。手順はSwiper のスタイル注入ガイドを参照してください。
Additional ion-slides Styles
ion-slides コンポーネントには、ネイティブに近い外観と操作感を実現するスタイルが追加されていました。Ionic で Swiper.js を使うためにこのスタイルは必須ではありませんが、ion-slides に近い外観を維持するには次の CSS を global.scss に追加してください。
swiper-container {
--swiper-pagination-bullet-inactive-color: var(--ion-text-color-step-800, #cccccc);
--swiper-pagination-color: var(--ion-color-primary, #0054e9);
--swiper-pagination-progressbar-bg-color: rgba(var(--ion-text-color-rgb, 0, 0, 0), 0.25);
--swiper-scrollbar-bg-color: rgba(var(--ion-text-color-rgb, 0, 0, 0), 0.1);
--swiper-scrollbar-drag-bg-color: rgba(var(--ion-text-color-rgb, 0, 0, 0), 0.5);
}
swiper-slide {
display: flex;
position: relative;
flex-direction: column;
flex-shrink: 0;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
font-size: 18px;
text-align: center;
box-sizing: border-box;
}
swiper-slide img {
width: auto;
max-width: 100%;
height: auto;
max-height: 100%;
}
The IonicSlides Module
ion-slides では、Ionic が Swiper の多数のプロパティを自動的に調整し、モバイルデバイスで滑らかにスワイプできるようにしていました。Swiper を直接使用する場合もこれらのプロパティを設定するには、IonicSlides モジュールを推奨します。ただし、Ionic で Swiper.js を使うためにこのモジュールは必須ではありません。
IonicSlidesによって設定されるプロパティを確認し、どのプロパティをカスタマイズしたいかを決めておくことが推奨されます。
IonicSlidesモジュールをインストールするには、そのモジュールをswiper-containerのmodulesプロパティに配列として渡す必要があります。
- Angular
- Angular (Standalone)
// home.page.ts
import { IonicSlides } from '@ionic/angular/lazy';
@Component({
...
})
export class HomePage {
swiperModules = [IonicSlides];
}
// home.page.ts
import { IonicSlides } from '@ionic/angular';
@Component({
...
})
export class HomePage {
swiperModules = [IonicSlides];
}
<!-- home.page.html -->
<swiper-container [modules]="swiperModules"> ... </swiper-container>
Swiper の Core 版で追加のモジュールをインストールしている場合は、IonicSlides を配列の最後に配置してください。これにより Pagination、Scrollbar、Zoom などのモジュール設定が自動で調整されます。
Properties
Swiper のオプションは、<swiper-container>コンポーネント上に個別のプロパティとして設定する必要があります。
例えば、ion-slidesを使用しているアプリにおいて、slidesPerViewやloopのオプションが設定されているとします。
<ion-slides [options]="{ slidesPerView: 3, loop: true }">
<ion-slide>Slide 1</ion-slide>
<ion-slide>Slide 3</ion-slide>
<ion-slide>Slide 3</ion-slide>
</ion-slides>
これらのオプションを<swiper-container>上のプロパティとして直接設定するには、以下のようにします:
<swiper-container [slidesPerView]="3" [loop]="true">
<swiper-slide>Slide 1</swiper-slide>
<swiper-slide>Slide 2</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
</swiper-container>
ion-slidesから Swiper Element へ変更する際の、プロパティの変更内容の完全なリストです。
| Name | Notes |
|---|---|
| options | 各オプションを、<swiper-container>コンポーネント上のプロパティとして直接設定してください。 |
| mode | モードに応じて、異なるスタイルを適用するために、CSS で.ios swiper-containerや.md swiper-containerを使ってスライドを指定することができます。 |
| pager | 代わりに、paginationプロパティを使用してください。 |
Swiper Element で利用可能なすべてのプロパティは、Swiper API パラメータドキュメントに記載されています。
Events
swiper-containerコンポーネントは Ionic Framework によって提供されていないため、イベント名にはionSlideというプレフィックスは付きません。また、すべてのイベント名は camelCase ではなく、小文字で表記されるべきです。
例えば、ion-slidesを使っているアプリで、ionSlideDidChangeイベントを使用したとします。
<ion-slides (ionSlideDidChange)="onSlideChange()">
<ion-slide>Slide 1</ion-slide>
<ion-slide>Slide 3</ion-slide>
<ion-slide>Slide 3</ion-slide>
</ion-slides>
移行するためには、そのイベントの名前をswiperslidechangeに変更する必要があります。
<swiper-container (swiperslidechange)="onSlideChange()">
<swiper-slide>Slide 1</swiper-slide>
<swiper-slide>Slide 2</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
</swiper-container>
以下は、ion-slidesから Swiper Angular へ変更する際に発生するイベント名の変更点の一覧です。
| ion-slides Event | Swiper Event |
|---|---|
ionSlideWillChange | swiperslidechangetransitionstart |
ionSlideDidChange | swiperslidechange |
ionSlideDoubleTap | swiperdoubletap |
ionSlideDrag | swiperslidermove |
ionSlideNextStart | swiperslidenexttransitionstart |
ionSlideNextEnd | swiperslidenexttransitionend |
ionSlidePrevStart | swiperslideprevtransitionstart |
ionSlidePrevEnd | swiperslideprevtransitionend |
ionSlideReachStart | swiperreachbeginning |
ionSlideReachEnd | swiperreachend |
ionSlideTap | swipertap |
ionSlideTouchStart | swipertouchstart |
ionSlideTouchEnd | swipertouchend |
ionSlideTransitionStart | swipertransitionstart |
ionSlideTransitionEnd | swipertransitionend |
ionSlidesDidLoad | swiperinit |
Swiper Element で利用可能なすべてのイベントはSwiper API イベントドキュメントで確認でき、イベント名をすべて小文字にして、先頭に swiper を付けます。
Methods
ほとんどのメソッドは削除され、Swiper インスタンスのプロパティに直接アクセスするようになりました。インスタンスを取得するには、まず ViewChild などで <swiper-container> 要素への参照を取得し、その swiper プロパティにアクセスします。
<!-- slides.component.html -->
<swiper-container #swiper>
<swiper-slide>Slide 1</swiper-slide>
<swiper-slide>Slide 2</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
</swiper-container>
// slides.component.ts
import { ..., ElementRef, ViewChild } from '@angular/core';
@Component({
...
})
export class SlidesExample {
@ViewChild('swiper')
swiperRef: ElementRef | undefined;
logActiveIndex() {
console.log(this.swiperRef?.nativeElement.swiper.activeIndex);
}
}
以下は、ion-slidesから Swiper Element へ変更する際に行われるメソッドの変更点の全リストです。
| ion-slides Method | Notes |
|---|---|
getActiveIndex() | 代わりに、activeIndexプロパティを使用してください。 |
getPreviousIndex() | 代わりに、previousIndexプロパティを使用してください。 |
getSwiper() | swiper prop から Swiper インスタンスへの参照を取得します。上の例を参照してください。 |
isBeginning() | 代わりに、isBeginningプロパティを使用してください。 |
isEnd() | 代わりに、isEndプロパティを使用してください。 |
length() | 代わりに、slidesプロパティを使用してください。(つまり、swiper.slides.length) |
lockSwipeToNext() | 代わりに、allowSlidesNextプロパティを使用してください。 |
lockSwipeToPrev() | 代わりに、allowSlidePrevプロパティを使用してください。 |
lockSwipes() | 代わりに、allowSlideNext、allowSlidePrev、allowTouchMoveというプロパティを使用してください。 |
startAutoplay() | 代わりに、autoplayプロパティを使用してください。 |
stopAutoplay() | 代わりに、autoplayプロパティを使用してください。 |
Swiper インスタンスで利用可能なすべてのメソッドとプロパティは、Swiper API のメソッドおよびプロパティドキュメントで確認できます。
Effects
Swiper のバンドル版を使用している場合、Swiper Element で Cube や Fade といった効果を使うために追加のインポートは不要です。たとえば、次のコードではスライドの切り替えに flip エフェクトが適用されます。
<swiper-container effect="flip"> ... </swiper-container>
Swiper のエフェクトについての詳細は、Swiper API フェードエフェクトドキュメントをご参照ください。
Wrap Up
Swiper がインストールされたら、新しい機能がたくさん利用できます。まずはSwiper Element のドキュメントを参照してみてください。その後、Swiper API のドキュメントも確認してみてください。
FAQ
この移行の実装例はどこで確認できますか?
ion-slides を使ったサンプルアプリと、同じ動作を Swiper で実装した例はこちらで確認できます: https://github.com/ionic-team/slides-migration-samples.
この移行に関して、どこで助けを得ることができますか?
もし、移行に何か問題が発生している場合は、Ionic Forumに投稿してください。
バグ報告はどこに提出すればよいですか?
Issue を作成する前に、Swiper ディスカッションボードまたはIonic フォーラムに投稿して、コミュニティによって問題が解決できるか確認することを検討してください。
Swiper ライブラリで問題が発生している場合、新しいバグはSwiper の Issue trackerに報告してください。
IonicSlidesモジュールで問題が発生している場合、新しいバグはIonic Framework の Issue trackerに報告してください。