File tree Expand file tree Collapse file tree 4 files changed +44
-17
lines changed
packages/floating-selection-ui-plugin/src Expand file tree Collapse file tree 4 files changed +44
-17
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " @mdxeditor/floating-selection-ui-plugin " : patch
3+ ---
4+
5+ Floating selection UI works with absolutely positioned editor. Fixes #19 .
Original file line number Diff line number Diff line change 7878 title : " chore: version packages"
7979 env :
8080 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
81- # NPM_TOKEN: Required for initial publish, optional after OIDC is configured
82- NPM_TOKEN : ${{ secrets.NPM_TOKEN }}
8381
8482 - name : Auto-merge non-major version PR
8583 if : steps.changesets.outputs.hasChangesets == 'true' && steps.changesets.outputs.pullRequestNumber != '' && steps.major.outputs.found == 'false'
Original file line number Diff line number Diff line change @@ -11,12 +11,14 @@ import type {
1111 SelectionRectangle ,
1212} from "./types" ;
1313import { getSelectionRectangle } from "./utils/lexicalHelpers" ;
14+ import { editorRootElementRef$ , useCellValue } from "@mdxeditor/editor" ;
1415
1516export function FloatingSelectionUI ( {
1617 component : Component ,
1718 shouldShow,
1819} : FloatingSelectionUIPluginParams ) {
1920 const [ editor ] = useLexicalComposerContext ( ) ;
21+ const editorRootElementRef = useCellValue ( editorRootElementRef$ ) ;
2022 const [ selectionRect , setSelectionRect ] = useState < SelectionRectangle | null > (
2123 null ,
2224 ) ;
@@ -131,28 +133,32 @@ export function FloatingSelectionUI({
131133 < Popover . Root open = { isVisible } modal = { false } >
132134 < Popover . Anchor
133135 style = { {
134- position : "absolute " ,
136+ position : "fixed " ,
135137 top : `${ String ( selectionRect . top ) } px` ,
136138 left : `${ String ( selectionRect . left ) } px` ,
137139 width : `${ String ( selectionRect . width ) } px` ,
138140 height : `${ String ( selectionRect . height ) } px` ,
139141 pointerEvents : "none" ,
142+ border : "1px solid red" ,
140143 } }
141144 />
142- < Popover . Content
143- side = "top"
144- sideOffset = { 5 }
145- onOpenAutoFocus = { ( e ) => {
146- e . preventDefault ( ) ;
147- } }
148- >
149- < Component
150- editor = { editor }
151- close = { ( ) => {
152- setIsVisible ( false ) ;
145+
146+ < Popover . Portal container = { editorRootElementRef ?. current } >
147+ < Popover . Content
148+ side = "top"
149+ sideOffset = { 5 }
150+ onOpenAutoFocus = { ( e ) => {
151+ e . preventDefault ( ) ;
153152 } }
154- />
155- </ Popover . Content >
153+ >
154+ < Component
155+ editor = { editor }
156+ close = { ( ) => {
157+ setIsVisible ( false ) ;
158+ } }
159+ />
160+ </ Popover . Content >
161+ </ Popover . Portal >
156162 </ Popover . Root >
157163 ) ;
158164}
Original file line number Diff line number Diff line change @@ -22,7 +22,25 @@ export const GetSelectionAsMarkdown: Story = () => {
2222 const mdxEditorRef = React . useRef < MDXEditorMethods > ( null ) ;
2323 return (
2424 < div className = "App" style = { { padding : "2rem" } } >
25- < div style = { { marginTop : "2rem" } } >
25+ < h1 > Floating Selection UI Plugin Demo</ h1 >
26+ < p >
27+ Select text in the editor below to see the floating UI appear above your
28+ selection.
29+ </ p >
30+ < p > The button will log the selected text to the console.</ p >
31+
32+ < p >
33+ The absoltue position is to ensure the selection works as expected in
34+ more complicated cases.
35+ </ p >
36+ < div
37+ style = { {
38+ marginTop : "2rem" ,
39+ position : "absolute" ,
40+ top : "100px" ,
41+ left : "100px" ,
42+ } }
43+ >
2644 < MDXEditor
2745 readOnly
2846 ref = { mdxEditorRef }
You can’t perform that action at this time.
0 commit comments