UNPKG

10.6 kBMarkdownView Raw
1# `<mwc-button>` [![Published on npm](https://img.shields.io/npm/v/@material/mwc-button.svg)](https://www.npmjs.com/package/@material/mwc-button)
2
3> IMPORTANT: The Material Web Components are a work in progress and subject to
4> major changes until 1.0 release.
5
6Buttons allow users to take actions, and make choices, with a single tap.
7
8[Material Design Guidelines: Button](https://material.io/design/components/buttons.html)
9
10[Demo](https://material-components.github.io/material-web/demos/button/)
11
12## Installation
13
14```sh
15npm install @material/mwc-button
16```
17
18> NOTE: The Material Web Components are distributed as ES2017 JavaScript
19> Modules, and use the Custom Elements API. They are compatible with all modern
20> browsers including Chrome, Firefox, Safari, Edge, and IE11, but an additional
21> tooling step is required to resolve *bare module specifiers*, as well as
22> transpilation and polyfills for IE11. See
23> [here](https://github.com/material-components/material-components-web-components#quick-start)
24> for detailed instructions.
25
26## Example Usage
27
28### Standard
29
30![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/standard.png)
31![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/standard_with_icon.png)
32
33```html
34<mwc-button label="standard"></mwc-button>
35<mwc-button label="standard" icon="code"></mwc-button>
36```
37
38### Outlined
39
40![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/outlined.png)
41![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/outlined_with_icon.png)
42
43```html
44<mwc-button outlined label="outlined"></mwc-button>
45<mwc-button outlined label="outlined" icon="code"></mwc-button>
46```
47
48Warning: Do not attempt to use multiple variant attributes on the same button to
49achieve intended styles. For example, `<mwc-button raised outlined>`. Doing so
50may subject you to future breaking changes. Instead, pick the single variant
51most appropriate for the use-case and write Sass to customize the buttons
52individually.
53
54### Raised
55
56![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/raised.png)
57![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/raised_with_icon.png)
58
59```html
60<mwc-button raised label="raised"></mwc-button>
61<mwc-button raised label="raised" icon="code"></mwc-button>
62```
63
64### Unelevated
65
66![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/unelevated.png)
67![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/unelevated_with_icon.png)
68
69```html
70<mwc-button unelevated label="unelevated"></mwc-button>
71<mwc-button unelevated label="unelevated" icon="code"></mwc-button>
72```
73
74### Dense
75
76![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/dense.png)
77![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/dense_with_icon.png)
78
79```html
80<mwc-button dense unelevated label="dense"></mwc-button>
81<mwc-button dense unelevated label="dense" icon="code"></mwc-button>
82```
83
84### Trailing Icon
85
86![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/trailing_icon.png)
87
88```html
89<mwc-button label="trailing icon" icon="code" trailingIcon></mwc-button>
90```
91
92### Disabled
93
94![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/disabled.png)
95![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/disabled_with_icon.png)
96
97```html
98<mwc-button disabled label="disabled"></mwc-button>
99<mwc-button disabled label="disabled" icon="code"></mwc-button>
100```
101
102### Customize Colors
103
104![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/custom_color.png)
105
106```css
107mwc-button {
108 --mdc-theme-primary: #e9437a;
109 --mdc-theme-on-primary: white;
110}
111```
112
113## API
114
115### Slots
116| Name | Description
117| -------------- | -----------
118| `icon` | Leading icon. Overrides `icon` property. Use `label` or the `icon` property to set the `aria-label`.
119| `trailingIcon` | Icon to show _after_ the label. Overrides `trailingIcon` property. Use `label` or the `trailingIcon` property to set the `aria-label`.
120| _default_ | Default content to display between both icons and after label. __NOTE:__ It is highly recommended to set the `label` property instead of projecting text as it will also set the `aria-label`
121
122### Properties/Attributes
123| Name | Type | Default | Description
124| ---- | ---- | ------- | -----------
125| `aria-haspopup` | `string` | `undefined` | Indicates the availability and type of an interactive popup element, such as menu or dialog, that can be triggered by the button.
126| `icon` | `string` | `''` | Icon to display, and `aria-label` value when `label` is not defined.
127| `label` | `string` | `''` | Label to display for the button, and `aria-label`.
128| `raised` | `boolean` | `false` | Creates a contained button that is elevated above the surface.
129| `unelevated` | `boolean` | `false` | Creates a contained button that is flush with the surface.
130| `outlined` | `boolean` | `false` | Creates an outlined button that is flush with the surface.
131| `dense` | `boolean` | `false` | Makes the button text and container slightly smaller.
132| `disabled` | `boolean` | `false` | Disabled buttons cannot be interacted with and have no visual interaction effect.
133| `trailingIcon` | `boolean` | `false` | When `true`, `icon` will be displayed _after_ `label`.
134| `expandContent` | `boolean` | `false` | When `true`, the space after the label and before any trailing icon, where default slotted content is rendered, is expanded to fit the available space inside the button.
135| `fullwidth` | `boolean` | `false` | When `true`, the button is expanded to fit the entire available space.
136
137### Methods
138*None*
139
140### Events
141*None*
142
143### CSS Custom Properties
144
145| Name | Default | Description
146| ------------------------------------- | -------------------------------------------------- | ---
147| `--mdc-button-horizontal-padding` | filled: `16px` outlined: `15px` default: `8px` | Left and right padding of the button label (for outlined buttons the outline width is automatically subtracted).
148| `--mdc-button-outline-width` | `1px` | `width` of the outline of an outlined button and attempts to keep the component size constant.
149| `--mdc-button-outline-color` | ![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/color_0,0,0,12.png) | Color of the outline of an outlined element.
150| `--mdc-button-disabled-fill-color` | ![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/color_0,0,0,12.png) `rgba(0,0,0,0.12)` | Background fill color of a disabled raised or unelevated button.
151| `--mdc-button-disabled-ink-color` | ![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/color_0,0,0,37.png) `rgba(0,0,0,0.37)` | Text color of a disabled button as well as the outline color of a disabled outlined button.
152| `--mdc-button-disabled-outline-color` | ![](https://raw.githubusercontent.com/material-components/material-components-web-components/6347c3e1b0264cc34cad24e4e52a23e2100a4926/packages/button/images/color_0,0,0,12.png) | Sets the color of the outline of a disabled outlined button.
153| `--mdc-button-raised-box-shadow` | mdc elevation 2 | Sets the box shadow of the raised button.
154| `--mdc-button-raised-box-shadow-hover` | mdc elevation 4 | Sets the box shadow of the raised button when focused or hovered.
155| `--mdc-button-raised-box-shadow-focus` | `--mdc-button-raised-box-shadow-hover` | Sets the box shadow of the raised button when focused. Note, if button is both focused and hovered, then hover will override focus box-shadow.
156| `--mdc-button-raised-box-shadow-active` | mdc elevation 8 | Sets the box shadow of the raised button when active.
157| `--mdc-button-raised-box-shadow-disabled` | mdc elevation 0 | Sets the box shadow of the raised button when disabled.
158
159#### Global Custom Properties
160
161This component exposes the following global [theming](https://github.com/material-components/material-components-web-components/blob/master/docs/theming.md)
162custom properties.
163
164| Name | Description
165| ------------------------------------ | -----------
166| `--mdc-theme-primary` | Color of a flat or outlined button. Background color of a raised button.
167| `--mdc-theme-on-primary` | Text color of a raised button.
168| `--mdc-icon-font` | Font to use for the icon.
169| `--mdc-typography-button-<PROPERTY>` | Styles the typography of the button label.
170
171#### Elevation values
172
173| Elevation Level | CSS Value
174| --- | ---
175| `2` | `0px 3px 1px -2px rgba(0, 0, 0, 0.2), 0px 2px 2px 0px rgba(0, 0, 0, 0.14), 0px 1px 5px 0px rgba(0, 0, 0, 0.12)`
176| `4` | `0px 2px 4px -1px rgba(0, 0, 0, 0.2), 0px 4px 5px 0px rgba(0, 0, 0, 0.14), 0px 1px 10px 0px rgba(0, 0, 0, 0.12)`
177| `8` | `0px 5px 5px -3px rgba(0, 0, 0, 0.2), 0px 8px 10px 1px rgba(0, 0, 0, 0.14), 0px 3px 14px 2px rgba(0, 0, 0, 0.12)`
178| `0` | `0px 0px 0px 0px rgba(0, 0, 0, 0.2), 0px 0px 0px 0px rgba(0, 0, 0, 0.14), 0px 0px 0px 0px rgba(0, 0, 0, 0.12)`
179
180## Additional references
181
182- [MDC Web: Button](https://material.io/develop/web/components/buttons/)