Skip to content

Commit

Permalink
refactor!: re-apply button changes (#2411)
Browse files Browse the repository at this point in the history
  • Loading branch information
vursen authored and web-padawan committed Aug 28, 2021
1 parent 709314f commit bad4586
Show file tree
Hide file tree
Showing 15 changed files with 316 additions and 344 deletions.
2 changes: 1 addition & 1 deletion packages/button/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@

END OF TERMS AND CONDITIONS

Copyright 2017 Vaadin Ltd.
Copyright 2021 Vaadin Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
64 changes: 3 additions & 61 deletions packages/button/README.md
Original file line number Diff line number Diff line change
@@ -1,63 +1,5 @@
# <vaadin-button>
# @vaadin/button

[Live Demo ↗](https://vaadin.com/components/vaadin-button/html-examples)
|
[API documentation ↗](https://vaadin.com/components/vaadin-button/html-api)
> ⚠️ Work in progress, please do not use this component yet.
[<vaadin-button>](https://vaadin.com/components/vaadin-button) is a Web Component providing an accessible and customizable button, part of the [Vaadin components](https://vaadin.com/components).

[![npm version](https://badgen.net/npm/v/@vaadin/vaadin-button)](https://www.npmjs.com/package/@vaadin/vaadin-button)
[![Published on Vaadin Directory](https://img.shields.io/badge/Vaadin%20Directory-published-00b4f0.svg)](https://vaadin.com/directory/component/vaadinvaadin-button)
[![Discord](https://img.shields.io/discord/732335336448852018?label=discord)](https://discord.gg/PHmkCKC)

```html
<vaadin-button theme="primary">Primary</vaadin-button>
<vaadin-button theme="secondary">Secondary</vaadin-button>
<vaadin-button theme="tertiary">Tertiary</vaadin-button>
```

[<img src="https://raw.githubusercontent.com/vaadin/vaadin-button/master/screenshot.png" alt="Screenshot of vaadin-button, using the default Lumo theme">](https://vaadin.com/components/vaadin-button)

## Installation

Install `vaadin-button`:

```sh
npm i @vaadin/vaadin-button --save
```

Once installed, import it in your application:

```js
import '@vaadin/vaadin-button/vaadin-button.js';
```

## Getting started

Vaadin components use the Lumo theme by default.

To use the Material theme, import the correspondent file from the `theme/material` folder.

## Entry points

- The component with the Lumo theme:

`theme/lumo/vaadin-button.js`

- The component with the Material theme:

`theme/material/vaadin-button.js`

- Alias for `theme/lumo/vaadin-button.js`:

`vaadin-button.js`

## Contributing

Read the [contributing guide](https://vaadin.com/docs/latest/guide/contributing/overview) to learn about our development process, how to propose bugfixes and improvements, and how to test your changes to Vaadin components.

## License

Apache License 2.0

Vaadin collects development time usage statistics to improve this product. For details and to opt-out, see https://github.com/vaadin/vaadin-usage-statistics.
The new version of `vaadin-button` component.
9 changes: 4 additions & 5 deletions packages/button/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{
"name": "@vaadin/vaadin-button",
"name": "@vaadin/button",
"version": "22.0.0-alpha3",
"description": "vaadin-button",
"main": "vaadin-button.js",
"module": "vaadin-button.js",
"repository": "vaadin/vaadin-button",
"repository": "vaadin/web-components",
"keywords": [
"Vaadin",
"button",
"web-components",
"web-component",
"polymer"
"web-component"
],
"author": "Vaadin Ltd",
"license": "Apache-2.0",
Expand All @@ -26,6 +25,7 @@
],
"dependencies": {
"@polymer/polymer": "^3.0.0",
"@vaadin/field-base": "^22.0.0-alpha3",
"@vaadin/vaadin-control-state-mixin": "^22.0.0-alpha3",
"@vaadin/vaadin-element-mixin": "^22.0.0-alpha3",
"@vaadin/vaadin-lumo-styles": "^22.0.0-alpha3",
Expand All @@ -35,7 +35,6 @@
"devDependencies": {
"@esm-bundle/chai": "^4.3.4",
"@vaadin/testing-helpers": "^0.2.1",
"@vaadin/vaadin-icon": "^22.0.0-alpha3",
"sinon": "^9.2.4"
},
"publishConfig": {
Expand Down
Binary file removed packages/button/screenshot.png
Binary file not shown.
61 changes: 13 additions & 48 deletions packages/button/src/vaadin-button.d.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,18 @@
import { GestureEventListeners } from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';

import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';

import { ControlStateMixin } from '@vaadin/vaadin-control-state-mixin/vaadin-control-state-mixin.js';

import { ElementMixin } from '@vaadin/vaadin-element-mixin/vaadin-element-mixin.js';

/**
* `<vaadin-button>` is a Web Component providing an accessible and customizable button.
*
* ```html
* <vaadin-button>
* </vaadin-button>
* ```
*
* ```js
* document.querySelector('vaadin-button').addEventListener('click', () => alert('Hello World!'));
* ```
*
* ### Styling
*
* The following shadow DOM parts are exposed for styling:
*
* Part name | Description
* ----------------|----------------
* `label` | The label (text) inside the button
* `prefix` | A slot for e.g. an icon before the label
* `suffix` | A slot for e.g. an icon after the label
*
*
* The following attributes are exposed for styling:
*
* Attribute | Description
* --------- | -----------
* `active` | Set when the button is pressed down, either with mouse, touch or the keyboard.
* `disabled` | Set when the button is disabled.
* `focus-ring` | Set when the button is focused using the keyboard.
* `focused` | Set when the button is focused.
*
* See [Styling Components](https://vaadin.com/docs/latest/ds/customization/styling-components) documentation.
* @license
* Copyright (c) 2021 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
declare class ButtonElement extends ElementMixin(ControlStateMixin(ThemableMixin(GestureEventListeners(HTMLElement)))) {
readonly focusElement: Element | null;
}
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { ElementMixin } from '@vaadin/vaadin-element-mixin/vaadin-element-mixin.js';
import { ActiveMixin } from '@vaadin/field-base/src/active-mixin.js';
import { ControlStateMixin } from '@vaadin/vaadin-control-state-mixin/vaadin-control-state-mixin.js';

declare global {
interface HTMLElementTagNameMap {
'vaadin-button': ButtonElement;
}
declare class Button extends ControlStateMixin(ActiveMixin(ElementMixin(ThemableMixin(HTMLElement)))) {
/**
* A getter that returns the native button as a focusable element for ControlStateMixin.
*/
readonly focusElement: HTMLButtonElement | null;
}

export { ButtonElement };
export { Button };
136 changes: 25 additions & 111 deletions packages/button/src/vaadin-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,17 @@
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
import { GestureEventListeners } from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
import { addListener } from '@polymer/polymer/lib/utils/gestures.js';
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { ControlStateMixin } from '@vaadin/vaadin-control-state-mixin/vaadin-control-state-mixin.js';
import { ElementMixin } from '@vaadin/vaadin-element-mixin/vaadin-element-mixin.js';
import { TabindexMixin } from '@vaadin/field-base/src/tabindex-mixin.js';
import { ActiveMixin } from '@vaadin/field-base/src/active-mixin.js';
import { FocusMixin } from '@vaadin/field-base/src/focus-mixin.js';

class Button extends ActiveMixin(TabindexMixin(FocusMixin(ElementMixin(ThemableMixin(PolymerElement))))) {
static get is() {
return 'vaadin-button';
}

/**
* `<vaadin-button>` is a Web Component providing an accessible and customizable button.
*
* ```html
* <vaadin-button>
* </vaadin-button>
* ```
*
* ```js
* document.querySelector('vaadin-button').addEventListener('click', () => alert('Hello World!'));
* ```
*
* ### Styling
*
* The following shadow DOM parts are exposed for styling:
*
* Part name | Description
* ----------------|----------------
* `label` | The label (text) inside the button
* `prefix` | A slot for e.g. an icon before the label
* `suffix` | A slot for e.g. an icon after the label
*
*
* The following attributes are exposed for styling:
*
* Attribute | Description
* --------- | -----------
* `active` | Set when the button is pressed down, either with mouse, touch or the keyboard.
* `disabled` | Set when the button is disabled.
* `focus-ring` | Set when the button is focused using the keyboard.
* `focused` | Set when the button is focused.
*
* See [Styling Components](https://vaadin.com/docs/latest/ds/customization/styling-components) documentation.
*
* @extends HTMLElement
* @mixes ElementMixin
* @mixes ControlStateMixin
* @mixes ThemableMixin
* @mixes GestureEventListeners
*/
class ButtonElement extends ElementMixin(ControlStateMixin(ThemableMixin(GestureEventListeners(PolymerElement)))) {
static get template() {
return html`
<style>
Expand Down Expand Up @@ -81,9 +45,10 @@ class ButtonElement extends ElementMixin(ControlStateMixin(ThemableMixin(Gesture
height: 100%;
min-height: inherit;
text-shadow: inherit;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
background: transparent;
padding: 0;
border: none;
box-shadow: none;
}
[part='prefix'],
Expand All @@ -96,82 +61,31 @@ class ButtonElement extends ElementMixin(ControlStateMixin(ThemableMixin(Gesture
overflow: hidden;
text-overflow: ellipsis;
}
#button {
position: absolute;
top: 0;
left: 0;
right: 0;
width: 100%;
height: 100%;
opacity: 0;
cursor: inherit;
}
</style>
<div class="vaadin-button-container">
<div part="prefix">
<span part="prefix">
<slot name="prefix"></slot>
</div>
<div part="label">
</span>
<span part="label">
<slot></slot>
</div>
<div part="suffix">
</span>
<span part="suffix">
<slot name="suffix"></slot>
</div>
</span>
</div>
<button id="button" type="button"></button>
`;
}

static get is() {
return 'vaadin-button';
}

/** @protected */
ready() {
super.ready();

// Leaving default role in the native button, makes navigation announcement
// being different when using focus navigation (tab) versus using normal
// navigation (arrows). The first way announces the label on a button
// since the focus is moved programmatically, and the second on a group.
this.setAttribute('role', 'button');
this.$.button.setAttribute('role', 'presentation');

this._addActiveListeners();
}

/**
* @protected
*/
disconnectedCallback() {
super.disconnectedCallback();

// `active` state is preserved when the element is disconnected between keydown and keyup events.
// reproducible in `<vaadin-date-picker>` when closing on `Cancel` or `Today` click.
this.toggleAttribute('active', false);
}

/** @private */
_addActiveListeners() {
addListener(this, 'down', () => !this.disabled && this.setAttribute('active', ''));
addListener(this, 'up', () => this.removeAttribute('active'));
this.addEventListener(
'keydown',
(e) => !this.disabled && [13, 32].indexOf(e.keyCode) >= 0 && this.setAttribute('active', '')
);
this.addEventListener('keyup', () => this.removeAttribute('active'));
this.addEventListener('blur', () => this.removeAttribute('active'));
}

/**
* @protected
* @return {Element}
*/
get focusElement() {
return this.$.button;
// By default, if the user hasn't provided a custom role,
// the role attribute is set to "button".
if (!this.hasAttribute('role')) {
this.setAttribute('role', 'button');
}
}
}

customElements.define(ButtonElement.is, ButtonElement);

export { ButtonElement };
export { Button };
Loading

0 comments on commit bad4586

Please sign in to comment.