Skip to content

Commit

Permalink
Fix: Allow tooltips to follow animations (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverfoster authored Sep 30, 2024
1 parent e5a18ef commit 5e08cfa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
23 changes: 20 additions & 3 deletions js/views/TooltipItemView.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ export default class TooltipItemView extends Backbone.View {
this.parent = parent;
this.$target.attr('aria-describedby', `tooltip-${this.model.get('_id')}`);
this.listenTo(this.model, 'change', this.changed);
this.listenTo(Adapt, 'device:resize', this.onDeviceResize);
this.listenTo(Adapt, {
'contentObjectView:ready': this.changed,
'view:animationStart': this.changed,
'device:resize': this.onDeviceResize
});
this.listenTo(Adapt.parentView, 'preRemove', this.remove);
if (!this.isStatic) {
// Should not hide static tooltips on blur
$(document).on('mouseleave blur', '[data-tooltip-id]', this.onMouseOut);
}
this.unchangedRenders = 0;
this.changed();
}

Expand Down Expand Up @@ -152,7 +157,7 @@ export default class TooltipItemView extends Backbone.View {
}

doSubsequentPasses() {
if (!this.model) return;
if (!this.model) return false;
this.model.set('hasLoaded', true, { silent: true });
const multipassCache = {};
// First pass - render at the requested position
Expand All @@ -179,6 +184,10 @@ export default class TooltipItemView extends Backbone.View {
this.model.set('isShown', true, { silent: true });
this.render();
this.model.set('wasShown', true, { silent: true });
const endHash = JSON.stringify(this.model.get('tooltipStyles'));
const hasChanged = (this.startHash !== endHash);
this.startHash = endHash;
return hasChanged;
}

render() {
Expand All @@ -193,7 +202,15 @@ export default class TooltipItemView extends Backbone.View {
if (!this.model) return;
requestAnimationFrame(() => {
this.doFirstPass();
this.doSubsequentPasses();
const hasChanged = this.doSubsequentPasses();
// rerender if unchanged for 34 frames (1second @ 30fps)
if (!hasChanged) this.unchangedRenders++;
if (hasChanged) this.unchangedRenders = 0;
if (this.unchangedRenders >= 34) {
this.unchangedRenders = 0;
return;
}
this.changed();
});
}

Expand Down
2 changes: 2 additions & 0 deletions js/views/adaptView.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class AdaptView extends Backbone.View {
const minVerticalInview = onscreen._percentInviewVertical || 33;
if (m.percentInviewVertical < minVerticalInview) return;
this.$el.addClass(`${onscreen._classes}-after`).off('onscreen.adaptView');
const type = this.model.get('_type');
Adapt.trigger(`${type}View:animationStart view:animationStart`, this);
});
}

Expand Down

0 comments on commit 5e08cfa

Please sign in to comment.