Cleaned up hooks
This commit is contained in:
@@ -24,6 +24,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.symbol {
|
.symbol {
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: 14px;
|
||||||
aspect-ratio: 1 / 1;
|
aspect-ratio: 1 / 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+7
-21
@@ -7,7 +7,6 @@ import {
|
|||||||
isTouchEvent,
|
isTouchEvent,
|
||||||
minmax,
|
minmax,
|
||||||
positionToValue,
|
positionToValue,
|
||||||
valueToPosition,
|
|
||||||
} from "@/util";
|
} from "@/util";
|
||||||
|
|
||||||
if (typeof TouchEvent === "undefined") {
|
if (typeof TouchEvent === "undefined") {
|
||||||
@@ -18,10 +17,6 @@ if (typeof TouchEvent === "undefined") {
|
|||||||
export function useCrosshair({
|
export function useCrosshair({
|
||||||
origin,
|
origin,
|
||||||
dimensions,
|
dimensions,
|
||||||
setXPosition,
|
|
||||||
setYPosition,
|
|
||||||
xValue,
|
|
||||||
yValue,
|
|
||||||
setXValue,
|
setXValue,
|
||||||
setYValue,
|
setYValue,
|
||||||
xValueRange,
|
xValueRange,
|
||||||
@@ -29,10 +24,6 @@ export function useCrosshair({
|
|||||||
}: {
|
}: {
|
||||||
origin: CartesianSpace;
|
origin: CartesianSpace;
|
||||||
dimensions: CartesianSpace;
|
dimensions: CartesianSpace;
|
||||||
setXPosition: Setter<number>;
|
|
||||||
setYPosition: Setter<number>;
|
|
||||||
xValue: number;
|
|
||||||
yValue: number;
|
|
||||||
setXValue: Setter<number>;
|
setXValue: Setter<number>;
|
||||||
setYValue: Setter<number>;
|
setYValue: Setter<number>;
|
||||||
xValueRange: Range;
|
xValueRange: Range;
|
||||||
@@ -41,8 +32,11 @@ export function useCrosshair({
|
|||||||
const [isDragging, setIsDragging] = useState(false);
|
const [isDragging, setIsDragging] = useState(false);
|
||||||
const crosshairRef = useRef<HTMLDivElement>(null);
|
const crosshairRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
// Crosshair UI refs
|
||||||
const originRef = useRef(origin);
|
const originRef = useRef(origin);
|
||||||
const dimensionsRef = useRef(dimensions);
|
const dimensionsRef = useRef(dimensions);
|
||||||
|
|
||||||
|
// Crosshair value refs
|
||||||
const setXValueRef = useRef(setXValue);
|
const setXValueRef = useRef(setXValue);
|
||||||
const setYValueRef = useRef(setYValue);
|
const setYValueRef = useRef(setYValue);
|
||||||
const xValueRangeRef = useRef(xValueRange);
|
const xValueRangeRef = useRef(xValueRange);
|
||||||
@@ -51,11 +45,12 @@ export function useCrosshair({
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
originRef.current = origin;
|
originRef.current = origin;
|
||||||
dimensionsRef.current = dimensions;
|
dimensionsRef.current = dimensions;
|
||||||
setXValueRef.current = setXValue;
|
}, [origin, dimensions]);
|
||||||
setYValueRef.current = setYValue;
|
|
||||||
|
useEffect(() => {
|
||||||
xValueRangeRef.current = xValueRange;
|
xValueRangeRef.current = xValueRange;
|
||||||
yValueRangeRef.current = yValueRange;
|
yValueRangeRef.current = yValueRange;
|
||||||
}, [origin, dimensions, setXValue, setYValue, xValueRange, yValueRange]);
|
}, [xValueRange, yValueRange]);
|
||||||
|
|
||||||
const calculatePositions = useCallback((event: MouseEvent | TouchEvent) => {
|
const calculatePositions = useCallback((event: MouseEvent | TouchEvent) => {
|
||||||
const orig = originRef.current;
|
const orig = originRef.current;
|
||||||
@@ -122,15 +117,6 @@ export function useCrosshair({
|
|||||||
[calculatePositions, handleMove, handleEnd],
|
[calculatePositions, handleMove, handleEnd],
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const dims = dimensionsRef.current;
|
|
||||||
const newXPos = valueToPosition(xValue, dims.x - 1, xValueRangeRef.current);
|
|
||||||
const newYPos = valueToPosition(yValue, dims.y - 1, yValueRangeRef.current);
|
|
||||||
|
|
||||||
if (newXPos === newXPos) setXPosition(newXPos);
|
|
||||||
if (newYPos === newYPos) setYPosition(newYPos);
|
|
||||||
}, [xValue, yValue, setXPosition, setYPosition]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const currentRef = crosshairRef.current;
|
const currentRef = crosshairRef.current;
|
||||||
if (currentRef) {
|
if (currentRef) {
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ export function useSlider({
|
|||||||
|
|
||||||
// Internal position management
|
// Internal position management
|
||||||
const [position, setPosition] = useState(0);
|
const [position, setPosition] = useState(0);
|
||||||
const positionRef = useRef(0);
|
const positionRef = useRef(position);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
directionRef.current = direction;
|
directionRef.current = direction;
|
||||||
@@ -68,8 +68,11 @@ export function useSlider({
|
|||||||
dimensions.x,
|
dimensions.x,
|
||||||
dimensions.y,
|
dimensions.y,
|
||||||
);
|
);
|
||||||
|
}, [direction, origin, dimensions]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
valueRangeRef.current = valueRange;
|
valueRangeRef.current = valueRange;
|
||||||
}, [direction, origin, dimensions, valueRangeRef]);
|
}, [valueRangeRef]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setValueRef.current = setValue;
|
setValueRef.current = setValue;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
|
|
||||||
import type { CartesianSpace } from "@/types";
|
import type { CartesianSpace } from "@/types";
|
||||||
|
import { valueToPosition } from "@/util";
|
||||||
|
|
||||||
import { useCrosshair } from "../crosshair";
|
import { useCrosshair } from "../crosshair";
|
||||||
|
|
||||||
@@ -9,20 +10,16 @@ import { useCrosshair } from "../crosshair";
|
|||||||
function TestSquare() {
|
function TestSquare() {
|
||||||
const [origin, setOrigin] = useState<CartesianSpace>({ x: 0, y: 0 });
|
const [origin, setOrigin] = useState<CartesianSpace>({ x: 0, y: 0 });
|
||||||
const [dimensions, setDimensions] = useState<CartesianSpace>({ x: 0, y: 0 });
|
const [dimensions, setDimensions] = useState<CartesianSpace>({ x: 0, y: 0 });
|
||||||
const [xPosition, setXPosition] = useState(0);
|
|
||||||
const [yPosition, setYPosition] = useState(0);
|
|
||||||
const [xValue, setXValue] = useState(0);
|
const [xValue, setXValue] = useState(0);
|
||||||
const [yValue, setYValue] = useState(0);
|
const [yValue, setYValue] = useState(0);
|
||||||
const xValueRange = { min: 0, max: 100 };
|
const xValueRange = { min: 0, max: 100 };
|
||||||
const yValueRange = { min: 0, max: 100 };
|
const yValueRange = { min: 0, max: 100 };
|
||||||
|
const [xPosition, setXPosition] = useState(0);
|
||||||
|
const [yPosition, setYPosition] = useState(0);
|
||||||
|
|
||||||
const { crosshairRef, isDragging } = useCrosshair({
|
const { crosshairRef, isDragging } = useCrosshair({
|
||||||
origin,
|
origin,
|
||||||
dimensions,
|
dimensions,
|
||||||
setXPosition,
|
|
||||||
setYPosition,
|
|
||||||
xValue,
|
|
||||||
yValue,
|
|
||||||
setXValue,
|
setXValue,
|
||||||
setYValue,
|
setYValue,
|
||||||
xValueRange,
|
xValueRange,
|
||||||
@@ -44,6 +41,13 @@ function TestSquare() {
|
|||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const newXPos = valueToPosition(xValue, dimensions.x - 1, xValueRange);
|
||||||
|
const newYPos = valueToPosition(yValue, dimensions.y - 1, yValueRange);
|
||||||
|
setXPosition(newXPos);
|
||||||
|
setYPosition(newYPos);
|
||||||
|
}, [xValue, yValue]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div
|
<div
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ function TestSlider({
|
|||||||
const [origin, setOrigin] = useState<CartesianSpace>({ x: 0, y: 0 });
|
const [origin, setOrigin] = useState<CartesianSpace>({ x: 0, y: 0 });
|
||||||
const [dimensions, setDimensions] = useState<CartesianSpace>({ x: 0, y: 0 });
|
const [dimensions, setDimensions] = useState<CartesianSpace>({ x: 0, y: 0 });
|
||||||
const [value, setValue] = useState(0);
|
const [value, setValue] = useState(0);
|
||||||
const position = useRef(0);
|
const [position, setPosition] = useState(0);
|
||||||
const valueRange = { min: 0, max: 100 };
|
const valueRange = { min: 0, max: 100 };
|
||||||
const { sliderRef, isDragging } = useSlider({
|
const { sliderRef, isDragging } = useSlider({
|
||||||
direction,
|
direction,
|
||||||
@@ -46,9 +46,7 @@ function TestSlider({
|
|||||||
const maxValue =
|
const maxValue =
|
||||||
direction == Direction.HORIZONTAL ? dimensions.x : dimensions.y;
|
direction == Direction.HORIZONTAL ? dimensions.x : dimensions.y;
|
||||||
if (maxValue > 0) {
|
if (maxValue > 0) {
|
||||||
const percentage = parseFloat(
|
const percentage = parseFloat(((position / maxValue) * 100).toFixed(3));
|
||||||
((position.current / maxValue) * 100).toFixed(3),
|
|
||||||
);
|
|
||||||
setValue(percentage);
|
setValue(percentage);
|
||||||
} else {
|
} else {
|
||||||
setValue(0);
|
setValue(0);
|
||||||
@@ -61,7 +59,8 @@ function TestSlider({
|
|||||||
dimensions.x,
|
dimensions.x,
|
||||||
dimensions.y,
|
dimensions.y,
|
||||||
);
|
);
|
||||||
position.current = valueToPosition(value, maxPosition, valueRange);
|
const newPosition = valueToPosition(value, maxPosition, valueRange);
|
||||||
|
setPosition(newPosition);
|
||||||
}, [value, direction, dimensions, valueRange]);
|
}, [value, direction, dimensions, valueRange]);
|
||||||
|
|
||||||
const isHorizontal = direction === Direction.HORIZONTAL;
|
const isHorizontal = direction === Direction.HORIZONTAL;
|
||||||
@@ -98,8 +97,8 @@ function TestSlider({
|
|||||||
style={{
|
style={{
|
||||||
position: "absolute",
|
position: "absolute",
|
||||||
...(isHorizontal
|
...(isHorizontal
|
||||||
? { left: position.current, top: 0 }
|
? { left: position, top: 0 }
|
||||||
: { left: 0, top: position.current }),
|
: { left: 0, top: position }),
|
||||||
width: 50,
|
width: 50,
|
||||||
height: 50,
|
height: 50,
|
||||||
background: "rgba(255,0,0,0.5)",
|
background: "rgba(255,0,0,0.5)",
|
||||||
@@ -109,8 +108,7 @@ function TestSlider({
|
|||||||
</div>
|
</div>
|
||||||
<p>
|
<p>
|
||||||
Value: <span data-cy="value-display">{value}</span>
|
Value: <span data-cy="value-display">{value}</span>
|
||||||
<br /> Position:{" "}
|
<br /> Position: <span data-cy="position-display">{position}</span>px
|
||||||
<span data-cy="position-display">{position.current}</span>px
|
|
||||||
</p>
|
</p>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user