Skip to content

Let's use AI to Earn!

feat: Implement the component ScrollButtonContainer

gaoSir4b56e5c7e9b1
diff --git a/src/app/[lng]/accounts/components/CalendarTiming/CalendarTimingItem/RecordCore.tsx b/src/app/[lng]/accounts/components/CalendarTiming/CalendarTimingItem/RecordCore.tsx
index fcc5ffed..ca4bf067 100644
--- a/src/app/[lng]/accounts/components/CalendarTiming/CalendarTimingItem/RecordCore.tsx
+++ b/src/app/[lng]/accounts/components/CalendarTiming/CalendarTimingItem/RecordCore.tsx
@@ -34,6 +34,7 @@ import { deletePublishRecordApi, nowPubTaskApi } from "@/api/plat/publish";
import { getDays } from "@/app/[lng]/accounts/components/CalendarTiming/calendarTiming.utils";
import { getOssUrl } from "@/utils/oss";
import { useTransClient } from "@/app/i18n/client";
+import ScrollButtonContainer from "@/components/ScrollButtonContainer";
export interface IRecordCoreRef {}
@@ -206,7 +207,11 @@ const RecordCore = memo(
)}
</div>
</div>
- <div className="recordDetails-info"></div>
+ <ScrollButtonContainer>
+ <div className="recordDetails-info">
+
+ </div>
+ </ScrollButtonContainer>
<div className="recordDetails-bottom">
{publishRecord.workLink && (
<Button
diff --git a/src/components/ScrollButtonContainer/index.tsx b/src/components/ScrollButtonContainer/index.tsx
new file mode 100644
index 00000000..5950909b
--- /dev/null
+++ b/src/components/ScrollButtonContainer/index.tsx
@@ -0,0 +1,146 @@
+import {
+ ForwardedRef,
+ forwardRef,
+ memo,
+ PropsWithChildren,
+ useCallback,
+ useRef,
+ useEffect,
+ useState,
+} from "react";
+import styles from "./scrollButtonContainer.module.scss";
+import { Button } from "antd";
+import { LeftOutlined, RightOutlined } from "@ant-design/icons";
+
+export interface IScrollButtonContainerRef {}
+
+export interface IScrollButtonContainerProps {
+ width?: string;
+ step?: number;
+ duration?: number; // 降级动画时长(ms)
+ edgeOffset?: number; // 边界容差
+}
+
+const ScrollButtonContainer = memo(
+ forwardRef(function ScrollButtonContainerInner(
+ {
+ children,
+ width = "100%",
+ step = 200,
+ duration = 300,
+ edgeOffset = 2,
+ }: PropsWithChildren<IScrollButtonContainerProps>,
+ ref: ForwardedRef<IScrollButtonContainerRef>,
+ ) {
+ const scrollContentRef = useRef<HTMLDivElement>(null);
+ const [atStart, setAtStart] = useState(true);
+ const [atEnd, setAtEnd] = useState(false);
+
+ const updateEdgeState = useCallback(() => {
+ const el = scrollContentRef.current;
+ if (!el) return;
+ const { scrollLeft, scrollWidth, clientWidth } = el;
+ const noOverflow = scrollWidth <= clientWidth + edgeOffset;
+ if (noOverflow) {
+ setAtStart(true);
+ setAtEnd(true);
+ return;
+ }
+ setAtStart(scrollLeft <= edgeOffset);
+ setAtEnd(scrollLeft >= scrollWidth - clientWidth - edgeOffset);
+ }, [edgeOffset]);
+
+ useEffect(() => {
+ updateEdgeState();
+ const el = scrollContentRef.current;
+ if (!el) return;
+ const handler = () => updateEdgeState();
+ el.addEventListener("scroll", handler, { passive: true });
+ // 尺寸变化时也重算
+ const ro = new ResizeObserver(() => updateEdgeState());
+ ro.observe(el);
+ return () => {
+ el.removeEventListener("scroll", handler);
+ ro.disconnect();
+ };
+ }, [updateEdgeState, children]);
+
+ const fallbackAnimate = useCallback(
+ (target: number) => {
+ const el = scrollContentRef.current;
+ if (!el) return;
+ const start = el.scrollLeft;
+ const diff = target - start;
+ if (diff === 0) return;
+ const startTime = performance.now();
+ const animate = (now: number) => {
+ const progress = Math.min((now - startTime) / duration, 1);
+ // ease-out
+ const eased = 1 - Math.pow(1 - progress, 3);
+ el.scrollLeft = start + diff * eased;
+ if (progress < 1) {
+ requestAnimationFrame(animate);
+ }
+ };
+ requestAnimationFrame(animate);
+ },
+ [duration],
+ );
+
+ const smoothScrollTo = useCallback(
+ (delta: number) => {
+ const el = scrollContentRef.current;
+ if (!el) return;
+ const target = el.scrollLeft + delta;
+ if ("scrollBy" in el) {
+ try {
+ el.scrollBy({ left: delta, behavior: "smooth" });
+ return;
+ } catch {
+ // fallback
+ }
+ }
+ fallbackAnimate(target);
+ },
+ [fallbackAnimate],
+ );
+
+ const leftMove = useCallback(() => {
+ if (!atStart) smoothScrollTo(-step);
+ }, [smoothScrollTo, step, atStart]);
+
+ const rightMove = useCallback(() => {
+ if (!atEnd) smoothScrollTo(step);
+ }, [smoothScrollTo, step, atEnd]);
+
+ return (
+ <div className={styles.scrollButtonContainer}>
+ {!atStart && (
+ <Button
+ className="scrollButtonContainer-btn scrollButtonContainer-btn--left"
+ icon={<LeftOutlined />}
+ size="small"
+ onClick={leftMove}
+ />
+ )}
+ <div
+ ref={scrollContentRef}
+ className="scrollButtonContainer-content"
+ style={{ width }}
+ >
+ {children}
+ </div>
+ {!atEnd && (
+ <Button
+ className="scrollButtonContainer-btn scrollButtonContainer-btn--right"
+ icon={<RightOutlined />}
+ size="small"
+ onClick={rightMove}
+ />
+ )}
+ </div>
+ );
+ }),
+);
+
+export default ScrollButtonContainer;
diff --git a/src/components/ScrollButtonContainer/scrollButtonContainer.module.scss b/src/components/ScrollButtonContainer/scrollButtonContainer.module.scss
new file mode 100644
index 00000000..f1481a7c
--- /dev/null
+++ b/src/components/ScrollButtonContainer/scrollButtonContainer.module.scss
@@ -0,0 +1,33 @@
+.scrollButtonContainer {
+ position: relative;
+ :global {
+ .scrollButtonContainer-content {
+ overflow-x: auto;
+ &::-webkit-scrollbar { display: none; }
+ scrollbar-width: none;
+ -ms-overflow-style: none;
+ }
+
+ .scrollButtonContainer-btn {
+ position: absolute;
+ top: 50%;
+ transform: translateY(-50%);
+ opacity: 0;
+ &--left {
+ left: 0;
+ }
+ &--right {
+ right: 0;
+ }
+ }
+ }
+
+ &:hover {
+ :global {
+ .scrollButtonContainer-btn {
+ opacity: 1;
+ height: 30px;
+ }
+ }
+ }
+}