Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Lightbox Feature #242

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@

&-picture {
max-width: 100%;
vertical-align: bottom;
display: block;
}

img {
cursor: zoom-in;
}

&-preloader {
width: 50px;
height: 50px;
Expand Down Expand Up @@ -157,3 +160,48 @@
transform: rotate(360deg);
}
}

.image-lightbox {
z-index: 1000;
display: block;
position: fixed;
overflow: hidden;
top:0;
right: 0;
left: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.1);
transition: opacity .15s linear;
touch-action: pinch-zoom;

&__item {
display: flex;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
justify-content: center;
align-items: center;
will-change: transform opacity;

img {
width: auto;
height: auto;
max-width: 100vw;
max-height: 100vh;
cursor: pointer;
}
}

&__close {
position: absolute;
top: 5px;
right: 5px;
cursor: pointer;
color: #fff;
font-size: 24px;
line-height: 1;
padding: 20px;
}
}
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,18 @@ export default class ImageTool {
*/
static get tunes() {
return [
{
name: 'withBorder',
icon: IconAddBorder,
title: 'With border',
toggle: true,
},
{
name: 'stretched',
icon: IconStretch,
title: 'Stretch image',
toggle: true,
},
{
name: 'withBorder',
icon: IconAddBorder,
title: 'With border',
toggle: true,
},
{
name: 'withBackground',
icon: IconAddBackground,
Expand Down Expand Up @@ -290,7 +290,7 @@ export default class ImageTool {
* Drag n drop file from into the Editor
*/
files: {
mimeTypes: [ 'image/*' ],
mimeTypes: [ 'image/*', 'video/mp4' ],
},
};
}
Expand Down
40 changes: 38 additions & 2 deletions src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default class Ui {
/**
* Check for a source extension to compose element correctly: video tag for mp4, img — for others
*/
const tag = /\.mp4$/.test(url) ? 'VIDEO' : 'IMG';
const tag = /\.mp4$/i.test(url) ? 'VIDEO' : 'IMG';

const attributes = {
src: url,
Expand Down Expand Up @@ -208,6 +208,12 @@ export default class Ui {
if (this.nodes.imagePreloader) {
this.nodes.imagePreloader.style.backgroundImage = '';
}

if (tag === 'IMG') {
this.nodes.imageEl.addEventListener('click', () => {
this.openLightbox(this.nodes.imageEl.src);
});
}
});

this.nodes.imageContainer.appendChild(this.nodes.imageEl);
Expand Down Expand Up @@ -249,5 +255,35 @@ export default class Ui {
applyTune(tuneName, status) {
this.nodes.wrapper.classList.toggle(`${this.CSS.wrapper}--${tuneName}`, status);
}
}

/**
* Open lightbox with an image
*
* @param {string} url - image source url
*/
openLightbox(url) {
const overflow = document.body.style.overflow;

const lightbox = make('div', 'image-lightbox');
const imageContainer = make('div', 'image-lightbox__item');
const image = make('img', 'image-lightbox__image', { src: url });
const closeBtn = make('button', 'image-lightbox__close');

const closeLightbox = () => {
document.body.style.overflow = overflow;
lightbox.remove();
};

closeBtn.innerHTML = '<svg width="20" height="20" viewBox="0 0 20 20"><line fill="none" stroke="#000" stroke-width="1.4" x1="1" y1="1" x2="19" y2="19"></line><line fill="none" stroke="#000" stroke-width="1.4" x1="19" y1="1" x2="1" y2="19"></line></svg>';

imageContainer.appendChild(image);
lightbox.appendChild(imageContainer);
lightbox.appendChild(closeBtn);

closeBtn.addEventListener('click', closeLightbox);
image.addEventListener('click', closeLightbox);

document.body.style.overflow = 'hidden';
document.body.appendChild(lightbox);
}
}