Skip to content

Commit

Permalink
Don't refer to more content button if always_true is set
Browse files Browse the repository at this point in the history
If you set `always_show: true` on a trip field and it has more than 50 characters there is a javascript error:

    Error: Missing target element "moreContentButton" for "trix-body" controller

This is because the trix field show component only adds the more content button when `always_show` is `false` but the trix controller always refers to that component without checking if it exists first.

I've updated it so that it checks if that target exists before manipulating it.
  • Loading branch information
iainbeeston authored Nov 25, 2024
1 parent c60e854 commit 9460798
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions app/javascript/js/controllers/trix_body_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class extends Controller {
checkContentHeight() {
const contentHeight = this.contentTarget.scrollHeight

if (contentHeight > 50) {
if (contentHeight > 50 && this.hasMoreContentButtonTarget) {
this.moreContentButtonTarget.classList.remove('hidden')
}
}
Expand All @@ -21,7 +21,7 @@ export default class extends Controller {
}

toggleButtons() {
this.moreContentButtonTarget.classList.toggle('hidden')
this.lessContentButtonTarget.classList.toggle('hidden')
if (this.hasMoreContentButtonTarget) this.moreContentButtonTarget.classList.toggle('hidden')
if (this.hasLessContentButtonTarget) this.lessContentButtonTarget.classList.toggle('hidden')
}
}

0 comments on commit 9460798

Please sign in to comment.