Fix tess-expandable behaviour when multiple under the same parent e…#1234
Fix tess-expandable behaviour when multiple under the same parent e…#1234
tess-expandable behaviour when multiple under the same parent e…#1234Conversation
There was a problem hiding this comment.
Pull request overview
Fixes incorrect “Show more/less” toggling when multiple .tess-expandable sections exist under the same parent (Issue #1231), by explicitly associating each button with its intended expandable element instead of relying on a parent selector.
Changes:
- Create the “Show more” button and bind it to a specific
.tess-expandableelement. - Update the click handler to use the bound target rather than
parentElement.querySelector(...).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| var div = this.parentElement.querySelector('.tess-expandable'); | ||
| var maxHeight = parseInt(div.dataset.origHeight) + 80; | ||
| var limit = parseInt(div.dataset.heightLimit || "300"); | ||
| const div = this.expandableTarget; |
There was a problem hiding this comment.
Binding the expandable div via a custom JS property (btn[0].expandableTarget) is not preserved across Turbolinks snapshot caching (custom/expando properties are lost when the DOM is serialized/cloned). Because the code also short-circuits when data-orig-height is present ("Prevent double bind"), a cached page restored via back/forward navigation can leave .tess-expandable-btn in the DOM without expandableTarget, causing the click handler to throw when it reads div.dataset.... Prefer persisting the association in the DOM (e.g., assign an id to the .tess-expandable and store it on the button via aria-controls/data-target, or re-bind expandableTarget to any existing adjacent button even when data-orig-height is already set).
| const div = this.expandableTarget; | |
| // Prefer the explicitly bound target, but fall back to the preceding | |
| // .tess-expandable element. This avoids relying solely on expando | |
| // properties, which are lost across Turbolinks snapshot caching. | |
| var div = this.expandableTarget || $(this).prev('.tess-expandable')[0]; | |
| if (!div) { | |
| // No associated expandable div found; do nothing to avoid errors. | |
| return false; | |
| } |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
app/assets/javascripts/application.js:288
- There are now two separate
dataset.origHeightguards (one before computinglimit, and another inside theclientHeight > limitblock). The inner check looks redundant and usesreturn true(continue) while the outer usesreturn(also continue), which makes the control flow harder to follow—consider keeping just one guard for clarity.
$('.tess-expandable').each(function () {
if (this.dataset.origHeight) {
return;
}
var limit = this.dataset.heightLimit || 300;
if (this.clientHeight > limit) {
if (this.dataset.origHeight) { // Prevent double bind
return true;
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Summary of changes
Motivation and context
#1231
Checklist
to license it to the TeSS codebase under the
BSD license.