-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexpandable-img.vue
More file actions
145 lines (123 loc) · 3.03 KB
/
Copy pathexpandable-img.vue
File metadata and controls
145 lines (123 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<template>
<div ref="expandableImageRef" class="expandable-image" :class="{ expanded }" @click="expanded = true">
<div class="close-button" v-if="expanded" @click="closeViewer">
<j-icon name="x" size="xl" />
</div>
<div v-else class="expand-button">
<j-icon name="arrows-angle-expand" />
</div>
</div>
</template>
<script setup lang="ts">
import { nextTick, ref, watch } from 'vue';
defineOptions({ inheritAttrs: false });
const expanded = ref(false);
const clonedEl = ref<HTMLElement | null>(null);
const closeButtonRef = ref<HTMLElement | null>(null);
const expandableImageRef = ref<HTMLElement>();
function closeViewer() {
expanded.value = false;
}
// Watcher for expanded state
watch(expanded, async (isExpanded) => {
await nextTick();
if (isExpanded) {
// Clone the current element
clonedEl.value = expandableImageRef.value?.cloneNode(true) as HTMLElement;
if (clonedEl.value) {
// Find the close button in the cloned element
closeButtonRef.value = clonedEl.value.querySelector('.close-button');
// Add event listener to the close button
if (closeButtonRef.value) closeButtonRef.value.addEventListener('click', closeViewer);
// Append to body and set styles
document.body.appendChild(clonedEl.value);
document.body.style.overflow = 'hidden';
// Trigger opacity transition
setTimeout(() => {
if (clonedEl.value) clonedEl.value.style.opacity = '1';
}, 0);
}
} else {
// Close the expanded view
if (clonedEl.value) {
clonedEl.value.style.opacity = '0';
setTimeout(() => {
if (clonedEl.value) {
clonedEl.value.remove();
clonedEl.value = null;
closeButtonRef.value = null;
document.body.style.overflow = 'auto';
}
}, 250);
}
}
});
</script>
<style scoped>
.expandable-image {
position: relative;
transition: 0.25s opacity;
}
:global(body > .expandable-image.expanded) {
position: fixed;
z-index: 999999;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
display: flex;
align-items: center;
opacity: 0;
padding-bottom: 0 !important;
cursor: default;
background-origin: center;
background-position: center;
background-repeat: no-repeat;
}
:global(body > .expandable-image.expanded > img) {
width: 100%;
max-width: 1200px;
max-height: 100%;
object-fit: contain;
margin: 0 auto;
}
:global(body > .expandable-image.expanded > .close-button) {
display: block;
}
.close-button {
position: fixed;
top: 10px;
right: 10px;
display: none;
cursor: pointer;
}
:global(svg path) {
fill: #fff;
}
.expand-button {
position: absolute;
z-index: 999;
right: 10px;
top: 10px;
padding: 0px;
align-items: center;
justify-content: center;
padding: 3px;
opacity: 0;
transition: 0.2s opacity;
}
.expandable-image:hover .expand-button {
opacity: 1;
}
.expand-button svg {
width: 20px;
height: 20px;
}
.expand-button path {
fill: #fff;
}
.expandable-image img {
width: 100%;
}
</style>