Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Read More Button Display Logic to Avoid Unnecessary Display #9627

Closed
wants to merge 6 commits into from
13 changes: 11 additions & 2 deletions openlibrary/plugins/openlibrary/js/readmore.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ export class ReadMoreComponent {
this.$readMoreButton = container.querySelector('.read-more__toggle--more');
this.$readLessButton = container.querySelector('.read-more__toggle--less');

this.collapsedHeight = parseFloat(this.$content.style.maxHeight);
if (!this.$content || !this.$readMoreButton || !this.$readLessButton) {
return;
}
this.collapsedHeight = parseFloat(getComputedStyle(this.$content).maxHeight);
this.fullHeight = this.$content.scrollHeight;
this.manuallyExpanded = false;
}

attach() {
Expand Down Expand Up @@ -44,18 +48,23 @@ export class ReadMoreComponent {
expand() {
this.$container.classList.add('read-more--expanded');
this.$content.style.maxHeight = `${this.fullHeight}px`;
this.$readMoreButton.style.display = 'none';
this.$readLessButton.style.display = 'block';
}

collapse() {
this.$container.classList.remove('read-more--expanded');
this.$content.style.maxHeight = `${this.collapsedHeight}px`;
this.$readMoreButton.style.display = 'block';
this.$readLessButton.style.display = 'none';
}

reset() {
if (!this.$content || !this.$readMoreButton) return;
this.fullHeight = this.$content.scrollHeight;
// Fudge factor to account for non-significant read/more
// (e.g missing a bit of padding)
if (this.$content.scrollHeight <= (this.collapsedHeight + 1)) {
if (this.$content.scrollHeight <= (this.collapsedHeight + this.$readMoreButton.offsetHeight + 1)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be the only change that is necessary here. Please revert the other changes since they are unnecessary.

this.expand();
this.$container.classList.add('read-more--unnecessary');
} else {
Expand Down
Loading