Skip to content

Commit 036e0a2

Browse files
authored
feat!: use role attribute instead of native button (#2394)
1 parent 1ecac6a commit 036e0a2

7 files changed

Lines changed: 310 additions & 41 deletions

File tree

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
77
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
88
import { ElementMixin } from '@vaadin/vaadin-element-mixin/vaadin-element-mixin.js';
9+
import { TabindexMixin } from '@vaadin/field-base/src/tabindex-mixin.js';
910
import { ActiveMixin } from '@vaadin/field-base/src/active-mixin.js';
10-
import { ControlStateMixin } from '@vaadin/vaadin-control-state-mixin/vaadin-control-state-mixin.js';
11+
import { FocusMixin } from '@vaadin/field-base/src/focus-mixin.js';
1112

12-
class Button extends ControlStateMixin(ActiveMixin(ElementMixin(ThemableMixin(PolymerElement)))) {
13+
class Button extends ActiveMixin(TabindexMixin(FocusMixin(ElementMixin(ThemableMixin(PolymerElement))))) {
1314
static get is() {
1415
return 'vaadin-button';
1516
}
@@ -61,7 +62,7 @@ class Button extends ControlStateMixin(ActiveMixin(ElementMixin(ThemableMixin(Po
6162
text-overflow: ellipsis;
6263
}
6364
</style>
64-
<button id="button" type="button" part="button">
65+
<div part="button">
6566
<span part="prefix">
6667
<slot name="prefix"></slot>
6768
</span>
@@ -71,19 +72,19 @@ class Button extends ControlStateMixin(ActiveMixin(ElementMixin(ThemableMixin(Po
7172
<span part="suffix">
7273
<slot name="suffix"></slot>
7374
</span>
74-
</button>
75+
</div>
7576
`;
7677
}
7778

78-
/**
79-
* A getter that returns the native button as a focusable element for ControlStateMixin.
80-
*
81-
* @protected
82-
* @override
83-
* @return {HTMLButtonElement}
84-
*/
85-
get focusElement() {
86-
return this.$.button;
79+
/** @protected */
80+
ready() {
81+
super.ready();
82+
83+
// By default, if the user hasn't provided a custom role,
84+
// the role attribute is set to "button".
85+
if (!this.hasAttribute('role')) {
86+
this.setAttribute('role', 'button');
87+
}
8788
}
8889
}
8990

‎packages/button/test/button.test.js‎

Lines changed: 127 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,70 @@ describe('vaadin-button', () => {
3838
});
3939
});
4040

41-
describe('default', () => {
42-
let button, label;
41+
describe('role', () => {
42+
describe('default', () => {
43+
beforeEach(() => {
44+
element = fixtureSync('<vaadin-button>Press me</vaadin-button>');
45+
});
4346

44-
beforeEach(() => {
45-
element = fixtureSync('<vaadin-button>Vaadin <i>Button</i></vaadin-button>');
46-
button = element.$.button;
47-
label = element.shadowRoot.querySelector('[part=label]');
47+
it('should set role attribute to button by default', () => {
48+
expect(element.getAttribute('role')).to.equal('button');
49+
});
4850
});
4951

50-
it('should have [type=button] on the native button', () => {
51-
expect(button.getAttribute('type')).to.be.eql('button');
52+
describe('custom', () => {
53+
beforeEach(() => {
54+
element = fixtureSync('<vaadin-button role="menuitem">Press me</vaadin-button>');
55+
});
56+
57+
it('should not override custom role attribute', () => {
58+
expect(element.getAttribute('role')).to.equal('menuitem');
59+
});
60+
});
61+
});
62+
63+
describe('label', () => {
64+
let label;
65+
66+
beforeEach(() => {
67+
element = fixtureSync('<vaadin-button>Press me</vaadin-button>');
68+
label = element.shadowRoot.querySelector('[part=label]');
5269
});
5370

5471
it('should define the button label using light DOM', () => {
5572
const children = FlattenedNodesObserver.getFlattenedNodes(label);
56-
expect(children[1].textContent).to.be.equal('Vaadin ');
57-
expect(children[2].outerHTML).to.be.equal('<i>Button</i>');
73+
expect(children[1].textContent).to.be.equal('Press me');
5874
});
75+
});
5976

60-
it('should disable the native button', () => {
61-
element.disabled = true;
62-
expect(button.hasAttribute('disabled')).to.be.true;
77+
describe('mixins', () => {
78+
beforeEach(() => {
79+
element = fixtureSync('<vaadin-button>Press me</vaadin-button>');
6380
});
6481

65-
// TODO: Remove when it will be possible to detect whether the button element inherits ActiveMixin.
66-
// A related issue: https://github.com/vaadin/web-components/issues/2357
67-
describe('active', () => {
82+
// TODO: Remove when it would be possible for an element:
83+
// – to detect if it inherits DisabledMixin.
84+
// – or to run a suit of the tests defined in DisabledMixin.
85+
describe('DisabledMixin', () => {
86+
it('should set disabled property to false by default', () => {
87+
expect(element.disabled).to.be.false;
88+
});
89+
90+
it('should reflect disabled property to attribute', () => {
91+
element.disabled = true;
92+
expect(element.hasAttribute('disabled')).to.be.true;
93+
});
94+
95+
it('should set the aria-disabled attribute when disabled', () => {
96+
element.disabled = true;
97+
expect(element.getAttribute('aria-disabled')).to.equal('true');
98+
});
99+
});
100+
101+
// TODO: Remove when it would be possible for an element:
102+
// – to detect if it inherits ActiveMixin.
103+
// – or to run a suit of the tests defined in ActiveMixin.
104+
describe('ActiveMixin', () => {
68105
(isIOS ? it.skip : it)('should have active attribute on mousedown', () => {
69106
mousedown(element);
70107
expect(element.hasAttribute('active')).to.be.true;
@@ -135,23 +172,85 @@ describe('vaadin-button', () => {
135172
});
136173
});
137174

138-
describe('focus-ring', () => {
139-
beforeEach(async () => {
140-
// Focus on the button
141-
await sendKeys({ press: 'Tab' });
175+
// TODO: Remove when it would be possible for an element:
176+
// – to detect if it inherits TabindexMixin.
177+
// – or to run a suit of the tests defined in TabindexMixin.
178+
describe('TabindexMixin', () => {
179+
it('should set tabindex attribute to 0 by default', () => {
180+
expect(element.getAttribute('tabindex')).to.be.equal('0');
181+
});
182+
183+
it('should reflect tabindex property to the attribute', () => {
184+
element.tabindex = 1;
185+
expect(element.getAttribute('tabindex')).to.be.equal('1');
142186
});
143187

144-
it('should set focus-ring attribute when focusing with Tab', () => {
145-
expect(element.hasAttribute('focus-ring')).to.be.true;
188+
it('should reflect native tabIndex property to the attribute', () => {
189+
element.tabIndex = 1;
190+
expect(element.getAttribute('tabindex')).to.be.equal('1');
146191
});
147192

148-
it('should remove focus-ring attribute when loosing focus with Shift+Tab', async () => {
149-
// Focus out of the button
150-
await sendKeys({ down: 'Shift' });
151-
await sendKeys({ press: 'Tab' });
152-
await sendKeys({ up: 'Shift' });
193+
it('should set tabindex attribute to -1 when disabled', () => {
194+
element.tabIndex = 1;
195+
element.disabled = true;
196+
expect(element.getAttribute('tabindex')).to.be.equal('-1');
197+
});
198+
199+
it('should restore tabindex attribute when enabled', () => {
200+
element.tabIndex = 1;
201+
element.disabled = true;
202+
element.disabled = false;
203+
expect(element.getAttribute('tabindex')).to.be.equal('1');
204+
});
205+
206+
it('should restore tabindex attribute with the last known value when enabled', () => {
207+
element.tabIndex = 1;
208+
element.disabled = true;
209+
element.tabIndex = 2;
210+
expect(element.getAttribute('tabindex')).to.be.equal('-1');
211+
212+
element.disabled = false;
213+
expect(element.getAttribute('tabindex')).to.be.equal('2');
214+
});
215+
});
216+
217+
// TODO: Remove when it would be possible for an element:
218+
// – to detect if it inherits FocusMixin.
219+
// – or to run a suit of the tests defined in FocusMixin.
220+
describe('FocusMixin', () => {
221+
describe('focusing with Tab', () => {
222+
beforeEach(async () => {
223+
// Focus on the button
224+
await sendKeys({ press: 'Tab' });
225+
});
226+
227+
it('should set focused attribute', () => {
228+
expect(element.hasAttribute('focused')).to.be.true;
229+
});
230+
231+
it('should set focus-ring attribute', () => {
232+
expect(element.hasAttribute('focus-ring')).to.be.true;
233+
});
234+
});
235+
236+
describe('loosing focus with Shift+Tab', () => {
237+
beforeEach(async () => {
238+
// Focus on the button
239+
await sendKeys({ press: 'Tab' });
240+
241+
// Focus out of the button
242+
await sendKeys({ down: 'Shift' });
243+
await sendKeys({ press: 'Tab' });
244+
await sendKeys({ up: 'Shift' });
245+
});
246+
247+
it('should remove focused attribute', () => {
248+
expect(element.hasAttribute('focused')).to.be.false;
249+
});
153250

154-
expect(element.hasAttribute('focus-ring')).to.be.false;
251+
it('should remove focus-ring attribute', () => {
252+
expect(element.hasAttribute('focus-ring')).to.be.false;
253+
});
155254
});
156255
});
157256
});

‎packages/field-base/index.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ export { SlotMixin } from './src/slot-mixin.js';
1616
export { TextAreaSlotMixin } from './src/text-area-slot-mixin.js';
1717
export { TextFieldMixin } from './src/text-field-mixin.js';
1818
export { ValidateMixin } from './src/validate-mixin.js';
19+
export { TabindexMixin } from './src/tabindex-mixin.js';
20+
export { ActiveMixin } from './src/active-mixin.js';

‎packages/field-base/index.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ export { SlotMixin } from './src/slot-mixin.js';
1616
export { TextAreaSlotMixin } from './src/text-area-slot-mixin.js';
1717
export { TextFieldMixin } from './src/text-field-mixin.js';
1818
export { ValidateMixin } from './src/validate-mixin.js';
19+
export { TabindexMixin } from './src/tabindex-mixin.js';
20+
export { ActiveMixin } from './src/active-mixin.js';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2021 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import { DisabledMixin } from './disabled-mixin.js';
7+
8+
/**
9+
* A mixin to provide the `tabindex` attribute.
10+
*
11+
* By default, the attribute is set to 0 that makes the element focusable.
12+
*
13+
* The attribute is removed whenever the user disables the element
14+
* and restored with the last known value once the element is enabled again.
15+
*/
16+
declare function TabindexMixin<T extends new (...args: any[]) => {}>(base: T): T & TabindexMixinConstructor;
17+
18+
interface TabindexMixinConstructor {
19+
new (...args: any[]): TabindexMixin;
20+
}
21+
22+
interface TabindexMixin extends DisabledMixin {
23+
/**
24+
* Indicates whether the element can be focused and where it participates in sequential keyboard navigation.
25+
*/
26+
tabindex: number | undefined | null;
27+
}
28+
29+
export { TabindexMixinConstructor, TabindexMixin };
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2021 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
7+
import { DisabledMixin } from './disabled-mixin.js';
8+
9+
const TabindexMixinImplementation = (superclass) =>
10+
class TabindexMixinClass extends DisabledMixin(superclass) {
11+
static get properties() {
12+
return {
13+
/**
14+
* Indicates whether the element can be focused and where it participates in sequential keyboard navigation.
15+
*/
16+
tabindex: {
17+
type: Number,
18+
value: 0,
19+
reflectToAttribute: true,
20+
observer: '_tabindexChanged'
21+
},
22+
23+
/**
24+
* Stores the last known tabindex since the element has been disabled.
25+
*
26+
* @private
27+
*/
28+
__lastTabIndex: {
29+
type: Number,
30+
value: 0
31+
}
32+
};
33+
}
34+
35+
/**
36+
* When the element gets disabled, the observer saves the last known tabindex
37+
* and makes the element not focusable by setting tabindex to -1.
38+
* As soon as the element gets enabled, the observer restores the last known tabindex
39+
* so that the element can be focusable again.
40+
*
41+
* @protected
42+
* @override
43+
*/
44+
_disabledChanged(disabled) {
45+
super._disabledChanged(disabled);
46+
47+
if (disabled) {
48+
this.__lastTabIndex = this.tabindex;
49+
this.tabindex = -1;
50+
} else {
51+
this.tabindex = this.__lastTabIndex;
52+
}
53+
}
54+
55+
/**
56+
* When the user has changed tabindex while the element is disabled,
57+
* the observer reverts tabindex to -1 and rather saves the new tabindex value to apply it later.
58+
* The new value will be applied as soon as the element becomes enabled.
59+
*
60+
* @protected
61+
*/
62+
_tabindexChanged(tabindex) {
63+
if (this.disabled && tabindex !== -1) {
64+
this.__lastTabIndex = tabindex;
65+
this.tabindex = -1;
66+
}
67+
}
68+
};
69+
70+
/**
71+
* A mixin to provide the `tabindex` attribute.
72+
*
73+
* By default, the attribute is set to 0 that makes the element focusable.
74+
*
75+
* The attribute is removed whenever the user disables the element
76+
* and restored with the last known value once the element is enabled.
77+
*/
78+
export const TabindexMixin = dedupingMixin(TabindexMixinImplementation);

0 commit comments

Comments
 (0)