-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
[icons][docs] Virtualize icons svg #43939
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d14634c
Virtualize icons svg
Janpot c6cdb93
[test] Point Istanbul to correct URL (#43935)
Janpot 098a40e
Update SearchIcons.js
Janpot 38f5471
Update SearchIcons.js
Janpot 2c0b951
Update SearchIcons.js
Janpot 7d98c77
Update SearchIcons.js
Janpot e5da1c6
Update SearchIcons.js
Janpot 389f29b
Update SearchIcons.js
Janpot 5d93ff2
Update SearchIcons.js
Janpot d0348a5
Update SearchIcons.js
Janpot 1ecf2f4
Merge remote-tracking branch 'upstream/master' into icons-opt
Janpot cf1db92
Update SearchIcons.js
Janpot 3f5bed7
Update SearchIcons.js
Janpot bfb3d32
Update SearchIcons.js
Janpot ae2bbc9
Update SearchIcons.js
Janpot 67b256a
Update SearchIcons.js
Janpot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ import * as mui from '@mui/icons-material'; | |
| import { Link } from '@mui/docs/Link'; | ||
| import { useTranslate } from '@mui/docs/i18n'; | ||
| import useQueryParameterState from 'docs/src/modules/utils/useQueryParameterState'; | ||
|
|
||
| // For Debugging | ||
| // import Menu from '@mui/icons-material/Menu'; | ||
| // import MenuOutlined from '@mui/icons-material/MenuOutlined'; | ||
|
|
@@ -110,6 +111,12 @@ const StyledIcon = styled('span')(({ theme }) => ({ | |
| }, | ||
| })); | ||
|
|
||
| const PlaceholderIcon = styled('div')({ | ||
| width: 69, | ||
| height: 69, | ||
Janpot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| margin: '4px 0', | ||
| }); | ||
|
|
||
| const StyledSvgIcon = styled(SvgIcon)(({ theme }) => ({ | ||
| boxSizing: 'content-box', | ||
| cursor: 'pointer', | ||
|
|
@@ -147,23 +154,61 @@ function handleLabelClick(event) { | |
| selectNode(event.currentTarget); | ||
| } | ||
|
|
||
| function isElmVisible(elm, margin = 0) { | ||
| const rect = elm.getBoundingClientRect(); | ||
| return rect.bottom >= -margin && rect.top <= window.innerHeight + margin; | ||
| } | ||
|
|
||
| function Icon(props) { | ||
| const { icon, onOpenClick } = props; | ||
| const { icon, onOpenClick, initiallyVisible = false } = props; | ||
|
|
||
| const rootRef = React.useRef(null); | ||
| const [isVisible, setIsVisible] = React.useState(initiallyVisible); | ||
|
|
||
| // Virtualize the icons to reduce page size and React rendering time. | ||
| // Only render the icons after they become visible in the viewport. | ||
| React.useEffect(() => { | ||
| const margin = 200; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kept it here to prevent it from being used for anything other than virtualization. Separation of concerns. |
||
| if (!rootRef.current) { | ||
| throw new Error('missing ref'); | ||
| } | ||
Janpot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (isElmVisible(rootRef.current, margin)) { | ||
| setIsVisible(true); | ||
| } | ||
| const observer = new IntersectionObserver( | ||
| (entries) => { | ||
| if (isElmVisible(entries[0].target, margin)) { | ||
| setIsVisible(true); | ||
| } | ||
| }, | ||
| { rootMargin: `${margin}px 0px` }, | ||
| ); | ||
| observer.observe(rootRef.current); | ||
| return () => { | ||
| observer.disconnect(); | ||
| }; | ||
| }, []); | ||
|
|
||
| /* eslint-disable jsx-a11y/click-events-have-key-events */ | ||
| return ( | ||
| <StyledIcon | ||
| key={icon.importName} | ||
| ref={rootRef} | ||
| onClick={handleIconClick} | ||
| data-icon-theme={icon.theme} | ||
| data-icon-name={icon.name} | ||
| > | ||
| <StyledSvgIcon | ||
| component={icon.Component} | ||
| tabIndex={-1} | ||
| onClick={onOpenClick} | ||
| title={icon.importName} | ||
| /> | ||
| {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions -- TODO: a11y */} | ||
| {isVisible ? ( | ||
oliviertassinari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <StyledSvgIcon | ||
| component={icon.Component} | ||
| tabIndex={-1} | ||
| onClick={onOpenClick} | ||
| title={icon.importName} | ||
| /> | ||
| ) : ( | ||
| <PlaceholderIcon /> | ||
| )} | ||
| {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions -- TODO: a11y */} | ||
Janpot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <div onClick={handleLabelClick}>{icon.importName}</div> | ||
| {/* eslint-enable jsx-a11y/click-events-have-key-events */} | ||
| </StyledIcon> | ||
|
|
@@ -175,8 +220,14 @@ const Icons = React.memo(function Icons(props) { | |
|
|
||
| return ( | ||
| <div> | ||
| {icons.map((icon) => ( | ||
| <Icon key={icon.importName} icon={icon} onOpenClick={handleOpenClick} /> | ||
| {icons.map((icon, i) => ( | ||
| <Icon | ||
| key={icon.importName} | ||
| icon={icon} | ||
| onOpenClick={handleOpenClick} | ||
| // Render the first 50 icons immediately as they would be visible on page load | ||
| initiallyVisible={i < 50} | ||
| /> | ||
| ))} | ||
| </div> | ||
| ); | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.