Published on

How To Build A Responsive Multi File Upload With Drop-on And Preview With Tailwind CSS From Scratch

Responsive Multi File Upload with drop-on and preview

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.

The description of Responsive Multi File Upload with drop-on and preview ui component

Responsive, file upload modal. supports multi file handling. and shows image or text files in a preview before submit. powered by vanillajs.

Why use Tailwind CSS to build a Responsive Multi File Upload with drop-on and preview ui component?

  • It can make the building process of Responsive Multi File Upload with drop-on and preview ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Responsive Multi File Upload with drop-on and preview component file.

The preview of Responsive Multi File Upload with drop-on and preview ui component

Free download of the Responsive Multi File Upload with drop-on and preview's source code

The source code of Responsive Multi File Upload with drop-on and preview ui component

<div class="bg-gray-500 h-screen w-screen sm:px-8 md:px-16 sm:py-8">
      <main class="container mx-auto max-w-screen-lg h-full">
        <!-- file upload modal -->
        <article aria-label="File Upload Modal" class="relative h-full flex flex-col bg-white shadow-xl rounded-md" ondrop="dropHandler(event);" ondragover="dragOverHandler(event);" ondragleave="dragLeaveHandler(event);" ondragenter="dragEnterHandler(event);">
          <!-- overlay -->
          <div id="overlay" class="w-full h-full absolute top-0 left-0 pointer-events-none z-50 flex flex-col items-center justify-center rounded-md">
            <i>
              <svg class="fill-current w-12 h-12 mb-3 text-blue-700" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
                <path d="M19.479 10.092c-.212-3.951-3.473-7.092-7.479-7.092-4.005 0-7.267 3.141-7.479 7.092-2.57.463-4.521 2.706-4.521 5.408 0 3.037 2.463 5.5 5.5 5.5h13c3.037 0 5.5-2.463 5.5-5.5 0-2.702-1.951-4.945-4.521-5.408zm-7.479-1.092l4 4h-3v4h-2v-4h-3l4-4z" />
              </svg>
            </i>
            <p class="text-lg text-blue-700">Drop files to upload</p>
          </div>

          <!-- scroll area -->
          <section class="h-full overflow-auto p-8 w-full h-full flex flex-col">
            <header class="border-dashed border-2 border-gray-400 py-12 flex flex-col justify-center items-center">
              <p class="mb-3 font-semibold text-gray-900 flex flex-wrap justify-center">
                <span>Drag and drop your</span>&nbsp;<span>files anywhere or</span>
              </p>
              <input id="hidden-input" type="file" multiple class="hidden" />
              <button id="button" class="mt-2 rounded-sm px-3 py-1 bg-gray-200 hover:bg-gray-300 focus:shadow-outline focus:outline-none">
                Upload a file
              </button>
            </header>

            <h1 class="pt-8 pb-3 font-semibold sm:text-lg text-gray-900">
              To Upload
            </h1>

            <ul id="gallery" class="flex flex-1 flex-wrap -m-1">
              <li id="empty" class="h-full w-full text-center flex flex-col items-center justify-center items-center">
                <img class="mx-auto w-32" src="https://user-images.githubusercontent.com/507615/54591670-ac0a0180-4a65-11e9-846c-e55ffce0fe7b.png" alt="no data" />
                <span class="text-small text-gray-500">No files selected</span>
              </li>
            </ul>
          </section>

          <!-- sticky footer -->
          <footer class="flex justify-end px-8 pb-8 pt-4">
            <button id="submit" class="rounded-sm px-3 py-1 bg-blue-700 hover:bg-blue-500 text-white focus:shadow-outline focus:outline-none">
              Upload now
            </button>
            <button id="cancel" class="ml-3 rounded-sm px-3 py-1 hover:bg-gray-300 focus:shadow-outline focus:outline-none">
              Cancel
            </button>
          </footer>
        </article>
      </main>
    </div>

    <!-- using two similar templates for simplicity in js code -->
    <template id="file-template">
      <li class="block p-1 w-1/2 sm:w-1/3 md:w-1/4 lg:w-1/6 xl:w-1/8 h-24">
        <article tabindex="0" class="group w-full h-full rounded-md focus:outline-none focus:shadow-outline elative bg-gray-100 cursor-pointer relative shadow-sm">
          <img alt="upload preview" class="img-preview hidden w-full h-full sticky object-cover rounded-md bg-fixed" />

          <section class="flex flex-col rounded-md text-xs break-words w-full h-full z-20 absolute top-0 py-2 px-3">
            <h1 class="flex-1 group-hover:text-blue-800"></h1>
            <div class="flex">
              <span class="p-1 text-blue-800">
                <i>
                  <svg class="fill-current w-4 h-4 ml-auto pt-1" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
                    <path d="M15 2v5h5v15h-16v-20h11zm1-2h-14v24h20v-18l-6-6z" />
                  </svg>
                </i>
              </span>
              <p class="p-1 size text-xs text-gray-700"></p>
              <button class="delete ml-auto focus:outline-none hover:bg-gray-300 p-1 rounded-md text-gray-800">
                <svg class="pointer-events-none fill-current w-4 h-4 ml-auto" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
                  <path class="pointer-events-none" d="M3 6l3 18h12l3-18h-18zm19-4v2h-20v-2h5.711c.9 0 1.631-1.099 1.631-2h5.316c0 .901.73 2 1.631 2h5.711z" />
                </svg>
              </button>
            </div>
          </section>
        </article>
      </li>
    </template>

    <template id="image-template">
      <li class="block p-1 w-1/2 sm:w-1/3 md:w-1/4 lg:w-1/6 xl:w-1/8 h-24">
        <article tabindex="0" class="group hasImage w-full h-full rounded-md focus:outline-none focus:shadow-outline bg-gray-100 cursor-pointer relative text-transparent hover:text-white shadow-sm">
          <img alt="upload preview" class="img-preview w-full h-full sticky object-cover rounded-md bg-fixed" />

          <section class="flex flex-col rounded-md text-xs break-words w-full h-full z-20 absolute top-0 py-2 px-3">
            <h1 class="flex-1"></h1>
            <div class="flex">
              <span class="p-1">
                <i>
                  <svg class="fill-current w-4 h-4 ml-auto pt-" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
                    <path d="M5 8.5c0-.828.672-1.5 1.5-1.5s1.5.672 1.5 1.5c0 .829-.672 1.5-1.5 1.5s-1.5-.671-1.5-1.5zm9 .5l-2.519 4-2.481-1.96-4 5.96h14l-5-8zm8-4v14h-20v-14h20zm2-2h-24v18h24v-18z" />
                  </svg>
                </i>
              </span>

              <p class="p-1 size text-xs"></p>
              <button class="delete ml-auto focus:outline-none hover:bg-gray-300 p-1 rounded-md">
                <svg class="pointer-events-none fill-current w-4 h-4 ml-auto" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
                  <path class="pointer-events-none" d="M3 6l3 18h12l3-18h-18zm19-4v2h-20v-2h5.711c.9 0 1.631-1.099 1.631-2h5.316c0 .901.73 2 1.631 2h5.711z" />
                </svg>
              </button>
            </div>
          </section>
        </article>
      </li>
    </template>

