Visão geral do botão React
The React Button Component lets you enable clickable elements that trigger actions in your React 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 React Button clicked callback, toggle the React button, disable the React button, and more.
React Button Example
Uso
Primeiro, você precisa instalar o pacote npm Ignite UI for React correspondente executando o seguinte comando:
npm install igniteui-react
Você então precisará importar oIgrButton CSS necessário e o necessário, assim:
import { IgrButton } from 'igniteui-react';
import 'igniteui-webcomponents/themes/light/bootstrap.css';
<IgrButton />
Prefixo / Sufixo
With prefix and suffix slots of the IgrButton component, we can add different content before and after the main content of the button.
<IgrButton type="button" variant="contained">
<span slot="prefix">+</span>Click me<span slot="suffix">-</span>
</IgrButton>
Tipo
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 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
Variantes de Botões
Botão contido
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.
<IgrButton variant="contained"><span>Contained</span></IgrButton>
Botão delineado
All you have to do to create an outlined button is to change the value of the variant property:
<IgrButton variant="outlined"><span>Outlined</span></IgrButton>
Botão plano
Analogically, we can switch to flat variant.
<IgrButton variant="flat"><span>Flat</span></IgrButton>
Botão de ação flutuante
We can create a floating action button by setting the variant property to fab:
<IgrButton variant="fab"><span>Fab</span></IgrButton>
Tamanho dos botões
Users can change the size of the IgrButton 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 { IgrButton, IgrRadio, IgrRadioGroup } from 'igniteui-react';
const [size, setSize] = useState("small");
const onRadioChange = (e: IgrRadioChangeEventArgs) => {
setSize(e.detail.value);
};
<IgrRadioGroup alignment="horizontal" style={{ display: "flex", margin: "0 auto", width: "15%" }}>
<IgrRadio name="size" value="small" labelPosition="after" checked={size === "small"} onChange={onRadioChange}>
<span>Small</span>
</IgrRadio>
<IgrRadio name="size" value="medium" labelPosition="after" onChange={onRadioChange}>
<span>Medium</span>
</IgrRadio>
<IgrRadio name="size" value="large" labelPosition="after" onChange={onRadioChange}>
<span>Large</span>
</IgrRadio>
</IgrRadioGroup>
<div className="button-container">
<IgrButton className={"size-" + size} variant="flat">
<span>Flat</span>
</IgrButton>
<IgrButton className={"size-" + size} variant="contained">
<span>Contained</span>
</IgrButton>
<IgrButton className={"size-" + size} variant="outlined">
<span>Outlined</span>
</IgrButton>
<IgrButton className={"size-" + size} variant="fab">
<span>Like</span>
</IgrButton>
</div>
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.
<IgrButton
href=""
variant="contained"
download="url"
target="_blank" >
<span>Download</span>
</IgrButton>
Estilização
The IgrButton 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;
}