Patch #43641 » 0003-Hide-sticky-header-when-table-is-outside-viewport.patch
| app/javascript/controllers/sticky_table_header_controller.js | ||
|---|---|---|
|
static values = {sticky: Boolean}
|
||
|
connect() {
|
||
|
if (!this.isIntersecting && !this.isHeaderOverflowX) {
|
||
|
this.stickyValue = true;
|
||
|
}
|
||
|
this.headIntersecting = this.isHeadIntersecting;
|
||
|
this.tableIntersecting = this.isTableIntersecting;
|
||
|
this.updateStickyVisibility();
|
||
|
this.observe();
|
||
|
}
|
||
| ... | ... | |
|
}
|
||
|
disconnect() {
|
||
|
this.intersectionObserver?.disconnect();
|
||
|
this.headIntersectionObserver?.disconnect();
|
||
|
this.tableIntersectionObserver?.disconnect();
|
||
|
this.resizeObserver?.disconnect();
|
||
|
}
|
||
|
observe() {
|
||
|
this.intersectionObserver = new IntersectionObserver(entries => {
|
||
|
this.headIntersectionObserver = new IntersectionObserver(entries => {
|
||
|
entries.forEach(entry => {
|
||
|
if (!entry.isIntersecting && !this.isHeaderOverflowX) {
|
||
|
this.stickyValue = true;
|
||
|
}
|
||
|
if (entry.isIntersecting && !this.isHeaderOverflowX) {
|
||
|
this.stickyValue = false;
|
||
|
}
|
||
|
this.headIntersecting = entry.isIntersecting;
|
||
|
this.updateStickyVisibility();
|
||
|
})
|
||
|
}, {
|
||
|
rootMargin: `-${this.stickyTopOffset}px 0px 0px 0px`
|
||
|
});
|
||
|
this.tableIntersectionObserver = new IntersectionObserver(entries => {
|
||
|
entries.forEach(entry => {
|
||
|
this.tableIntersecting = entry.isIntersecting;
|
||
|
this.updateStickyVisibility();
|
||
|
})
|
||
|
}, {
|
||
|
rootMargin: `-${this.stickyTopOffset}px 0px 0px 0px`
|
||
|
});
|
||
|
this.resizeObserver = new ResizeObserver(() => {
|
||
|
if (!this.isIntersecting && this.isHeaderOverflowX) {
|
||
|
this.stickyValue = false;
|
||
|
}
|
||
|
if (!this.isIntersecting && !this.isHeaderOverflowX) {
|
||
|
this.stickyValue = true;
|
||
|
}
|
||
|
this.headIntersecting = this.isHeadIntersecting;
|
||
|
this.tableIntersecting = this.isTableIntersecting;
|
||
|
this.updateStickyVisibility();
|
||
|
this.syncWidth();
|
||
|
});
|
||
|
this.intersectionObserver.observe(this.headTarget);
|
||
|
this.headIntersectionObserver.observe(this.headTarget);
|
||
|
this.tableIntersectionObserver.observe(this.originalTable);
|
||
|
this.resizeObserver.observe(this.headTarget);
|
||
|
}
|
||
|
updateStickyVisibility() {
|
||
|
this.stickyValue = !this.headIntersecting && this.tableIntersecting && !this.isHeaderOverflowX;
|
||
|
}
|
||
|
syncWidth(e) {
|
||
|
const headRect = this.headTarget.getBoundingClientRect()
|
||
| ... | ... | |
|
}
|
||
|
}
|
||
|
get isIntersecting() {
|
||
|
get isHeadIntersecting() {
|
||
|
return this.headTarget.getBoundingClientRect().top > this.stickyTopOffset
|
||
|
}
|
||
|
get isTableIntersecting() {
|
||
|
const tableRect = this.originalTable.getBoundingClientRect();
|
||
|
return tableRect.bottom > this.stickyTopOffset && tableRect.top < window.innerHeight
|
||
|
}
|
||
|
get isHeaderOverflowX() {
|
||
|
return this.element.scrollWidth > this.element.clientWidth;
|
||
|
}
|
||
|
get originalTable() {
|
||
|
return this.headTarget.closest('table');
|
||
|
}
|
||
|
get stickyTopOffset() {
|
||
|
this.prepare()
|
||
| test/system/sticky_table_header_test.rb | ||
|---|---|---|
|
assert page.has_no_css?('table.list.sticky thead', visible: true)
|
||
|
end
|
||
|
def test_sticky_table_header_is_hidden_when_issue_list_is_below_viewport
|
||
|
log_user('jsmith', 'jsmith')
|
||
|
page.current_window.resize_to(1600, 300)
|
||
|
visit '/projects/ecookbook/issues'
|
||
|
# expand the filters to make the issue list further down the page
|
||
|
find('fieldset#options > legend').click
|
||
|
assert page.has_css?('fieldset#options > div', visible: true)
|
||
|
assert issue_table_below_viewport?
|
||
|
assert page.has_no_css?('table.list.sticky thead', visible: true)
|
||
|
end
|
||
|
private
|
||
|
def scroll_issue_list_header_out_of_view
|
||
| ... | ... | |
|
JS
|
||
|
end
|
||
|
def issue_table_below_viewport?
|
||
|
page.evaluate_script(<<~JS)
|
||
|
(function() {
|
||
|
const table = document.querySelector('table.list.issues');
|
||
|
return table.getBoundingClientRect().top >= window.innerHeight;
|
||
|
})();
|
||
|
JS
|
||
|
end
|
||
|
def issue_table_head_width
|
||
|
page.evaluate_script(<<~JS)
|
||
|
(function() {
|
||