@justeattakeaway/pie-button 0.30.0 → 0.32.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +105 -32
- package/dist/index.d.ts +67 -2
- package/dist/index.js +594 -73
- package/dist/react.d.ts +67 -2
- package/dist/react.js +41 -38
- package/package.json +7 -4
- package/src/defs.ts +51 -2
- package/src/index.ts +106 -1
- package/LICENSE +0 -17
package/README.md
CHANGED
|
@@ -11,46 +11,42 @@
|
|
|
11
11
|
# Table of Contents
|
|
12
12
|
|
|
13
13
|
1. [Introduction](#pie-button)
|
|
14
|
-
2. [
|
|
15
|
-
3. [
|
|
14
|
+
2. [Installation](#installation)
|
|
15
|
+
3. [Importing the component](#importing-the-component)
|
|
16
16
|
4. [Peer Dependencies](#peer-dependencies)
|
|
17
|
-
5. [
|
|
17
|
+
5. [Local Development](#local-development)
|
|
18
|
+
6. [Props](#props)
|
|
19
|
+
7. [Events](#events)
|
|
18
20
|
- [HTML example](#html)
|
|
19
21
|
- [Vue example (using Nuxt 3)](#vue-templates-using-nuxt-3)
|
|
20
22
|
- [React example (using Next 13)](#react-templates-using-next-13)
|
|
21
|
-
6. [
|
|
23
|
+
6. [Forms usage](#forms-usage)
|
|
24
|
+
7. [Testing](#testing)
|
|
22
25
|
- [Browser Tests](#browser-tests)
|
|
23
26
|
- [Visual Tests](#visual-tests)
|
|
24
27
|
|
|
25
28
|
|
|
26
|
-
##
|
|
29
|
+
## pie-button
|
|
27
30
|
|
|
28
31
|
`pie-button` is a Web Component built using the Lit library. It offers a simple and accessible button component for web applications.
|
|
29
32
|
|
|
30
33
|
This component can be easily integrated into various frontend frameworks and customized through a set of properties.
|
|
31
34
|
|
|
32
|
-
## Local development
|
|
33
|
-
|
|
34
|
-
Install the dependencies. Note that this, and the following commands below, should be run from the **root of the monorepo**:
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
yarn
|
|
38
|
-
```
|
|
36
|
+
## Installation
|
|
39
37
|
|
|
40
|
-
To
|
|
38
|
+
To install `pie-button` in your application, run the following on your command line:
|
|
41
39
|
|
|
42
40
|
```bash
|
|
43
|
-
|
|
41
|
+
# npm
|
|
42
|
+
$ npm i @justeattakeaway/pie-button
|
|
43
|
+
|
|
44
|
+
# yarn
|
|
45
|
+
$ yarn add @justeattakeaway/pie-button
|
|
44
46
|
```
|
|
45
47
|
|
|
46
|
-
|
|
48
|
+
For full information on using PIE components as part of an application, check out the [Getting Started Guide](https://github.com/justeattakeaway/pie/wiki/Getting-started-with-PIE-Web-Components).
|
|
47
49
|
|
|
48
|
-
```bash
|
|
49
|
-
yarn watch --filter=pie-button
|
|
50
|
-
|
|
51
|
-
# in a separate terminal tab, run
|
|
52
|
-
yarn dev --filter=pie-storybook
|
|
53
|
-
```
|
|
54
50
|
|
|
55
51
|
### Importing the component
|
|
56
52
|
|
|
@@ -67,23 +63,57 @@ import { PieButton } from '@justeattakeaway/pie-button';
|
|
|
67
63
|
|
|
68
64
|
import { PieButton } from '@justeattakeaway/pie-button/dist/react';
|
|
69
65
|
```
|
|
66
|
+
|
|
67
|
+
|
|
70
68
|
## Peer Dependencies
|
|
71
69
|
|
|
72
|
-
>
|
|
73
|
-
>
|
|
74
|
-
|
|
70
|
+
> [!IMPORTANT]
|
|
71
|
+
> When using `pie-button`, you will also need to include a couple of dependencies to ensure the component renders as expected. See [the PIE Wiki for more information and how to include these in your application](https://github.com/justeattakeaway/pie/wiki/Getting-started-with-PIE-Web-Components#expected-dependencies).
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
## Local development
|
|
75
|
+
|
|
76
|
+
Install the dependencies. Note that this, and the following commands below, should be run from the **root of the monorepo**:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
yarn
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
To build the `pie-button` package, run the following command:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
yarn build --filter=pie-button
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
If you'd like to develop using the component storybook, then you should build the component in `watch` mode, and run storybook in a separate terminal tab:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
yarn watch --filter=pie-button
|
|
92
|
+
|
|
93
|
+
# in a separate terminal tab, run
|
|
94
|
+
yarn dev --filter=pie-storybook
|
|
95
|
+
```
|
|
96
|
+
|
|
75
97
|
|
|
76
98
|
## Props
|
|
77
99
|
|
|
78
|
-
| Property
|
|
79
|
-
|
|
80
|
-
| size
|
|
81
|
-
| type
|
|
82
|
-
| variant
|
|
83
|
-
| disabled
|
|
84
|
-
| isFullWidth
|
|
85
|
-
| isLoading
|
|
86
|
-
| iconPlacement
|
|
100
|
+
| Property | Type | Default | Description |
|
|
101
|
+
|----------------|-----------|------------|----------------------------------------------------------------------------------------------------------------------|
|
|
102
|
+
| size | `String` | `medium` | Size of the button, one of `sizes` – `xsmall`, `small-expressive`, `small-productive`, `medium`, `large` |
|
|
103
|
+
| type | `String` | `submit` | Type of the button, one of `types` – `submit`, `button`, `reset`. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type)|
|
|
104
|
+
| variant | `String` | `primary` | Variant of the button, one of `variants` – `primary`, `secondary`, `outline`, `ghost`, `destructive`, `destructive-ghost`, `inverse`, `ghost-inverse`, `outline-inverse` |
|
|
105
|
+
| disabled | `Boolean` | `false` | If `true`, disables the button. |
|
|
106
|
+
| isFullWidth | `Boolean` | `false` | If `true`, sets the button width to 100% of its container. |
|
|
107
|
+
| isLoading | `Boolean` | `false` | If `true`, displays a loading indicator inside the button. |
|
|
108
|
+
| iconPlacement | `String` | `leading` | Icon placements of the icon slot, if provided, one of `iconPlacements` - `leading`, `trailing` |
|
|
109
|
+
| name | `String` | `undefined`| The name of the button, to be submitted with form data. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-name) |
|
|
110
|
+
| value | `String` | `undefined`| The value of the button, to be submitted with form data. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-value) |
|
|
111
|
+
| formaction | `String` | `undefined`| Designates an alternative URL for form data submission. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formaction) |
|
|
112
|
+
| formenctype | `String` | `undefined`| Specifies the form data encoding type. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formenctype) |
|
|
113
|
+
| formmethod | `String` | `undefined`| Sets the HTTP method for form data. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formmethod) |
|
|
114
|
+
| formnovalidate | `Boolean` | `undefined` | Ensures the form is submitted without native HTML validation. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formnovalidate) |
|
|
115
|
+
| formtarget | `String` | `undefined`| Dictates where to display the response after form submission. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formtarget) |
|
|
116
|
+
|
|
87
117
|
|
|
88
118
|
In your markup or JSX, you can then use these to set the properties for the `pie-button` component:
|
|
89
119
|
|
|
@@ -95,6 +125,7 @@ In your markup or JSX, you can then use these to set the properties for the `pie
|
|
|
95
125
|
<PieButton size='medium' type='button' variant='primary'>Click me!</PieButton>
|
|
96
126
|
```
|
|
97
127
|
|
|
128
|
+
|
|
98
129
|
## Slots
|
|
99
130
|
|
|
100
131
|
| Slot | Description |
|
|
@@ -146,6 +177,48 @@ For example, to add a click handler in various templates:
|
|
|
146
177
|
|
|
147
178
|
```
|
|
148
179
|
|
|
180
|
+
## Forms usage
|
|
181
|
+
|
|
182
|
+
The `pie-button` web component is designed to integrate with standard HTML forms just like a native HTML button. When positioned inside a form, the component will automatically associate itself, enabling it to directly interact with the form context.
|
|
183
|
+
|
|
184
|
+
### Button Attributes
|
|
185
|
+
|
|
186
|
+
The `pie-button` provides a set of attributes to customize its behavior within forms:
|
|
187
|
+
|
|
188
|
+
- `type`: Determines the button's function. Set to `submit` for form submissions or `reset` to clear form fields.
|
|
189
|
+
- `formaction`: Designates an alternative URL for form data submission when this specific button is clicked.
|
|
190
|
+
- `formenctype`: Specifies the form data encoding type during submission via this button.
|
|
191
|
+
- `formmethod`: Sets the HTTP method (e.g., GET or POST) for form data when initiated by this button.
|
|
192
|
+
- `formnovalidate`: If present, ensures the form is submitted without validation checks.
|
|
193
|
+
- `formtarget`: Dictates where to display the response after form submission.
|
|
194
|
+
|
|
195
|
+
Please see the [MDN docs]((https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)) for more information on these attributes.
|
|
196
|
+
|
|
197
|
+
### Integration Example
|
|
198
|
+
|
|
199
|
+
```html
|
|
200
|
+
<form action="/default-endpoint" method="post">
|
|
201
|
+
<input type="text" name="username" required>
|
|
202
|
+
<pie-button
|
|
203
|
+
type="submit"
|
|
204
|
+
formaction="/alternate-endpoint"
|
|
205
|
+
formenctype="multipart/form-data"
|
|
206
|
+
formmethod="post"
|
|
207
|
+
formnovalidate
|
|
208
|
+
formtarget="_blank">
|
|
209
|
+
Submit
|
|
210
|
+
</pie-button>
|
|
211
|
+
</form>
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
In this example:
|
|
215
|
+
|
|
216
|
+
- The pie-button, when clicked, will send the form data to /alternate-endpoint instead of the form's default /default-endpoint.
|
|
217
|
+
- It uses the multipart/form-data encoding type for form submission.
|
|
218
|
+
- The form will submit using the POST method.
|
|
219
|
+
- No validation will be performed during submission, thanks to formnovalidate.
|
|
220
|
+
- The form's submission response will be opened in a new browser tab/window because of the formtarget="_blank" attribute.
|
|
221
|
+
|
|
149
222
|
## Testing
|
|
150
223
|
|
|
151
224
|
### Browser tests
|
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export declare interface ButtonProps {
|
|
|
8
8
|
*/
|
|
9
9
|
size: typeof sizes[number];
|
|
10
10
|
/**
|
|
11
|
-
* What type attribute should be applied to the button. For example submit, button
|
|
11
|
+
* What type attribute should be applied to the button. For example submit, button.
|
|
12
12
|
*/
|
|
13
13
|
type: typeof types[number];
|
|
14
14
|
/**
|
|
@@ -31,8 +31,52 @@ export declare interface ButtonProps {
|
|
|
31
31
|
* When true, displays a loading indicator inside the button.
|
|
32
32
|
*/
|
|
33
33
|
isLoading: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* The name of the button, submitted as a pair with the button's value as part of the form data, when that button is used to submit the form.
|
|
36
|
+
* [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
|
|
37
|
+
*/
|
|
38
|
+
name?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Defines the value associated with the button's name when it's submitted with the form data. This value is passed to the server in params when the form is submitted using this button.
|
|
41
|
+
* [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
|
|
42
|
+
*/
|
|
43
|
+
value?: string;
|
|
44
|
+
/**
|
|
45
|
+
* The URL that processes the information submitted by the button. Overrides the action attribute of the button's form owner. Does nothing if there is no form owner.
|
|
46
|
+
* [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
|
|
47
|
+
*/
|
|
48
|
+
formaction?: string;
|
|
49
|
+
/**
|
|
50
|
+
* If the button is a submit button (it's inside/associated with a <form> and doesn't have type="button"), specifies how to encode the form data that is submitted.
|
|
51
|
+
* If this attribute is specified, it overrides the enctype attribute of the button's form owner.
|
|
52
|
+
* [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
|
|
53
|
+
*/
|
|
54
|
+
formenctype?: typeof formEncodingtypes[number];
|
|
55
|
+
/**
|
|
56
|
+
* If the button is a submit button (it's inside/associated with a <form> and doesn't have type="button"), this attribute specifies the HTTP method used to submit the form.
|
|
57
|
+
* If specified, this attribute overrides the method attribute of the button's form owner.
|
|
58
|
+
* [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
|
|
59
|
+
*/
|
|
60
|
+
formmethod?: typeof formMethodTypes[number];
|
|
61
|
+
/**
|
|
62
|
+
* If the button is a submit button, this Boolean attribute specifies that the form is not to be validated when it is submitted.
|
|
63
|
+
* If this attribute is specified, it overrides the novalidate attribute of the button's form owner.
|
|
64
|
+
* [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
|
|
65
|
+
*/
|
|
66
|
+
formnovalidate?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* If the button is a submit button, this attribute is an author-defined name or standardized, underscore-prefixed keyword indicating where to display the response from submitting the form.
|
|
69
|
+
* [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
|
|
70
|
+
*/
|
|
71
|
+
formtarget?: typeof formTargetTypes[number];
|
|
34
72
|
}
|
|
35
73
|
|
|
74
|
+
export declare const formEncodingtypes: readonly ["application/x-www-form-urlencoded", "multipart/form-data", "text/plain"];
|
|
75
|
+
|
|
76
|
+
export declare const formMethodTypes: readonly ["post", "get", "dialog"];
|
|
77
|
+
|
|
78
|
+
export declare const formTargetTypes: readonly ["_self", "_blank", "_parent", "_top"];
|
|
79
|
+
|
|
36
80
|
export declare const iconPlacements: readonly ["leading", "trailing"];
|
|
37
81
|
|
|
38
82
|
/**
|
|
@@ -40,6 +84,10 @@ export declare const iconPlacements: readonly ["leading", "trailing"];
|
|
|
40
84
|
* @slot - Default slot
|
|
41
85
|
*/
|
|
42
86
|
export declare class PieButton extends LitElement implements ButtonProps {
|
|
87
|
+
static formAssociated: boolean;
|
|
88
|
+
private readonly _internals;
|
|
89
|
+
get form(): HTMLFormElement | null;
|
|
90
|
+
constructor();
|
|
43
91
|
size: ButtonProps['size'];
|
|
44
92
|
type: ButtonProps['type'];
|
|
45
93
|
variant: ButtonProps['variant'];
|
|
@@ -47,6 +95,23 @@ export declare class PieButton extends LitElement implements ButtonProps {
|
|
|
47
95
|
disabled: boolean;
|
|
48
96
|
isLoading: boolean;
|
|
49
97
|
isFullWidth: boolean;
|
|
98
|
+
name?: string;
|
|
99
|
+
value?: string;
|
|
100
|
+
formaction: ButtonProps['formaction'];
|
|
101
|
+
formenctype: ButtonProps['formenctype'];
|
|
102
|
+
formmethod: ButtonProps['formmethod'];
|
|
103
|
+
formnovalidate: ButtonProps['formnovalidate'];
|
|
104
|
+
formtarget: ButtonProps['formtarget'];
|
|
105
|
+
/**
|
|
106
|
+
* This method creates an invisible button of the same type as pie-button. It is then clicked, and immediately removed from the DOM.
|
|
107
|
+
* This is done so that we trigger native form actions, such as submit and reset in the browser. The performance impact of adding and removing a single button to the DOM
|
|
108
|
+
* should be neglible, however this should be monitored.
|
|
109
|
+
* This is the only viable way of guaranteeing native button behaviour when using a web component in place of an actual HTML button.
|
|
110
|
+
*
|
|
111
|
+
* TODO: if we need to repeat this logic elsewhere, then we should consider moving this code to a shared class or mixin.
|
|
112
|
+
*/
|
|
113
|
+
private _simulateNativeButtonClick;
|
|
114
|
+
private _handleClick;
|
|
50
115
|
render(): TemplateResult<1>;
|
|
51
116
|
focus(): void;
|
|
52
117
|
static styles: CSSResult;
|
|
@@ -54,7 +119,7 @@ export declare class PieButton extends LitElement implements ButtonProps {
|
|
|
54
119
|
|
|
55
120
|
export declare const sizes: readonly ["xsmall", "small-productive", "small-expressive", "medium", "large"];
|
|
56
121
|
|
|
57
|
-
export declare const types: readonly ["submit", "button", "reset"
|
|
122
|
+
export declare const types: readonly ["submit", "button", "reset"];
|
|
58
123
|
|
|
59
124
|
export declare type Variant = typeof variants[number];
|
|
60
125
|
|