-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Image Cropper: add package and basic stories #72414
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
Conversation
be3336a to
ddcf6df
Compare
|
Size Change: +11 B (0%) Total Size: 2.54 MB
ℹ️ View Unchanged
|
aa5488c to
1324adf
Compare
|
I think there's only one thing left to do here, or two, depending on how we want to go ahead: The image's aspect ratio on trunk behaves a little erratically onRotate. It flips onRotate, but doesn't honour the users' chosen aspect ratio thereafter. Even if you select square (1:1) it loses the aspect ration onRotate. Kapture.2025-11-13.at.15.16.08.mp4This branch honours the users' chosen aspect ratio and maintains it onRotate, but the aspect ratio doesn't flip. Kapture.2025-11-13.at.15.17.51.mp4Here's how I think it should work: Unless the user has selected an aspect ratio other than the default (width/height) aspect ratio, then flip the aspect ratio. The second I was speaking out is that we split this PR seeing as we know it's working now:
I think that's the go! |
Yep, that's how I'd expect it to work, too.
Sounds good! We could also look at trying to add a Storybook story to the package as part of step 1 to provide example usage / put it through its paces? |
7f5b2e0 to
1b450e9
Compare
8772f33 to
51947b4
Compare
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
andrewserong
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm liking the direction of this package, and the overall idea of wrapping react-easy-crop so that consumers can have a simpler way of interacting with cropping, and that we've got a package for implementing additional long-requested features like free form cropping 👍
Other than what Isabel mentioned about the story, I just noticed a couple of places where any is being used as a type, and I wondered if it'd be worth copying over a couple of react-easy-crop's types since they look pretty useful?
|
Thanks for testing!
I'm thinking this should be the responsibility of the implementation. For example, it could be done by calculating the max scaled size of the container when the image renders. The current block editor has its own funky workaround, which something like that might break. See #73277 Edit: Did you just mean in the story? Sorry, I jumped ahead. If so, then... can do!
No reason. It's up to the implementation, but no harm in adding them. Thanks! |
Thanks again, folks! I fixed the types. I'll update the story with the above tomorrow. |
* Reintroduced the `@wordpress/interactivity` package in package-lock.json. * Removed unused `useTransformImage` hook from the image editing context. * Integrated `ImageEditingProvider` into the `ImageEditor` component to manage image editing state. * Updated `RotationButton` and `ZoomDropdown` components to utilize the editing context for disabling buttons during image processing. * Adjusted `useSaveImage` to derive crop and rotation state from the image cropper context.
* Added useImageCropper hook to manage aspect ratio state. * Updated setAspect function to modify cropper state based on user selection. * Derived defaultAspect from media size for improved aspect ratio handling.
* Removed unused `useState` import to streamline the code. * Enhanced JSDoc comments for better clarity on component props. * Adjusted comment formatting for improved readability.
…er package * Removed references to `@wordpress/image-cropper` from various components. * Integrated `react-easy-crop` for image cropping functionality. * Updated `ImageEditingProvider` to manage crop and rotation state. * Enhanced `AspectRatioDropdown`, `RotationButton`, and `ZoomDropdown` to utilize the new context for improved state management.
* Removed `@wordpress/image-cropper` dependency from package-lock.json. * Introduced new story for the ImageCropper component in `index.story.tsx`. * Updated Storybook main configuration to include image-cropper stories.
…ct-easy-crop` dependency for image cropping functionality.
* Introduced rotation, flip, and zoom controls in the WithControlsComponent. * Updated the DefaultComponent to include minZoom and maxZoom props. * Enhanced the story for ImageCropper to demonstrate new interactive features.
… the ongoing refactor to eliminate the dependency on the image-cropper package.
I didn't mean specifically one or the other 😄 It would be nice if this just worked out of the box when adding the package. But if that's not doable, then adding the necessary code in the story for it to work properly is the next best thing. Both because the story represents the component, so it's not great if it appears broken, and also because the story is a good reference for when you need to understand how to use the component, so if there are necessary additions to make it work properly they should be included there. |
Makes sense, thanks for the explainer. The reason I hesitate to add custom code that changes the behaviour of the container in the package is because it assumes an environment that might not be there. I think, for now, it's good to be flexible until we see this working in several "containers", e.g., the existing block editor, and in the future, a media library or something. I'll update the story and we can always come back to it. |
* Added aspect ratio selection functionality to the WithControlsComponent. * Introduced a new CSS file for styling the story components. * Refactored layout to improve the presentation of the ImageCropper in the story.
8c30e4f to
b5697d5
Compare
andrewserong
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates, this is looking good to get in to me! As with these sorts of PRs, I think it's good to land it when it's feeling like a good first step, and look at building out additional features in smaller PRs once it lands. Since this is a private package, too, and isn't being used anywhere, that should set things up well for iterations IMO, but it's already testing nicely for me in Storybook, and I like the overall shape of it.
I think once it lands, then next feature I'd like to see would be to default the aspect ratio to being the same as the natural aspect ratio of the image itself. That way folks could click to rotate an image before selecting an aspect ratio to apply just a rotation without also cropping.
In any case, this looks good to land to me, I think I'm just excited to see more image editing tools (and APIs to do so) in Gutenberg 😄
|
Thank you @andrewserong! I'll follow up on: |

What? How? Why??!!
This PR adds a new, private package
@wordpress/image-cropper, which is fundamentally a wrapper aroundreact-easy-crop.The block editor currently uses the
react-easy-croppackage for the inline image editor.The rationale behind this PR is formalize the API to interact with the image cropper, and, eventually, to make the package available across the application, and not just from the
block-editorpackage.The latter is being worked on in a block editor integration PR.
Testing
Check the basic implementation in Storybook:
npm run storybook:devhttp://localhost:50240/?path=/docs/imagecropper-imagecropper--docs
http://localhost:50240/?path=/story/imagecropper-imagecropper--with-controls
2025-11-20.16.13.04.mp4
Related