Skip to content

Commit 9c232d5

Browse files
web-padawanvursen
andauthored
refactor: update select to not use button (#3612)
Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>
1 parent dc8cca2 commit 9c232d5

11 files changed

Lines changed: 181 additions & 137 deletions

‎packages/button/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"web-component"
3333
],
3434
"dependencies": {
35+
"@open-wc/dedupe-mixin": "^1.3.0",
3536
"@polymer/polymer": "^3.0.0",
3637
"@vaadin/component-base": "23.1.0-alpha1",
3738
"@vaadin/vaadin-lumo-styles": "23.1.0-alpha1",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2017 - 2022 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import { Constructor } from '@open-wc/dedupe-mixin';
7+
import { ActiveMixinClass } from '@vaadin/component-base/src/active-mixin.js';
8+
import { DisabledMixinClass } from '@vaadin/component-base/src/disabled-mixin.js';
9+
import { FocusMixinClass } from '@vaadin/component-base/src/focus-mixin.js';
10+
import { KeyboardMixinClass } from '@vaadin/component-base/src/keyboard-mixin.js';
11+
import { TabindexMixinClass } from '@vaadin/component-base/src/tabindex-mixin.js';
12+
13+
/**
14+
* A mixin providing common button functionality.
15+
*/
16+
export declare function ButtonMixin<T extends Constructor<HTMLElement>>(
17+
base: T
18+
): T &
19+
Constructor<ActiveMixinClass> &
20+
Constructor<DisabledMixinClass> &
21+
Constructor<FocusMixinClass> &
22+
Constructor<KeyboardMixinClass> &
23+
Constructor<TabindexMixinClass>;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2017 - 2022 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import { ActiveMixin } from '@vaadin/component-base/src/active-mixin.js';
7+
import { FocusMixin } from '@vaadin/component-base/src/focus-mixin.js';
8+
import { TabindexMixin } from '@vaadin/component-base/src/tabindex-mixin.js';
9+
10+
/**
11+
* A mixin providing common button functionality.
12+
*
13+
* @polymerMixin
14+
* @mixes ActiveMixin
15+
* @mixes FocusMixin
16+
* @mixes TabindexMixin
17+
*/
18+
export const ButtonMixin = (superClass) =>
19+
class ButtonMixinClass extends ActiveMixin(TabindexMixin(FocusMixin(superClass))) {
20+
static get properties() {
21+
return {
22+
/**
23+
* Indicates whether the element can be focused and where it participates in sequential keyboard navigation.
24+
*
25+
* @override
26+
* @protected
27+
*/
28+
tabindex: {
29+
value: 0
30+
}
31+
};
32+
}
33+
34+
/**
35+
* By default, `Space` is the only possible activation key for a focusable HTML element.
36+
* Nonetheless, the button is an exception as it can be also activated by pressing `Enter`.
37+
* See the "Keyboard Support" section in https://www.w3.org/TR/wai-aria-practices/examples/button/button.html.
38+
*
39+
* @protected
40+
* @override
41+
*/
42+
get _activeKeys() {
43+
return ['Enter', ' '];
44+
}
45+
46+
/** @protected */
47+
ready() {
48+
super.ready();
49+
50+
// By default, if the user hasn't provided a custom role,
51+
// the role attribute is set to "button".
52+
if (!this.hasAttribute('role')) {
53+
this.setAttribute('role', 'button');
54+
}
55+
}
56+
57+
/**
58+
* Since the button component is designed on the base of the `[role=button]` attribute,
59+
* and doesn't have a native <button> inside, in order to be fully accessible from the keyboard,
60+
* it should manually fire the `click` event once an activation key is pressed,
61+
* as it follows from the WAI-ARIA specifications:
62+
* https://www.w3.org/TR/wai-aria-practices-1.1/#button
63+
*
64+
* According to the UI Events specifications,
65+
* the `click` event should be fired exactly on `keydown`:
66+
* https://www.w3.org/TR/uievents/#event-type-keydown
67+
*
68+
* @param {KeyboardEvent} event
69+
* @protected
70+
* @override
71+
*/
72+
_onKeyDown(event) {
73+
super._onKeyDown(event);
74+
75+
if (this._activeKeys.includes(event.key)) {
76+
event.preventDefault();
77+
78+
// `DisabledMixin` overrides the standard `click()` method
79+
// so that it doesn't fire the `click` event when the element is disabled.
80+
this.click();
81+
}
82+
}
83+
};

‎packages/button/src/vaadin-button.d.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
* Copyright (c) 2017 - 2022 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6-
import { ActiveMixin } from '@vaadin/component-base/src/active-mixin.js';
76
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
8-
import { FocusMixin } from '@vaadin/component-base/src/focus-mixin.js';
9-
import { TabindexMixin } from '@vaadin/component-base/src/tabindex-mixin.js';
107
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
8+
import { ButtonMixin } from './vaadin-button-mixin.js';
119

1210
/**
1311
* `<vaadin-button>` is an accessible and customizable button that allows users to perform actions.
@@ -37,7 +35,7 @@ import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mix
3735
*
3836
* See [Styling Components](https://vaadin.com/docs/latest/ds/customization/styling-components) documentation.
3937
*/
40-
declare class Button extends ActiveMixin(TabindexMixin(FocusMixin(ElementMixin(ThemableMixin(HTMLElement))))) {}
38+
declare class Button extends ButtonMixin(ElementMixin(ThemableMixin(HTMLElement))) {}
4139

4240
declare global {
4341
interface HTMLElementTagNameMap {

‎packages/button/src/vaadin-button.js‎

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
7-
import { ActiveMixin } from '@vaadin/component-base/src/active-mixin.js';
87
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
9-
import { FocusMixin } from '@vaadin/component-base/src/focus-mixin.js';
10-
import { TabindexMixin } from '@vaadin/component-base/src/tabindex-mixin.js';
118
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
9+
import { ButtonMixin } from './vaadin-button-mixin.js';
1210

1311
/**
1412
* `<vaadin-button>` is an accessible and customizable button that allows users to perform actions.
@@ -39,31 +37,15 @@ import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mix
3937
* See [Styling Components](https://vaadin.com/docs/latest/ds/customization/styling-components) documentation.
4038
*
4139
* @extends HTMLElement
42-
* @mixes ActiveMixin
43-
* @mixes TabindexMixin
44-
* @mixes FocusMixin
40+
* @mixes ButtonMixin
4541
* @mixes ElementMixin
4642
* @mixes ThemableMixin
4743
*/
48-
class Button extends ActiveMixin(TabindexMixin(FocusMixin(ElementMixin(ThemableMixin(PolymerElement))))) {
44+
class Button extends ButtonMixin(ElementMixin(ThemableMixin(PolymerElement))) {
4945
static get is() {
5046
return 'vaadin-button';
5147
}
5248

53-
static get properties() {
54-
return {
55-
/**
56-
* Indicates whether the element can be focused and where it participates in sequential keyboard navigation.
57-
*
58-
* @override
59-
* @protected
60-
*/
61-
tabindex: {
62-
value: 0
63-
}
64-
};
65-
}
66-
6749
static get template() {
6850
return html`
6951
<style>
@@ -125,56 +107,6 @@ class Button extends ActiveMixin(TabindexMixin(FocusMixin(ElementMixin(ThemableM
125107
</div>
126108
`;
127109
}
128-
129-
/**
130-
* By default, `Space` is the only possible activation key for a focusable HTML element.
131-
* Nonetheless, the button is an exception as it can be also activated by pressing `Enter`.
132-
* See the "Keyboard Support" section in https://www.w3.org/TR/wai-aria-practices/examples/button/button.html.
133-
*
134-
* @protected
135-
* @override
136-
*/
137-
get _activeKeys() {
138-
return ['Enter', ' '];
139-
}
140-
141-
/** @protected */
142-
ready() {
143-
super.ready();
144-
145-
// By default, if the user hasn't provided a custom role,
146-
// the role attribute is set to "button".
147-
if (!this.hasAttribute('role')) {
148-
this.setAttribute('role', 'button');
149-
}
150-
}
151-
152-
/**
153-
* Since the button component is designed on the base of the `[role=button]` attribute,
154-
* and doesn't have a native <button> inside, in order to be fully accessible from the keyboard,
155-
* it should manually fire the `click` event once an activation key is pressed,
156-
* as it follows from the WAI-ARIA specifications:
157-
* https://www.w3.org/TR/wai-aria-practices-1.1/#button
158-
*
159-
* According to the UI Events specifications,
160-
* the `click` event should be fired exactly on `keydown`:
161-
* https://www.w3.org/TR/uievents/#event-type-keydown
162-
*
163-
* @param {KeyboardEvent} event
164-
* @protected
165-
* @override
166-
*/
167-
_onKeyDown(event) {
168-
super._onKeyDown(event);
169-
170-
if (this._activeKeys.includes(event.key)) {
171-
event.preventDefault();
172-
173-
// `DisabledMixin` overrides the standard `click()` method
174-
// so that it doesn't fire the `click` event when the element is disabled.
175-
this.click();
176-
}
177-
}
178110
}
179111

180112
customElements.define(Button.is, Button);

‎packages/select/src/vaadin-select-value-button.js‎

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,75 @@
33
* Copyright (c) 2017 - 2022 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6-
import { Button } from '@vaadin/button/src/vaadin-button.js';
7-
import { css, registerStyles } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
8-
9-
registerStyles(
10-
'vaadin-select-value-button',
11-
css`
12-
:host {
13-
margin: 0;
14-
min-width: 0;
15-
width: 0;
16-
height: auto;
17-
}
18-
19-
::slotted(:not([slot])) {
20-
padding-left: 0;
21-
padding-right: 0;
22-
flex: auto;
23-
}
24-
25-
/* placeholder styles */
26-
::slotted(:not([slot]):not([selected])) {
27-
line-height: 1;
28-
}
29-
30-
/* TODO: unsupported selector */
31-
.vaadin-button-container {
32-
text-align: inherit;
33-
}
34-
35-
[part='label'] {
36-
width: 100%;
37-
padding: 0;
38-
line-height: inherit;
39-
}
40-
`,
41-
{ moduleId: 'vaadin-select-value-button-styles' }
42-
);
6+
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
7+
import { ButtonMixin } from '@vaadin/button/src/vaadin-button-mixin.js';
8+
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
439

4410
/**
4511
* An element used internally by `<vaadin-select>`. Not intended to be used separately.
4612
*
47-
* @extends Button
13+
* @extends HTMLElement
14+
* @mixes ButtonMixin
15+
* @mixes ThemableMixin
4816
* @protected
4917
*/
50-
class SelectValueButton extends Button {
18+
class SelectValueButton extends ButtonMixin(ThemableMixin(PolymerElement)) {
5119
static get is() {
5220
return 'vaadin-select-value-button';
5321
}
22+
23+
static get template() {
24+
return html`
25+
<style>
26+
:host {
27+
display: inline-block;
28+
position: relative;
29+
outline: none;
30+
white-space: nowrap;
31+
-webkit-user-select: none;
32+
-moz-user-select: none;
33+
user-select: none;
34+
min-width: 0;
35+
width: 0;
36+
}
37+
38+
::slotted(*) {
39+
padding-left: 0;
40+
padding-right: 0;
41+
flex: auto;
42+
}
43+
44+
/* placeholder styles */
45+
::slotted(*:not([selected])) {
46+
line-height: 1;
47+
}
48+
49+
.vaadin-button-container {
50+
display: inline-flex;
51+
align-items: center;
52+
justify-content: center;
53+
text-align: inherit;
54+
width: 100%;
55+
height: 100%;
56+
min-height: inherit;
57+
text-shadow: inherit;
58+
}
59+
60+
[part='label'] {
61+
white-space: nowrap;
62+
overflow: hidden;
63+
text-overflow: ellipsis;
64+
width: 100%;
65+
line-height: inherit;
66+
}
67+
</style>
68+
<div class="vaadin-button-container">
69+
<span part="label">
70+
<slot></slot>
71+
</span>
72+
</div>
73+
`;
74+
}
5475
}
5576

5677
customElements.define(SelectValueButton.is, SelectValueButton);

‎packages/select/src/vaadin-select.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ class Select extends DelegateFocusMixin(FieldMixin(SlotMixin(ElementMixin(Themab
142142
<style>
143143
::slotted([slot='value']) {
144144
flex-grow: 1;
145-
background-color: transparent;
146145
}
147146
</style>
148147

0 commit comments

Comments
 (0)