Skip to content

Commit 240d2ae

Browse files
authored
fix: button text for file-select slot can now be customized (muxinc#1252)
## Changes - Prevent translating the text if the file picker button is a custom one. - Added a test for this case
1 parent 673b7ff commit 240d2ae

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

packages/mux-uploader/src/mux-uploader-file-select.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const fileSelectFragment = /*html*/ `
3232
3333
</style>
3434
35-
<button id="file-select" type="button" part="file-select-button"></button>
35+
<button id="file-select" type="button" part="file-select-button">Upload a video</button>
3636
`;
3737

3838
const template = document.createElement('template');
@@ -157,13 +157,10 @@ class MuxUploaderFileSelectElement extends globalThis.HTMLElement {
157157
const locale = (this.#uploaderEl as MuxUploaderElement)?.locale;
158158
const text = t('Upload a video', locale);
159159

160-
if (this.filePickerEl) {
161-
this.filePickerEl.textContent = text;
162-
} else {
163-
const buttonEl = this.shadowRoot?.querySelector('#file-select') as HTMLButtonElement;
164-
if (buttonEl) {
165-
buttonEl.textContent = text;
166-
}
160+
// Translate text only if it's the default button (inside the shadow DOM)
161+
const buttonEl = this.shadowRoot?.querySelector('#file-select') as HTMLButtonElement;
162+
if (buttonEl) {
163+
buttonEl.textContent = text;
167164
}
168165
}
169166
}

packages/mux-uploader/test/file-select.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,32 @@ describe('<mux-uploader-file-select>', () => {
126126
assert.equal(button.textContent, t('Upload a video', 'fr'), 'displays French text');
127127
assert.equal(button.textContent, 'Télécharger une vidéo', 'matches French translation');
128128
});
129+
130+
it('preserves custom button text when locale changes', async function () {
131+
const uploader = await fixture(`
132+
<mux-uploader
133+
endpoint="https://my-authenticated-url/storage?your-url-params">
134+
<button class="btn" type="button" slot="file-select">Pick a file</button>
135+
</mux-uploader>
136+
`);
137+
138+
const el = uploader.shadowRoot.querySelector('mux-uploader-file-select');
139+
const slot = el.querySelector('slot');
140+
const button = slot.assignedNodes()[0];
141+
142+
// Verify initial custom text
143+
assert.equal(button.textContent, 'Pick a file', 'custom button text is preserved initially');
144+
145+
// Change locale to Spanish
146+
uploader.setAttribute('locale', 'es');
147+
148+
// Custom button text should still be preserved (not overwritten by translation)
149+
assert.equal(button.textContent, 'Pick a file', 'custom button text is preserved after locale change');
150+
assert.notEqual(
151+
button.textContent,
152+
t('Upload a video', 'es'),
153+
'custom button text is not overwritten by translation'
154+
);
155+
});
129156
});
130157
});

0 commit comments

Comments
 (0)