--- title: 'Button: Use Cases' parts: - Use Cases - Button eleventyNavigation: key: Button >> Use Cases title: Use Cases order: 20 parent: Button --- # Button: Use Cases ```js script import { html } from '@mdjs/mdjs-preview'; import '@lion/ui/define/lion-button.js'; import '@lion/ui/define/lion-button-reset.js'; import '@lion/ui/define/lion-button-submit.js'; ``` ## With click handler ```js preview-story export const handler = () => html` Click | Space | Enter me and see log `; ``` ## Disabled button ```js preview-story export const disabled = () => html`Disabled`; ``` ## Minimum click area The minimum click area needs to be at least `44px` by `44px` according to [WCAG Success Criterion 2.5.5 Target Size (Enhanced)](https://www.w3.org/TR/WCAG22/#target-size-enhanced). ```js preview-story export const minimumClickArea = () => html` xs`; ``` ## Usage with native form `` and `` are especially created to supports the following use cases: - Submit on button click - Submit on button enter or space keypress - Submit on enter keypress inside an input - Reset native form fields when using type="reset" ```js preview-story export const withinForm = () => html`
{ ev.preventDefault(); console.log('submit handler', ev.target); }} > console.log('click submit handler', ev.target)} >Submit console.log('click reset handler', ev.target)} >Reset
`; ``` Important notes: - A `` is mandatory for the last use case, if you have multiple inputs. This is native behaviour. - `@click` on `` and `@submit` on `
` are triggered by these use cases. We strongly encourage you to listen to the submit handler if your goal is to do something on form-submit. - To prevent form submission full page reloads, add a **submit handler on the form** `@submit` with `event.preventDefault()`. Adding it on the `` is not enough. ## Considerations ### Why a Web Component? There are multiple reasons why we used a Web Component as opposed to a CSS component. - **Target size**: The minimum target size is 40 pixels, which makes even the small buttons easy to activate. A container element was needed to make this size possible. - **Advanced styling**: There are advanced styling options regarding icons in buttons, where it is a lot more maintainable to handle icons in our button using slots. An example is that a sticky icon-only buttons may looks different from buttons which have both icons and text. - **Native form integration**: The `` works with native `` submission, and even implicit form submission on-enter. A lot of delegation logic had to be created for this to work. ### Event target We want to ensure that the event target returned to the user is ``, not `