Creating custom Vue components | laravel-medialibrary | Spatie

 SPATIE

  Laravel Media Library
========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-medialibrary](https://spatie.be/docs/laravel-medialibrary/v10)  Handling-uploads-with-media-library-pro  Creating custom Vue components

 Version   v11   v10   v9   v8   v7   v6   v5   v4   v3

 Other versions for crawler [v11](https://spatie.be/docs/laravel-medialibrary/v11) [v10](https://spatie.be/docs/laravel-medialibrary/v10) [v9](https://spatie.be/docs/laravel-medialibrary/v9) [v8](https://spatie.be/docs/laravel-medialibrary/v8) [v7](https://spatie.be/docs/laravel-medialibrary/v7) [v6](https://spatie.be/docs/laravel-medialibrary/v6) [v5](https://spatie.be/docs/laravel-medialibrary/v5) [v4](https://spatie.be/docs/laravel-medialibrary/v4) [v3](https://spatie.be/docs/laravel-medialibrary/v3)

- [ Introduction ](https://spatie.be/docs/laravel-medialibrary/v10/introduction)
- [ Support us ](https://spatie.be/docs/laravel-medialibrary/v10/support-us)
- [ Base installation ](https://spatie.be/docs/laravel-medialibrary/v10/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-medialibrary/v10/questions-issues)
- [ Requirements ](https://spatie.be/docs/laravel-medialibrary/v10/requirements)
- [ Upgrading ](https://spatie.be/docs/laravel-medialibrary/v10/upgrading)
- [ Changelog ](https://spatie.be/docs/laravel-medialibrary/v10/changelog)
- [ Troubleshooting ](https://spatie.be/docs/laravel-medialibrary/v10/troubleshooting)
- [ About us ](https://spatie.be/docs/laravel-medialibrary/v10/about-us)

Basic usage
-----------

- [ Preparing your model ](https://spatie.be/docs/laravel-medialibrary/v10/basic-usage/preparing-your-model)
- [ Associating files ](https://spatie.be/docs/laravel-medialibrary/v10/basic-usage/associating-files)
- [ Retrieving media ](https://spatie.be/docs/laravel-medialibrary/v10/basic-usage/retrieving-media)

Working with media collections
------------------------------

- [ Simple media collections ](https://spatie.be/docs/laravel-medialibrary/v10/working-with-media-collections/simple-media-collections)
- [ Defining media collections ](https://spatie.be/docs/laravel-medialibrary/v10/working-with-media-collections/defining-media-collections)

Converting images
-----------------

- [ Defining conversions ](https://spatie.be/docs/laravel-medialibrary/v10/converting-images/defining-conversions)
- [ Retrieving converted images ](https://spatie.be/docs/laravel-medialibrary/v10/converting-images/retrieving-converted-images)
- [ Optimizing converted images ](https://spatie.be/docs/laravel-medialibrary/v10/converting-images/optimizing-converted-images)
- [ Regenerating images ](https://spatie.be/docs/laravel-medialibrary/v10/converting-images/regenerating-images)

Handling uploads with Media Library Pro
---------------------------------------

- [ Introduction ](https://spatie.be/docs/laravel-medialibrary/v10/handling-uploads-with-media-library-pro/introduction)
- [ Installation ](https://spatie.be/docs/laravel-medialibrary/v10/handling-uploads-with-media-library-pro/installation)
- [ Processing uploads on the server ](https://spatie.be/docs/laravel-medialibrary/v10/handling-uploads-with-media-library-pro/processing-uploads-on-the-server)
- [ Handling uploads with Livewire 2 ](https://spatie.be/docs/laravel-medialibrary/v10/handling-uploads-with-media-library-pro/handling-uploads-with-livewire-2)
- [ Handling uploads with Livewire 3 ](https://spatie.be/docs/laravel-medialibrary/v10/handling-uploads-with-media-library-pro/handling-uploads-with-livewire-3)
- [ Handling uploads with Vue ](https://spatie.be/docs/laravel-medialibrary/v10/handling-uploads-with-media-library-pro/handling-uploads-with-vue)
- [ Creating custom Vue components ](https://spatie.be/docs/laravel-medialibrary/v10/handling-uploads-with-media-library-pro/creating-custom-vue-components)
- [ Handling uploads with React ](https://spatie.be/docs/laravel-medialibrary/v10/handling-uploads-with-media-library-pro/handling-uploads-with-react)
- [ Creating custom React components ](https://spatie.be/docs/laravel-medialibrary/v10/handling-uploads-with-media-library-pro/creating-custom-react-components)
- [ Customizing CSS ](https://spatie.be/docs/laravel-medialibrary/v10/handling-uploads-with-media-library-pro/customizing-css)
- [ Upgrading ](https://spatie.be/docs/laravel-medialibrary/v10/handling-uploads-with-media-library-pro/upgrading)

Responsive images
-----------------

- [ Getting started with responsive images ](https://spatie.be/docs/laravel-medialibrary/v10/responsive-images/getting-started-with-responsive-images)
- [ Using your own width calculator ](https://spatie.be/docs/laravel-medialibrary/v10/responsive-images/using-your-own-width-calculator)
- [ Customizing the rendered HTML ](https://spatie.be/docs/laravel-medialibrary/v10/responsive-images/customizing-the-rendered-html)
- [ Generating your own tiny placeholder ](https://spatie.be/docs/laravel-medialibrary/v10/responsive-images/generating-your-own-tiny-placeholder)
- [ Responsive images demo ](https://spatie.be/docs/laravel-medialibrary/v10/responsive-images/demo)

Converting other file types
---------------------------

- [ Using image generators ](https://spatie.be/docs/laravel-medialibrary/v10/converting-other-file-types/using-image-generators)
- [ Creating a custom image generator ](https://spatie.be/docs/laravel-medialibrary/v10/converting-other-file-types/creating-a-custom-image-generator)

Downloading media
-----------------

- [ Downloading a single file ](https://spatie.be/docs/laravel-medialibrary/v10/downloading-media/downloading-a-single-file)
- [ Downloading multiple files ](https://spatie.be/docs/laravel-medialibrary/v10/downloading-media/downloading-multiple-files)

Advanced usage
--------------

- [ Working with multiple filesystems ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/working-with-multiple-filesystems)
- [ Using custom properties ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/using-custom-properties)
- [ Storing media specific manipulations ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/storing-media-specific-manipulations)
- [ Using your own model ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/using-your-own-model)
- [ Outputting media ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/outputting-media)
- [ Rendering media ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/rendering-media)
- [ Using a custom directory structure ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/using-a-custom-directory-structure)
- [ Using a custom file removal strategy ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/using-a-custom-file-removal-strategy)
- [ Ordering media ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/ordering-media)
- [ Using a custom media downloader ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/using-a-custom-media-downloader)
- [ Moving media ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/moving-media)
- [ Consuming events ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/consuming-events)
- [ Attaching media in mails ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/attaching-media-in-mails)
- [ Generating custom URLs ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/generating-custom-urls)
- [ Overriding default filesystem behavior ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/overriding-the-default-filesystem-behaviour)
- [ Naming generated files ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/naming-files)
- [ Disable CDN ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/disable-cdn)
- [ Customising Database Connections ](https://spatie.be/docs/laravel-medialibrary/v10/advanced-usage/customising-database-connections)

API
---

- [ Adding files ](https://spatie.be/docs/laravel-medialibrary/v10/api/adding-files)
- [ Defining conversions ](https://spatie.be/docs/laravel-medialibrary/v10/api/defining-conversions)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-medialibrary                                                                                                                                                                                                                                    `

Creating custom Vue components
==============================

###  On this page

1. [ Getting started ](#content-getting-started)
2. [ Examples ](#content-examples)
3. [ Helper components ](#content-helper-components)
4. [ Props ](#content-props)
5. [ Returned parameters in scoped slot ](#content-returned-parameters-in-scoped-slot)

Both the Vue and UI components are built on top of the same core, with a language-specific abstraction layer in between that exposes some helper functions. You can extend the core that the UI components are built on. This allows you to customize the UI. This page will go into detail about these abstraction layers.

The Vue implementation uses a renderless component that exposes all the functions and values through a slot scope.

For more extensive examples, [see the pre-built UI components on GitHub](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js)

Getting started
-----------------------------------------------------------------------------------------------------

```

    import { MediaLibraryRenderless } from 'media-library-pro-vue2';

    export default{
        components: { MediaLibraryRenderless },
    }

```

In Vue 3, you would use the new `v-slot` attribute instead of `slot-scope`:

```
v-slot="{ state, getImgProps, … }"
```

You can find a full list of available props for the renderless component [at the bottom of this page](#props).

Examples
--------------------------------------------------------------------------------

For extensive examples you can have a look at the source of the pre-built UI components:

- [Vue 2 attachment component](https://github.com/spatie/laravel-medialibrary-pro/tree/master/resources/js/media-library-pro-vue2-attachment)
- [Vue 2 collection component](https://github.com/spatie/laravel-medialibrary-pro/tree/master/resources/js/media-library-pro-vue2-collection)
- [Vue 3 attachment component](https://github.com/spatie/laravel-medialibrary-pro/tree/master/resources/js/media-library-pro-vue3-attachment)
- [Vue 3 collection component](https://github.com/spatie/laravel-medialibrary-pro/tree/master/resources/js/media-library-pro-vue3-collection)

Helper components
-----------------------------------------------------------------------------------------------------------

When building your own UI component using the Media Library Pro, you can also make use of these helper components. These are the same components that are used in the UI components.

### DropZone

See code on GitHub: [Vue 2](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue2/src/DropZone.vue) / [Vue 3](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue3/src/DropZone.vue)

Renderless component that exposes some props needed to render a file dropzone. Has a scoped slot that exposes `hasDragObject`, `isDropTarget` and `isValid`.

Emits `@clicked` and `@dropped`.

**props**

```
props: {
    validationAccept: { default: () => [], type: Array },
},
```

### HiddenFields

See code on GitHub: [Vue 2](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue2/src/HiddenFields.vue) / [Vue 3](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue3/src/HiddenFields.vue)

Component that renders hidden input fields with the values of the entire MediaLibrary instance's media state. Only needed if you're planning on submitting forms traditionally (not with AJAX).

**props**

```
props: {
    name: { required: true, type: String },
    mediaState: { default: () => [], type: Array },
},
```

### ItemErrors

See code on GitHub: [Vue 2](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue2/src/ItemErrors.vue) / [Vue 3](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue3/src/ItemErrors.vue)

Component that renders the errors for one media object.

Emits `@back`.

**props**

```
props: {
    objectErrors: { required: true, type: Array },
},
```

### ListErrors

See code on GitHub: [Vue 2](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue2/src/listErrors/ListErrors.vue) / [Vue 3](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue3/src/listErrors/ListErrors.vue)

Component that can render the MediaLibrary instance's invalid media (`mediaLibrary.state.invalidMedia`).

Emits `@cleared`.

**props**

```
props: {
    invalidMedia: { default: () => ({}), type: Array },
    topLevelErrors: { default: () => [], type: Array },
},
```

### Thumb

See code on GitHub: [Vue 2](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue2/src/Thumb.vue) / [Vue 3](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue3/src/Thumb.vue)

Component to display a file's thumbnail. If no preview for the file exists, it will attempt to display its extension. Also implements the [Uploader](#uploader) component to replace files.

Emits `@replaced`.

**props**

```
props: {
    uploadInfo: { required: true, type: Object },
    validationRules: { required: false, type: Object },
    imgProps: { required: true, type: Object },
},
```

### Uploader

See code on GitHub: [Vue 2](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue2/src/Uploader.vue) / [Vue 3](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue3/src/Uploader.vue)

Component used to upload new media objects, or to replace an existing object's file. Is used by the [Thumb](#thumb) component.

Emits `@changed` and `@dropped`.

**props**

```
props: {
    add: { default: true, type: Boolean },
    uploadInfo: { required: false, type: Object },
    multiple: { default: false, type: Boolean },
    validationRules: { required: false, type: Object },
    maxItems: { required: false, type: Number },
    fileTypeHelpText: { required: false, type: String },
},
```

### Icons

See code on GitHub: [Vue 2](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue2/src/components/Icons.vue) / [Vue 3](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue3/src/components/Icons.vue)

Component that sets svg values for the packaged icons, required if you're planning on using the [icon](#icon) component.

### Icon

See code on GitHub: [Vue 2](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue2/src/components/Icon.vue) / [Vue 3](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue3/src/components/Icon.vue)

Renders an icon. Requires [icons](#icons) to be rendered on the page.

**Props**

```
props: {
    icon: { required: true, type: String },
},
```

### IconButton

See code on GitHub: [Vue 2](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue2/src/components/IconButton.vue) / [Vue 3](https://github.com/spatie/laravel-medialibrary-pro/blob/master/resources/js/media-library-pro-vue3/src/components/IconButton.vue)

Renders a button with an icon. Requires [icons](#icons) to be rendered on the page.

**Props**

```
props: {
    icon: { required: true, type: String },
},
```

**Parameters**

```
handleClass?: string
```

Props
-----------------------------------------------------------------------

prop nameDefault valueDescriptionnameinitial-value`[]`route-prefix`"media-library-pro"`upload-domainUse this if you're uploading your files to a separate (sub)domain, e.g. `files.mydomain.com` (leave out the trailing slash)validation-rulesRefer to the ["validation"](./handling-uploads-with-vue#validation-rules) sectionvalidation-errorsThe standard Laravel validation error objectmax-itemsmax-size-for-preview-in-bytestranslationsRefer to the ["Translations"](./handling-uploads-with-vue#translations) sectionvapor`false`Set to true if you will deploy your application to Vapor, this enables uploading of the files to S3. [Read more](./handling-uploads-with-vue#usage-with-laravel-vapor)vapor-signed-storage-url`"vapor/signed-storage-url"`multiple`true`before-uploadA method that is run right before a temporary upload is started. You can throw an `Error` from this function with a custom validation messageafter-uploadA method that is run right after a temporary upload has completed, `{ success: true, uuid }`@change@is-ready-to-submit-changeEmits a boolean that tells whether the MediaLibrary instance is ready to submit (has no uploads in progress and has no frontend validation errors)@has-uploads-in-progress-changeEmits a boolean that tells whether the MediaLibrary instance currently has uploads in progressReturned parameters in scoped slot
--------------------------------------------------------------------------------------------------------------------------------------------------------------

variable nameDescriptionmediaLibraryRef to the MediaLibrary instancestateThe state of the MediaLibrary instance. Includes `media` (an array of currently added files), `invalidMedia` (files that encountered a frontend validation error) and `validationErrors` (backend validation errors mapped to uuids)isReadyToSubmitBoolean that tells whether the MediaLibrary instance is ready to submit (has no uploads in progress and has no frontend validation errors)hasUploadsInProgressBoolean that tells whether the MediaLibrary instance currently has uploads in progressgetImgPropsMethod that expects a media object, and returns an object with props needed for an `img` taggetNameInputPropsMethod that expects a media object, and returns an object with props needed for an `input type=text` tag that controls an object's name attributegetNameInputListenersMethod that expects a media object, and returns an object with listeners needed for an `input type=text` tag that controls an object's name attributegetNameInputErrorsMethod that expects a media object, and returns an array of backend validation errors for the `name` attributegetCustomPropertyInputPropsMethod that expects a media object and a custom property name, and returns an object with props needed for an `input` tag that controls that custom propertygetCustomPropertyInputListenersMethod that expects a media object and a custom property name, and returns an object with listeners needed for an `input` tag that controls that custom propertygetCustomPropertyInputErrorsMethod that expects a media object and a custom property name, and returns an array of backend validation errors for that custom propertygetFileInputPropsMethod that expects a media object, and returns an object with props needed for an `input type=file` tag that controls an object's name attributegetFileInputListenersMethod that expects a media object, and returns an object with listeners needed for an `input type=file` tag that controls an object's name attributegetDropZonePropsMethod that expects a media object, and returns an object with props needed for a file dropzone (`validationRules` and `maxItems`)getDropZoneListenersMethod that expects a media object, and returns an object with listeners needed for a file dropzone (`@drop`)addFileMethod that allows a user to pass in an array of files to be added to the MediaLibrary instance's media stateaddFileMethod that allows a user to pass in a File to be added to the MediaLibrary instance's media stateremoveMediaMethod that expects a media object, and removes it from the MediaLibrary instance's media statesetOrderMethod that expects an array of uuids, and sets the `order` attribute of the media objects in the media statesetPropertyMethod that expects a media object, a key (`client_preview`, `attributes.preview_url`, `attributes.size`, `attributes.extension` …), and a value, and sets this property on the media object. This method usually shouldn't be used outside of the coresetCustomPropertyMethod that expects a media object, a custom property key (`tags`, `caption`, `alt` …), and a value, and sets this property on the media objectreplaceMediaMethod that expects a media object and a File, and uploads this file and replaces it on the media objectgetErrorsMethod that expects a media object, and returns an array of errors that it may have encounteredclearObjectErrorsMethod that expects a media object, and clears its errorsclearInvalidMediaMethod that clears the MediaLibrary instance's invalidMedia state
