Skip to content

Commit fdf3ba2

Browse files
committed
Set App's blocklyComponent in a callback named onBlocklyComponentCreated rather than via ref=.
1 parent d09b9ce commit fdf3ba2

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/App.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,10 @@ const AppContent: React.FC<AppContentProps> = ({ project, setProject }): React.J
425425
}
426426
}, [currentModule]);
427427

428+
const setupBlocklyComponent = (newBlocklyComponent: BlocklyComponentType) => {
429+
blocklyComponent.current = newBlocklyComponent;
430+
};
431+
428432
const setupWorkspace = (newWorkspace: Blockly.WorkspaceSvg) => {
429433
if (!blocklyComponent.current || !storage) {
430434
return;
@@ -547,9 +551,9 @@ const AppContent: React.FC<AppContentProps> = ({ project, setProject }): React.J
547551
<Antd.Layout>
548552
<Content>
549553
<BlocklyComponent
554+
onBlocklyComponentCreated={setupBlocklyComponent}
550555
theme={theme}
551556
onWorkspaceRecreated={setupWorkspace}
552-
ref={blocklyComponent}
553557
/>
554558
</Content>
555559
<Sider

src/reactComponents/BlocklyComponent.tsx

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface BlocklyComponentType {
3939

4040
/** Interface for props passed to the BlocklyComponent. */
4141
export interface BlocklyComponentProps {
42+
onBlocklyComponentCreated: (blocklyComponent: BlocklyComponentType) => void;
4243
theme: string;
4344
onWorkspaceRecreated: (workspace: Blockly.WorkspaceSvg) => void;
4445
}
@@ -80,8 +81,8 @@ const WORKSPACE_STYLE: React.CSSProperties = {
8081
* React component that renders a Blockly workspace with proper initialization,
8182
* cleanup, and resize handling.
8283
*/
83-
const BlocklyComponent = React.forwardRef<BlocklyComponentType | null, BlocklyComponentProps>(
84-
(props, ref): React.JSX.Element => {
84+
export default function BlocklyComponent(props: BlocklyComponentProps): React.JSX.Element {
85+
// TODO(lizlooney): Fix indentation in a separate commit.
8586
const blocklyDiv = React.useRef<HTMLDivElement | null>(null);
8687
const workspaceRef = React.useRef<Blockly.WorkspaceSvg | null>(null);
8788

@@ -249,6 +250,12 @@ const BlocklyComponent = React.forwardRef<BlocklyComponentType | null, BlocklyCo
249250

250251
// Initialize Blockly workspace
251252
React.useEffect(() => {
253+
if (props.onBlocklyComponentCreated) {
254+
const blocklyComponent: BlocklyComponentType = {
255+
getBlocklyWorkspace,
256+
};
257+
props.onBlocklyComponentCreated(blocklyComponent);
258+
}
252259
initializeWorkspace();
253260
return cleanupWorkspace;
254261
}, []);
@@ -270,23 +277,9 @@ const BlocklyComponent = React.forwardRef<BlocklyComponentType | null, BlocklyCo
270277
return setupResizeObserver();
271278
}, []);
272279

273-
// Expose methods through ref
274-
React.useImperativeHandle(
275-
ref,
276-
(): BlocklyComponentType => ({
277-
getBlocklyWorkspace,
278-
}),
279-
[]
280-
);
281-
282280
return (
283281
<div className="blockly-workspace-container" style={FULL_SIZE_STYLE}>
284282
<div ref={blocklyDiv} style={WORKSPACE_STYLE} />
285283
</div>
286284
);
287285
}
288-
);
289-
290-
BlocklyComponent.displayName = 'BlocklyComponent';
291-
292-
export default BlocklyComponent;

0 commit comments

Comments
 (0)