From 9460798fd9b9e29c3e342b1e72fb45c6a1443759 Mon Sep 17 00:00:00 2001 From: Iain Beeston Date: Mon, 25 Nov 2024 14:23:03 +0000 Subject: [PATCH] Don't refer to more content button if always_true is set 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. --- app/javascript/js/controllers/trix_body_controller.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/javascript/js/controllers/trix_body_controller.js b/app/javascript/js/controllers/trix_body_controller.js index a63860c5b..9b8eff22c 100644 --- a/app/javascript/js/controllers/trix_body_controller.js +++ b/app/javascript/js/controllers/trix_body_controller.js @@ -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') } } @@ -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') } }