-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7840886
commit be5c949
Showing
11 changed files
with
188 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,68 @@ | ||
export const Group = () => {} | ||
|
||
// ({ children, axis, onReorder, as = "ul" }) => { | ||
// const Component = useConstant(() => motion<typeof as>(as)) | ||
// const order: ItemData<T>[] = [] | ||
// const isReordering = useRef(false) | ||
|
||
// const context: ReorderContextProps<any> = { | ||
// axis, | ||
// registerItem: (id, layout) => { | ||
// order.push({ id, layout: layout[axis] }) | ||
// }, | ||
// updateOrder: (id, offset, velocity) => { | ||
// if (isReordering.current) return | ||
|
||
// const newOrder = checkReorder(order, id, offset, velocity) | ||
|
||
// if (order !== newOrder) { | ||
// isReordering.current = true | ||
// onReorder(newOrder.map((item) => item.id)) | ||
// } | ||
// }, | ||
// } | ||
|
||
// useEffect(() => { | ||
// isReordering.current = false | ||
// }) | ||
|
||
// return ( | ||
// <Component> | ||
// <ReorderContext.Provider value={context}> | ||
// {children} | ||
// </ReorderContext.Provider> | ||
// </Component> | ||
// ) | ||
// }, | ||
import * as React from "react" | ||
import { | ||
FunctionComponent, | ||
PropsWithChildren, | ||
ReactHTML, | ||
useEffect, | ||
useRef, | ||
} from "react" | ||
import { ReorderContext } from "../../context/ReorderContext" | ||
import { motion } from "../../render/dom/motion" | ||
import { HTMLMotionProps } from "../../render/html/types" | ||
import { useConstant } from "../../utils/use-constant" | ||
import { ItemData, ReorderContextProps } from "./types" | ||
import { checkReorder } from "./utils/check-reorder" | ||
|
||
interface Props<V> { | ||
as?: keyof ReactHTML | ||
axis?: "x" | "y" | ||
onReorder: (newOrder: V[]) => void | ||
} | ||
|
||
export function Group<V>({ | ||
children, | ||
as = "ul", | ||
axis = "y", | ||
onReorder, | ||
...props | ||
}: Props<V> & HTMLMotionProps<any> & PropsWithChildren<{}>) { | ||
const Component = useConstant(() => motion(as)) as FunctionComponent< | ||
HTMLMotionProps<any> | ||
> | ||
|
||
const order: ItemData<V>[] = [] | ||
const isReordering = useRef(false) | ||
|
||
const context: ReorderContextProps<any> = { | ||
axis, | ||
registerItem: (value, layout) => { | ||
order.push({ value, layout: layout[axis] }) | ||
}, | ||
updateOrder: (id, offset, velocity) => { | ||
if (isReordering.current) return | ||
|
||
const newOrder = checkReorder(order, id, offset, velocity) | ||
|
||
if (order !== newOrder) { | ||
isReordering.current = true | ||
onReorder(newOrder.map(getValue)) | ||
} | ||
}, | ||
} | ||
|
||
useEffect(() => { | ||
isReordering.current = false | ||
}) | ||
|
||
return ( | ||
<Component {...props}> | ||
<ReorderContext.Provider value={context}> | ||
{children} | ||
</ReorderContext.Provider> | ||
</Component> | ||
) | ||
} | ||
|
||
function getValue<V>(item: ItemData<V>) { | ||
return item.value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,79 @@ | ||
import { ReactHTML } from "hoist-non-react-statics/node_modules/@types/react" | ||
import { invariant } from "hey-listen" | ||
import * as React from "react" | ||
import { motion } from "../.." | ||
import { MotionStyle } from "../../motion/types" | ||
import { | ||
PropsWithChildren, | ||
ReactHTML, | ||
FunctionComponent, | ||
useContext, | ||
useEffect, | ||
useRef, | ||
} from "react" | ||
import { Box, motion, useMotionValue, useTransform } from "../.." | ||
import { ReorderContext } from "../../context/ReorderContext" | ||
import { HTMLMotionProps } from "../../render/html/types" | ||
import { useConstant } from "../../utils/use-constant" | ||
import { MotionValue } from "../../value" | ||
import { isMotionValue } from "../../value/utils/is-motion-value" | ||
|
||
interface Props<V> { | ||
as?: keyof ReactHTML | ||
x?: MotionValue<number> | ||
y?: MotionValue<number> | ||
value: V | ||
style?: MotionStyle | ||
} | ||
|
||
function useDefaultMotionValue(value: any, defaultValue: number = 0) { | ||
return isMotionValue(value) ? value : useMotionValue(defaultValue) | ||
} | ||
|
||
/** | ||
* TODO: Fix slippage when dragging | ||
*/ | ||
export function Item<V>({ | ||
children, | ||
value, | ||
style, | ||
x, | ||
y, | ||
as, | ||
value, | ||
as = "li", | ||
...props | ||
}: Props<V> & HTMLMotionProps<typeof as>) { | ||
const Component = useConstant(() => motion(as)) | ||
}: Props<V> & HTMLMotionProps<any> & PropsWithChildren<{}>) { | ||
const Component = useConstant(() => motion(as)) as FunctionComponent< | ||
HTMLMotionProps<any> | ||
> | ||
|
||
return <Component {...props}></Component> | ||
} | ||
const context = useContext(ReorderContext) | ||
const point = { | ||
x: useDefaultMotionValue(style?.x), | ||
y: useDefaultMotionValue(style?.y), | ||
} | ||
|
||
const zIndex = useTransform([point.x, point.y], ([latestX, latestY]) => | ||
latestX || latestY ? 1 : 0 | ||
) | ||
|
||
// ({ children, value, style, x, y, as = "li", ...props }) => { | ||
// const Component = useConstant(() => motion<typeof as>(as)) | ||
// const context = useContext(ReorderContext) | ||
// const point = { | ||
// x: x || useMotionValue(0), | ||
// y: y || useMotionValue(0), | ||
// } | ||
// const zIndex = useTransform([point.x, point.y], ([latestX, latestY]) => | ||
// latestX || latestY ? 1 : 0 | ||
// ) | ||
// const layout = useRef<Box | null>(null) | ||
const layout = useRef<Box | null>(null) | ||
|
||
// invariant( | ||
// Boolean(context), | ||
// "Reorder.Item must be a child of Reorder.Group" | ||
// ) | ||
invariant(Boolean(context), "Reorder.Item must be a child of Reorder.Group") | ||
|
||
// const { axis, registerItem, updateOrder } = context! | ||
const { axis, registerItem, updateOrder } = context! | ||
|
||
// useEffect(() => { | ||
// registerItem(value, layout.current!) | ||
// }) | ||
useEffect(() => { | ||
registerItem(value, layout.current!) | ||
}) | ||
|
||
// return ( | ||
// <Component | ||
// {...props} | ||
// style={{ ...style, x: point.x, y: point.y, zIndex }} | ||
// layout | ||
// drag | ||
// dragConstraints={originConstraints} | ||
// dragElastic={1} | ||
// onDrag={(_event, { velocity }) => { | ||
// velocity[axis] && | ||
// updateOrder(value, point[axis].get(), velocity[axis]) | ||
// }} | ||
// onLayoutMeasure={(measured) => (layout.current = measured)} | ||
// > | ||
// {children} | ||
// </Component> | ||
// ) | ||
// } | ||
return ( | ||
<Component | ||
drag={axis} | ||
{...props} | ||
style={{ ...style, x: point.x, y: point.y, zIndex }} | ||
layout | ||
dragConstraints={originConstraints} | ||
dragElastic={1} | ||
onDrag={(_event, { velocity }) => { | ||
velocity[axis] && | ||
updateOrder(value, point[axis].get(), velocity[axis]) | ||
}} | ||
onLayoutMeasure={(measured) => (layout.current = measured)} | ||
> | ||
{children} | ||
</Component> | ||
) | ||
} | ||
|
||
const originConstraints = { top: 0, left: 0, right: 0, bottom: 0 } |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.