<script>
const fileTempl = document.getElementById("file-template"),
  imageTempl = document.getElementById("image-template"),
  empty = document.getElementById("empty");

// use to store pre selected files
let FILES = {};

// check if file is of type image and prepend the initialied
// template to the target element
function addFile(target, file) {
  const isImage = file.type.match("image.*"),
    objectURL = URL.createObjectURL(file);

  const clone = isImage
    ? imageTempl.content.cloneNode(true)
    : fileTempl.content.cloneNode(true);

  clone.querySelector("h1").textContent = file.name;
  clone.querySelector("li").id = objectURL;
  clone.querySelector(".delete").dataset.target = objectURL;
  clone.querySelector(".size").textContent =
    file.size > 1024
      ? file.size > 1048576
        ? Math.round(file.size / 1048576) + "mb"
        : Math.round(file.size / 1024) + "kb"
      : file.size + "b";

  isImage &&
    Object.assign(clone.querySelector("img"), {
      src: objectURL,
      alt: file.name
    });

  empty.classList.add("hidden");
  target.prepend(clone);

  FILES[objectURL] = file;
}

const gallery = document.getElementById("gallery"),
  overlay = document.getElementById("overlay");

// click the hidden input of type file if the visible button is clicked
// and capture the selected files
const hidden = document.getElementById("hidden-input");
document.getElementById("button").onclick = () => hidden.click();
hidden.onchange = (e) => {
  for (const file of e.target.files) {
    addFile(gallery, file);
  }
};

// use to check if a file is being dragged
const hasFiles = ({ dataTransfer: { types = [] } }) =>
  types.indexOf("Files") > -1;

// use to drag dragenter and dragleave events.
// this is to know if the outermost parent is dragged over
// without issues due to drag events on its children
let counter = 0;

// reset counter and append file to gallery when file is dropped
function dropHandler(ev) {
  ev.preventDefault();
  for (const file of ev.dataTransfer.files) {
    addFile(gallery, file);
    overlay.classList.remove("draggedover");
    counter = 0;
  }
}

// only react to actual files being dragged
function dragEnterHandler(e) {
  e.preventDefault();
  if (!hasFiles(e)) {
    return;
  }
  ++counter && overlay.classList.add("draggedover");
}

function dragLeaveHandler(e) {
  1 > --counter && overlay.classList.remove("draggedover");
}

