-
-
Notifications
You must be signed in to change notification settings - Fork 216
refactor(theme-default): refactor mediumZoom as <ImgZoom /> in getCustomMDXComponent #2368
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
base: main
Are you sure you want to change the base?
refactor(theme-default): refactor mediumZoom as <ImgZoom /> in getCustomMDXComponent #2368
Conversation
❌ Deploy Preview for rspress-v2 failed. Why did it fail? →
|
❌ Deploy Preview for rspress failed. Why did it fail? →
|
@@ -1,6 +1,42 @@ | |||
import { normalizeImagePath } from '@rspress/runtime'; | |||
import mediumZoom from 'medium-zoom'; |
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 your contribution,
what's the difference between 'react-medium-image-zoom'
and 'medium-zoom'
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.
Both of them can be used to implement image zooming.
The 'medium-zoom'
is the package used in @rspress/plugin-medium-zoom
, it's a standalone vanilla JavaScript library and it's a lower-level, framework-agnostic JavaScript library.
The 'react-medium-image-zoom'
is a React component library designed specifically for React applications. It typically wrap the element with its component, and it handles the zoom functionality, including managing its own state or allowing for controlled state management.
When I refactored this part, I considered using the existing solution within the @rspress/plugin-medium-zoom
plugin. If 'react-medium-image-zoom'
is a better fit for this project, I will make another commit using it.
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.

medium-zoom
has not been maintained for a long time. so I recommend 'react-medium-image-zoom'
more
BTW, there is an interesting challenge for you here, which is a bit more complex than this PR, you can continue with it.
You can try using the default value of mediumZoom
config in rspress.config.ts
to switch Img
component to ImgZoom
component by default for users
Here are some tips that you might find useful.
- you can pass
process.env.MEDIUM_ZOOM
viainitRsbuild.ts
source.define
from compileTime to runtime
- switch
Img
component toImgZoom
component by default
Some pseudocode
import { A } from './a';
import { Code } from './code';
import { Hr } from './hr';
import { Img, ImgZoom } from './img';
import { Li, Ol, Ul } from './list';
import { Blockquote, P, Strong } from './paragraph';
import { PreWithCodeButtonGroup } from './pre';
import { Table, Td, Th, Tr } from './table';
import { H1, H2, H3, H4, H5, H6 } from './title';
export function getCustomMDXComponent() {
return {
h1: H1,
h2: H2,
h3: H3,
h4: H4,
h5: H5,
h6: H6,
ul: Ul,
ol: Ol,
li: Li,
table: Table,
td: Td,
th: Th,
tr: Tr,
hr: Hr,
p: P,
blockquote: Blockquote,
strong: Strong,
a: A,
code: Code,
pre: PreWithCodeButtonGroup,
img: process.env.MEDIUM_ZOOM ? ImgZoom : Img,
};
}
- remove this plugin
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.
Is that possible to check the img size to determine whether the img should be considered as zoomable?
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.
Is that possible to check the img size to determine whether the img should be considered as zoomable?
This seems to be a separate feature that can be implemented in other PRs.
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.
It seems a little challenging. I will complete this challenge in my PR.
There is an issue that is very similar to this PR, you can also try it out~ |
I'll try it after I finish this PR, thank you! |
…age-zoom' and refactor configuration - Replace 'medium-zoom' library with 'react-medium-image-zoom' - Refactor 'mediumZoom' configuration to use process.env for conditional component selection - Update Img/ImgZoom component mapping logic
Hi @SoonIter, I've completed this PR following your guidance. I've replaced the I'm considering whether |
Sorry, I made a mistake There are two image syntaxes in md, after this change, only images with the md syntax We also need to use <img src="" />
![]() So I might pending this PR, there are still some tasks. |
Summary
refactor the
plugin-medium-zoom
toImgZoom
ingetCustomMDXComponent
with React 19 Compatibility.This PR refactors the image component implementation by:
Img
and add a new componentImgZoom
to make the image zoomable.The ImgZoom component maintains all previous zoom functionality using medium-zoom library, with enhanced ref forwarding capabilities and proper cleanup logic to prevent memory leaks. Both components are properly exported and registered in the MDX component map for immediate use in documentation.
To add a click-to-zoom image in MDX, use the following:
<ImgZoom src="./example.png" alt="An image that can be zoomed" />
Related Issue
close: #2325
Checklist