-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[Material][Popover] Add support for virtual element as anchorEl #37465
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
228e079
[Popover] accept virtual element as anchorEl
DiegoAndai e7d7191
[Popover] add virtual element demo to docs
DiegoAndai d5589c3
[Popover] add virtual element test
DiegoAndai c1a25de
Update docs/data/material/components/popover/popover.md
DiegoAndai 25c8838
Update docs/data/material/components/popover/popover.md
DiegoAndai 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
58 changes: 58 additions & 0 deletions
58
docs/data/material/components/popover/VirtualElementPopover.js
This file contains 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as React from 'react'; | ||
import Popover from '@mui/material/Popover'; | ||
import Typography from '@mui/material/Typography'; | ||
import Paper from '@mui/material/Paper'; | ||
|
||
export default function VirtualElementPopover() { | ||
const [open, setOpen] = React.useState(false); | ||
const [anchorEl, setAnchorEl] = React.useState(null); | ||
|
||
const handleClose = () => { | ||
setOpen(false); | ||
}; | ||
|
||
const handleMouseUp = () => { | ||
const selection = window.getSelection(); | ||
|
||
// Skip if selection has a length of 0 | ||
if (!selection || selection.anchorOffset === selection.focusOffset) { | ||
return; | ||
} | ||
|
||
const getBoundingClientRect = () => { | ||
return selection.getRangeAt(0).getBoundingClientRect(); | ||
}; | ||
|
||
setOpen(true); | ||
|
||
setAnchorEl({ getBoundingClientRect, nodeType: 1 }); | ||
}; | ||
|
||
const id = open ? 'virtual-element-popover' : undefined; | ||
|
||
return ( | ||
<div> | ||
<Typography aria-describedby={id} onMouseUp={handleMouseUp}> | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ipsum purus, | ||
bibendum sit amet vulputate eget, porta semper ligula. Donec bibendum | ||
vulputate erat, ac fringilla mi finibus nec. Donec ac dolor sed dolor | ||
porttitor blandit vel vel purus. Fusce vel malesuada ligula. Nam quis | ||
vehicula ante, eu finibus est. Proin ullamcorper fermentum orci, quis finibus | ||
massa. Nunc lobortis, massa ut rutrum ultrices, metus metus finibus ex, sit | ||
amet facilisis neque enim sed neque. Quisque accumsan metus vel maximus | ||
consequat. Suspendisse lacinia tellus a libero volutpat maximus. | ||
</Typography> | ||
<Popover | ||
id={id} | ||
open={open} | ||
anchorEl={anchorEl} | ||
anchorOrigin={{ vertical: 'bottom', horizontal: 'left' }} | ||
onClose={handleClose} | ||
> | ||
<Paper> | ||
<Typography sx={{ p: 2 }}>The content of the Popover.</Typography> | ||
</Paper> | ||
</Popover> | ||
</div> | ||
); | ||
} |
58 changes: 58 additions & 0 deletions
58
docs/data/material/components/popover/VirtualElementPopover.tsx
This file contains 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as React from 'react'; | ||
import Popover, { PopoverProps } from '@mui/material/Popover'; | ||
import Typography from '@mui/material/Typography'; | ||
import Paper from '@mui/material/Paper'; | ||
|
||
export default function VirtualElementPopover() { | ||
const [open, setOpen] = React.useState(false); | ||
const [anchorEl, setAnchorEl] = React.useState<PopoverProps['anchorEl']>(null); | ||
|
||
const handleClose = () => { | ||
setOpen(false); | ||
}; | ||
|
||
const handleMouseUp = () => { | ||
const selection = window.getSelection(); | ||
|
||
// Skip if selection has a length of 0 | ||
if (!selection || selection.anchorOffset === selection.focusOffset) { | ||
return; | ||
} | ||
|
||
const getBoundingClientRect = () => { | ||
return selection.getRangeAt(0).getBoundingClientRect(); | ||
}; | ||
|
||
setOpen(true); | ||
|
||
setAnchorEl({ getBoundingClientRect, nodeType: 1 }); | ||
}; | ||
|
||
const id = open ? 'virtual-element-popover' : undefined; | ||
|
||
return ( | ||
<div> | ||
<Typography aria-describedby={id} onMouseUp={handleMouseUp}> | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ipsum purus, | ||
bibendum sit amet vulputate eget, porta semper ligula. Donec bibendum | ||
vulputate erat, ac fringilla mi finibus nec. Donec ac dolor sed dolor | ||
porttitor blandit vel vel purus. Fusce vel malesuada ligula. Nam quis | ||
vehicula ante, eu finibus est. Proin ullamcorper fermentum orci, quis finibus | ||
massa. Nunc lobortis, massa ut rutrum ultrices, metus metus finibus ex, sit | ||
amet facilisis neque enim sed neque. Quisque accumsan metus vel maximus | ||
consequat. Suspendisse lacinia tellus a libero volutpat maximus. | ||
</Typography> | ||
<Popover | ||
id={id} | ||
open={open} | ||
anchorEl={anchorEl} | ||
anchorOrigin={{ vertical: 'bottom', horizontal: 'left' }} | ||
onClose={handleClose} | ||
> | ||
<Paper> | ||
<Typography sx={{ p: 2 }}>The content of the Popover.</Typography> | ||
</Paper> | ||
</Popover> | ||
</div> | ||
); | ||
} |
This file contains 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 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 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 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 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
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.
Can you explain this conditional:
selection.anchorOffset === selection.focusOffset
?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 checks that the selection doesn't start and ends at the same character. I tested that you could get that by creating the range programmatically, I don't know if you can get it just by interacting with it. In other words is to make sure that the selection spans at least one character.