Skip to content

Commit

Permalink
Update Stage component with TypeScript interface, switch to Concurren…
Browse files Browse the repository at this point in the history
…tRoot, and enhance test cases for better type safety.
  • Loading branch information
lavrton committed Dec 6, 2024
1 parent e7c8c53 commit cd90e40
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
25 changes: 19 additions & 6 deletions src/ReactKonvaCore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@

import React from 'react';
import Konva from 'konva/lib/Core.js';
import type { Stage as KonvaStage } from 'konva/lib/Stage.js';
import ReactFiberReconciler, {
RootTag,
SuspenseHydrationCallbacks,
TransitionTracingCallbacks,
} from 'react-reconciler';
import { LegacyRoot } from 'react-reconciler/constants.js';
import { ConcurrentRoot } from 'react-reconciler/constants.js';
import * as HostConfig from './ReactKonvaHostConfig.js';
import { applyNodeProps, toggleStrictMode } from './makeUpdates.js';
import { useContextBridge, FiberProvider } from 'its-fine';
Expand Down Expand Up @@ -90,19 +91,21 @@ const StageWrap = (props) => {
// @ts-ignore
fiberRef.current = (KonvaRenderer.createContainer as NewCreateContainer)(
stage.current,
LegacyRoot,
ConcurrentRoot,
null,
false,
null,
undefined,
undefined,
'',
console.error,
console.error,
console.error,
null
);
KonvaRenderer.updateContainer(
React.createElement(Bridge, {}, props.children),
fiberRef.current
fiberRef.current,
null,
() => {}
);

return () => {
Expand Down Expand Up @@ -171,7 +174,17 @@ KonvaRenderer.injectIntoDevTools({
rendererPackageName: 'react-konva',
});

export const Stage = React.forwardRef((props, ref) => {
// Add this interface
interface StageProps extends React.RefAttributes<KonvaStage> {
children?: React.ReactNode;
width?: number;
height?: number;
name?: string;
[key: string]: any;
}

// Update Stage component declaration
export const Stage: React.FC<StageProps> = React.forwardRef((props, ref) => {
return React.createElement(
FiberProvider,
{},
Expand Down
2 changes: 2 additions & 0 deletions src/ReactKonvaHostConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ export function shouldSetTextContent(type, props) {
export const isPrimaryRenderer = false;
export const warnsIfNotActing = true;
export const supportsMutation = true;
export const supportsPersistence = false;
export const supportsHydration = false;

export function appendChild(parentInstance, child) {
if (child.parent === parentInstance) {
Expand Down
12 changes: 6 additions & 6 deletions test/react-konva-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ describe('initial mounting and refs', () => {

it('check initial props', async () => {
const App = ({ width, height }) => {
const ref = React.useRef<Konva.Stage>(null);
React.useEffect(() => {
expect(ref.current.width()).equals(100);
expect(ref.current?.width()).equals(100);
});
const ref = React.useRef<Konva.Stage>(null);
return <Stage ref={ref} width={width} heigh={height} />;
return <Stage ref={ref} width={width} height={height} />;
};
await render(<App width={100} height={100} />);
});
Expand All @@ -123,13 +123,13 @@ describe('initial mounting and refs', () => {
const MyRect = React.forwardRef((props, ref) => <Rect ref={ref} />);

const App = () => {
const ref = React.useRef();
const stageRef = React.useRef();
const ref = React.useRef<Konva.Rect>(null);
const stageRef = React.useRef<Konva.Stage>(null);
React.useEffect(() => {
expect((ref.current as any) instanceof Konva.Rect).to.be.true;
});
return (
<Stage ref={stageRef} name='hello'>
<Stage ref={stageRef} name="hello">
<Layer>
<MyRect ref={ref} />
</Layer>
Expand Down

0 comments on commit cd90e40

Please sign in to comment.