function dragOverHandler(e) {
  if (hasFiles(e)) {
    e.preventDefault();
  }
}

// event delegation to caputre delete events
// fron the waste buckets in the file preview cards
gallery.onclick = ({ target }) => {
  if (target.classList.contains("delete")) {
    const ou = target.dataset.target;
    document.getElementById(ou).remove(ou);
    gallery.children.length === 1 && empty.classList.remove("hidden");
    delete FILES[ou];
  }
};

// print all selected files
document.getElementById("submit").onclick = () => {
  alert(`Submitted Files:\n${JSON.stringify(FILES)}`);
  console.log(FILES);
};

// clear entire selection
document.getElementById("cancel").onclick = () => {
  while (gallery.children.length > 0) {
    gallery.lastChild.remove();
  }
  FILES = {};
  empty.classList.remove("hidden");
  gallery.append(empty);
};

</script>

<style>
.hasImage:hover section {
  background-color: rgba(5, 5, 5, 0.4);
}
.hasImage:hover button:hover {
  background: rgba(5, 5, 5, 0.45);
}

#overlay p,
i {
  opacity: 0;
}

#overlay.draggedover {
  background-color: rgba(255, 255, 255, 0.7);
}
#overlay.draggedover p,
#overlay.draggedover i {
  opacity: 1;
}

.group:hover .group-hover\:text-blue-800 {
  color: #2b6cb0;
}
</style>

How to build a Responsive Multi File Upload with drop-on and preview with Tailwind CSS?

Install tailwind css of verion 1.2.0

Use the link html tag to import the stylesheet of Tailwind CSS of the version 1.2.0

<link href=https://unpkg.com/[email protected]/dist/tailwind.min.css rel="stylesheet">

All the unility class needed to build a Responsive Multi File Upload with drop-on and preview component

  • bg-gray-500
  • h-screen
  • w-screen
  • sm:px-8
  • md:px-16
  • sm:py-8
  • mx-auto
  • max-w-screen-lg
  • h-full
  • relative
  • flex
  • flex-col
  • bg-white
  • w-full
  • absolute
  • top-0
  • left-0
  • z-50
  • w-12
  • h-12
  • mb-3
  • text-blue-700
  • text-lg
  • overflow-auto
  • p-8
  • border-dashed
  • border-2
  • border-gray-400
  • py-12
  • text-gray-900
  • flex-wrap
  • hidden
  • mt-2
  • px-3
  • py-1
  • bg-gray-200
  • hover:bg-gray-300
  • pt-8
  • pb-3
  • sm:text-lg
  • flex-1
  • -m-1
  • text-center
  • w-32
  • text-small
  • text-gray-500
  • px-8
  • pb-8
  • pt-4
  • bg-blue-700
  • hover:bg-blue-500
  • text-white
  • ml-3
  • block
  • p-1
  • w-1/2
  • sm:w-1/3
  • md:w-1/4
  • lg:w-1/6
  • xl:w-1/8
  • h-24
  • bg-gray-100
  • sticky
  • bg-fixed
  • text-xs
  • z-20
  • py-2
  • group-hover:text-blue-800
  • text-blue-800
  • w-4
  • h-4
  • ml-auto
  • pt-1
  • text-gray-700
  • text-gray-800
  • text-transparent
  • hover:text-white
  • pt-

