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

WIP: fix: absolute Sticky left position #277

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
25 changes: 21 additions & 4 deletions packages/arcodesign/components/sticky/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ const Sticky = forwardRef((props: StickyProps, ref: Ref<StickyRef>) => {
const needTop = position === 'top' || position === 'both';
const needBottom = position === 'bottom' || position === 'both';

const offsetParent = contentRef.current.offsetParent;
const offsetParentRect = offsetParent?.getBoundingClientRect();
const placeholderClientRect = placeholderRef.current!.getBoundingClientRect();
const contentClientRect = contentRef.current.getBoundingClientRect();
const calculatedHeight = contentClientRect.height;
Expand All @@ -211,7 +213,11 @@ const Sticky = forwardRef((props: StickyProps, ref: Ref<StickyRef>) => {

const disFromTop = Math.round(placeholderClientRect.top - containerTop);
const disFromBottom = Math.round(
placeholderClientRect.top + calculatedHeight - containerBottom,
placeholderClientRect.top +
calculatedHeight -
(stickyStyle === 'absolute'
? offsetParentRect?.bottom || containerBottom
: containerBottom),
);
const topFollowDifference =
followBottom - followOffset - calculatedHeight - topOffset - containerTop;
Expand All @@ -226,11 +232,22 @@ const Sticky = forwardRef((props: StickyProps, ref: Ref<StickyRef>) => {
? disFromBottom >= -bottomOffset && followTop < containerBottom - followOffset
: false;
const newStickyState = isTopSticky || isBottomSticky;

const cssTop = (stickyStyle === 'absolute' ? 0 : containerTop) + topOffset;
const cssTop =
(stickyStyle === 'absolute'
? Math.max(
0,
placeholderClientRect.top -
(offsetParentRect?.top || placeholderClientRect.top), // offsetParentRect.top不存在时,用placeholderClientRect.top兜底使计算值为0
)
: containerTop) + topOffset;
const cssBottom =
(stickyStyle === 'absolute' ? 0 : window.innerHeight - containerBottom) +
bottomOffset;
const cssLeft =
stickyStyle === 'absolute'
? placeholderClientRect.left - (offsetParentRect?.left || 0)
: placeholderClientRect.left;

let stickyCssStyle: CSSProperties = {};
if (newStickyState) {
stickyCssStyle = {
Expand All @@ -251,7 +268,7 @@ const Sticky = forwardRef((props: StickyProps, ref: Ref<StickyRef>) => {
: cssBottom + bottomFollowDifference,
}
: {}),
left: placeholderClientRect.left,
left: cssLeft,
width: placeholderClientRect.width,
...(userSetStickyCssStyle || {}),
};
Expand Down
2 changes: 1 addition & 1 deletion sites/composite-comp/sticky-tabs/sticky-tab-css.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default function StickyTabsCss() {
.sticky-tabs-wrapper-css-nav-bar {
position: sticky;
background: #fff;
top: 44px;
top: 64px;
}
}
```
6 changes: 3 additions & 3 deletions sites/composite-comp/sticky-tabs/sticky-tab-hide.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ export default function StickyTabsHide() {
.rem(font-size, 20);
.use-var(color, sub-info-font-color);
}
.arcodesign-mobile-demo-content {
position: relative;
}
}

&.arcodesign-mobile-demo-content {
position: relative;
}
```
6 changes: 3 additions & 3 deletions sites/composite-comp/sticky-tabs/sticky-tab-position.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ export default function StickyTabsPosition() {
.rem(font-size, 20);
.use-var(color, sub-info-font-color);
}
.arcodesign-mobile-demo-content {
position: relative;
}
}
&.arcodesign-mobile-demo-content {
position: relative;
}
```
Loading