-
-
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
Open
Tsukistar
wants to merge
7
commits into
web-infra-dev:main
Choose a base branch
from
Tsukistar:tsukistar_mediumZoom_refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+44
−14
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9ee5a53
refactor(theme-default): refactor mediumZoom as <ImgZoom /> in getCus…
Tsukistar 9b6b107
fix(theme-default): add dependency "medium-zoom" in package.json
Tsukistar 6b72fd4
fix(theme-default): update the pnpm-lock.yaml
Tsukistar 8f4e7d8
Merge branch 'main' into tsukistar_mediumZoom_refactor
Tsukistar 070b4c9
refactor(theme-default)!: replace 'medium-zoom' with 'react-medium-im…
Tsukistar 1dc597f
Merge branch 'main' into tsukistar_mediumZoom_refactor
Tsukistar 5b5d6ba
Update index.module.scss
Tsukistar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/theme-default/src/layout/DocLayout/docComponents/img.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,42 @@ | ||
import { normalizeImagePath } from '@rspress/runtime'; | ||
import mediumZoom from 'medium-zoom'; | ||
import type React from 'react'; | ||
import type { ComponentProps } from 'react'; | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
|
||
export const Img = (props: ComponentProps<'img'>) => { | ||
return <img {...props} src={normalizeImagePath(props.src || '')} />; | ||
}; | ||
|
||
export const ImgZoom = ({ | ||
ref: refProp, | ||
...props | ||
}: ComponentProps<'img'> & { ref?: React.Ref<HTMLImageElement> }) => { | ||
const imgRef = useRef<HTMLImageElement>(null); | ||
const combinedRef = useCallback( | ||
(node: HTMLImageElement) => { | ||
if (refProp) { | ||
if (typeof refProp === 'function') { | ||
refProp(node); | ||
} else { | ||
refProp.current = node; | ||
} | ||
} | ||
imgRef.current = node; | ||
}, | ||
[refProp], | ||
); | ||
|
||
useEffect(() => { | ||
if (!imgRef.current) return; | ||
const zoom = mediumZoom(imgRef.current, { | ||
background: 'var(--rp-c-bg)', | ||
}); | ||
|
||
return () => { | ||
zoom.detach(); | ||
}; | ||
}, []); | ||
|
||
return <img ref={combinedRef} {...props} />; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'
Uh oh!
There was an error while loading. Please reload this page.
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
element with its component, and it handles the zoom functionality, including managing its own state or allowing for controlled state management.
'react-medium-image-zoom'
is a React component library designed specifically for React applications. It typically wrap theWhen 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.Uh oh!
There was an error while loading. Please reload this page.
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'
moreBTW, 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 inrspress.config.ts
to switchImg
component toImgZoom
component by default for usersHere are some tips that you might find useful.
process.env.MEDIUM_ZOOM
viainitRsbuild.ts
source.define
from compileTime to runtimehttps://github.com/web-infra-dev/rspress/blob/e0b4b135218ad6065c0a1eabf9d7e63a2447f9db/packages/core/src/node/initRsbuild.ts#L226
Img
component toImgZoom
component by defaulthttps://github.com/web-infra-dev/rspress/blob/e0b4b135218ad6065c0a1eabf9d7e63a2447f9db/packages/theme-default/src/layout/DocLayout/docComponents/index.tsx#L11
Some pseudocode
https://github.com/web-infra-dev/rspress/blob/e0b4b135218ad6065c0a1eabf9d7e63a2447f9db/packages/core/src/node/medium-zoom/index.ts#L6
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.
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.