Skip to content

Commit 270a0a7

Browse files
authored
Merge pull request #2868 from L0kayata/dev
🐛 Fix `carousel` caption height overflow and layout jump
2 parents a57db85 + 1f0a84a commit 270a0a7

1 file changed

Lines changed: 44 additions & 7 deletions

File tree

layouts/shortcodes/carousel.html

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@
3939
<div
4040
id="{{ $id }}"
4141
class="relative"
42+
style="container-type: inline-size;"
4243
data-twe-carousel-init
4344
data-twe-ride="carousel"
4445
data-twe-interval="{{ $interval }}">
4546
<div
46-
class="absolute right-0 bottom-0 left-0 z-2 mx-[15%] mb-10 flex list-none justify-center p-0"
47+
class="absolute right-0 left-0 z-2 mx-[15%] mb-10 flex list-none justify-center p-0"
48+
style="top: calc(100cqi * {{ $aspecty }} / {{ $aspectx }} - 2.5rem);"
4749
data-twe-carousel-indicators>
4850
{{ $num := 0 }}
4951
{{ range $images }}
@@ -89,18 +91,19 @@
8991
class="block absolute top-0 object-cover w-full h-full not-prose nozoom"
9092
alt="carousel image {{ add $index 1 }}">
9193
</div>
92-
{{ if $caption }}
94+
{{ if or $caption $captionsParam }}
9395
<figcaption
94-
class="absolute left-0 right-0"
95-
style="top: calc(100%);"
96-
>{{ $caption | markdownify }}</figcaption>
96+
class="mt-1 {{ if not $caption }}invisible{{ end }}"
97+
{{ if not $caption }}aria-hidden="true"{{ end }}
98+
>{{ if $caption }}{{ $caption | markdownify }}{{ else }}&nbsp;{{ end }}</figcaption>
9799
{{ end }}
98100
</div>
99101
{{ end }}
100102
</div>
101103

102104
<button
103-
class="absolute top-0 bottom-0 left-0 z-2 flex w-[15%] items-center justify-center border-0 bg-none p-0 text-center opacity-50 transition-opacity duration-150 ease-[cubic-bezier(0.25,0.1,0.25,1.0)] hover:no-underline hover:opacity-90 hover:outline-none focus:no-underline focus:opacity-90 focus:outline-none motion-reduce:transition-none"
105+
class="absolute top-0 left-0 z-2 flex w-[15%] items-center justify-center border-0 bg-none p-0 text-center opacity-50 transition-opacity duration-150 ease-[cubic-bezier(0.25,0.1,0.25,1.0)] hover:no-underline hover:opacity-90 hover:outline-none focus:no-underline focus:opacity-90 focus:outline-none motion-reduce:transition-none"
106+
style="height: calc(100cqi * {{ $aspecty }} / {{ $aspectx }});"
104107
type="button"
105108
data-twe-target="#{{ $id }}"
106109
data-twe-slide="prev">
@@ -122,7 +125,8 @@
122125
</button>
123126

124127
<button
125-
class="absolute top-0 bottom-0 right-0 z-[1] flex w-[15%] items-center justify-center border-0 bg-none p-0 text-center opacity-50 transition-opacity duration-150 ease-[cubic-bezier(0.25,0.1,0.25,1.0)] hover:no-underline hover:opacity-90 hover:outline-none focus:no-underline focus:opacity-90 focus:outline-none motion-reduce:transition-none"
128+
class="absolute top-0 right-0 z-[1] flex w-[15%] items-center justify-center border-0 bg-none p-0 text-center opacity-50 transition-opacity duration-150 ease-[cubic-bezier(0.25,0.1,0.25,1.0)] hover:no-underline hover:opacity-90 hover:outline-none focus:no-underline focus:opacity-90 focus:outline-none motion-reduce:transition-none"
129+
style="height: calc(100cqi * {{ $aspecty }} / {{ $aspectx }});"
126130
type="button"
127131
data-twe-target="#{{ $id }}"
128132
data-twe-slide="next">
@@ -143,3 +147,36 @@
143147
>
144148
</button>
145149
</div>
150+
151+
{{ if $captionsParam }}
152+
<script>
153+
(() => {
154+
const root = document.getElementById("{{ $id }}");
155+
if (!root) return;
156+
157+
const items = Array.from(root.querySelectorAll("[data-twe-carousel-item]"));
158+
const captions = items
159+
.map((item) => item.querySelector("figcaption"))
160+
.filter((caption) => caption);
161+
if (captions.length < 2) return;
162+
163+
const measureAndFixCaptionHeight = () => {
164+
const hiddenItems = items.filter((item) => item.classList.contains("hidden"));
165+
hiddenItems.forEach((item) => item.classList.remove("hidden"));
166+
167+
const maxHeight = Math.max(
168+
...captions.map((caption) => Math.ceil(caption.getBoundingClientRect().height)),
169+
);
170+
171+
hiddenItems.forEach((item) => item.classList.add("hidden"));
172+
captions.forEach((caption) => {
173+
caption.style.minHeight = `${maxHeight}px`;
174+
});
175+
};
176+
177+
window.requestAnimationFrame(measureAndFixCaptionHeight);
178+
window.addEventListener("load", measureAndFixCaptionHeight, { once: true });
179+
window.addEventListener("resize", measureAndFixCaptionHeight);
180+
})();
181+
</script>
182+
{{ end }}

0 commit comments

Comments
 (0)