Allow your users to upload content directly to Mux.
Quick start
The fastest way to build Mux Uploader into your application
Upload a video
Basics of uploading videos with Mux Uploader
Customizing the appearance
Make Mux Uploader match the rest of your application
Listening for events
Get notified when the uploader status changes
<mux-uploader>
is a drop-in web component that makes it easy to upload video files to Mux.
This component allows you to build a fully-functional video upload UI in your application using a single line of code.
Here are some examples of Mux Uploader in action.
First, install it:
npm i @mux/mux-uploader
Then, import it:
import '@mux/mux-uploader';
Finally, add the HTML tag:
<div>
<mux-uploader></mux-uploader>
</div>
Alternatively, use the CDN hosted version of this package:
<script src="https://cdn.jsdelivr.net/npm/@mux/mux-uploader@1.0.0-beta.6"></script>
If you are using ECMAScript modules, you can also load the mux-uploader.mjs
file with type=module
:
<script
type="module"
src="https://cdn.jsdelivr.net/npm/@mux/mux-uploader@1.0.0-beta.6/dist/mux-uploader.mjs"
></script>
First, install it:
npm i @mux/mux-uploader-react
Then, import it:
import '@mux/mux-uploader-react';
Finally, add the JSX tag:
<div>
<MuxUploader />
</div>
<mux-uploader>
takes care of rendering a file selector, uploading your video file, displaying progress updates to the user, handling retries, and more.
There's only one required attribute that you must set to upload your file – the endpoint
attribute. It looks like this:
<mux-uploader
endpoint="https://my-authenticated-url/storage?your-url-params"
></mux-uploader>
The endpoint
attribute provides mux-uploader
with a direct upload URL that will receive the video file you're uploading.
You can generate a signed direct upload URL by making a server-side API call to Mux's Create Direct Upload endpoint.
In a successful API response, you will receive a unique signed upload URL that can then be passed along to your client application and set as the endpoint
property on a mux-uploader
element. The URL for a Direct Upload looks like "https://storage.googleapis.com/video..."
.
In the following examples, you will replace the value of the endpoint
property with your unique direct upload URL.
At the time you render the <mux-uploader>
, you may not have the direct upload URL yet. Instead, you might want to retrieve it async from your server after a user selects a file. You can do that by setting the endpoint
property value to a custom function instead of a URL.
<mux-uploader></mux-uploader>
<script>
const muxUploader = document.querySelector("mux-uploader");
/*
Endpoint should be a function that returns a promise and resolves
with a string for the upload URL.
*/
muxUploader.endpoint = function () {
/*
In this example, your server endpoint would return the upload URL
in the response body "https://storage.googleapis.com/video..."
*/
return fetch("/your-server/api/create-upload").then((res) => res.text());
};
</script>
We've built <mux-uploader>
with sensible styling defaults that might Just Work™ for your application. Of course, there are times when you'll want to change the default appearance or behavior to something a little more custom.
<mux-uploader>
uses HTML slots, allowing you to replace default UI elements with your own HTML elements.
View the slots reference to learn about all of the available slots in <mux-uploader>
.
You can use your own button with the slot="file-select"
attribute.
This is really handy if, for example, you already have a .btn
class that styles buttons in your application.
<mux-uploader>
exposes CSS variables that allow you to change the default styling of the uploader UI.
View the CSS variables reference to learn about all of the available CSS variables in <mux-uploader>
.
If you're using React to build your application, you may be using CSS-in-JS or a CSS framework to handle styling. Here's an example of how you can style Mux Uploader using CSS modules:
Another common approach to styling React applications is using Tailwind, so here's an example of styling Mux Uploader using the Tailwind framework:
By default, the progress bar color is black with a gray background. Here's how you can change the progress bar color to purple:
If you're looking to build a custom UI for uploading videos in your application, advanced customization is for you.
You can use the individual web components that come packaged with <mux-uploader>
to build out a custom upload UI that meets your needs.
To use this approach, add an id
attribute to your <mux-uploader>
element with a unique value.
You can then associate the <mux-uploader>
element with any of the packaged components by adding a mux-uploader=""
attribute to each component.
<!-- add a mux-uploader tag with an id attribute and hide it with CSS -->
<mux-uploader id="my-uploader" style="display: none;"></mux-uploader>
<!-- ...then, somewhere else in your app, add a reference back to it -->
<mux-uploader-file-select mux-uploader="my-uploader">
<button slot="file-select">Pick a video</button>
</mux-uploader-file-select>
If you'd like to prevent certain components from rendering within the <mux-uploader>
element, you can pass attributes that will disable their output.
Possible values include no-drop
, no-progress
, no-retry
, and no-status
<!-- This will remove file drag-and-drop functionality and remove the status messages -->
<mux-uploader no-drop no-status></mux-uploader>
There may be times when you want to include a video upload field as part of a larger form in your application. For example, imagine we're building a video contest submission form. We might have other form fields to associate with the video, like a video_title
, video_description
etc.
One way you can solve for this is to create the Direct Upload URL on the server when a request to your form page is made. You'd then retrieve the resulting upload URL and upload id and send it down to the client.
You might consider keeping your form submit button disabled until you've received a success
event from Mux Uploader. You can find an example of this flow using Next.js on this CodeSandbox.
Here are all of the available web components you can use with <mux-uploader>
:
Component name | Description |
---|---|
<mux-uploader> | Manages the uploading state. Required |
<mux-uploader-status> | A single-line message informing the user of the current status of the upload, i.e. Upload complete! |
<mux-uploader-file-select> | Displays the system file-select dialog when clicked |
<mux-uploader-drop> | Enables drag-and-drop functionality for handling file input |
<mux-uploader-retry> | Renders a "retry" button if the upload fails |
<mux-uploader-progress> | Displays a progress indicator showing how much of your file has been uploaded. This can be a bar or percentage type |
Check out the mux-uploader-modal.html example application to see a real-world example of how these components can be used.
<mux-uploader-drop>
is available for drag and drop functionality. It works like a <div>
or other "container" element in the sense that you can style it and populate it with whatever children you see fit (including but not necessarily a <mux-uploader>
).
Similar to <input>
and <label>
relationships, you associate a <mux-uploader-drop>
with its corresponding <mux-uploader>
via id
using the mux-uploader
attribute.
The <mux-uploader-drop>
component will get the active
attribute when the drag is active, this allows you to write some CSS to style it for your application.
Here's a full example of a custom button, customized progress text and drag and drop functionality that changes the background when the drag is active
When a file is dropped, <mux-uploader>
will dispatch a custom file-ready
event with the relevant file.
You can implement your own drag and drop completely separate from <mux-uploader>
and as long as you dispatch a custom file-ready
with the file in the detail
property then <mux-uploader>
will handle the upload upon receiving the event.
<script>
const muxUploader = document.querySelector("mux-uploader");
// Dispatch custom event to trigger upload
muxUploader.dispatchEvent(
new CustomEvent("file-ready", {
composed: true,
bubbles: true,
detail: file,
})
);
</script>
<mux-uploader>
uses custom events to share details about how the upload is progressing.
You can listen for the progress
event to receive details on how far along your file upload is.
const muxUploader = document.querySelector("mux-uploader");
muxUploader.addEventListener("progress", function (e) {
console.log(`My upload is ${e.detail}% complete!`);
});
When the upload is complete, you'll see 100% on the progress bar and the success
event will fire.
If an error occurs during the upload, an uploaderror
event will fire.
<mux-uploader endpoint="https://my-authenticated-url/storage?your-url-params"></mux-uploader>
<script>
const muxUploader = document.querySelector('mux-uploader');
muxUploader.addEventListener('success', function () {
/* Handle upload success */
});
muxUploader.addEventListener('uploaderror', function () {
/* Handle upload error */
});
</script>
<mux-uploader>
Attribute | Type | Description | Default |
---|---|---|---|
endpoint | string | Promise | Either a) the authenticated URL that your file will be uploaded to or b) an async function that yields a promise that resolves to that url. Check out the direct uploads docs. Required. | undefined |
id | string | An ID that allows mux-uploader-drop to locate mux-uploader . Required if you use advanced customization. | N/A |
upload-in-progress | boolean | (Read-only) Toggles visual status of progress bar while upload is in progress. Can be targeted with CSS if you want to control styles while in progress i.e. mux-uploader[upload-in-progress]. | false |
upload-error | boolean | (Read-only) Toggles visual status of progress bar when upload encounters an error. Can be targeted with CSS if you want to control styles when an error occurs i.e. mux-uploader[upload-error]. | false |
dynamic-chunk-size | boolean | Toggles uploading with dynamic chunk sizes. Chunk sizes will change with upload speed to attempt to optimize upload. | false |
no-drop | boolean | Renders a div wrapper instead of a <mux-uploader-drop> component | false |
no-progress | boolean | Prevents the ouput of <mux-uploader-progress> elements as children of <mux-uploader> | false |
no-retry | boolean | Prevents the ouput of the <mux-uploader-retry> element as a child of <mux-uploader> | false |
no-status | boolean | Prevents the ouput of the <mux-uploader-status> element as a child of <mux-uploader> | false |
max-file-size | number | Enforces a client-side limit on the maximum video file size the uploader component will accept as valid input. | undefined |
<mux-uploader-drop>
Attribute | Type | Description | Default |
---|---|---|---|
overlay | boolean | Toggles showing an overlay on dragover. | false |
overlay-text | string | Optional text to display on dragover when overlay is on. | '' |
mux-uploader | string | Must match the id on MuxUploader . Required. | N/A |
Web Component | Slot Name | Slot Description | Code Example |
---|---|---|---|
<mux-uploader> | file-select | Used to customize the file select button in the uploader component. | <mux-uploader><button slot="file-select">Custom File Select</button></mux-uploader> |
<mux-uploader-drop> | heading | Used to customize the text shown when prompting users to drop a file. | <mux-uploader-drop><span slot="heading">Custom heading</span></mux-uploader-drop> |
<mux-uploader-drop> | separator | Used to customize the separator text between the drop area and the file picker. | <mux-uploader-drop><span slot="separator">Custom separator</span></mux-uploader-drop> |
<mux-uploader-drop> | default | Used to customize the file picker button or input. | <mux-uploader-drop><button slot="default">Custom file picker</button></mux-uploader-drop> |
<mux-uploader>
<mux-uploader>
fires several events throughout the uploading lifecycle to monitor uploading state. These events match the events fired by Upchunk under the hood.
Event | Description |
---|---|
file-ready | Fired when a file has been selected to be uploaded |
uploadstart | Fired when the upload begins. |
chunkattempt | Fired immediately before a chunk upload is attempted. |
chunksuccess | Fired when an indvidual chunk is successfully uploaded. Sample response: { detail: { chunk: Integer, attempts: Integer, response: XhrResponse } } |
uploaderror | Fired when an error occurs in the chunked upload process. |
progress | Fired continuously with incremental upload progress. This returns the current percentage of the file that's been uploaded. Sample response: { detail: [0..100] } |
success | Fired when the entire file has successfully completed uploading. |
<mux-uploader>
and its packaged components can be styled with standard CSS, but also includes the following CSS variables for "under the hood" styling.
<mux-uploader>
Name | CSS Property | Default Value | Description | Notes |
---|---|---|---|---|
--progress-bar-fill-color | background | #000000 | background color for progress bar div | |
--progress-radial-fill-color | stroke | black | stroke color for circle SVG (wip) | |
--progress-percentage-display | display | block | display value for percentage progress |
<mux-uploader-drop/>
Name | CSS Property | Default Value | Description | Notes |
---|---|---|---|---|
--overlay-background-color | background-color | rgba(226, 253, 255, 0.95) | background color of the overlay div | Visible only when component has fullscreen attribute |