Web Components Button Overview
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
First, you need to install the Ignite UI for Web Components by running the following command:
npm install igniteui-webcomponents
You will then need to import the IgcButtonComponent
, its necessary CSS, and register its module, like so:
import { defineComponents, IgcButtonComponent } from "igniteui-webcomponents";
import 'igniteui-webcomponents/themes/light/bootstrap.css';
defineComponents(IgcButtonComponent);
For a complete introduction to the Ignite UI for Web Components, read the Getting Started topic.
The simplest way to start using the IgcButtonComponent
is as follows:
<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 button
component 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})`);
});
The result of implementing the above code should look like the following:
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 button component exposes base
CSS part that allows us to style the wrapped element (<button>
or <a>
).
igc-button::part(base) {
background-color: #e99221;
color: #011627;
padding: 18px;
}
API References
IgcButtonBaseComponent
IgcButtonComponent
Type
download
href
IgcRadioGroupComponent
IgcRadioComponent