React 배너 개요
Ignite UI for React Banner 구성 요소는 스낵바보다 덜 일시적이고 대화 상자보다 덜 눈에 거슬리는 방식으로 애플리케이션 사용자에게 눈에 띄는 메시지를 쉽게 표시할 수 있는 방법을 제공합니다. 또한 메시지의 컨텍스트에 따라 수행할 작업을 나타낼 수도 있습니다.
Ignite UI for React Banner Example
Usage
먼저, 다음 명령을 실행하여 Ignite UI for React 설치해야 합니다.
npm install igniteui-react
그 다음에는 다음과 같이 필요한 CSS를IgrBanner 가져오면 됩니다:
import { IgrBanner } from 'igniteui-react';
import 'igniteui-webcomponents/themes/light/bootstrap.css';
Ignite UI for React에 대한 완전한 소개는 '시작 주제'를 읽어보세요.
Show Banner
배너 컴포넌트를 표시하려면 그show 메서드를 사용하고 버튼 클릭으로 호출하세요. 배너는 해당 요소가 페이지 템플릿에 삽입된 위치에 대해 기준으로 나타나며, 나머지 모든 콘텐츠를 이동시킵니다. 보통 최소한의 사용자 상호작용만으로도 해산되는 비침해적인 콘텐츠를 보여줍니다.
<IgrButton onClick={() => bannerRef.current.show()}>
<span>Show Banner</span>
</IgrButton>
<IgrBanner ref={bannerRef}>
<span>You are currently offline.</span>
</IgrBanner>
[!NOTE] The
IgrBannerincludes a default action buttonOK, which closes the banner.
Examples
이 컴포넌트는IgrBanner 콘텐츠 템플릿을 허용하면서도 머티리얼 디자인 배너 지침을 최대한 준수할 수 있습니다.
Changing the banner message
배너에 표시되는 메시지 설정은 간단합니다 - 태그에IgrBanner 전달하는 내용을 바꾸기만 하면 됩니다. 텍스트는 지정된 배너 영역에 나타나고, 배너는 기본 템플릿을 사용해 표시됩니다. 아래에서 샘플 배너의 내용을 좀 더 설명적으로 변경할 예정입니다:
<IgrBanner ref={bannerRef}>
<span>You have lost connection to the internet. This app is offline.</span>
</IgrBanner>
Adding an icon
AnIgrIcon 배너에 표시할 수 있는 것은 배너의prefix 슬롯. 아이콘은 항상 배너 메시지 시작 부분에 위치합니다.
[!NOTE] If several
IgrIconelements are inserted, the banner will try to position all of them at the beginning. It is strongly advised to pass only oneIgrIcondirectly to the banner.
배너로 a를 전달IgrIcon 하려면 다음 슬롯을 사용하세요prefix:
<IgrBanner ref={bannerRef}>
<IgrIcon slot="prefix" name="signal_wifi_off"></IgrIcon>
<span>You have lost connection to the internet. This app is offline.</span>
</IgrBanner>
배너 내 메시지를 사용하고IgrIcon 싶다면, 배너 내용에 삽입하면 됩니다:
<IgrBanner ref={bannerRef}>
<span>You have lost connection to the internet. This app is offline.</span>
<IgrIcon name="signal_wifi_off"></IgrIcon>
</IgrBanner>
Changing the banner button
이렇게 하면IgrBanner 배너 버튼을 템플릿 넣을 수 있는 슬롯이actions 드러납니다. 이 기능을 통해 기본 배너 버튼(OK)을 덮어쓰고 사용자 정의 커스텀 액션을 추가할 수 있습니다.
<IgrBanner ref={bannerRef}>
<IgrIcon slot="prefix" name="signal_wifi_off"></IgrIcon>
<span>You have lost connection to the internet. This app is offline.</span>
<div slot="actions">
<IgrButton variant="flat" onClick={() => bannerRef.current.toggle()}>
<IgrRipple />
<span>Toggle Banner</span>
</IgrButton>
</div>
</IgrBanner>
Binding to events
배너 구성 요소는 닫힐 때 이벤트Closing를 방출합니다Closed. 이벤트는Closing 취소 가능하며, 인터페이스를 사용하고CustomEvent 방출된 객체의 속성이cancelable true로 설정되어 있습니다. 이벤트를Closing 취소하면 해당 종료 액션과 이벤트가 발동되지 않고, 배너가 닫히지 않고 이벤트가Closed 발동되지 않습니다.
종료 이벤트를 취소하려면 메서드를preventDefault 호출하세요.
<IgrBanner onClosing={(event) => event.preventDefault()}>
...
</IgrBanner>
[!NOTE] If the changes above are applied, the banner will never close, as the closing event is always cancelled.
Advanced Example
알림 해제용과 연결 활성화용 버튼 두 개의 커스텀 버튼이 있는 배너를 만들어봅시다. 우리는 다음 슬롯을 사용하여actions 커스텀 액션 핸들러를 전달할 수 있습니다:
<IgrBanner ref={bannerRef}>
<IgrIcon slot="prefix" name="signal_wifi_off"></IgrIcon>
<span>You have lost connection to the internet. This app is offline.</span>
<div slot="actions">
<IgrButton variant="flat" onClick={() => bannerRef.current.hide()}>
<IgrRipple />
<span>Continue Offline</span>
</IgrButton>
<IgrButton variant="flat" onClick={refreshBanner}>
<IgrRipple />
<span>Turn On Wifi</span>
</IgrButton>
</div>
</IgrBanner>
구글의 머티리얼 디자인 가이드라인에 따르면, 배너에는 최대 2개의 버튼이 있어야 합니다.
IgrBanner슬롯 아래actions요소 수를 명시적으로 제한하지는 않지만, 재료 디자인 지침을 따르려면 최대 2개까지 사용하는 것이 강력히 권장됩니다.
디사이트 옵션(오프라인 계속 진행)은 추가 로직이 필요 없으니 메서드를hide 호출하면 됩니다. 하지만 확인 동작(Turn On Wifi)은 추가 로직이 필요하기 때문에 컴포넌트에서 정의해야 합니다. 그 다음에는 이벤트에 대한click 리스너를 추가할 예정입니다. 마지막 단계는 각 변경 시 메서드를refreshBanner() 호출하는 것으로, 이 메서드는 필요에wifiState 따라 배너를 토글링합니다.
내비게이션바에는 와이파이 아이콘이 있고, 이벤트에 대한click 이벤트 리스너도 추가할 예정입니다.refreshBanner() 메서드가 변경될 때마다 호출되면, 아이콘은 배너를 토글할 뿐만 아니라 연결 상태에 따라 바뀝니다:
const bannerRef = useRef<IgrBanner>(null);
const iconRef = useRef<IgrIcon>(null);
const [wifiState, setWifiState] = useState(false);
const [iconName, setIconName] = useState("signal_wifi_off");
const refreshBanner = () => {
const nextWifiState = !wifiState;
setWifiState(nextWifiState);
setIconName(nextWifiState ? "signal_wifi_4_bar" : "signal_wifi_off");
if (nextWifiState) {
bannerRef.current.hide();
} else {
bannerRef.current.show();
}
}
return(
<>
<IgrNavbar>
<h1>Gallery</h1>
<IgrIcon ref={iconRef} name={iconName} slot="end" onClick={refreshBanner}></IgrIcon>
</IgrNavbar>
<IgrBanner ref={bannerRef}>
...
<div slot="actions">
...
<IgrButton variant="flat" onClick={refreshBanner}>
<IgrRipple />
<span>Turn On Wifi</span>
</IgrButton>
</div>
</IgrBanner>
</>
);
마지막으로, WiFi 상태에 관한 메시지를 표시하는 aIgrToast를 추가할 예정입니다. 템플릿 배너의 결과는 아래 데모에서 확인할 수 있습니다:
Styling
이 컴포넌트는IgrBanner 스타일을 완전히 제어할 수 있는 여러 CSS 파트를 제공합니다:
| 이름 | 설명 |
|---|---|
base |
배너 구성 요소의 기본 래퍼입니다. |
spacer |
배너 주위의 공간을 설정하는 내부 래퍼입니다. |
message |
텍스트와 일러스트레이션을 담고 있는 부분입니다. |
illustration |
배너 아이콘/일러스트레이션을 보유하는 부분입니다. |
content |
배너 텍스트 콘텐츠를 보유하는 부분입니다. |
actions |
배너 작업 버튼을 보유하는 부분입니다. |
igc-banner::part(spacer) {
background: var(--ig-surface-600);
}
igc-banner::part(illustration) {
color: var(--ig-surface-600-contrast);
}
igc-banner::part(content) {
color: var(--ig-gray-900);
}