Mux Elements

repository·main·Indexed 18 days ago

https://github.com/muxinc/elements

A collection of custom web components and React components for simplifying media handling in web applications, including video, audio, uploading, and viewer counts. The library includes packages such as @mux/mux-video, @mux/mux-player, @mux/mux-player-react, @mux/mux-audio, @mux/mux-audio-react, and @mux/mux-active-viewer-count.

Tokens
90.9K
Snippets
242
Records
342
Agent score
59%

What's inside Mux Elements

  1. Overview of @mux/mux-uploader components

    main

    The @mux/mux-uploader package provides a suite of components for handling file uploads. You can use the main <mux-uploader> component for a complete, 'batteries included' UI that manages both the upload state and the interface. Alternatively, you can use subcomponents directly for more granular control over the UI while using <mux-uploader> for state management.

    Available Components:

    • <mux-uploader>: The required main component. Manages upload state and provides a full UI.
    • <mux-uploader-drop>: Enables drag-and-drop functionality.
    • <mux-uploader-file-select>: Triggers the system file-select dialog.
    • <mux-uploader-pause>: Provides pause/resume functionality.
    • <mux-uploader-progress>: Displays upload progress (bar, percentage, or radial).
    • <mux-uploader-retry>: Renders a retry button on upload failure.
    • <mux-uploader-status>: Displays upload status (e.g., completed, error, or offline).
  2. Overview of <mux-video>

    main

    <mux-video> is a Mux-flavored HTML5 video web component. It provides a high-level abstraction over the standard <video> element and Hls.js.

    Key benefits include:

    • Automatic Error Recovery: Handles recoverable playback errors, which is essential for live streams that may experience disconnects.
    • Optimized Configuration: Automatically uses optimal Hls.js settings specifically tuned for Mux Video.
    • Managed Dependencies: Periodically tests and upgrades to stable versions of Hls.js automatically, reducing maintenance overhead.
  3. Overview of Mux Elements

    main
    Mux Elements provides custom web components and framework-specific components designed for working with media in the browser. The library offers pure Web Component versions that work with standard HTML and most modern web frameworks, as well as dedicated React components for seamless integration into React applications.
  4. Overview of @mux/mux-uploader-react components

    main

    The @mux/mux-uploader-react package provides several components for managing and displaying file uploads. You can use the primary <MuxUploader> component for a complete, 'batteries included' UI that handles both state management and rendering. For more granular control, you can use subcomponents directly, using <MuxUploader> solely for state management (e.g., by setting it to display: none;) and building your own UI with the following pieces:

    | Component name | Description |
    | -------------- | ----------- |
    | `<MuxUploader>` | A fully-featured UI component. Also manages the uploading state.  **Required** |
    | `<MuxUploaderDrop>` | Enables drag-and-drop functionality for handling file input. |
    | `<MuxUploaderFileSelect>` | Displays the system file-select dialog when clicked. |
    | `<MuxUploaderPause>` | Displays a pause/resume button |
    | `<MuxUploaderProgress>` | Displays a progress indicator |
    | `<MuxUploaderRetry>` | Renders a "retry" button if the upload fails. |
    | `<MuxUploaderStatus>` | Displays the upload status (e.g. completed or error statuses). |
  5. Overview of Playback Core

    main

    Playback Core is a collection of utility functions designed for managing video/audio playback and integrating Mux Data with various Mux elements. While it is primarily an internal dependency for packages like mux-video, mux-video-react, mux-audio, and mux-audio-react, developers building custom playback solutions or specialized Mux integrations may use it directly.

    To see the full list of available functions and types, refer to the src/index.ts file in the package.

  6. Customize @mux/mux-uploader-astro with CSS Parts and Slots

    main

    The @mux/mux-uploader-astro component supports customization via CSS Parts and Slots.

    • CSS Parts: Use the ::part() pseudo-element in your CSS to style specific internal elements of the uploader. For a complete list of available parts, refer to the base @mux/mux-uploader reference.
    • Slots: Use <slot /> elements within the component's context (if applicable via Astro) or provide content to predefined slots to inject custom UI. For a complete list of available slots, refer to the base @mux/mux-uploader reference.
  7. Astro project structure overview

    main

    A standard Astro project follows this directory structure:

    • public/: Contains static assets like favicon.svg that are served directly.
    • src/layouts/: Contains reusable layout components (e.g., Layout.astro).
    • src/pages/: Contains your application routes; index.astro serves as the home page.
    • package.json: Defines project dependencies and scripts.
    /
    ├── public/
    │   └── favicon.svg
    ├── src/
    │   ├── layouts/
    │   │   └── Layout.astro
    │   └── pages/
    │       └── index.astro
    └── package.json
  8. Lazy-load the MuxPlayer component

    main

    To defer loading the player and improve performance, import from @mux/mux-player-react/lazy. When using the lazy version, you can use the loading prop (e.g., loading="viewport") to control when the player initializes.

    If you are using a Node.js server (like Next.js), it is recommended to combine @mux/mux-player-react/lazy with @mux/blurup to provide a placeholder image while the player loads.

    import MuxPlayer from '@mux/mux-player-react/lazy';
    
    <MuxPlayer
      loading="viewport"
      playbackId="DS00Spx1CV902MCtPj5WknGlR102V5HFkDe"
      metadata={{
        video_id: 'video-id-123456',
        video_title: 'Bick Buck Bunny',
        viewer_user_id: 'user-id-bc-789',
      }}
    />;
  9. Configure <mux-video/> with VAST ads

    main

    You can enable ads by providing a VAST ad tag URL via attributes or properties. You can also configure the player to continue playing content even if an ad blocker prevents an ad from loading.

    <!-- Using Attributes -->
    <mux-video 
      ad-tag-url="https://example.com/vast-tag" 
      allow-ad-blocker
    ></mux-video>
  10. Install @mux/mux-audio-react

    main

    Install the @mux/mux-audio-react package using your preferred package manager, then import it into your application to register the <mux-audio> custom web component.

    yarn add @mux/mux-audio-react
    # or
    npm i @mux/mux-audio-react
    import '@mux/mux-audio-react';
    // or
    require('@mux/mux-audio-react');