ui: remove sticky-kit

This commit is contained in:
undefined committed 2026-04-17 22:30:25 +08:00
1 parent 792267bc76
commit b670d5b7e1
5 files changed
+64 -52

No files matched your search

@@ -1,5 +1,3 @@
import 'sticky-kit/dist/sticky-kit';
import $ from 'jquery'; import $ from 'jquery';
import _ from 'lodash'; import _ from 'lodash';
import responsiveCutoff from 'vj/breakpoints.json'; import responsiveCutoff from 'vj/breakpoints.json';
@@ -10,38 +8,24 @@ const navHeight = isBelow(responsiveCutoff.mobile)
? 0 ? 0
: $('.nav').height(); : $('.nav').height();
function updateStickies($stickies) {
$stickies.get().forEach((element) => {
const $sticky = $(element);
const shouldEnableSticky = (isAbove($sticky.data('sticky-cutoff-min')));
const stickyEnabled = $sticky.data('sticky-enabled');
if (shouldEnableSticky && !stickyEnabled) {
const stickyOptions = {};
const $stickyParent = $sticky.closest('[data-sticky-parent]');
if ($stickyParent.length > 0) {
stickyOptions.parent = $stickyParent;
}
stickyOptions.offset_top = 10 + navHeight;
$sticky.stick_in_parent(stickyOptions);
$sticky.data('sticky-enabled', true);
} else if (!shouldEnableSticky && stickyEnabled) {
$sticky.trigger('sticky_kit:detach');
$sticky.data('sticky-enabled', false);
}
});
}
function getCutoff(str) { function getCutoff(str) {
if (str === 'medium') { if (str === 'medium') return responsiveCutoff.mobile;
return responsiveCutoff.mobile; if (str === 'large') return responsiveCutoff.desktop;
} if (str === 'large') {
return responsiveCutoff.desktop;
}
return 0; return 0;
} }
function stickyRelayout() { function updateStickies($stickies) {
$('body').trigger('sticky_kit:recalc'); $stickies.get().forEach((element) => {
const $sticky = $(element);
const shouldEnableSticky = isAbove($sticky.data('sticky-cutoff-min'));
if (shouldEnableSticky) {
element.style.position = 'sticky';
element.style.top = `${10 + navHeight}px`;
} else {
element.style.position = '';
element.style.top = '';
}
});
} }
const stickyPage = new AutoloadPage('stickyPage', () => { const stickyPage = new AutoloadPage('stickyPage', () => {
@@ -54,13 +38,11 @@ const stickyPage = new AutoloadPage('stickyPage', () => {
shouldListenResize = true; shouldListenResize = true;
} }
$sticky.data('sticky-cutoff-min', getCutoff(minEnabledSize)); $sticky.data('sticky-cutoff-min', getCutoff(minEnabledSize));
$sticky.data('sticky-enabled', false);
}); });
updateStickies($stickies); updateStickies($stickies);
if (shouldListenResize) { if (shouldListenResize) {
$(window).on('resize', _.throttle(() => updateStickies($stickies), 300)); $(window).on('resize', _.throttle(() => updateStickies($stickies), 300));
} }
$(document).on('vjLayout', _.throttle(stickyRelayout, 100));
}); });
export default stickyPage; export default stickyPage;
@@ -0,0 +1,11 @@
[data-sticky-parent]
display: flex
flex-wrap: wrap
&::before,
&::after
display: none
> .columns,
> .column
float: none
@@ -1,7 +1,4 @@
import 'sticky-kit/dist/sticky-kit';
import $ from 'jquery'; import $ from 'jquery';
import _ from 'lodash';
import responsiveCutoff from 'vj/breakpoints.json'; import responsiveCutoff from 'vj/breakpoints.json';
import DOMAttachedObject from 'vj/components/DOMAttachedObject'; import DOMAttachedObject from 'vj/components/DOMAttachedObject';
import { isBelow } from 'vj/utils/mediaQuery'; import { isBelow } from 'vj/utils/mediaQuery';
@@ -23,29 +20,53 @@ export default class StyledTable extends DOMAttachedObject {
super($dom); super($dom);
this.$container = $('<div>').addClass('section__table-container'); // Sentinel to detect when header becomes stuck
this.$container.insertBefore(this.$dom); this.$sentinel = $('<div>').css({ height: 0, margin: 0, padding: 0 });
// Header sits outside the scroll container so sticky works against viewport
this.$header = $('<table>'); this.$header = $('<table>');
this.$header.attr('class', `${this.$dom.attr('class')} section__table-header`); this.$header.attr('class', `${this.$dom.attr('class')} section__table-header`);
this.$header.css({
position: 'sticky',
top: `${navHeight}px`,
});
this.$container // Scroll container only wraps the data table
.append(this.$header) this.$container = $('<div>').addClass('section__table-container');
.append(this.$dom); this.$sentinel.insertBefore(this.$dom);
this.$header.insertBefore(this.$dom);
this.$container.insertBefore(this.$dom);
this.$container.append(this.$dom);
this.$header.empty(); this.$header.empty();
this.$dom.children('colgroup').clone().appendTo(this.$header); this.$dom.children('colgroup').clone().appendTo(this.$header);
this.$dom.children('thead').appendTo(this.$header); this.$dom.children('thead').appendTo(this.$header);
const stickyOptions = { // Sync horizontal scroll between header and body
parent: this.$container, this.$container.on('scroll', () => {
offset_top: navHeight, this.$header.css('transform', `translateX(-${this.$container.scrollLeft()}px)`);
}; });
_.defer(() => this.$header.stick_in_parent(stickyOptions));
// Detect sticky state via IntersectionObserver
const header = this.$header[0];
const sentinel = this.$sentinel[0];
if (window.IntersectionObserver) {
this._observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
header.classList.remove('is_stuck');
} else {
header.classList.add('is_stuck');
}
},
{ threshold: [0], rootMargin: `-${navHeight}px 0px 0px 0px` },
);
this._observer.observe(sentinel);
}
} }
detach() { detach() {
if (this._observer) this._observer.disconnect();
super.detach(); super.detach();
this.$header.trigger('sticky_kit:detach');
} }
} }
@@ -46,22 +46,21 @@
font-size: rem($font-size-small) font-size: rem($font-size-small)
.section__table-container .section__table-container
overflow: hidden
overflow-x: auto overflow-x: auto
.section__table-header .section__table-header
background: $table-header-bg-color background: $table-header-bg-color
box-shadow: $table-header-shadow box-shadow: $table-header-shadow
position: relative
z-index: 100 z-index: 100
overflow: hidden
transition: box-shadow .2s linear transition: box-shadow .2s linear
thead > tr
border-bottom: 0
&.is_stuck &.is_stuck
box-shadow: $table-header-shadow-floating box-shadow: $table-header-shadow-floating
thead > tr
border-bottom: 0
.section__table-container, .section__table-container,
.section__body .section__body
& > .data-table & > .data-table
-1
View File
@@ -105,7 +105,6 @@
"sass-loader": "^16.0.7", "sass-loader": "^16.0.7",
"schemastery": "^3.18.0", "schemastery": "^3.18.0",
"shorty.js": "^1.0.1", "shorty.js": "^1.0.1",
"sticky-kit": "^1.1.3",
"style-loader": "^4.0.0", "style-loader": "^4.0.0",
"stylus": "^0.64.0", "stylus": "^0.64.0",
"stylus-loader": "7.1.2", "stylus-loader": "7.1.2",