78 steps to build a Responsive Multi File Upload with drop-on and preview component with Tailwind CSS

  1. Control the background color of an element to gray-500 using the bg-gray-500 utilities.

  2. Use h-screen to make an element span the entire height of the viewport.

  3. Use w-screen to make an element span the entire width of the viewport.

  4. Control the horizontal padding of an element to 2rem at only small screen sizes using the sm:px-8 utilities.

  5. Control the horizontal padding of an element to 4rem at only medium screen sizes using the md:px-16 utilities.

  6. Control the vertical padding of an element to 2rem at only small screen sizes using the sm:py-8 utilities.

  7. Control the horizontal margin of an element to auto using the mx-auto utilities.

  8. Set the maximum width/height of an element using the max-w-screen-lg utilities.

  9. Use h-full to set an element’s height to 100% of its parent, as long as the parent has a defined height.

  10. Use relative to position an element according to the normal flow of the document.

  11. Use flex to create a block-level flex container.

  12. Use flex to create a block-level flex container.

  13. Control the background color of an element to white using the bg-white utilities.

  14. Use w-full to set an element to a 100% based width.

  15. Use absolute to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn’t exist.

  16. Use the top-0 utilities to set the top position of a positioned element to 0rem.

  17. Use the left-0 utilities to set the left position of a positioned element to 0rem.

  18. Control the stack order (or three-dimensional positioning) of an element to 50 in Tailwind, regardless of order it has been displayed, using the z-50 utilities.

  19. Use w-12 to set an element to a fixed width(3rem).

  20. Use h-12 to set an element to a fixed height(3rem).

  21. Control the margin on bottom side of an element to 0.75rem using the mb-3 utilities.

  22. Control the text color of an element to blue-700 using the text-blue-700 utilities.

  23. Control the text color of an element to lg using the text-lg utilities.

  24. Use overflow-auto to add scrollbars to an element in the event that its content overflows the bounds of that element. Unlike .overflow-scroll, which always shows scrollbars, this utility will only show them if scrolling is necessary.

  25. Control the padding on all sides of an element to 2rem using the p-8 utilities.

  26. Control the border color of an element to dashed using the border-dashed utilities.

  27. Control the border color of an element to 0.5rem using the border-2 utilities.

  28. Control the border color of an element to gray-400 using the border-gray-400 utilities.

  29. Control the vertical padding of an element to 3rem using the py-12 utilities.

  30. Control the text color of an element to gray-900 using the text-gray-900 utilities.

  31. Use flex to create a block-level flex container.

  32. Use hidden to set an element to display: none and remove it from the page layout.

  33. Control the margin on top side of an element to 0.5rem using the mt-2 utilities.

  34. Control the horizontal padding of an element to 0.75rem using the px-3 utilities.

  35. Control the vertical padding of an element to 0.25rem using the py-1 utilities.

  36. Control the background color of an element to gray-200 using the bg-gray-200 utilities.

  37. Control the background color of an element to gray-300 using the hover:bg-gray-300 utilities on hover.

  38. Control the padding on top side of an element to 2rem using the pt-8 utilities.

  39. Control the padding on bottom side of an element to 0.75rem using the pb-3 utilities.

  40. Control the text color of an element to lg at only small screen sizes using the sm:text-lg utilities.

  41. Use flex to create a block-level flex container.

  42. Control the margin on all sides of an element to -0.25rem using the -m-1 utilities.

  43. Control the text color of an element to center using the text-center utilities.

  44. Use w-32 to set an element to a fixed width(8rem).

  45. Control the text color of an element to small using the text-small utilities.

  46. Control the text color of an element to gray-500 using the text-gray-500 utilities.

  47. Control the horizontal padding of an element to 2rem using the px-8 utilities.

  48. Control the padding on bottom side of an element to 2rem using the pb-8 utilities.

  49. Control the padding on top side of an element to 1rem using the pt-4 utilities.

  50. Control the background color of an element to blue-700 using the bg-blue-700 utilities.

  51. Control the background color of an element to blue-500 using the hover:bg-blue-500 utilities on hover.

  52. Control the text color of an element to white using the text-white utilities.

  53. Control the margin on left side of an element to 0.75rem using the ml-3 utilities.

  54. Use inline utilities to put the element on its own line and fill its parent.

  55. Control the padding on all sides of an element to 0.25rem using the p-1 utilities.

  56. Use w-1/2 to set an element to a fixed width(1/2).

  57. Use sm:w-1/3 to set an element to a fixed width(1/3) at only small screen sizes.

  58. Use md:w-1/4 to set an element to a fixed width(1/4) at only medium screen sizes.

  59. Use lg:w-1/6 to set an element to a fixed width(1/6) at only large screen sizes.

  60. Use xl:w-1/8 to set an element to a fixed width(1/8) at only extremely large screen sizes.

  61. Use h-24 to set an element to a fixed height(6rem).

  62. Control the background color of an element to gray-100 using the bg-gray-100 utilities.

  63. Use sticky to position an element as relative until it crosses a specified threshold, then treat it as fixed until its parent is off screen.

  64. Control the background color of an element to fixed using the bg-fixed utilities.

  65. Control the text color of an element to xs using the text-xs utilities.

  66. Control the stack order (or three-dimensional positioning) of an element to 20 in Tailwind, regardless of order it has been displayed, using the z-20 utilities.

  67. Control the vertical padding of an element to 0.5rem using the py-2 utilities.

  68. Control the text color of an element to blue-800undefined using the group-hover:text-blue-800 utilities.

  69. Control the text color of an element to blue-800 using the text-blue-800 utilities.

  70. Use w-4 to set an element to a fixed width(1rem).

  71. Use h-4 to set an element to a fixed height(1rem).

  72. Control the margin on left side of an element to auto using the ml-auto utilities.

  73. Control the padding on top side of an element to 0.25rem using the pt-1 utilities.

  74. Control the text color of an element to gray-700 using the text-gray-700 utilities.

  75. Control the text color of an element to gray-800 using the text-gray-800 utilities.

  76. Control the text color of an element to transparent using the text-transparent utilities.

  77. Control the text color of an element to white on hover using the hover:text-white utilities.

  78. Control the padding on top side of an element to using the pt- utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to build a Responsive Multi File Upload with drop-on and preview components, learn and follow along to implement your own components.