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

Fix missing affixed sidebar offset #4142

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions framework/core/js/src/forum/components/AffixedSidebar.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import Component from '../../common/Component';

/**
* The `AffixedSidebar` component uses Bootstrap's "affix" plugin to keep a
* Calculate the height of element with margin.
* @param {HTMLElement} element The element.
* @returns {number} The height of element with margin.
*/
function heightWithMargin(element) {
const style = getComputedStyle(element);
return element.getBoundingClientRect().height + parseInt(style.marginBottom, 10) + parseInt(style.marginTop, 10);
}

/**
* The `AffixedSidebar` component uses sticky position to keep a
* sidebar navigation at the top of the viewport when scrolling.
*
* ### Children
*
* The component must wrap an element that itself wraps an <ul> element, which
* will be "affixed".
*
* @see https://getbootstrap.com/docs/3.4/javascript/#affix
*/
export default class AffixedSidebar extends Component {
view(vnode) {
Expand All @@ -19,35 +27,36 @@ export default class AffixedSidebar extends Component {
oncreate(vnode) {
super.oncreate(vnode);

// Register the affix plugin to execute on every window resize (and trigger)
// Register the affix to execute on every window resize (and trigger)
this.boundOnresize = this.onresize.bind(this);
$(window).on('resize', this.boundOnresize).resize();
window.addEventListener('resize', this.boundOnresize);
window.dispatchEvent(new Event('resize'));
}

onremove(vnode) {
super.onremove(vnode);

$(window).off('resize', this.boundOnresize);
window.removeEventListener('resize', this.boundOnresize);
}

onresize() {
const $sidebar = this.$();
const $header = $('#header');
const $footer = $('#footer');
const $affixElement = $sidebar.find('> ul');

$(window).off('.affix');
$affixElement.removeClass('affix affix-top affix-bottom').removeData('bs.affix');
const header = document.getElementById('header');
const affixElement = this.element.querySelector(':scope > ul');
const pageSidebar = this.element.closest('.Page-sidebar');

// Don't affix the sidebar if it is taller than the viewport (otherwise
// there would be no way to scroll through its content).
if ($sidebar.outerHeight(true) > $(window).height() - $header.outerHeight(true)) return;

$affixElement.affix({
offset: {
top: () => $sidebar.offset().top - $header.outerHeight(true) - parseInt($sidebar.css('margin-top'), 10),
bottom: () => (this.bottom = $footer.outerHeight(true)),
},
});
const enabled = heightWithMargin(this.element) <= window.innerHeight - heightWithMargin(header);
affixElement.classList.toggle('affix', enabled);
if (enabled) {
const top = heightWithMargin(header) + parseInt(getComputedStyle(pageSidebar ?? this.element).marginTop, 10);
affixElement.style.position = 'sticky';
affixElement.style.top = top + 'px';
this.element.style.display = 'initial'; // Workaround for sticky not working
} else {
affixElement.style.position = '';
affixElement.style.top = '';
this.element.style.display = '';
}
}
}
Loading