Skip to content

Commit b670d5b

Browse files
committed
ui: remove sticky-kit
1 parent 792267b commit b670d5b

5 files changed

Lines changed: 63 additions & 51 deletions

File tree

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'sticky-kit/dist/sticky-kit';
2-
31
import $ from 'jquery';
42
import _ from 'lodash';
53
import responsiveCutoff from 'vj/breakpoints.json';
@@ -10,40 +8,26 @@ const navHeight = isBelow(responsiveCutoff.mobile)
108
? 0
119
: $('.nav').height();
1210

11+
function getCutoff(str) {
12+
if (str === 'medium') return responsiveCutoff.mobile;
13+
if (str === 'large') return responsiveCutoff.desktop;
14+
return 0;
15+
}
16+
1317
function updateStickies($stickies) {
1418
$stickies.get().forEach((element) => {
1519
const $sticky = $(element);
16-
const shouldEnableSticky = (isAbove($sticky.data('sticky-cutoff-min')));
17-
const stickyEnabled = $sticky.data('sticky-enabled');
18-
if (shouldEnableSticky && !stickyEnabled) {
19-
const stickyOptions = {};
20-
const $stickyParent = $sticky.closest('[data-sticky-parent]');
21-
if ($stickyParent.length > 0) {
22-
stickyOptions.parent = $stickyParent;
23-
}
24-
stickyOptions.offset_top = 10 + navHeight;
25-
$sticky.stick_in_parent(stickyOptions);
26-
$sticky.data('sticky-enabled', true);
27-
} else if (!shouldEnableSticky && stickyEnabled) {
28-
$sticky.trigger('sticky_kit:detach');
29-
$sticky.data('sticky-enabled', false);
20+
const shouldEnableSticky = isAbove($sticky.data('sticky-cutoff-min'));
21+
if (shouldEnableSticky) {
22+
element.style.position = 'sticky';
23+
element.style.top = `${10 + navHeight}px`;
24+
} else {
25+
element.style.position = '';
26+
element.style.top = '';
3027
}
3128
});
3229
}
3330

34-
function getCutoff(str) {
35-
if (str === 'medium') {
36-
return responsiveCutoff.mobile;
37-
} if (str === 'large') {
38-
return responsiveCutoff.desktop;
39-
}
40-
return 0;
41-
}
42-
43-
function stickyRelayout() {
44-
$('body').trigger('sticky_kit:recalc');
45-
}
46-
4731
const stickyPage = new AutoloadPage('stickyPage', () => {
4832
let shouldListenResize = false;
4933
const $stickies = $('[data-sticky]');
@@ -54,13 +38,11 @@ const stickyPage = new AutoloadPage('stickyPage', () => {
5438
shouldListenResize = true;
5539
}
5640
$sticky.data('sticky-cutoff-min', getCutoff(minEnabledSize));
57-
$sticky.data('sticky-enabled', false);
5841
});
5942
updateStickies($stickies);
6043
if (shouldListenResize) {
6144
$(window).on('resize', _.throttle(() => updateStickies($stickies), 300));
6245
}
63-
$(document).on('vjLayout', _.throttle(stickyRelayout, 100));
6446
});
6547

6648
export default stickyPage;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[data-sticky-parent]
2+
display: flex
3+
flex-wrap: wrap
4+
5+
&::before,
6+
&::after
7+
display: none
8+
9+
> .columns,
10+
> .column
11+
float: none
Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import 'sticky-kit/dist/sticky-kit';
2-
31
import $ from 'jquery';
4-
import _ from 'lodash';
52
import responsiveCutoff from 'vj/breakpoints.json';
63
import DOMAttachedObject from 'vj/components/DOMAttachedObject';
74
import { isBelow } from 'vj/utils/mediaQuery';
@@ -23,29 +20,53 @@ export default class StyledTable extends DOMAttachedObject {
2320

2421
super($dom);
2522

26-
this.$container = $('<div>').addClass('section__table-container');
27-
this.$container.insertBefore(this.$dom);
23+
// Sentinel to detect when header becomes stuck
24+
this.$sentinel = $('<div>').css({ height: 0, margin: 0, padding: 0 });
2825

26+
// Header sits outside the scroll container so sticky works against viewport
2927
this.$header = $('<table>');
3028
this.$header.attr('class', `${this.$dom.attr('class')} section__table-header`);
29+
this.$header.css({
30+
position: 'sticky',
31+
top: `${navHeight}px`,
32+
});
3133

32-
this.$container
33-
.append(this.$header)
34-
.append(this.$dom);
34+
// Scroll container only wraps the data table
35+
this.$container = $('<div>').addClass('section__table-container');
36+
this.$sentinel.insertBefore(this.$dom);
37+
this.$header.insertBefore(this.$dom);
38+
this.$container.insertBefore(this.$dom);
39+
this.$container.append(this.$dom);
3540

3641
this.$header.empty();
3742
this.$dom.children('colgroup').clone().appendTo(this.$header);
3843
this.$dom.children('thead').appendTo(this.$header);
3944

40-
const stickyOptions = {
41-
parent: this.$container,
42-
offset_top: navHeight,
43-
};
44-
_.defer(() => this.$header.stick_in_parent(stickyOptions));
45+
// Sync horizontal scroll between header and body
46+
this.$container.on('scroll', () => {
47+
this.$header.css('transform', `translateX(-${this.$container.scrollLeft()}px)`);
48+
});
49+
50+
// Detect sticky state via IntersectionObserver
51+
const header = this.$header[0];
52+
const sentinel = this.$sentinel[0];
53+
if (window.IntersectionObserver) {
54+
this._observer = new IntersectionObserver(
55+
([entry]) => {
56+
if (entry.isIntersecting) {
57+
header.classList.remove('is_stuck');
58+
} else {
59+
header.classList.add('is_stuck');
60+
}
61+
},
62+
{ threshold: [0], rootMargin: `-${navHeight}px 0px 0px 0px` },
63+
);
64+
this._observer.observe(sentinel);
65+
}
4566
}
4667

4768
detach() {
69+
if (this._observer) this._observer.disconnect();
4870
super.detach();
49-
this.$header.trigger('sticky_kit:detach');
5071
}
5172
}

packages/ui-default/components/table/table.page.styl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,21 @@
4646
font-size: rem($font-size-small)
4747

4848
.section__table-container
49-
overflow: hidden
5049
overflow-x: auto
5150

5251
.section__table-header
5352
background: $table-header-bg-color
5453
box-shadow: $table-header-shadow
55-
position: relative
5654
z-index: 100
55+
overflow: hidden
5756
transition: box-shadow .2s linear
5857

59-
thead > tr
60-
border-bottom: 0
61-
6258
&.is_stuck
6359
box-shadow: $table-header-shadow-floating
6460

61+
thead > tr
62+
border-bottom: 0
63+
6564
.section__table-container,
6665
.section__body
6766
& > .data-table

packages/ui-default/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@
105105
"sass-loader": "^16.0.7",
106106
"schemastery": "^3.18.0",
107107
"shorty.js": "^1.0.1",
108-
"sticky-kit": "^1.1.3",
109108
"style-loader": "^4.0.0",
110109
"stylus": "^0.64.0",
111110
"stylus-loader": "7.1.2",

0 commit comments

Comments
 (0)