@justeattakeaway/pie-button 0.0.0-snapshot-release-20231212141542

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 ADDED
@@ -0,0 +1,207 @@
1
+ <p align="center">
2
+ <img align="center" src="../../../readme_image.png" height="200" alt="">
3
+ </p>
4
+
5
+ <p align="center">
6
+ <a href="https://www.npmjs.com/@justeattakeaway/pie-button">
7
+ <img alt="GitHub Workflow Status" src="https://img.shields.io/npm/v/@justeattakeaway/pie-button.svg">
8
+ </a>
9
+ </p>
10
+
11
+ # Table of Contents
12
+
13
+ 1. [Introduction](#pie-button)
14
+ 2. [Installation](#installation)
15
+ 3. [Importing the component](#importing-the-component)
16
+ 4. [Peer Dependencies](#peer-dependencies)
17
+ 5. [Props](#props)
18
+ 6. [Events](#events)
19
+ - [HTML example](#html)
20
+ - [Vue example (using Nuxt 3)](#vue-templates-using-nuxt-3)
21
+ - [React example (using Next 13)](#react-templates-using-next-13)
22
+ 7. [Forms usage](#forms-usage)
23
+ 8. [Contributing](#contributing)
24
+
25
+
26
+ ## pie-button
27
+
28
+ `pie-button` is a Web Component built using the Lit library. It offers a simple and accessible button component for web applications.
29
+
30
+ This component can be easily integrated into various frontend frameworks and customized through a set of properties.
31
+
32
+
33
+ ## Installation
34
+
35
+ To install `pie-button` in your application, run the following on your command line:
36
+
37
+ ```bash
38
+ # npm
39
+ $ npm i @justeattakeaway/pie-button
40
+
41
+ # yarn
42
+ $ yarn add @justeattakeaway/pie-button
43
+ ```
44
+
45
+ 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).
46
+
47
+
48
+ ### Importing the component
49
+
50
+ #### JavaScript
51
+ ```js
52
+ // Default – for Native JS Applications, Vue, Angular, Svelte, etc.
53
+ import { PieButton } from '@justeattakeaway/pie-button';
54
+
55
+ // If you don't need to reference the imported object, you can simply
56
+ // import the module which registers the component as a custom element.
57
+ import '@justeattakeaway/pie-button';
58
+ ```
59
+
60
+ #### React
61
+ ```js
62
+ // React
63
+ // For React, you will need to import our React-specific component build
64
+ // which wraps the web component using ​@lit/react
65
+ import { PieButton } from '@justeattakeaway/pie-button/dist/react';
66
+ ```
67
+
68
+ > [!NOTE]
69
+ > When using the React version of the component, please make sure to also
70
+ > include React as a [peer dependency](#peer-dependencies) in your project.
71
+
72
+
73
+ ## Peer Dependencies
74
+
75
+ > [!IMPORTANT]
76
+ > 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).
77
+
78
+
79
+ ## Props
80
+
81
+ | Property | Type | Default | Description |
82
+ |----------------|-----------|------------|----------------------------------------------------------------------------------------------------------------------|
83
+ | size | `String` | `medium` | Size of the button, one of `sizes` – `xsmall`, `small-expressive`, `small-productive`, `medium`, `large` |
84
+ | 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)|
85
+ | variant | `String` | `primary` | Variant of the button, one of `variants` – `primary`, `secondary`, `outline`, `ghost`, `destructive`, `destructive-ghost`, `inverse`, `ghost-inverse`, `outline-inverse` |
86
+ | disabled | `Boolean` | `false` | If `true`, disables the button. |
87
+ | isFullWidth | `Boolean` | `false` | If `true`, sets the button width to 100% of its container. |
88
+ | isLoading | `Boolean` | `false` | If `true`, displays a loading indicator inside the button. |
89
+ | isResponsive | `Boolean` | `false` | If `true`, uses the next larger size on wide viewports. |
90
+ | iconPlacement | `String` | `leading` | Icon placements of the icon slot, if provided, one of `iconPlacements` - `leading`, `trailing` |
91
+ | 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) |
92
+ | 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) |
93
+ | 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) |
94
+ | 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) |
95
+ | 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) |
96
+ | 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) |
97
+ | 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) |
98
+ | responsiveSize | `String` | `productive`| Sets a specific size for xsmall button when rendered as responsive. It can be `productive` or `expressive`. |
99
+
100
+
101
+ In your markup or JSX, you can then use these to set the properties for the `pie-button` component:
102
+
103
+ ```html
104
+ <!-- Native HTML -->
105
+ <pie-button size='medium' type='button' variant='primary'>Click me!</pie-button>
106
+
107
+ <!-- JSX -->
108
+ <PieButton size='medium' type='button' variant='primary'>Click me!</PieButton>
109
+ ```
110
+
111
+
112
+ ## Slots
113
+
114
+ | Slot | Description |
115
+ |---------------|------------------------------------------------------------------------------------------------------------------------------------|
116
+ | Default slot | The default slot is used to pass text into the button component. |
117
+ | icon | Used to pass in an icon to the button component. The icon placement can be controlled via the `iconPlacement` prop and we recommend using `pie-icons-webc` for defining this icon, but this can also accept an SVG icon. |
118
+
119
+ ### Using `pie-icons-webc` with `pie-button` icon slot
120
+
121
+ We recommend using `pie-icons-webc` when using the `icon` slot. Here is an example of how you would do this:
122
+
123
+ ```html
124
+ <!--
125
+ Note that pie-button and the icon that you want to use will need to be imported as components into your application.
126
+ See the `pie-icons-webc` README for more info on importing these icons.
127
+ -->
128
+ <pie-button>
129
+ <icon-plus-circle slot="icon"></icon-plus-circle>
130
+ Search
131
+ </pie-button>
132
+ ```
133
+
134
+
135
+ ## Events
136
+
137
+ This component does not use any custom event handlers. In order to add event listening to this component, you can treat it like a native HTML element in your application.
138
+
139
+ For example, to add a click handler in various templates:
140
+
141
+ ### HTML
142
+
143
+ ```html
144
+ <!-- Other attributes omitted for clarity -->
145
+ <pie-button onclick="e => console.log(e)">Click me!</pie-button>
146
+ ```
147
+
148
+ ### Vue templates (using Nuxt 3)
149
+
150
+ ```html
151
+ <!-- Other attributes omitted for clarity -->
152
+ <pie-button @click="handleClick">Click me!</pie-button>
153
+ ```
154
+
155
+ ### React templates (using Next 13)
156
+
157
+ ```html
158
+ <!-- Other attributes omitted for clarity -->
159
+ <PieButton onClick={handleClick}>increment</PieButton>
160
+
161
+ ```
162
+
163
+ ## Forms usage
164
+
165
+ 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.
166
+
167
+ ### Button Attributes
168
+
169
+ The `pie-button` provides a set of attributes to customize its behavior within forms:
170
+
171
+ - `type`: Determines the button's function. Set to `submit` for form submissions or `reset` to clear form fields.
172
+ - `formaction`: Designates an alternative URL for form data submission when this specific button is clicked.
173
+ - `formenctype`: Specifies the form data encoding type during submission via this button.
174
+ - `formmethod`: Sets the HTTP method (e.g., GET or POST) for form data when initiated by this button.
175
+ - `formnovalidate`: If present, ensures the form is submitted without validation checks.
176
+ - `formtarget`: Dictates where to display the response after form submission.
177
+
178
+ Please see the [MDN docs]((https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)) for more information on these attributes.
179
+
180
+ ### Integration Example
181
+
182
+ ```html
183
+ <form action="/default-endpoint" method="post">
184
+ <input type="text" name="username" required>
185
+ <pie-button
186
+ type="submit"
187
+ formaction="/alternate-endpoint"
188
+ formenctype="multipart/form-data"
189
+ formmethod="post"
190
+ formnovalidate
191
+ formtarget="_blank">
192
+ Submit
193
+ </pie-button>
194
+ </form>
195
+ ```
196
+
197
+ In this example:
198
+
199
+ - The pie-button, when clicked, will send the form data to /alternate-endpoint instead of the form's default /default-endpoint.
200
+ - It uses the multipart/form-data encoding type for form submission.
201
+ - The form will submit using the POST method.
202
+ - No validation will be performed during submission, thanks to formnovalidate.
203
+ - The form's submission response will be opened in a new browser tab/window because of the formtarget="_blank" attribute.
204
+
205
+ ## Contributing
206
+
207
+ Check out our [contributing guide](https://github.com/justeattakeaway/pie/wiki/Contributing-Guide) for more information on [local development](https://github.com/justeattakeaway/pie/wiki/Contributing-Guide#local-development) and how to run specific [component tests](https://github.com/justeattakeaway/pie/wiki/Contributing-Guide#testing).
@@ -0,0 +1,9 @@
1
+ declare module '*.scss' {
2
+ const content: Record<string, string>;
3
+ export default content;
4
+ }
5
+
6
+ declare module '*.scss?inline' {
7
+ const content: Record<string, string>;
8
+ export default content;
9
+ }
@@ -0,0 +1,152 @@
1
+ import type { CSSResult } from 'lit';
2
+ import type { LitElement } from 'lit';
3
+ import type { PropertyValues } from 'lit';
4
+ import type { TemplateResult } from 'lit';
5
+
6
+ export declare interface ButtonProps {
7
+ /**
8
+ * What size the button should be.
9
+ */
10
+ size: typeof sizes[number];
11
+ /**
12
+ * What type attribute should be applied to the button. For example submit, button.
13
+ */
14
+ type: typeof types[number];
15
+ /**
16
+ * What style variant the button should be such as primary, outline or ghost.
17
+ */
18
+ variant: Variant;
19
+ /**
20
+ * The placement of the icon slot, if provided, such as leading or trailing
21
+ */
22
+ iconPlacement?: typeof iconPlacements[number];
23
+ /**
24
+ * When true, the button element is disabled.
25
+ */
26
+ disabled: boolean;
27
+ /**
28
+ * When true, the button element will occupy the full width of its container.
29
+ */
30
+ isFullWidth: boolean;
31
+ /**
32
+ * When true, displays a loading indicator inside the button.
33
+ */
34
+ isLoading: boolean;
35
+ /**
36
+ * When true, enables the responsive size feature.
37
+ */
38
+ isResponsive: boolean;
39
+ /**
40
+ * 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.
41
+ * [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
42
+ */
43
+ name?: string;
44
+ /**
45
+ * 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.
46
+ * [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
47
+ */
48
+ value?: string;
49
+ /**
50
+ * 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.
51
+ * [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
52
+ */
53
+ formaction?: string;
54
+ /**
55
+ * 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.
56
+ * If this attribute is specified, it overrides the enctype attribute of the button's form owner.
57
+ * [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
58
+ */
59
+ formenctype?: typeof formEncodingtypes[number];
60
+ /**
61
+ * 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.
62
+ * If specified, this attribute overrides the method attribute of the button's form owner.
63
+ * [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
64
+ */
65
+ formmethod?: typeof formMethodTypes[number];
66
+ /**
67
+ * If the button is a submit button, this Boolean attribute specifies that the form is not to be validated when it is submitted.
68
+ * If this attribute is specified, it overrides the novalidate attribute of the button's form owner.
69
+ * [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
70
+ */
71
+ formnovalidate?: boolean;
72
+ /**
73
+ * 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.
74
+ * [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
75
+ */
76
+ formtarget?: typeof formTargetTypes[number];
77
+ /**
78
+ * What size should be attributed to the button when isResponsive is true and the screen is wide.
79
+ */
80
+ responsiveSize?: typeof responsiveSizes[number];
81
+ }
82
+
83
+ export declare const formEncodingtypes: readonly ["application/x-www-form-urlencoded", "multipart/form-data", "text/plain"];
84
+
85
+ export declare const formMethodTypes: readonly ["post", "get", "dialog"];
86
+
87
+ export declare const formTargetTypes: readonly ["_self", "_blank", "_parent", "_top"];
88
+
89
+ export declare const iconPlacements: readonly ["leading", "trailing"];
90
+
91
+ /**
92
+ * @tagname pie-button
93
+ * @slot icon - The icon slot
94
+ * @slot - Default slot
95
+ */
96
+ export declare class PieButton extends LitElement implements ButtonProps {
97
+ static formAssociated: boolean;
98
+ private readonly _internals;
99
+ get form(): HTMLFormElement | null;
100
+ constructor();
101
+ connectedCallback(): void;
102
+ disconnectedCallback(): void;
103
+ updated(changedProperties: PropertyValues<this>): void;
104
+ size: ButtonProps['size'];
105
+ type: ButtonProps['type'];
106
+ variant: ButtonProps['variant'];
107
+ iconPlacement: ButtonProps['iconPlacement'];
108
+ disabled: boolean;
109
+ isLoading: boolean;
110
+ isFullWidth: boolean;
111
+ isResponsive: boolean;
112
+ name?: string;
113
+ value?: string;
114
+ formaction: ButtonProps['formaction'];
115
+ formenctype: ButtonProps['formenctype'];
116
+ formmethod: ButtonProps['formmethod'];
117
+ formnovalidate: ButtonProps['formnovalidate'];
118
+ formtarget: ButtonProps['formtarget'];
119
+ responsiveSize?: ButtonProps['responsiveSize'];
120
+ /**
121
+ * This method creates an invisible button of the same type as pie-button. It is then clicked, and immediately removed from the DOM.
122
+ * 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
123
+ * should be neglible, however this should be monitored.
124
+ * This is the only viable way of guaranteeing native button behaviour when using a web component in place of an actual HTML button.
125
+ *
126
+ * TODO: if we need to repeat this logic elsewhere, then we should consider moving this code to a shared class or mixin.
127
+ */
128
+ private _simulateNativeButtonClick;
129
+ private _handleClick;
130
+ private _handleFormKeyDown;
131
+ /**
132
+ * Template for the loading state
133
+ *
134
+ * @private
135
+ */
136
+ private renderSpinner;
137
+ render(): TemplateResult<1>;
138
+ focus(): void;
139
+ static styles: CSSResult;
140
+ }
141
+
142
+ export declare const responsiveSizes: readonly ["productive", "expressive"];
143
+
144
+ export declare const sizes: readonly ["xsmall", "small-productive", "small-expressive", "medium", "large"];
145
+
146
+ export declare const types: readonly ["submit", "button", "reset"];
147
+
148
+ export declare type Variant = typeof variants[number];
149
+
150
+ export declare const variants: readonly ["primary", "secondary", "outline", "outline-inverse", "ghost", "inverse", "ghost-inverse", "destructive", "destructive-ghost"];
151
+
152
+ export { }