1.2.1-alpha • Published 11 months ago

ts-medialibrary v1.2.1-alpha

Weekly downloads
-
License
-
Repository
github
Last release
11 months ago

Media Library Web Component

A client framework and server agnostic Media library Web Component made with vue3.

Installation

Install ts-medialibrary with npm

  npm install ts-medialibrary

Usage Example

Javascript

<form>
  <ts-medialibrary
      multiple
      title="Library Demo For Github"
      url="http://dummy.dev/media"
    ></ts-medialibrary>
    <div id="fileinput"></div>
</form>

<script src="node_modules/ts-medialibrary/dist/ts-medialibrary.js"></script>
<script>
    customElements.whenDefined("ts-medialibrary").then((c) => {
      const el = document.querySelector("ts-medialibrary");

      if(el){
        el.addEventListener("updated", function (e) {
          const payload = e.detail[0];

          //appending hidden input insdie form
          document.getElementById("fileinput").innerHTML = payload.html;
        });
      }
      
    });
</script>

Vue

  import 'ts-medialibrary/dist/ts-medialibrary.js';

  <ts-medialibrary url="REST_URL_HERE" @updated="onSelectionChanged"></ts-medialibrary>

React

You can either add ref to the ts-medialibrary and listen just like with js or can wrap and make new HOC react component.

import 'ts-medialibrary/dist/ts-medialibrary.js';

const MediaLibrary = ({ url, onChange }) => {
  const ref = useRef();


  useLayoutEffect(() => {
    const handleChange = customEvent => onChange(customEvent.detail[0]);

    const { current } = ref;

    current.addEventListener('updated', handleChange);

    return () => current.removeEventListener('updated', handleChange);
  }, [ref]);

   

  return (
    <ts-medialibrary
      ref={ref}
      url={url}
    />
  );
};

API Reference

Client

PropsTypeDefaultDescription
urlstringnoneRequired. REST Url to fetch and upload media
uidstringdefaultUnique identifer required for multiple instances in same page.
labelstringSelect File(s)Upload Button Label
titlestringMedia LibraryLibrary Modal Heading
udpate-labelstringClick To ChangeUpload Button Label after selections are made
choosenstringNoneComma separated id to preselect media.
can-removestring, boolean, number0Allow delete action if true
blockingstring, number, boolean0If true than backdrop blocks the UI and modal stays open when clicking on it
input-namestringfiles[]input name which is used to return html string on update
input-keystringidkey from media object to be used as input value
multiplestring, boolean, number0Allow multiple selection and upload
preview-linkstring, boolean, number1Open selected media in new tab
allow-filesboolean, number0By default only images are allowed, Passing value of 1 will allow upload of any files defined in accept prop
acceptstring"image/*, audio/*, video/*, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, .pdf, .doc, .docx, .csv, .txtFile types to accept when allow-files=1. Only for clientisde. For server validation you have to use your own.
limitnumber50Number of max items to display in the library. Your server can return 100s of items but library will only show limit items. To see the remaining items, either have to search or sort.

Emits

EventPayloadDescription
updatedselected: media[]Arry of selected media {id, url, thumb_url}[]
html: stringHidden input string with input-name as name and value mapped to media[input-key]

Events are dispatched as native CustomEvents on the custom element. Additional event arguments (payload) will be exposed as an array on the CustomEvent object as its detail property. const payload = e.detail[0];

Server

Server Api Reference.

To test the library there is a default solution provided for laravel, You have to install one of my other package Media Library Package which comes with all required url actions.

Get all items

  GET /PROPS_URL
Response
{"items": "MediaResourceCollection[]", "success": 1}

Upload item

  POST /PROPS_URL
BodyTypeDescription
files[]HTTPFileRequired. array of files
allow_filesnumber(0, 1)If the upload should support only image or any files.
Response
{"items": "UploadedMediaResourceCollection[]", "success": 1, "message": "UPLOADED SUCCESS MESSAGE"}

Delete item

  DELETE /PROPS_URL/${id}
ParameterTypeDescription
idstringRequired. Id of item to delete
Response
{ "success": 1, "message": "DELETED MESSAGE"}

Error Response

{ "error": 1, "message": "ERROR_MESSAGE"}

Customization and Theming

CSS VariableDefaultDescription
--tml-color-primary-rgb99, 102, 241Primary color RGB
--tml-color-danger-rgb255, 0, 0Primary color RGB
--tml-color-successgreenSuccess Color
--tml-color-secondaryrgb(249, 250, 251)Secondary Color
--tml-color-greygreyGrey Color
--tml-color-light#bbbbbbLight Color
--tml-color-lighter#ccccccLighter Color
--tml-modal-z-index100Z-Index for modal
--tml-modal-content-z-index10` | Z-Index for modal content
--tml-modal-animation-duration0.3sModal open animation duration
--tml-modal-background-colorrgba(30, 30, 30, 0.8)Modal background color
--tml-modal-widthfit-contentModal width
--tml-modal-min-width500pxModal min width
--tml-modal-max-width100vwModal max width
--tml-modal-height80%Modal height

There are a lot more variables which will be updated soon.

Tech Stack

Client: Vue3, Typescript, SCSS

Demo

SQ11g1JduD7-z-0-y-647a0fb8cfa57536f50cfc49

Acknowledgements

1.2.1-alpha

11 months ago

1.1.0-alpha

11 months ago

1.0.2-alpha

11 months ago

1.0.1-alpha

11 months ago

1.0.0-alpha

11 months ago

0.9.6

11 months ago

0.9.5

11 months ago