Skip to content

Commit 868104b

Browse files
committed
Support configuring the side/align of the plugin
1 parent 4a41dac commit 868104b

File tree

5 files changed

+39
-19
lines changed

5 files changed

+39
-19
lines changed

.changeset/pretty-crabs-roll.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@mdxeditor/floating-selection-ui-plugin": minor
3+
---
4+
5+
Add support for configuring the side and the align of the popup

packages/floating-selection-ui-plugin/src/FloatingSelectionUI.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { editorRootElementRef$, useCellValue } from "@mdxeditor/editor";
1616
export function FloatingSelectionUI({
1717
component: Component,
1818
shouldShow,
19+
side = "top",
20+
align = "center",
1921
}: FloatingSelectionUIPluginParams) {
2022
const [editor] = useLexicalComposerContext();
2123
const editorRootElementRef = useCellValue(editorRootElementRef$);
@@ -139,13 +141,13 @@ export function FloatingSelectionUI({
139141
width: `${String(selectionRect.width)}px`,
140142
height: `${String(selectionRect.height)}px`,
141143
pointerEvents: "none",
142-
border: "1px solid red",
143144
}}
144145
/>
145146

146147
<Popover.Portal container={editorRootElementRef?.current}>
147148
<Popover.Content
148-
side="top"
149+
side={side}
150+
align={align}
149151
sideOffset={5}
150152
onOpenAutoFocus={(e) => {
151153
e.preventDefault();

packages/floating-selection-ui-plugin/src/examples/basic.stories.tsx

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,22 @@ export const GetSelectionAsMarkdown: Story = () => {
2222
const mdxEditorRef = React.useRef<MDXEditorMethods>(null);
2323
return (
2424
<div className="App" style={{ padding: "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>
3625
<div
37-
style={{
38-
marginTop: "2rem",
39-
position: "absolute",
40-
top: "100px",
41-
left: "100px",
42-
}}
26+
style={{ fontSize: "0.875rem", marginBottom: "1rem", color: "#444" }}
4327
>
28+
<h1>Floating Selection UI Plugin Demo</h1>
29+
<p>
30+
Select text in the editor below to see the floating UI appear above
31+
your selection.
32+
</p>
33+
<p>The button will log the selected text to the console.</p>
34+
35+
<p>
36+
The absoltue position is to ensure the selection works as expected in
37+
more complicated cases.
38+
</p>
39+
</div>
40+
<div>
4441
<MDXEditor
4542
readOnly
4643
ref={mdxEditorRef}
@@ -79,6 +76,8 @@ Open your browser console to see the logged selections.`}
7976
log current selection as markdown
8077
</button>
8178
),
79+
align: "start",
80+
side: "top",
8281
// Only show when more than 3 characters are selected
8382
shouldShow: (editor) => {
8483
const text = editor.getEditorState().read(() => {

packages/floating-selection-ui-plugin/src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export const floatingSelectionUIPlugin =
5858
<FloatingSelectionUI
5959
component={params.component}
6060
shouldShow={params.shouldShow}
61+
side={params.side}
62+
align={params.align}
6163
/>
6264
),
6365
});

packages/floating-selection-ui-plugin/src/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ export interface FloatingSelectionUIPluginParams {
1313
* @returns true to show UI, false to hide
1414
*/
1515
shouldShow?: (editor: LexicalEditor) => boolean;
16+
17+
/**
18+
* The side of the selection where the popover should appear
19+
* @default 'top'
20+
*/
21+
side?: "top" | "right" | "bottom" | "left";
22+
23+
/**
24+
* The alignment of the popover relative to the selection
25+
* @default 'center'
26+
*/
27+
align?: "start" | "center" | "end";
1628
}
1729

1830
export interface FloatingUIComponentProps {

0 commit comments

Comments
 (0)