Web Components 버튼 오버view
The Web Components Button Component lets you enable clickable elements that trigger actions in your Web Components app. You get full control over how you set button variants, configure styles for the wrapped element, and define sizes. The Button Component also gives flexibility through the Web Components Button OnClick event, toggle the Web Components button, disable the Web Components button, and more.
Web Components Button Example
Usage
먼저 다음 명령을 실행하여 Ignite UI for Web Components 설치해야 합니다.
npm install igniteui-webcomponents
그 다음에는 필요한 CSS를 가져오IgcButtonComponent 고, 그 모듈을 등록해야 합니다. 다음과 같습니다:
import { defineComponents, IgcButtonComponent } from "igniteui-webcomponents";
import 'igniteui-webcomponents/themes/light/bootstrap.css';
defineComponents(IgcButtonComponent);
Ignite UI for Web Components에 대한 완전한 소개는 '시작 주제'를 읽어보세요.
사용하기 가장 간단한 방법은IgcButtonComponent 다음과 같습니다:
<igc-button>Click me</igc-button>
Prefix / Suffix
With prefix and suffix slots of the IgcButtonComponent component, we can add different content before and after the main content of the button.
<igc-button type="button" variant="contained">
<span slot="prefix">+</span>Click me<span slot="suffix">-</span>
</igc-button>
Type
The button component will change its internal structure from a <button> to an <a> type element when the href attribute is set. In that case the button can be thought of as a regular link. Setting the href attribute will allow you to also set the rel, target and download attributes.
In the case when the button component uses an actual <button> element internally, we can specify its Type by setting the property to any of the following values:
Submit- when we want to submit the form datareset- when we want to reset form data to its initial valuesbutton- when we want to add button with a custom functionality anywhere on a webpage
Button Variants
Contained Button
Use the variant attribute to add a simple contained button in your component template. Note that if you do not set variant, by default it will be set to contained.
<igc-button variant="contained">Contained</igc-button>
Outlined Button
All you have to do to create an outlined button is to change the value of the variant property:
<igc-button variant="outlined">Outlined</igc-button>
Flat Button
Analogically, we can switch to flat variant.
<igc-button variant="flat">Flat</igc-button>
Floating Action Button
We can create a floating action button by setting the variant property to fab:
<igc-button variant="fab">Fab</igc-button>
Button Sizing
Users can change the size of the IgcButtonComponent using the --ig-size CSS variable. In the following example, we will add some radio buttons to display all size values. This way whenever one gets selected, we will change the size of the button.
import { defineComponents, IgcButtonComponent, IgcRadioComponent, IgcRadioGroupComponent } from 'igniteui-webcomponents';
defineComponents(IgcButtonComponent, IgcRadioComponent, IgcRadioGroupComponent);
<igc-radio-group id="radio-group" alignment="horizontal">
<igc-radio name="size" value="small" label-position="after">Small</igc-radio>
<igc-radio name="size" value="medium" label-position="after" checked>Medium</igc-radio>
<igc-radio name="size" value="large" label-position="after">Large</igc-radio>
</igc-radio-group>
this.radioGroup = document.getElementById('radio-group') as IgcRadioGroupComponent;
this.outlinedButton = document.getElementById('outlined-btn') as IgcButtonComponent;
this.flatButton = document.getElementById('flat-btn') as IgcButtonComponent;
this.containedButton = document.getElementById('contained-btn') as IgcButtonComponent;
this.fabButton = document.getElementById('fab-btn') as IgcButtonComponent;
this.radioGroup.addEventListener('click', (radio: any) => {
this.outlinedButton.style.setProperty('--ig-size', `var(--ig-size-${radio.target.value})`);
this.flatButton.style.setProperty('--ig-size', `var(--ig-size-${radio.target.value})`);
this.containedButton.style.setProperty('--ig-size', `var(--ig-size-${radio.target.value})`);
this.fabButton.style.setProperty('--ig-size', `var(--ig-size-${radio.target.value})`);
});
위 코드를 구현한 결과는 다음과 같아야 합니다.
Download
Setting the download property will prompt the user to save the linked URL instead of navigating to it.
<igc-button
href=""
variant="contained"
download="url_to_content"
target="_blank">
Download
</igc-button>
Styling
The IgcButtonComponent exposes three CSS parts which we can use for styling:
| 이름 | 설명 |
|---|---|
base |
igc-button 구성 요소의 기본 button 요소입니다. |
prefix |
igc-button 구성 요소의 접두사 컨테이너입니다. |
suffix |
igc-button 구성 요소의 접미사 컨테이너입니다. |
The base CSS part allows us to style the wrapped element (<button> or <a>).
igc-button::part(base) {
background-color: var(--ig-primary-500);
color: var(--ig-primary-500-contrast);
padding: 18px;
}