Skip to content

Commit

Permalink
fix: move gestures into body component
Browse files Browse the repository at this point in the history
  • Loading branch information
jvllmr committed Oct 22, 2024
1 parent ef85c17 commit 422883d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 27 deletions.
5 changes: 4 additions & 1 deletion src/SchedulerBody/SchedulerBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from "./NowMarker";
import { DefaultResourceLabel, ResourceLabelProps } from "./ResourceLabel";

import { useSchedulerGestures } from "../controller/gestureControls";
import {
DefaultSchedulerEntry,
SchedulerEntryProps,
Expand Down Expand Up @@ -51,6 +52,7 @@ export interface SchedulerBodyProps<TData, TResource> {
gridLabelSize: number;
totalGridSize: number;
tz?: string;
enableGestures?: boolean;
}

const SchedulerEntries = <TData, TResource>({
Expand Down Expand Up @@ -219,9 +221,10 @@ export function SchedulerBody<TData, TResource>({
totalGridSize,
gridLabelSize,
tz,
enableGestures,
}: SchedulerBodyProps<TData, TResource>) {
const localBodyRef = useRef<HTMLDivElement | null>(null);

useSchedulerGestures(controller, localBodyRef, enableGestures);
const getResourceId = useStringAccessor(resourceIdField);
const getDataResourceId = useStringArrayAccessor(dataResourceIdField);
const getStartDate = useDateAccessor(startDateField);
Expand Down
2 changes: 0 additions & 2 deletions src/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export interface SchedulerController<TData, TResource> {
firstSelectedMoment: Dayjs | null;
lastSelectedMoment: Dayjs | null;

bodyRef: HTMLDivElement | null;
selectedMoments: Record<
string,
Record<string, { isSelected: boolean } | undefined> | undefined
Expand Down Expand Up @@ -246,7 +245,6 @@ export function useSchedulerController<TData, TResource>({
> {
const controller = useRef(
proxy<SchedulerController<TData, TResource>>({
bodyRef: null,
calculateDistancePercentage: () => 0,
displayUnit: "day",
firstSelectedMoment: null,
Expand Down
20 changes: 4 additions & 16 deletions src/controller/gestureControls.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
import { useGesture } from "@use-gesture/react";
import { useEffect, useRef } from "react";
import { subscribeKey } from "valtio/utils";
import { SchedulerController } from "./controller";

export const useSchedulerGestures = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
controller: SchedulerController<any, any>,
bodyRef: React.MutableRefObject<HTMLDivElement | null>,
enabled?: boolean,
) => {
const artificialRef = useRef(controller.bodyRef);

useEffect(() => {
const unsubscribe = subscribeKey(controller, "bodyRef", (value) => {
artificialRef.current = value;
console.log(artificialRef.current);
});

return () => {
unsubscribe();
};
}, [controller]);

useGesture(
{
// @ts-expect-error we don't need x, but unpacking is just so much more elegant
Expand Down Expand Up @@ -64,9 +51,10 @@ export const useSchedulerGestures = (
},
},
{
target: artificialRef,
target: bodyRef,
eventOptions: { passive: false },
move: { threshold: 1 },
enabled: enabled ?? false,
},
);
};
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type {
SchedulerControllerParams,
SchedulerDisplayUnit,
} from "./controller/controller";
export { useSchedulerGestures } from "./controller/gestureControls";

export type { OnSelectFn } from "./controller/selectControls";
export { Scheduler } from "./Scheduler/Scheduler";
export type { SchedulerProps } from "./Scheduler/Scheduler";
Expand Down
10 changes: 3 additions & 7 deletions stories/Gestures.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import dayjs from "dayjs";
import {
Scheduler,
useSchedulerController,
useSchedulerGestures,
} from "mantine-resource-timeline";
import { Scheduler, useSchedulerController } from "mantine-resource-timeline";
import { useMemo } from "react";

export function GestureControls() {
Expand All @@ -27,7 +23,6 @@ export function GestureControls() {
(typeof data)[number],
(typeof resources)[number]
>({});
useSchedulerGestures(controller);

return (
<Scheduler
Expand All @@ -39,7 +34,8 @@ export function GestureControls() {
endDateAccessor="endDate"
resourceIdAccessor="resourceId"
startDateAccessor="startDate"
></Scheduler>
enableGestures
/>
);
}

Expand Down

0 comments on commit 422883d

Please sign in to comment.