Skip to content

Conversation

@marklundin
Copy link
Collaborator

@marklundin marklundin commented May 8, 2025

This PR updates the Script component to support ref forwarding.

function Test(){
  // Define the ref
  const scriptRef = useRef()
  
  // scriptRef now points to the script instance :)
  useEffect(() => console.log(scriptRef.current.speed)) // logs 10!

  return <Script script={TestScript} speed={10} ref={scriptRef}/>
}

It also adds test for the Script Component

Fixes #126

  • Introduced comprehensive unit tests for the Script component, validating prop passing, ref forwarding, initialization, re-rendering behavior, and cleanup on unmount.
  • Refactored the useScript hook to support ref forwarding, enhancing the integration with the Script component.
  • Updated type definitions to improve type safety and clarity in the codebase.

These changes enhance the testing coverage and improve the overall functionality of the Script component and its associated hooks.

- Introduced comprehensive unit tests for the Script component, validating prop passing, ref forwarding, initialization, re-rendering behavior, and cleanup on unmount.
- Refactored the useScript hook to support ref forwarding, enhancing the integration with the Script component.
- Updated type definitions to improve type safety and clarity in the codebase.

These changes enhance the testing coverage and improve the overall functionality of the Script component and its associated hooks.
@marklundin marklundin self-assigned this May 8, 2025
@marklundin marklundin added the enhancement New feature or request label May 8, 2025
@marklundin marklundin requested review from Copilot May 8, 2025 15:01
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces ref forwarding for the Script component and enhances its test coverage.

  • Added the SubclassOf type for improved type safety in ref forwarding.
  • Updated the useScript hook to accept and handle a forwarded ref.
  • Refactored the Script component to use forwardRef and updated the associated type definitions, with additional tests covering prop passing, ref forwarding, initialization, re-rendering, and cleanup.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
packages/lib/src/utils/types-utils.ts Added SubclassOf type to support the updated ref forwarding API.
packages/lib/src/hooks/use-script.tsx Modified hook to accept a ref, assign it during creation and cleanup, and update types.
packages/lib/src/components/Script.tsx Converted to forwardRef-based component and updated prop types accordingly.
packages/lib/src/components/Script.test.tsx Added extensive tests to validate the ref forwarding and component behavior.
Comments suppressed due to low confidence (1)

packages/lib/src/hooks/use-script.tsx:42

  • The cast 'scriptConstructor as unknown as typeof Script' may obscure type safety issues. Consider refining the type definitions so that a double cast is not necessary.
const scriptInstance = scriptComponent.create(scriptConstructor as unknown as typeof Script, {

@pkg-pr-new
Copy link

pkg-pr-new bot commented May 8, 2025

Open in StackBlitz

npm i https://pkg.pr.new/playcanvas/react/@playcanvas/react@129

commit: b6bebd3

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@marklundin marklundin merged commit 84e9df5 into main May 8, 2025
5 checks passed
@marklundin marklundin deleted the feat-script-refs branch May 8, 2025 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Forward Refs for Script components

2 participants