Skip to content

Commit

Permalink
Prevent access to this.$sticky unless it is ready
Browse files Browse the repository at this point in the history
This change adds checks to ensure that this.$sticky exists and it is visible
before trying to access its methods.

Further information at #368
  • Loading branch information
marcotranchino authored and kr8n3r committed Oct 22, 2024
1 parent fe34f32 commit 30322df
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/assets/javascripts/_modules/table-of-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
this.isMonitoring = false
}
StickyOverlapMonitors.prototype.run = function () {
var stickyIsVisible = this.$sticky.is(':visible')
var stickyIsVisible = this.$sticky && this.$sticky.is(':visible')
if (stickyIsVisible && !this.isMonitoring) {
document.addEventListener('focus', this.onFocus, true)
this.isMonitoring = true
Expand All @@ -26,11 +26,13 @@

if (!applicable) { return }

var stickyEdge = this.$sticky.get(0).getBoundingClientRect().bottom + this.offset
var diff = focused.getBoundingClientRect().top - stickyEdge
if (this.$sticky && this.$sticky.is(':visible') && this.$sticky.get(0)) {
var stickyEdge = this.$sticky.get(0).getBoundingClientRect().bottom + this.offset
var diff = focused.getBoundingClientRect().top - stickyEdge

if (diff < 0) {
$(window).scrollTop($(window).scrollTop() + diff)
if (diff < 0) {
$(window).scrollTop($(window).scrollTop() + diff)
}
}
}

Expand Down

0 comments on commit 30322df

Please sign in to comment.