mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 17:11:10 +00:00
Adds theme previews to the themes list with a reusable image viewer
This commit is contained in:
1 parent
74e5efdf74
commit
ebafd743b5
16 files changed
+416
-7
No files matched your search
@@ -0,0 +1,8 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="120" viewBox="0 0 200 120">
|
||||
<rect width="200" height="120" fill="#232323"/>
|
||||
<rect x="0" y="0" width="200" height="3" fill="#404040"/>
|
||||
<rect x="70" y="30" width="60" height="46" rx="4" fill="none" stroke="#404040" stroke-width="4"/>
|
||||
<circle cx="90" cy="44" r="5" fill="#404040"/>
|
||||
<polygon points="70,76 100,50 116,66 130,54 140,76" fill="#404040"/>
|
||||
<text x="100" y="104" font-family="monospace" font-size="10" fill="#6DCBFE" text-anchor="middle">No preview</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 542 B |
@@ -0,0 +1,146 @@
|
||||
.image-viewer-overlay {
|
||||
display: none;
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
z-index: 1001;
|
||||
background-color: rgba(0, 0, 0, 0.85);
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.image-viewer-overlay.open {
|
||||
display: flex;
|
||||
animation: fadeIn 0.3s ease-out;
|
||||
}
|
||||
|
||||
.image-viewer-stage {
|
||||
position: relative;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
max-width: 92%;
|
||||
max-height: 92%;
|
||||
}
|
||||
|
||||
.image-viewer-image {
|
||||
max-width: 100%;
|
||||
max-height: 88vh;
|
||||
object-fit: contain;
|
||||
opacity: 0;
|
||||
border: 1px solid #505050;
|
||||
background-color: #000;
|
||||
box-shadow: 0 0 20px rgba(0, 0, 0, 0.6);
|
||||
}
|
||||
|
||||
.image-viewer-image.loaded {
|
||||
opacity: 1;
|
||||
transition: opacity 0.2s ease-out;
|
||||
}
|
||||
|
||||
.image-viewer-caption {
|
||||
margin-top: 12px;
|
||||
color: var(--text-foreground);
|
||||
font-family: monospace;
|
||||
font-size: 0.95rem;
|
||||
text-align: center;
|
||||
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
|
||||
}
|
||||
|
||||
.image-viewer-spinner {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
margin: -20px 0 0 -20px;
|
||||
border: 4px solid rgba(255, 255, 255, 0.25);
|
||||
border-top-color: #fff;
|
||||
border-radius: 50%;
|
||||
animation: image-viewer-spin 0.8s linear infinite;
|
||||
}
|
||||
|
||||
@keyframes image-viewer-spin {
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
.image-viewer-button {
|
||||
position: absolute;
|
||||
width: 44px;
|
||||
height: 44px;
|
||||
border: 1px solid #505050;
|
||||
border-radius: 50%;
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.image-viewer-button:hover {
|
||||
border-color: var(--text-foreground);
|
||||
background-color: rgba(0, 0, 0, 0.75);
|
||||
}
|
||||
|
||||
.image-viewer-button:disabled {
|
||||
opacity: 0.35;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.image-viewer-close {
|
||||
top: 18px;
|
||||
right: 18px;
|
||||
}
|
||||
|
||||
.image-viewer-close::before,
|
||||
.image-viewer-close::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
width: 20px;
|
||||
height: 2px;
|
||||
background-color: var(--text-foreground);
|
||||
}
|
||||
|
||||
.image-viewer-close::before {
|
||||
transform: rotate(45deg);
|
||||
}
|
||||
|
||||
.image-viewer-close::after {
|
||||
transform: rotate(-45deg);
|
||||
}
|
||||
|
||||
.image-viewer-prev {
|
||||
left: 18px;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
}
|
||||
|
||||
.image-viewer-prev::before {
|
||||
content: '';
|
||||
width: 12px;
|
||||
height: 12px;
|
||||
margin-left: 5px;
|
||||
border-left: 3px solid var(--text-foreground);
|
||||
border-bottom: 3px solid var(--text-foreground);
|
||||
transform: rotate(45deg);
|
||||
}
|
||||
|
||||
.image-viewer-next {
|
||||
right: 18px;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
}
|
||||
|
||||
.image-viewer-next::before {
|
||||
content: '';
|
||||
width: 12px;
|
||||
height: 12px;
|
||||
margin-right: 5px;
|
||||
border-right: 3px solid var(--text-foreground);
|
||||
border-top: 3px solid var(--text-foreground);
|
||||
transform: rotate(45deg);
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
/**
|
||||
* Manages the full-screen image viewer overlay, used to display a single image or a small photo gallery.
|
||||
*/
|
||||
class ImageViewerService {
|
||||
constructor() {
|
||||
this.overlay = null;
|
||||
this.imageElement = null;
|
||||
this.captionElement = null;
|
||||
this.spinnerElement = null;
|
||||
this.prevButton = null;
|
||||
this.nextButton = null;
|
||||
this.items = [];
|
||||
this.currentIndex = 0;
|
||||
this.keydownHandler = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the viewer with the given images.
|
||||
* @param {Array<{src: string, title?: string}>|{src: string, title?: string}} items - The image(s) to display.
|
||||
*/
|
||||
open(items) {
|
||||
this.items = Array.isArray(items) ? items : [items];
|
||||
if (this.items.length === 0)
|
||||
return;
|
||||
this.ensureOverlay();
|
||||
this.currentIndex = 0;
|
||||
this.show();
|
||||
this.showItem(this.currentIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the viewer and releases the currently displayed image.
|
||||
*/
|
||||
close() {
|
||||
if (!this.overlay)
|
||||
return;
|
||||
this.overlay.classList.remove('open');
|
||||
if (this.keydownHandler) {
|
||||
document.removeEventListener('keydown', this.keydownHandler);
|
||||
this.keydownHandler = null;
|
||||
}
|
||||
// stop the browser from finishing the load of the previous image in the background
|
||||
this.imageElement.removeAttribute('src');
|
||||
this.items = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the previous image of the gallery, when there is one.
|
||||
*/
|
||||
previous() {
|
||||
if (this.currentIndex > 0)
|
||||
this.showItem(this.currentIndex - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the next image of the gallery, when there is one.
|
||||
*/
|
||||
next() {
|
||||
if (this.currentIndex < this.items.length - 1)
|
||||
this.showItem(this.currentIndex + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the overlay and appends it to the document body the first time it is used.
|
||||
* @private
|
||||
*/
|
||||
ensureOverlay() {
|
||||
if (this.overlay)
|
||||
return;
|
||||
|
||||
this.overlay = document.createElement('div');
|
||||
this.overlay.className = 'image-viewer-overlay';
|
||||
this.overlay.innerHTML = `
|
||||
<button type="button" class="image-viewer-button image-viewer-close" aria-label="Close"></button>
|
||||
<button type="button" class="image-viewer-button image-viewer-prev" aria-label="Previous"></button>
|
||||
<div class="image-viewer-stage">
|
||||
<div class="image-viewer-spinner"></div>
|
||||
<img class="image-viewer-image" alt="" />
|
||||
<div class="image-viewer-caption"></div>
|
||||
</div>
|
||||
<button type="button" class="image-viewer-button image-viewer-next" aria-label="Next"></button>
|
||||
`;
|
||||
|
||||
this.imageElement = this.overlay.querySelector('.image-viewer-image');
|
||||
this.captionElement = this.overlay.querySelector('.image-viewer-caption');
|
||||
this.spinnerElement = this.overlay.querySelector('.image-viewer-spinner');
|
||||
this.prevButton = this.overlay.querySelector('.image-viewer-prev');
|
||||
this.nextButton = this.overlay.querySelector('.image-viewer-next');
|
||||
|
||||
// clicking the dimmed background closes the viewer, but not the image or the controls
|
||||
this.overlay.addEventListener('click', (e) => {
|
||||
if (e.target === this.overlay)
|
||||
this.close();
|
||||
});
|
||||
|
||||
// reveal the image only after it finished loading, so the spinner is visible meanwhile
|
||||
const handleImageLoaded = () => {
|
||||
this.spinnerElement.style.display = 'none';
|
||||
this.imageElement.classList.add('loaded');
|
||||
};
|
||||
this.imageElement.addEventListener('load', handleImageLoaded);
|
||||
this.imageElement.addEventListener('error', handleImageLoaded);
|
||||
|
||||
this.overlay.querySelector('.image-viewer-close').addEventListener('click', () => this.close());
|
||||
this.prevButton.addEventListener('click', (e) => {
|
||||
e.stopPropagation();
|
||||
this.previous();
|
||||
});
|
||||
this.nextButton.addEventListener('click', (e) => {
|
||||
e.stopPropagation();
|
||||
this.next();
|
||||
});
|
||||
|
||||
document.body.appendChild(this.overlay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the overlay and registers the keyboard navigation.
|
||||
* @private
|
||||
*/
|
||||
show() {
|
||||
this.overlay.classList.add('open');
|
||||
this.keydownHandler = (e) => {
|
||||
if (e.key === 'Escape')
|
||||
this.close();
|
||||
else if (e.key === 'ArrowLeft')
|
||||
this.previous();
|
||||
else if (e.key === 'ArrowRight')
|
||||
this.next();
|
||||
};
|
||||
document.addEventListener('keydown', this.keydownHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the image at the given index and updates the gallery controls.
|
||||
* @private
|
||||
* @param {number} index - The index of the image to display.
|
||||
*/
|
||||
showItem(index) {
|
||||
if (index < 0 || index >= this.items.length)
|
||||
return;
|
||||
this.currentIndex = index;
|
||||
const item = this.items[index];
|
||||
|
||||
this.spinnerElement.style.display = 'block';
|
||||
this.imageElement.classList.remove('loaded');
|
||||
this.imageElement.alt = item.title || '';
|
||||
this.imageElement.src = item.src;
|
||||
|
||||
if (item.title) {
|
||||
this.captionElement.textContent = item.title;
|
||||
this.captionElement.style.display = 'block';
|
||||
}
|
||||
else {
|
||||
this.captionElement.textContent = '';
|
||||
this.captionElement.style.display = 'none';
|
||||
}
|
||||
|
||||
const hasMultipleItems = this.items.length > 1;
|
||||
this.prevButton.style.display = hasMultipleItems ? 'flex' : 'none';
|
||||
this.nextButton.style.display = hasMultipleItems ? 'flex' : 'none';
|
||||
this.prevButton.disabled = index === 0;
|
||||
this.nextButton.disabled = index === this.items.length - 1;
|
||||
}
|
||||
}
|
||||
|
||||
// create a global instance of the ImageViewerService
|
||||
const imageViewer = new ImageViewerService();
|
||||
Reference in new issue
Block a user