-
-
Notifications
You must be signed in to change notification settings - Fork 514
/
Copy pathMUISuggestionMenu.tsx
186 lines (168 loc) · 5.21 KB
/
MUISuggestionMenu.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import {
DefaultReactSuggestionItem,
elementOverflow,
SuggestionMenuProps,
useBlockNoteEditor,
} from "@blocknote/react";
import {
Chip,
List,
ListItem,
ListItemButton,
ListItemIcon,
ListItemText,
ListSubheader,
Paper,
} from "@mui/material";
import { useEffect, useMemo, useRef } from "react";
import { TextBlockSchema } from "./schema";
// If you want to change the items in a Suggestion Menu, like the Slash Menu,
// you don't need to modify any of the components in this file. Instead, you
// should change the array returned in the getItems` prop of the
// `SuggestionMenuController` in `App.tsx`. The components in this file are only
// responsible for rendering a Suggestion Menu, not setting its content.
// This replaces the generic Mantine `SuggestionMenuItem` component with a
// simplified MUI version:
// https://github.com/TypeCellOS/BlockNote/blob/main/packages/mantine/src/suggestionMenu/SuggestionMenuItem.tsx
function MUISuggestionMenuItem(
props: Omit<SuggestionMenuProps<DefaultReactSuggestionItem>, "items"> & {
item: DefaultReactSuggestionItem & { index: number };
}
) {
const Icon = props.item.icon;
const editor = useBlockNoteEditor<TextBlockSchema>();
// Scrolls to the item if it's detected to overflow the Slash Menu.
const itemRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!itemRef.current || props.item.index !== props.selectedIndex) {
return;
}
const overflow = elementOverflow(itemRef.current, () => {
if (!itemRef.current) {
return;
}
let container: HTMLElement = itemRef.current;
while (
!container.classList.contains("MuiPaper-root") &&
container.parentElement
) {
container = container.parentElement;
}
if (!container.classList.contains("MuiPaper-root")) {
return;
}
return container;
});
if (overflow === "top") {
itemRef.current.scrollIntoView(true);
} else if (overflow === "bottom") {
itemRef.current.scrollIntoView(false);
}
}, [props.item.index, props.selectedIndex]);
return (
<ListItem
ref={itemRef as any}
key={props.item.title}
disablePadding
sx={{
backgroundColor: (theme) => theme.palette.background.paper,
}}>
<ListItemButton
selected={props.item.index === props.selectedIndex}
onClick={() => {
props.onItemClick?.(props.item);
editor.focus();
}}>
<ListItemIcon>{Icon}</ListItemIcon>
<ListItemText
primary={props.item.title}
secondary={props.item.subtext}
/>
{props.item.badge && <Chip label={props.item.badge} size={"small"} />}
</ListItemButton>
</ListItem>
);
}
// This replaces the generic Mantine `EmptySuggestionMenuItem` component with a
// simplified MUI version:
// https://github.com/TypeCellOS/BlockNote/blob/main/packages/mantine/src/suggestionMenu/EmptySuggestionMenuItem.tsx
function MUIEmptySuggestionMenuItem() {
return (
<ListItem disablePadding>
<ListItemButton>
<ListItemText primary={"No items found"} />
</ListItemButton>
</ListItem>
);
}
// This replaces the generic Mantine `SuggestionMenuLabel` component with a
// simplified MUI version:
// https://github.com/TypeCellOS/BlockNote/blob/main/packages/mantine/src/suggestionMenu/SuggestionMenuLabel.tsx
function MUISuggestionMenuLabel(props: { group: string }) {
return (
<ListSubheader
component="div"
id={props.group}
sx={{
backgroundColor: (theme) => theme.palette.background.paper,
}}>
{props.group}
</ListSubheader>
);
}
// This replaces the generic Mantine `SuggestionMenu` component with a
// simplified MUI version:
// https://github.com/TypeCellOS/BlockNote/blob/main/packages/mantine/src/suggestionMenu/SuggestionMenu.tsx
export function MUISuggestionMenu(
props: SuggestionMenuProps<DefaultReactSuggestionItem>
) {
// Sorts items into their groups.
const groups = useMemo(() => {
const groups: Record<
string,
(DefaultReactSuggestionItem & { index: number })[]
> = {};
for (let i = 0; i < props.items.length; i++) {
const item = props.items[i];
const group = item.group || item.title;
if (!groups[group]) {
groups[group] = [];
}
groups[group].push({ ...item, index: i });
}
return groups;
}, [props.items]);
return (
<Paper
sx={{
height: "fit-content",
maxHeight: "inherit",
maxWidth: 360,
overflow: "auto",
}}>
<nav aria-label="suggestion-menu">
{props.items.length > 0 ? (
Object.entries(groups).map(([group, items]) => (
<List
key={group}
component="nav"
aria-labelledby="nested-list-subheader"
subheader={<MUISuggestionMenuLabel group={group} />}>
{items.map((item) => (
<MUISuggestionMenuItem
key={item.index}
item={item}
{...props}
/>
))}
</List>
))
) : (
<List>
<MUIEmptySuggestionMenuItem />
</List>
)}
</nav>
</Paper>
);
}