Visão geral do botão de Web Components

    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

    Primeiro, você precisa instalar o Ignite UI for Web Components executando o seguinte comando:

    npm install igniteui-webcomponents
    

    Você então precisará importar oIgcButtonComponent CSS necessário e registrar seu módulo, assim:

    import { defineComponents, IgcButtonComponent } from "igniteui-webcomponents";
    import 'igniteui-webcomponents/themes/light/bootstrap.css';
    
    defineComponents(IgcButtonComponent);
    

    Para uma introdução completa ao Ignite UI for Web Components, leia o tópico Comecando.

    A maneira mais simples de começar a usar oIgcButtonComponent é a seguinte:

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

    O resultado da implementação do código acima deve ser semelhante ao seguinte:

    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:

    Nome Descrição
    base O elemento de botão nativo do componente igc-button.
    prefix O contêiner de prefixo do componente igc-button.
    suffix O contêiner de sufixo do componente 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;
    }
    

    API References

    Additional Resources