Close
Angular React Web Components Blazor
Open Source

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 Button, 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 Button is as follows:

<igc-button>Click me</igc-button>

Prefix / Suffix

With prefix and suffix slots of the Button component, we can add different content before and after the main content of the button.

We recommend using a <span> element when adding simple text, symbols, or emojis, and an <igc-icon> component when adding icons to the prefix and suffix slots.

<igc-button type="button" variant="contained">
    <span slot="prefix">Download</span>
    <igc-icon slot="suffix" name="download"></igc-icon>
</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 DisplayType by setting the property to any of the following values:

  • Submit - when we want to submit the form data
  • reset - when we want to reset form data to its initial values
  • button - 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 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 exposes three CSS parts which we can use for styling:

NameDescription
baseThe native button element of the igc-button component.
prefixThe prefix container of the igc-button component.
suffixThe suffix container of the igc-button component.

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;
}

API References

Additional Resources