Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add better type definitions for block props #52

Merged
merged 72 commits into from
Feb 19, 2025

Conversation

Swanand01
Copy link
Contributor

@Swanand01 Swanand01 commented Feb 7, 2025

What

This PR adds type definitions for block prop types.

Why

The exported block definitions can be used while overriding block definitions.

Related Issue(s):

https://github.com/rtCamp/snapwp/issues/53

How

The newly created prop types are exported from the component file. For now, the types are copied from the graphql generated types.

Testing Instructions

Screenshots

Additional Info

Checklist

  • I have read the Contribution Guidelines.
  • My code is tested to the best of my abilities.
  • My code passes all lints (ESLint, tsc, prettier etc.).
  • My code has detailed inline documentation.
  • I have added unit tests to verify the code works as intended.
  • I have updated the project documentation accordingly.

@Swanand01
Copy link
Contributor Author

@justlevine @ayushnirwal
The block prop types generated by graphql have a lot of attributes that are optional.
Should the block renderer/component assume them to be optional?
Also, can we please have graphql return undefined for an optional attribute, instead of undefined | null

Copy link

changeset-bot bot commented Feb 14, 2025

🦋 Changeset detected

Latest commit: 83f223a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@snapwp/blocks Patch
@snapwp/query Patch
@snapwp/types Patch
@snapwp/core Patch
@snapwp/next Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

}

export type BlockProps<
T extends Record< string, unknown > = Record< string, unknown >,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think T should extend BlockData

@ayushnirwal ayushnirwal self-requested a review February 18, 2025 05:46
Copy link
Collaborator

@ayushnirwal ayushnirwal left a comment

Choose a reason for hiding this comment

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

A Few minor changes required. Love to see the scope minimized back to the original scope of the issue ( Type Defs for Props and Attributes of Block Renderes ). Other types related to Block Manager class, Block overloading etc., should be handled later.

PS: update the PR description too. The issue link is broken.

Comment on lines +48 to +60
const customBlockDefinition =
BlockManager.blockDefinitions[ node.type ];
const defaultBlockDefinition = defaultBlockDefinitions[ node.type ];

if ( customBlockDefinition === null ) {
// If explicitly set to null in custom definitions, use default renderer
node.renderer = BlockManager.blockDefinitions.default || Default;
} else if ( customBlockDefinition ) {
// If custom definition exists, use it
node.renderer = customBlockDefinition;
} else if ( defaultBlockDefinition ) {
// If no custom definition but default definition exists, use default
node.renderer = defaultBlockDefinition;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this refactoring required now that we decided to minimize the scope of this PR to just add Type definitions and helper for BlockProps and BlockAttributes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't hurt to leave this here, since this is the desired behaviour anyways.

Comment on lines 32 to 35
// Removing renderer and children from props. Renderer should not be passed to the component.
delete props.renderer;
delete props.children;

Copy link
Collaborator

Choose a reason for hiding this comment

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

This used to delete the keys too. Revert this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to use Record<any, any> for the props currently. We should fix the renderer and prop types in a separate PR.

@@ -45,6 +45,7 @@
"peerDependencies": {
"react": "^19.0.0",
"@apollo/client": "^3.11.4",
"graphql": "^16.9.0"
"graphql": "^16.9.0",
"@snapwp/types": "*"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I may be wrong but the types package should be a DevDependencies. The types should be in the build artifacts. Please test that theory out and move to DevDependencies if Im correct

@SH4LIN Opinions?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be in devDependencies. The type used in the build artifact will be referenced by other TypeScript files. Since all TypeScript files are compiled during development and not executed at runtime, it should be a devDependency.

@justlevine justlevine requested review from SH4LIN and ayushnirwal and removed request for ayushnirwal February 18, 2025 16:18
ayushnirwal
ayushnirwal previously approved these changes Feb 19, 2025
SH4LIN
SH4LIN previously approved these changes Feb 19, 2025
Copy link
Collaborator

@SH4LIN SH4LIN left a comment

Choose a reason for hiding this comment

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

LGTM

@justlevine justlevine dismissed stale reviews from SH4LIN and ayushnirwal via b19eea7 February 19, 2025 13:43
@justlevine justlevine merged commit 47cad60 into rtCamp:develop Feb 19, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants