Skip to content

Commit 6154eb5

Browse files
authored
Merge pull request #362 from KevinBatdorf/move-block-into-view-on-collapse
2 parents 5482116 + d7fef33 commit 6154eb5

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

readme.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ Themes are rendered inside the editor as you type or make changes, so the code b
313313

314314
== Changelog ==
315315

316+
- Now scolls back into view after a max height collapse
317+
- Disables scroll while the container is expanding
318+
- Fixes a bug where collapsing doesn't account for headers
319+
316320
= 1.27.0 - 2025-05-11 =
317321
- Adds support for collapsing code blocks after expanding
318322
- Tweaks the blur effect for better readability

src/front/front.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ const handleSeeMore = () => {
132132
const pre = line.closest('pre');
133133
const initialHeight = pre.offsetHeight;
134134
let animationSpeed = 0;
135+
const transition = line.classList.contains('cbp-see-more-transition');
135136

136-
if (line.classList.contains('cbp-see-more-transition')) {
137+
if (transition) {
137138
const lineCount = pre.querySelectorAll('code > *').length;
138139
const linesBeforeCurrent = Array.from(
139140
line.closest('code').children,
@@ -169,17 +170,29 @@ const handleSeeMore = () => {
169170

170171
const handle = (event) => {
171172
event.preventDefault();
172-
button.classList.remove('cbp-see-more-simple-btn-hover');
173-
pre.style.maxHeight = initialHeight + 'px';
173+
// disable scrolling
174+
pre.style.setProperty('overflow', 'hidden', 'important');
175+
setTimeout(() => {
176+
pre.style.overflow = 'auto';
177+
}, animationSpeed * 1000);
178+
179+
pre.style.maxHeight = `${initialHeight}px`;
174180
// If there is data-see-more-collapse-string then we toggle
175181
if (!buttonContainer.dataset?.seeMoreCollapseString) {
176182
buttonContainer.remove();
177183
return;
178184
}
185+
// We're letting them collapse it
179186
if (button.getAttribute('aria-expanded') === 'true') {
180187
button.setAttribute('aria-expanded', 'false');
181188
button.innerText = buttonContainer.dataset.seeMoreString;
182-
pre.style.maxHeight = `${line.offsetTop + lineHeight}px`;
189+
pre.style.maxHeight = `${line.offsetTop + lineHeight - headerHeight}px`;
190+
191+
// Move the scroll so the button is in center view
192+
line.scrollIntoView({
193+
behavior: transition ? 'smooth' : 'auto',
194+
block: 'center',
195+
});
183196
return;
184197
}
185198
button.setAttribute('aria-expanded', 'true');

0 commit comments

Comments
 (0)