로딩 (ion-loading)
로딩(Loading)은 사용자 상호작용을 막으면서 활동 중임을 나타낼 때 사용하는 오버레이예요. 앱 콘텐츠 위에 표시되며, 앱이 다시 사용자 상호작용을 재개할 수 있도록 닫을 수 있습니다.
출처: 문서
본문
사용자 상호작용을 막으면서 활동 중임을 나타낼 때 사용할 수 있는 오버레이입니다. 로딩 표시기는 앱 콘텐츠 위에 나타나며, 앱이 다시 사용자 상호작용을 재개할 수 있도록 닫을 수 있어요. 선택적으로 배경(backdrop)을 포함하며, 생성 시 showBackdrop: false로 설정하면 배경을 비활성화할 수 있습니다.
기본 사용법 (Basic Usage)
표시된 후 로딩 표시기는 기본적으로 무기한 표시됩니다. 개발자는 컴포넌트의 dismiss() 메서드를 호출해 생성 후 로딩 표시기를 수동으로 닫을 수 있어요. onDidDismiss 함수를 호출하면 로딩 표시기가 닫힌 후 어떤 동작을 수행할 수 있습니다.
또는 로딩 옵션의 duration에 표시할 밀리초 수를 전달해 일정 시간이 지나면 자동으로 닫히도록 설정할 수도 있습니다.
인라인 (Inline, 권장)
JavaScript
<ion-button id="open-loading">Show Loading</ion-button>
<ion-loading trigger="open-loading" message="Dismissing after 3 seconds..." duration="3000"></ion-loading>
Angular
src/app/example.component.html
<ion-button id="open-loading">Show Loading</ion-button>
<ion-loading trigger="open-loading" message="Dismissing after 3 seconds..." duration="3000"></ion-loading>
src/app/example.component.ts
import { Component } from '@angular/core';
import { IonButton, IonLoading } from '@ionic/angular';
@Component({
selector: 'app-example',
templateUrl: 'example.component.html',
styleUrls: ['example.component.css'],
imports: [IonButton, IonLoading],
})
export class ExampleComponent {}
React
import React from 'react';
import { IonButton, IonLoading } from '@ionic/react';
function Example() {
return (
<>
<IonButton id="open-loading">Show Loading</IonButton>
<IonLoading trigger="open-loading" message="Dismissing after 3 seconds..." duration={3000} />
</>
);
}
export default Example;
Vue
<template>
<ion-button id="open-loading">Show Loading</ion-button>
<ion-loading trigger="open-loading" :duration="3000" message="Dismissing after 3 seconds..."> </ion-loading>
</template>
<script setup lang="ts">
import { IonButton, IonLoading } from '@ionic/vue';
</script>
컨트롤러 (Controller)
JavaScript
<ion-button onclick="showLoading()">Show Loading</ion-button>
<script>
var showLoading = async () => {
const loading = await loadingController.create({
message: 'Dismissing after 3 seconds...',
duration: 3000,
});
loading.present();
};
</script>
Angular
src/app/example.component.html
<ion-button (click)="showLoading()">Show Loading</ion-button>
src/app/example.component.ts
import { Component } from '@angular/core';
import { IonButton, LoadingController } from '@ionic/angular';
@Component({
selector: 'app-example',
templateUrl: 'example.component.html',
styleUrls: ['example.component.css'],
imports: [IonButton],
})
export class ExampleComponent {
constructor(private loadingCtrl: LoadingController) {}
async showLoading() {
const loading = await this.loadingCtrl.create({
message: 'Dismissing after 3 seconds...',
duration: 3000,
});
loading.present();
}
}
React
import React from 'react';
import { IonButton, useIonLoading } from '@ionic/react';
function Example() {
/**
* This example does not make use of the dismiss
* method returned from `useIonLoading`, but it can
* be used for more complex scenarios.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [present, dismiss] = useIonLoading();
return (
<IonButton
onClick={() => {
present({
message: 'Dismissing after 3 seconds...',
duration: 3000,
});
}}
>
Show Loading
</IonButton>
);
}
export default Example;
Vue
<template>
<ion-button @click="showLoading">Show Loading</ion-button>
</template>
<script setup lang="ts">
import { IonButton, loadingController } from '@ionic/vue';
const showLoading = async () => {
const loading = await loadingController.create({
message: 'Dismissing after 3 seconds...',
duration: 3000,
});
loading.present();
};
</script>
커스터마이징 (Customization)
스피너 (Spinners)
사용할 스피너는 spinner 프로퍼티로 커스터마이즈할 수 있어요. 전체 옵션 목록은 spinner 프로퍼티 문서를 참고하세요.
JavaScript
<ion-button id="open-loading">Show Loading</ion-button>
<ion-loading trigger="open-loading" message="Loading..." duration="3000" spinner="circles"></ion-loading>
Angular
src/app/example.component.html
<ion-button id="open-loading">Show Loading</ion-button>
<ion-loading trigger="open-loading" message="Loading..." [duration]="3000" spinner="circles"></ion-loading>
src/app/example.component.ts
import { Component } from '@angular/core';
import { IonButton, IonLoading } from '@ionic/angular';
@Component({
selector: 'app-example',
templateUrl: 'example.component.html',
styleUrls: ['example.component.css'],
imports: [IonButton, IonLoading],
})
export class ExampleComponent {}
React
import React from 'react';
import { IonButton, useIonLoading } from '@ionic/react';
function Example() {
return (
<>
<IonButton id="open-loading">Show Loading</IonButton>
<IonLoading trigger="open-loading" message="Loading..." duration={3000} spinner="circles" />
</>
);
}
export default Example;
Vue
<template>
<ion-button id="open-loading">Show Loading</ion-button>
<ion-loading trigger="open-loading" message="Loading..." duration="3000" spinner="circles"></ion-loading>
</template>
<script setup lang="ts">
import { IonButton, IonLoading } from '@ionic/vue';
</script>
테마 적용 (Theming)
Loading은 scoped 캡슐화를 사용합니다. 즉, 런타임에 각 스타일에 추가 클래스를 붙여 CSS를 자동으로 스코프한다는 뜻이에요. scoped 선택자를 CSS에서 오버라이드하려면 더 높은 specificity를 가진 선택자가 필요합니다.
커스텀 클래스를 전달하고 그 클래스를 사용해 host와 내부 요소에 커스텀 스타일을 추가하는 것을 권장합니다.
JavaScript
<ion-button id="open-loading">Show Loading</ion-button>
<ion-loading class="custom-loading" trigger="open-loading" message="Loading..." duration="3000"></ion-loading>
<style>
ion-loading.custom-loading {
--background: #e3edff;
--spinner-color: #1c6dff;
color: #1c6dff;
}
</style>
Angular
src/app/example.component.html
<ion-button id="open-loading">Show Loading</ion-button>
<ion-loading class="custom-loading" trigger="open-loading" message="Loading..." [duration]="3000"></ion-loading>
src/app/example.component.ts
import { Component } from '@angular/core';
import { IonButton, IonLoading } from '@ionic/angular';
@Component({
selector: 'app-example',
templateUrl: 'example.component.html',
styleUrls: ['example.component.css'],
imports: [IonButton, IonLoading],
})
export class ExampleComponent {}
src/global.css
ion-loading.custom-loading {
--background: #e3edff;
--spinner-color: #1c6dff;
color: #1c6dff;
}
React
src/main.tsx
import React from 'react';
import { IonButton, IonLoading } from '@ionic/react';
import './main.css';
function Example() {
return (
<>
<IonButton id="open-loading">Show Loading</IonButton>
<IonLoading className="custom-loading" trigger="open-loading" message="Loading" duration={3000} />
</>
);
}
export default Example;
src/main.css
ion-loading.custom-loading {
--background: #e3edff;
--spinner-color: #1c6dff;
color: #1c6dff;
}
Vue
<template>
<ion-button id="open-loading">Show Loading</ion-button>
<ion-loading class="custom-loading" trigger="open-loading" message="Loading..." duration="3000"></ion-loading>
</template>
<script setup lang="ts">
import { IonButton, IonLoading } from '@ionic/vue';
</script>
<style>
ion-loading.custom-loading {
--background: #e3edff;
--spinner-color: #1c6dff;
color: #1c6dff;
}
</style>
참고 (Note)
ion-loading은 애플리케이션의 루트에 표시되므로, ion-loading 스타일은 전역 스타일시트에 배치하는 것을 권장합니다.
접근성 (Accessibility)
Ionic은 자동으로 Loading의 role을 dialog로 설정합니다.
Loading에 message 프로퍼티가 정의되어 있으면 aria-labelledby 속성이 자동으로 메시지 요소의 ID로 설정됩니다. 그렇지 않으면 aria-labelledby가 설정되지 않으므로, 개발자가 htmlAttributes 프로퍼티를 사용해 aria-label을 직접 제공해야 해요.
모든 ARIA 속성은 Loading의 htmlAttributes 프로퍼티에 커스텀 값을 정의해 수동으로 덮어쓸 수 있습니다.
인터페이스 (Interfaces)
LoadingOptions
interface LoadingOptions {
spinner?: SpinnerTypes | null;
message?: string | IonicSafeString;
cssClass?: string | string[];
showBackdrop?: boolean;
duration?: number;
translucent?: boolean;
animated?: boolean;
backdropDismiss?: boolean;
mode?: Mode;
keyboardClose?: boolean;
id?: string;
htmlAttributes?: { [key: string]: any };
enterAnimation?: AnimationBuilder;
leaveAnimation?: AnimationBuilder;
}
Properties
animated
설명: true로 설정하면 로딩 표시기가 애니메이션됩니다.
속성(Attribute): animated
타입(Type): boolean
기본값(Default): true
backdropDismiss
설명: true로 설정하면 배경(backdrop)을 클릭했을 때 로딩 표시기가 닫힙니다.
속성(Attribute): backdrop-dismiss
타입(Type): boolean
기본값(Default): false
cssClass
설명: 커스텀 CSS를 적용하기 위한 추가 클래스입니다. 여러 클래스를 제공한다면 공백으로 구분해야 해요.
속성(Attribute): css-class
타입(Type): string | string[] | undefined
기본값(Default): undefined
duration
설명: 로딩 표시기를 닫기 전에 기다릴 밀리초 수입니다.
속성(Attribute): duration
타입(Type): number
기본값(Default): 0
enterAnimation
설명: 로딩 표시기가 표시될 때 사용할 애니메이션입니다.
속성(Attribute): undefined
타입(Type): ((baseEl: any, opts?: any) => Animation) | undefined
기본값(Default): undefined
htmlAttributes
설명: 로더에 전달할 추가 속성입니다.
속성(Attribute): undefined
타입(Type): undefined | { [key: string]: any; }
기본값(Default): undefined
isOpen
설명: true로 설정하면 로딩 표시기가 열립니다. false로 설정하면 닫힙니다. 표시를 더 세밀하게 제어해야 할 때 사용하세요. 그렇지 않으면 loadingController나 trigger 프로퍼티를 사용하면 됩니다. 참고: 로딩 표시기가 닫힐 때 isOpen이 자동으로 false로 돌아가지 않으므로, 코드에서 직접 설정해야 해요.
속성(Attribute): is-open
타입(Type): boolean
기본값(Default): false
keyboardClose
설명: true로 설정하면 오버레이가 표시될 때 키보드가 자동으로 닫힙니다.
속성(Attribute): keyboard-close
타입(Type): boolean
기본값(Default): true
leaveAnimation
설명: 로딩 표시기가 닫힐 때 사용할 애니메이션입니다.
속성(Attribute): undefined
타입(Type): ((baseEl: any, opts?: any) => Animation) | undefined
기본값(Default): undefined
message
설명: 로딩 표시기에 표시할 선택적 텍스트 콘텐츠입니다. 이 프로퍼티는 문자열로 커스텀 HTML을 허용합니다. 기본적으로 콘텐츠는 일반 텍스트로 파싱됩니다. 커스텀 HTML을 사용하려면 Ionic 설정에서 innerHTMLTemplatesEnabled가 true로 설정되어 있어야 해요.
속성(Attribute): message
타입(Type): IonicSafeString | string | undefined
기본값(Default): undefined
mode
설명: mode는 어떤 플랫폼 스타일을 사용할지 결정합니다. 이는 virtual property로, 초기화 시 한 번 설정되며 첫 렌더링 후 값을 변경해도 업데이트되지 않습니다.
속성(Attribute): mode
타입(Type): "ios" | "md"
기본값(Default): undefined
showBackdrop
설명: true로 설정하면 로딩 표시기 뒤에 배경이 표시됩니다.
속성(Attribute): show-backdrop
타입(Type): boolean
기본값(Default): true
spinner
설명: 표시할 스피너의 이름입니다.
속성(Attribute): spinner
타입(Type): "bubbles" | "circles" | "circular" | "crescent" | "dots" | "lines" | "lines-sharp" | "lines-sharp-small" | "lines-small" | null | undefined
기본값(Default): undefined
translucent
설명: true로 설정하면 로딩 표시기가 반투명해집니다. mode가 "ios"이고 기기가 backdrop-filter를 지원할 때만 적용됩니다.
속성(Attribute): translucent
타입(Type): boolean
기본값(Default): false
trigger
설명: 클릭했을 때 로딩 표시기가 열리게 하는 트리거 요소에 해당하는 ID입니다.
속성(Attribute): trigger
타입(Type): string | undefined
기본값(Default): undefined
Events
| 이름(Name) | 설명(Description) | 버블링(Bubbles) |
|---|---|---|
didDismiss |
로딩 표시기가 닫힌 후 발생합니다. ionLoadingDidDismiss의 약어입니다. | true |
didPresent |
로딩 표시기가 표시된 후 발생합니다. ionLoadingWillDismiss의 약어입니다. | true |
ionLoadingDidDismiss |
로딩이 닫힌 후 발생합니다. | true |
ionLoadingDidPresent |
로딩이 표시된 후 발생합니다. | true |
ionLoadingWillDismiss |
로딩이 닫히기 전에 발생합니다. | true |
ionLoadingWillPresent |
로딩이 표시되기 전에 발생합니다. | true |
willDismiss |
로딩 표시기가 닫히기 전에 발생합니다. ionLoadingWillDismiss의 약어입니다. | true |
willPresent |
로딩 표시기가 표시되기 전에 발생합니다. ionLoadingWillPresent의 약어입니다. | true |
Methods
dismiss
설명: 표시된 후 로딩 오버레이를 닫습니다. 아직 표시되지 않은 오버레이라면 아무 일도 하지 않습니다. 한 번도 표시되지 않은 오버레이를 DOM에서 제거하려면 remove 메서드를 사용하세요.
시그니처(Signature): dismiss(data?: any, role?: string) => Promise<boolean>
파라미터(Parameters): data: dismiss 이벤트에서 내보낼 데이터입니다. role: 로딩을 닫는 요소의 역할입니다. 버튼 핸들러에서 어떤 버튼이 로딩을 닫기 위해 클릭되었는지 판별할 때 유용합니다. "cancel", "destructive", "selected", "backdrop" 같은 예시가 있습니다.
onDidDismiss
설명: 로딩이 닫혔을 때 resolve되는 Promise를 반환합니다.
시그니처(Signature): onDidDismiss<T = any>() => Promise<OverlayEventDetail<T>>
onWillDismiss
설명: 로딩이 닫힐 때 resolve되는 Promise를 반환합니다.
시그니처(Signature): onWillDismiss<T = any>() => Promise<OverlayEventDetail<T>>
present
설명: 생성된 후 로딩 오버레이를 표시합니다.
시그니처(Signature): present() => Promise<void>
CSS Shadow Parts
이 컴포넌트에서 사용할 수 있는 CSS shadow part가 없습니다.
CSS Custom Properties
iOS
| 이름(Name) | 설명(Description) |
|---|---|
--backdrop-opacity |
배경의 불투명도 |
--background |
로딩 다이얼로그의 배경 |
--height |
로딩 다이얼로그의 높이 |
--max-height |
로딩 다이얼로그의 최대 높이 |
--max-width |
로딩 다이얼로그의 최대 너비 |
--min-height |
로딩 다이얼로그의 최소 높이 |
--min-width |
로딩 다이얼로그의 최소 너비 |
--spinner-color |
로딩 스피너의 색상 |
--width |
로딩 다이얼로그의 너비 |
MD
| 이름(Name) | 설명(Description) |
|---|---|
--backdrop-opacity |
배경의 불투명도 |
--background |
로딩 다이얼로그의 배경 |
--height |
로딩 다이얼로그의 높이 |
--max-height |
로딩 다이얼로그의 최대 높이 |
--max-width |
로딩 다이얼로그의 최대 너비 |
--min-height |
로딩 다이얼로그의 최소 높이 |
--min-width |
로딩 다이얼로그의 최소 너비 |
--spinner-color |
로딩 스피너의 색상 |
--width |
로딩 다이얼로그의 너비 |
Slots
이 컴포넌트에서 사용할 수 있는 슬롯이 없습니다.
더 알아보기 (Learn more)
ion-loading은 시간이 걸리는 작업을 하는 동안 사용자에게 진행되고 있음을 알려주는 오버레이예요. trigger로 버튼과 연결하거나 loadingController로 코드에서 직접 띄울 수 있고, duration으로 자동 닫힘을 설정할 수 있습니다.