Skip to content

Commit 92ef23b

Browse files
authored
refactor!: move upload file progress bar to light DOM (#4883)
1 parent a3fef5c commit 92ef23b

5 files changed

Lines changed: 60 additions & 25 deletions

File tree

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
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 { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
910
import { FocusMixin } from '@vaadin/component-base/src/focus-mixin.js';
11+
import { SlotController } from '@vaadin/component-base/src/slot-controller.js';
1012
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
1113

1214
/**
@@ -30,7 +32,6 @@ import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mix
3032
* `start-button` | Start file upload button
3133
* `retry-button` | Retry file upload button
3234
* `remove-button` | Remove file button
33-
* `progress` | Progress bar
3435
*
3536
* The following state attributes are available for styling:
3637
*
@@ -46,10 +47,11 @@ import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mix
4647
* See [Styling Components](https://vaadin.com/docs/latest/styling/custom-theme/styling-components) documentation.
4748
*
4849
* @extends HTMLElement
50+
* @mixes ControllerMixin
4951
* @mixes FocusMixin
5052
* @mixes ThemableMixin
5153
*/
52-
class UploadFile extends FocusMixin(ThemableMixin(PolymerElement)) {
54+
class UploadFile extends FocusMixin(ThemableMixin(ControllerMixin(PolymerElement))) {
5355
static get template() {
5456
return html`
5557
<style>
@@ -72,8 +74,8 @@ class UploadFile extends FocusMixin(ThemableMixin(PolymerElement)) {
7274
box-shadow: none;
7375
}
7476
75-
:host([complete]) [part='progress'],
76-
:host([error]) [part='progress'] {
77+
:host([complete]) ::slotted([slot='progress']),
78+
:host([error]) ::slotted([slot='progress']) {
7779
display: none !important;
7880
}
7981
</style>
@@ -119,12 +121,7 @@ class UploadFile extends FocusMixin(ThemableMixin(PolymerElement)) {
119121
</div>
120122
</div>
121123
122-
<vaadin-progress-bar
123-
part="progress"
124-
id="progress"
125-
value$="[[_formatProgressValue(file.progress)]]"
126-
indeterminate$="[[file.indeterminate]]"
127-
></vaadin-progress-bar>
124+
<slot name="progress"></slot>
128125
`;
129126
}
130127

@@ -147,6 +144,11 @@ class UploadFile extends FocusMixin(ThemableMixin(PolymerElement)) {
147144
value: 0,
148145
reflectToAttribute: true,
149146
},
147+
148+
/** @private */
149+
_progress: {
150+
type: Object,
151+
},
150152
};
151153
}
152154

@@ -157,13 +159,25 @@ class UploadFile extends FocusMixin(ThemableMixin(PolymerElement)) {
157159
'_toggleHostAttribute(file.indeterminate, "indeterminate")',
158160
'_toggleHostAttribute(file.uploading, "uploading")',
159161
'_toggleHostAttribute(file.complete, "complete")',
162+
'__updateProgress(_progress, file.progress, file.indeterminate)',
160163
];
161164
}
162165

163166
/** @protected */
164167
ready() {
165168
super.ready();
166169

170+
this.addController(
171+
new SlotController(
172+
this,
173+
'progress',
174+
() => document.createElement('vaadin-progress-bar'),
175+
(_, progress) => {
176+
this._progress = progress;
177+
},
178+
),
179+
);
180+
167181
// Handle moving focus to the button on Tab.
168182
this.shadowRoot.addEventListener('focusin', (e) => {
169183
const target = e.composedPath()[0];
@@ -211,8 +225,11 @@ class UploadFile extends FocusMixin(ThemableMixin(PolymerElement)) {
211225
}
212226

213227
/** @private */
214-
_formatProgressValue(progress) {
215-
return progress / 100;
228+
__updateProgress(progress, value, indeterminate) {
229+
if (progress) {
230+
progress.value = isNaN(value) ? 0 : value / 100;
231+
progress.indeterminate = indeterminate;
232+
}
216233
}
217234

218235
/** @private */

‎packages/upload/test/dom/__snapshots__/vaadin-upload-file.test.snap.js‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,8 @@ snapshots["vaadin-upload-file shadow default"] =
6363
</button>
6464
</div>
6565
</div>
66-
<vaadin-progress-bar
67-
aria-valuemax="1"
68-
aria-valuemin="0"
69-
aria-valuenow="0.6"
70-
id="progress"
71-
part="progress"
72-
role="progressbar"
73-
style="--vaadin-progress-value:0.6;"
74-
value="0.6"
75-
>
76-
</vaadin-progress-bar>
66+
<slot name="progress">
67+
</slot>
7768
`;
7869
/* end snapshot vaadin-upload-file shadow default */
7970

‎packages/upload/test/dom/__snapshots__/vaadin-upload.test.snap.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,44 @@ snapshots["vaadin-upload shadow default"] =
6464
complete=""
6565
tabindex="0"
6666
>
67+
<vaadin-progress-bar
68+
aria-valuemax="1"
69+
aria-valuemin="0"
70+
aria-valuenow="0"
71+
role="progressbar"
72+
slot="progress"
73+
style="--vaadin-progress-value:0;"
74+
>
75+
</vaadin-progress-bar>
6776
</vaadin-upload-file>
6877
</li>
6978
<li>
7079
<vaadin-upload-file tabindex="0">
80+
<vaadin-progress-bar
81+
aria-valuemax="1"
82+
aria-valuemin="0"
83+
aria-valuenow="0.6"
84+
role="progressbar"
85+
slot="progress"
86+
style="--vaadin-progress-value:0.6;"
87+
>
88+
</vaadin-progress-bar>
7189
</vaadin-upload-file>
7290
</li>
7391
<li>
7492
<vaadin-upload-file
7593
error=""
7694
tabindex="0"
7795
>
96+
<vaadin-progress-bar
97+
aria-valuemax="1"
98+
aria-valuemin="0"
99+
aria-valuenow="0"
100+
role="progressbar"
101+
slot="progress"
102+
style="--vaadin-progress-value:0;"
103+
>
104+
</vaadin-progress-bar>
78105
</vaadin-upload-file>
79106
</li>
80107
<dom-repeat

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ const uploadFile = css`
169169
color: var(--lumo-error-text-color);
170170
}
171171
172-
[part='progress'] {
172+
::slotted([slot='progress']) {
173173
width: auto;
174174
margin-left: calc(var(--lumo-icon-size-m) + var(--lumo-space-xs));
175175
margin-right: calc(var(--lumo-icon-size-m) + var(--lumo-space-xs));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ registerStyles(
235235
color: var(--material-error-text-color);
236236
}
237237
238-
[part='progress'] {
238+
::slotted([slot='progress']) {
239239
width: auto;
240240
margin-left: 28px;
241241
}

0 commit comments

Comments
 (0)