Skip to content

Commit a0bb490

Browse files
committed
okay, im somewhat happy with it
1 parent 162af9b commit a0bb490

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

packages/web/docs/src/components/nested-sticky.tsx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ export function NestedSticky({
3535

3636
let width = 0;
3737
let height = 0;
38-
let isSticky = false;
3938
let rafId: number | null = null;
4039

40+
// relative at the top
41+
// fixed when we scroll
42+
// absolute when we're near the bottom
43+
let state: 'fixed' | 'absolute' | 'relative' = 'relative';
44+
let prevState: 'fixed' | 'absolute' | 'relative' = 'relative';
45+
4146
const measureDimensions = () => {
4247
const rect = sticky.getBoundingClientRect();
4348
width = rect.width;
@@ -48,13 +53,23 @@ export function NestedSticky({
4853
};
4954

5055
const updateStyles = () => {
51-
placeholder.style.height = isSticky ? `${height}px` : '0';
56+
placeholder.style.height = state !== 'relative' ? `${height}px` : '0';
5257

53-
if (isSticky) {
58+
if (state === 'fixed') {
5459
sticky.style.position = 'fixed';
5560
sticky.style.top = `${offsetTop}px`;
5661
sticky.style.width = `${width}px`;
5762
sticky.setAttribute('data-fixed', '');
63+
} else if (state === 'absolute') {
64+
const containerRect = container.getBoundingClientRect();
65+
const stickyRect = sticky.getBoundingClientRect();
66+
67+
const relativeTop = stickyRect.top - containerRect.top;
68+
69+
sticky.style.position = 'absolute';
70+
sticky.style.top = `${relativeTop}px`;
71+
sticky.style.width = `${width}px`;
72+
sticky.setAttribute('data-fixed', '');
5873
} else {
5974
sticky.style.position = 'relative';
6075
sticky.style.top = '';
@@ -74,13 +89,14 @@ export function NestedSticky({
7489

7590
const shouldBeFixed = containerRect.top < offsetTop;
7691

77-
if (shouldBeFixed && parentRect.bottom < offsetTop + height + offsetBottom + 10) {
78-
isSticky = false;
79-
} else {
80-
isSticky = shouldBeFixed;
81-
}
92+
const nearBottom = parentRect.bottom < offsetTop + height + offsetBottom + 10;
8293

83-
updateStyles();
94+
state = shouldBeFixed && nearBottom ? 'absolute' : shouldBeFixed ? 'fixed' : 'relative';
95+
96+
if (state !== prevState) {
97+
prevState = state;
98+
updateStyles();
99+
}
84100
});
85101
};
86102

packages/web/docs/src/components/pricing/plans-table.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ function MobileNavbar({
345345
// --nextra-navbar-height
346346
64
347347
}
348-
offsetBottom={256}
348+
offsetBottom={427}
349349
>
350350
<div className="bg-beige-100 before:bg-beige-100 before:border-b-beige-400 relative top-0 z-10 w-full rounded-2xl p-2 duration-100 ease-[var(--hive-ease-overshoot-a-bit)] before:absolute before:inset-0 before:opacity-0 before:transition md:hidden [[data-fixed]>&:before]:scale-x-125 [[data-fixed]>&:before]:border-b [[data-fixed]>&:before]:opacity-100 [[data-fixed]>&:before]:shadow-sm">
351351
<div className="relative flex w-full">

0 commit comments

Comments
 (0)