Skip to content

Commit

Permalink
Fix reanimated onSize (#1654) (#1687)
Browse files Browse the repository at this point in the history
Co-authored-by: William Candillon <[email protected]>
Co-authored-by: Valery Smirnov <[email protected]>
  • Loading branch information
wcandillon and XantreDev authored Jun 28, 2023
1 parent f3241d3 commit 4f2d7c3
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions package/src/renderer/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {
useMemo,
forwardRef,
useRef,
useLayoutEffect,
} from "react";
import type {
RefObject,
Expand Down Expand Up @@ -31,29 +32,34 @@ export interface CanvasProps extends SkiaBaseViewProps {
onTouch?: TouchHandler;
}

const useOnSizeEvent = (resultValue: SkiaBaseViewProps["onSize"]) => {
const onSize = useValue({
width: 0,
height: 0,
});

useLayoutEffect(() => {
if (!resultValue) {
return;
}
return onSize.addListener((newValue) => {
if (isValue(resultValue)) {
resultValue.current = newValue;
} else {
resultValue.value = newValue;
}
});
}, [resultValue, onSize]);

return onSize;
};

export const Canvas = forwardRef<SkiaDomView, CanvasProps>(
(
{
children,
style,
debug,
mode,
onTouch,
onSize: onSizeReanimatedOrSkia,
...props
},
{ children, style, debug, mode, onTouch, onSize: _onSize, ...props },
forwardedRef
) => {
const size = useValue({ width: 0, height: 0 });
const onSize = isValue(onSizeReanimatedOrSkia)
? onSizeReanimatedOrSkia
: size;
useEffect(() => {
if (!isValue(onSizeReanimatedOrSkia) && onSizeReanimatedOrSkia) {
return size.addListener((v) => (onSizeReanimatedOrSkia.value = v));
}
return undefined;
}, [onSizeReanimatedOrSkia, size]);
const onSize = useOnSizeEvent(_onSize);
const innerRef = useCanvasRef();
const ref = useCombinedRefs(forwardedRef, innerRef);
const redraw = useCallback(() => {
Expand Down

0 comments on commit 4f2d7c3

Please sign in to comment.