앱 컨테이너 (ion-app)
ion-app은 Ionic 애플리케이션을 감싸는 컨테이너 요소예요. 프로젝트당 <ion-app> 요소는 하나만 있어야 해요. 앱에는 메뉴, 헤더, 콘텐츠, 푸터 등 많은 Ionic 컴포넌트가 들어갈 수 있고, 오버레이 컴포넌트들은 표시될 때 <ion-app>에 추가돼요.
출처: 문서
본문
앱(App)은 Ionic 애플리케이션의 컨테이너 요소예요. 프로젝트당 <ion-app> 요소는 하나만 있어야 해요. 앱에는 메뉴, 헤더, 콘텐츠, 푸터를 포함한 많은 Ionic 컴포넌트가 들어갈 수 있어요. 오버레이 컴포넌트들은 표시될 때 <ion-app>에 추가돼요.
ion-app을 사용하면 다음과 같은 동작들이 활성화돼요.
- 네이티브 플러그인 없이도 키보드 라이프사이클 이벤트
- Android 기기에서 하드웨어 뒤로 가기 버튼 동작을 커스터마이즈하기 위한 하드웨어 뒤로 가기 버튼 리스너
- Capacitor나 Cordova에서 상태 바 지원. 사용자가 상태 바를 탭하면 뷰 맨 위로 스크롤할 수 있어요
- 포커스된 텍스트 입력이 화상 키보드에 가려지지 않도록 콘텐츠를 스크롤해 주는 스크롤 보조 유틸리티
- Material Design 모드에서 버튼 활성화 시 리플 효과
- Ionic 앱을 사용하는 경험을 더 네이티브처럼 느끼게 해 주는 기타 탭/포커스 유틸리티
Programmatic Focus
Ionic은 ion-focusable 클래스가 있는 컴포넌트를 위한 포커스 유틸리티를 제공해요. 이 유틸리티는 Tab 같은 특정 키보드 키가 눌릴 때 컴포넌트의 포커스를 자동으로 관리해요. 컴포넌트는 ion-app의 setFocus 메서드를 사용해서 사용자 동작에 대한 응답으로 프로그래밍 방식으로 포커스할 수도 있어요.
JavaScript
<ion-button id="buttonToFocus" fill="outline">Button</ion-button>
<ion-radio-group value="a">
<ion-radio id="radioToFocus" value="a">Radio</ion-radio>
</ion-radio-group>
<br />
<ion-button onClick="focusElement('#buttonToFocus')">Focus Button</ion-button>
<ion-button onClick="focusElement('#radioToFocus')">Focus Radio</ion-button>
<script>
function focusElement(id) {
const el = document.querySelector(id);
const app = el.closest('ion-app');
if (app) {
app.setFocus([el]);
}
}
</script>
Angular
src/app/example.component.html
<ion-button id="buttonToFocus" fill="outline">Button</ion-button>
<ion-radio-group value="a">
<ion-radio id="radioToFocus" value="a">Radio</ion-radio>
</ion-radio-group>
<br />
<ion-button (click)="focusElement('#buttonToFocus')">Focus Button</ion-button>
<ion-button (click)="focusElement('#radioToFocus')">Focus Radio</ion-button>
src/app/example.component.ts
import { Component } from '@angular/core';
import { IonButton, IonRadio, IonRadioGroup } from '@ionic/angular';
@Component({
selector: 'app-example',
templateUrl: 'example.component.html',
styleUrls: ['example.component.css'],
imports: [IonButton, IonRadio, IonRadioGroup],
})
export class ExampleComponent {
focusElement(id: string) {
const el = document.querySelector(id) as HTMLElement;
const app = el.closest('ion-app');
if (app) {
app.setFocus([el]);
}
}
}
React
import React from 'react';
import { IonButton, IonRadio, IonRadioGroup } from '@ionic/react';
function Example() {
function focusElement(id: string) {
const el = document.querySelector(id) as HTMLElement;
const app = el.closest('ion-app');
if (app) {
app.setFocus([el]);
}
}
return (
<>
<IonButton id="buttonToFocus" fill="outline">
Button
</IonButton>
<IonRadioGroup value="a">
<IonRadio id="radioToFocus" value="a">
Radio
</IonRadio>
</IonRadioGroup>
<br />
<IonButton onClick={() => focusElement('#buttonToFocus')}>Focus Button</IonButton>
<IonButton onClick={() => focusElement('#radioToFocus')}>Focus Radio</IonButton>
</>
);
}
export default Example;
Vue
<template>
<ion-button id="buttonToFocus" fill="outline">Button</ion-button>
<ion-radio-group value="a">
<ion-radio id="radioToFocus" value="a">Radio</ion-radio>
</ion-radio-group>
<br />
<ion-button @click="focusElement('#buttonToFocus')">Focus Button</ion-button>
<ion-button @click="focusElement('#radioToFocus')">Focus Radio</ion-button>
</template>
<script setup lang="ts">
import { IonButton, IonRadio, IonRadioGroup } from '@ionic/vue';
const focusElement = (id) => {
const el = document.querySelector(id);
const app = el.closest('ion-app');
if (app) {
app.setFocus([el]);
}
};
</script>
Properties
No properties available for this component.
Events
No events available for this component.
Methods
setFocus
Description: Used to set focus on an element that uses ion-focusable. Do not use this if focusing the element as a result of a keyboard event as the focus utility should handle this for us. This method should be used when we want to programmatically focus an element as a result of another user action. (Ex: We focus the first element inside of a popover when the user presents it, but the popover is not always presented as a result of keyboard action.)
Signature: setFocus(elements: HTMLElement[]) => Promise<void>
Parameters: elements: An array of HTML elements to set focus on.
CSS Shadow Parts
No CSS shadow parts available for this component.
CSS Custom Properties
No CSS custom properties available for this component.
Slots
No slots available for this component.
더 알아보기 (Learn more)
ion-app은 Ionic 앱 전체를 감싸는 최상위 컨테이너예요. 키보드 이벤트, 하드웨어 뒤로 가기 버튼, 리플 효과 같은 앱 전반의 동작이 여기에 묶여 있어요. 앱당 하나만 두어야 하니, 다른 뷰 컴포넌트들은 모두 이 안에 배치하세요.