Skip to content

Commit 20e0bf1

Browse files
authored
feat: add focus-ring attribute to vaadin-upload-file (#3156)
1 parent 1352f4b commit 20e0bf1

5 files changed

Lines changed: 129 additions & 33 deletions

File tree

‎packages/upload/src/vaadin-upload-file.js‎

Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import '@vaadin/progress-bar/src/vaadin-progress-bar.js';
77
import './vaadin-upload-icons.js';
88
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
9+
import { FocusMixin } from '@vaadin/component-base/src/focus-mixin.js';
910
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
1011

1112
/**
@@ -15,36 +16,40 @@ import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mix
1516
*
1617
* The following shadow DOM parts are available for styling:
1718
*
18-
* Part name | Description
19-
* ---|---
20-
* `row` | File container
21-
* `info` | Container for file status icon, file name, status and error messages
22-
* `done-icon` | File done status icon
23-
* `warning-icon` | File warning status icon
24-
* `meta` | Container for file name, status and error messages
25-
* `name` | File name
26-
* `error` | Error message, shown when error happens
27-
* `status` | Status message
28-
* `commands` | Container for file command icons
29-
* `start-button` | Start file upload button
30-
* `retry-button` | Retry file upload button
31-
* `remove-button` | Remove file button
32-
* `progress`| Progress bar
19+
* Part name | Description
20+
* -----------------|-------------
21+
* `row` | File container
22+
* `info` | Container for file status icon, file name, status and error messages
23+
* `done-icon` | File done status icon
24+
* `warning-icon` | File warning status icon
25+
* `meta` | Container for file name, status and error messages
26+
* `name` | File name
27+
* `error` | Error message, shown when error happens
28+
* `status` | Status message
29+
* `commands` | Container for file command buttons
30+
* `start-button` | Start file upload button
31+
* `retry-button` | Retry file upload button
32+
* `remove-button` | Remove file button
33+
* `progress` | Progress bar
3334
*
3435
* The following state attributes are available for styling:
3536
*
36-
* Attribute | Description | Part name
37-
* ---|---|---
38-
* `error` | An error has happened during uploading | `:host`
39-
* `indeterminate` | Uploading is in progress, but the progress value is unknown | `:host`
40-
* `uploading` | Uploading is in progress | `:host`
41-
* `complete` | Uploading has finished successfully | `:host`
37+
* Attribute | Description
38+
* -----------------|-------------
39+
* `focus-ring` | Set when the element is focused using the keyboard.
40+
* `focused` | Set when the element is focused.
41+
* `error` | An error has happened during uploading.
42+
* `indeterminate` | Uploading is in progress, but the progress value is unknown.
43+
* `uploading` | Uploading is in progress.
44+
* `complete` | Uploading has finished successfully.
4245
*
4346
* See [Styling Components](https://vaadin.com/docs/latest/ds/customization/styling-components) documentation.
4447
*
48+
* @extends HTMLElement
49+
* @mixes FocusMixin
4550
* @mixes ThemableMixin
4651
*/
47-
class UploadFile extends ThemableMixin(PolymerElement) {
52+
class UploadFile extends FocusMixin(ThemableMixin(PolymerElement)) {
4853
static get template() {
4954
return html`
5055
<style>
@@ -129,7 +134,17 @@ class UploadFile extends ThemableMixin(PolymerElement) {
129134
return {
130135
file: Object,
131136

132-
i18n: Object
137+
i18n: Object,
138+
139+
/**
140+
* Indicates whether the element can be focused and where it participates in sequential keyboard navigation.
141+
* @protected
142+
*/
143+
tabindex: {
144+
type: Number,
145+
value: 0,
146+
reflectToAttribute: true
147+
}
133148
};
134149
}
135150

@@ -143,6 +158,38 @@ class UploadFile extends ThemableMixin(PolymerElement) {
143158
];
144159
}
145160

161+
/** @protected */
162+
ready() {
163+
super.ready();
164+
165+
// Handle moving focus to the button on Tab.
166+
this.shadowRoot.addEventListener('focusin', (e) => {
167+
const target = e.composedPath()[0];
168+
169+
if (target.getAttribute('part').endsWith('button')) {
170+
this._setFocused(false);
171+
}
172+
});
173+
174+
// Handle moving focus from the button on Shift Tab.
175+
this.shadowRoot.addEventListener('focusout', (e) => {
176+
if (e.relatedTarget === this) {
177+
this._setFocused(true);
178+
}
179+
});
180+
}
181+
182+
/**
183+
* Override method inherited from `FocusMixin` to mark the file as focused
184+
* only when the host is focused.
185+
* @param {Event} event
186+
* @return {boolean}
187+
* @protected
188+
*/
189+
_shouldSetFocus(event) {
190+
return event.composedPath()[0] === this;
191+
}
192+
146193
/** @private */
147194
_fileAborted(abort) {
148195
if (abort) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class Upload extends ElementMixin(ThemableMixin(PolymerElement)) {
106106
<ul id="fileList" part="file-list">
107107
<template is="dom-repeat" items="[[files]]" as="file">
108108
<li>
109-
<vaadin-upload-file tabindex="0" file="[[file]]" i18n="[[i18n]]"></vaadin-upload-file>
109+
<vaadin-upload-file file="[[file]]" i18n="[[i18n]]"></vaadin-upload-file>
110110
</li>
111111
</template>
112112
</ul>

‎packages/upload/test/file.test.js‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from '@esm-bundle/chai';
22
import { fixtureSync } from '@vaadin/testing-helpers';
3+
import { sendKeys } from '@web/test-runner-commands';
34
import '../vaadin-upload.js';
45
import { createFile } from './common.js';
56

@@ -49,4 +50,55 @@ describe('<vaadin-upload-file> element', () => {
4950
expect(fileElement.hasAttribute('error')).to.be.true;
5051
});
5152
});
53+
54+
describe('focus', () => {
55+
beforeEach(() => {
56+
// Show the "Start" button
57+
fileElement.set('file.held', true);
58+
});
59+
60+
it('should not add focus-ring to the host on programmatic focus', () => {
61+
fileElement.focus();
62+
expect(fileElement.hasAttribute('focus-ring')).to.be.false;
63+
});
64+
65+
it('should add focus-ring to the host on keyboard focus', async () => {
66+
await sendKeys({ press: 'Tab' });
67+
expect(fileElement.hasAttribute('focus-ring')).to.be.true;
68+
});
69+
70+
it('should remove focus-ring when a button is focused', async () => {
71+
await sendKeys({ press: 'Tab' });
72+
73+
// Focus the button
74+
await sendKeys({ press: 'Tab' });
75+
76+
expect(fileElement.hasAttribute('focus-ring')).to.be.false;
77+
});
78+
79+
it('should restore focus-ring when focus moves back', async () => {
80+
const button = fileElement.shadowRoot.querySelector('button');
81+
button.focus();
82+
83+
// Move focus back to the upload file.
84+
await sendKeys({ down: 'Shift' });
85+
await sendKeys({ press: 'Tab' });
86+
await sendKeys({ up: 'Shift' });
87+
88+
expect(fileElement.hasAttribute('focus-ring')).to.be.true;
89+
});
90+
91+
it('should not set focus-ring on Shift Tab to button', async () => {
92+
const button = fileElement.shadowRoot.querySelector('[part="remove-button"]');
93+
button.focus();
94+
await sendKeys({ press: 'Tab' });
95+
96+
// Move focus back to the button.
97+
await sendKeys({ down: 'Shift' });
98+
await sendKeys({ press: 'Tab' });
99+
await sendKeys({ up: 'Shift' });
100+
101+
expect(fileElement.hasAttribute('focus-ring')).to.be.false;
102+
});
103+
});
52104
});

‎packages/upload/theme/lumo/vaadin-upload-styles.js‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,10 @@ registerStyles(
7373
const uploadFile = css`
7474
:host {
7575
padding: var(--lumo-space-s) 0;
76-
}
77-
78-
:host(:focus) {
7976
outline: none;
8077
}
8178
82-
:host(:focus) [part='row'] {
79+
:host([focus-ring]) [part='row'] {
8380
border-radius: var(--lumo-border-radius-s);
8481
box-shadow: 0 0 0 2px var(--lumo-primary-color-50pct);
8582
}

‎packages/upload/theme/material/vaadin-upload-styles.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,18 @@ registerStyles(
9999
registerStyles(
100100
'vaadin-upload-file',
101101
css`
102+
:host {
103+
outline: none;
104+
}
105+
102106
[part='row'] {
103107
display: flex;
104108
align-items: flex-start;
105109
justify-content: space-between;
106110
padding: 8px;
107111
}
108112
109-
:host(:focus) {
110-
outline: none;
111-
}
112-
113-
:host(:focus) [part='row'] {
113+
:host([focus-ring]) [part='row'] {
114114
background-color: var(--material-divider-color);
115115
}
116116

0 commit comments

Comments
 (0)