Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

✨ Add click and drag behaviour to desktop poll (#2296)

Luke Vella7b974caf51a8
diff --git a/apps/web/src/components/poll/desktop-poll.tsx b/apps/web/src/components/poll/desktop-poll.tsx
index 915fb196b..36acbfb8d 100644
--- a/apps/web/src/components/poll/desktop-poll.tsx
+++ b/apps/web/src/components/poll/desktop-poll.tsx
@@ -24,6 +24,7 @@ import {
EmptyStateTitle,
} from "@/components/empty-state";
import { useVotingForm } from "@/components/poll/voting-form";
+import { ScrollContainer } from "@/components/scroll-container";
import { usePermissions } from "@/contexts/permissions";
import { usePoll } from "@/contexts/poll";
import { Trans, useTranslation } from "@/i18n/client";
@@ -53,38 +54,6 @@ function EscapeListener({ onEscape }: { onEscape: () => void }) {
return null;
}
-const useIsOverflowing = <E extends Element | null>(
- ref: React.RefObject<E>,
-) => {
- const [isOverflowing, setIsOverflowing] = React.useState(false);
-
- React.useEffect(() => {
- const checkOverflow = () => {
- if (ref.current) {
- const element = ref.current;
- const overflowX = element.scrollWidth > element.clientWidth;
- const overflowY = element.scrollHeight > element.clientHeight;
-
- setIsOverflowing(overflowX || overflowY);
- }
- };
-
- if (ref.current) {
- const resizeObserver = new ResizeObserver(checkOverflow);
- resizeObserver.observe(ref.current);
-
- // Initial check
- checkOverflow();
-
- return () => {
- resizeObserver.disconnect();
- };
- }
- }, [ref]);
-
- return isOverflowing;
-};
-
const DesktopPoll: React.FunctionComponent = () => {
const poll = usePoll();
@@ -95,7 +64,10 @@ const DesktopPoll: React.FunctionComponent = () => {
const goToNextPage = () => {
setDidScroll(true);
if (scrollRef.current) {
- scrollRef.current.scrollLeft += 235;
+ scrollRef.current.scrollTo({
+ left: scrollRef.current.scrollLeft + 235,
+ behavior: "smooth",
+ });
}
};
@@ -114,7 +86,10 @@ const DesktopPoll: React.FunctionComponent = () => {
const goToPreviousPage = () => {
if (scrollRef.current) {
- scrollRef.current.scrollLeft -= 235;
+ scrollRef.current.scrollTo({
+ left: scrollRef.current.scrollLeft - 235,
+ behavior: "smooth",
+ });
}
};
const { t } = useTranslation();
@@ -126,7 +101,7 @@ const DesktopPoll: React.FunctionComponent = () => {
const scrollRef = React.useRef<HTMLDivElement>(null);
- const isOverflowing = useIsOverflowing(scrollRef);
+ const [isOverflowing, setIsOverflowing] = React.useState(false);
const { x } = useScroll(scrollRef as React.RefObject<HTMLElement>);
@@ -301,15 +276,18 @@ const DesktopPoll: React.FunctionComponent = () => {
x > 0 ? "opacity-100" : "opacity-0",
)}
/>
- <div
+ <ScrollContainer
onScroll={() => {
if (!didScroll) {
setDidScroll(true);
}
}}
+ onOverflowChange={(isOverflowing) => {
+ setIsOverflowing(isOverflowing);
+ }}
ref={scrollRef}
className={cn(
- "scrollbar-thin dark:scrollbar-thumb-gray-600 dark:scrollbar-track-gray-800 hover:scrollbar-thumb-gray-400 dark:hover:scrollbar-thumb-gray-500 scrollbar-thumb-gray-300 scrollbar-track-gray-100 relative z-10 h-full min-h-0 grow overflow-auto overscroll-x-none scroll-smooth",
+ "scrollbar-thin dark:scrollbar-thumb-gray-600 dark:scrollbar-track-gray-800 hover:scrollbar-thumb-gray-400 dark:hover:scrollbar-thumb-gray-500 scrollbar-thumb-gray-300 scrollbar-track-gray-100 relative z-10 h-full min-h-0 grow overflow-auto overscroll-x-none",
)}
>
<table className="w-full table-auto border-separate border-spacing-0 bg-muted/50">
@@ -356,7 +334,7 @@ const DesktopPoll: React.FunctionComponent = () => {
: null}
</tbody>
</table>
- </div>
+ </ScrollContainer>
</div>
) : (
<EmptyState className="p-16">
diff --git a/apps/web/src/components/scroll-container.tsx b/apps/web/src/components/scroll-container.tsx
new file mode 100644
index 000000000..cbc96f8a3
--- /dev/null
+++ b/apps/web/src/components/scroll-container.tsx
@@ -0,0 +1,148 @@
+import { cn } from "@rallly/ui";
+import { animate, motion, useDragControls } from "motion/react";
+import * as React from "react";
+
+const useIsOverflowing = <E extends Element | null>(
+ ref: React.RefObject<E>,
+) => {
+ const [isOverflowing, setIsOverflowing] = React.useState(false);
+
+ React.useEffect(() => {
+ const checkOverflow = () => {
+ if (ref.current) {
+ const element = ref.current;
+ const overflowX = element.scrollWidth > element.clientWidth;
+ const overflowY = element.scrollHeight > element.clientHeight;
+
+ setIsOverflowing(overflowX || overflowY);
+ }
+ };
+
+ if (ref.current) {
+ const resizeObserver = new ResizeObserver(checkOverflow);
+ resizeObserver.observe(ref.current);
+ if (ref.current.firstElementChild) {
+ resizeObserver.observe(ref.current.firstElementChild);
+ }
+
+ const mutationObserver = new MutationObserver(checkOverflow);
+ mutationObserver.observe(ref.current, { childList: true, subtree: true });
+
+ checkOverflow();
+
+ return () => {
+ resizeObserver.disconnect();
+ mutationObserver.disconnect();
+ };
+ }
+ }, [ref]);
+
+ return isOverflowing;
+};
+
+export function ScrollContainer({
+ className,
+ children,
+ onScroll,
+ onOverflowChange,
+ ref,
+}: {
+ ref: React.RefObject<HTMLDivElement | null>;
+ className?: string;
+ children: React.ReactNode;
+ onScroll?: React.UIEventHandler<HTMLDivElement>;
+ onOverflowChange?: (isOverflowing: boolean) => void;
+}) {
+ const controls = useDragControls();
+ const [isDragging, setIsDragging] = React.useState(false);
+ const [isPointerDown, setIsPointerDown] = React.useState(false);
+ const animation = React.useRef<{ stop: () => void } | null>(null);
+ const isOverflowing = useIsOverflowing(ref);
+
+ React.useEffect(() => {
+ if (onOverflowChange) {
+ onOverflowChange(isOverflowing);
+ }
+ }, [isOverflowing, onOverflowChange]);
+
+ return (
+ <div
+ ref={ref}
+ onScroll={onScroll}
+ onPointerDown={(e) => {
+ if (e.defaultPrevented) return;
+ const target = e.target as HTMLElement;
+ if (
+ target.closest(
+ 'button, a, input, textarea, select, [role="button"], [contenteditable]',
+ )
+ )
+ return;
+ controls.start(e);
+ setIsPointerDown(true);
+ }}
+ onPointerUp={() => {
+ setIsPointerDown(false);
+ }}
+ className={cn(
+ "scroll-auto",
+ {
+ "cursor-grab": isOverflowing && !isPointerDown,
+ "cursor-grabbing": isOverflowing && isPointerDown,
+ "select-none": isOverflowing && isDragging,
+ },
+ className,
+ )}
+ >
+ <motion.div
+ drag
+ dragControls={controls}
+ dragListener={false}
+ dragConstraints={{ left: 0, right: 0, top: 0, bottom: 0 }}
+ dragElastic={0}
+ dragMomentum={false}
+ onDragStart={() => {
+ animation.current?.stop();
+ setIsDragging(true);
+ }}
+ onDrag={(_, info) => {
+ if (ref.current) {
+ ref.current.scrollLeft -= info.delta.x;
+ ref.current.scrollTop -= info.delta.y;
+ }
+ }}
+ onDragEnd={(_, info) => {
+ setIsDragging(false);
+ if (!ref.current) return;
+
+ const el = ref.current;
+ const vx = info.velocity.x;
+ const vy = info.velocity.y;
+
+ if (Math.abs(vx) < 50 && Math.abs(vy) < 50) return;
+
+ const fromX = el.scrollLeft;
+ const fromY = el.scrollTop;
+ const maxScrollX = el.scrollWidth - el.clientWidth;
+ const maxScrollY = el.scrollHeight - el.clientHeight;
+ const toX = Math.max(0, Math.min(fromX - vx * 0.2, maxScrollX));
+ const toY = Math.max(0, Math.min(fromY - vy * 0.2, maxScrollY));
+
+ animation.current = animate(0, 1, {
+ type: "spring",
+ bounce: 0,
+ duration: 0.6,
+ onUpdate: (p) => {
+ if (ref.current) {
+ ref.current.scrollLeft = fromX + (toX - fromX) * p;
+ ref.current.scrollTop = fromY + (toY - fromY) * p;
+ }
+ },
+ });
+ }}
+ >
+ {children}
+ </motion.div>
+ </div>
+ );
+}