-
Notifications
You must be signed in to change notification settings - Fork 1
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
daf5340
commit e04e42a
Showing
4 changed files
with
66 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { isTrackedTransform } from '@dreamlab.gg/core/math' | ||
import type { | ||
PositionListener, | ||
RotationListener, | ||
Transform, | ||
ZIndexListener, | ||
} from '@dreamlab.gg/core/math' | ||
import { | ||
useCallback, | ||
useEffect, | ||
useState, | ||
} from 'https://esm.sh/v136/react@18.2.0' | ||
|
||
/** | ||
* Return a reference to a {@link Transform} that will automatically trigger React re-renders | ||
* | ||
* @param transform - Transform | ||
*/ | ||
export const useTransform = (transform: Transform): Transform => { | ||
const [_x, setX] = useState<number>() | ||
const [_y, setY] = useState<number>() | ||
const [_r, setRotation] = useState<number>() | ||
const [_z, setZIndex] = useState<number>() | ||
|
||
const onPositionChanged = useCallback<PositionListener>( | ||
(component, value, _delta) => { | ||
if (component === 'x') setX(value) | ||
else if (component === 'y') setY(value) | ||
}, | ||
[setX, setY], | ||
) | ||
|
||
const onRotationChanged = useCallback<RotationListener>( | ||
(value, _delta) => setRotation(value), | ||
[setRotation], | ||
) | ||
|
||
const onZIndexChanged = useCallback<ZIndexListener>( | ||
value => setZIndex(value), | ||
[setZIndex], | ||
) | ||
|
||
useEffect(() => { | ||
if (!isTrackedTransform(transform)) { | ||
throw new Error('`useTransform` only works with tracked transforms') | ||
} | ||
|
||
transform.addPositionListener(onPositionChanged) | ||
transform.addRotationListener(onRotationChanged) | ||
transform.addZIndexListener(onZIndexChanged) | ||
|
||
return () => { | ||
transform.removeListener(onPositionChanged) | ||
transform.removeListener(onRotationChanged) | ||
transform.removeListener(onZIndexChanged) | ||
} | ||
}) | ||
|
||
return transform | ||
} |