-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Material][Popover] Add support for virtual element as anchorEl (#37465)
- Loading branch information
1 parent
90f2f79
commit 7aa3fab
Showing
7 changed files
with
193 additions
and
6 deletions.
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