Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: make toolbar responsive

thecodrr68968b59c8d1
diff --git a/packages/editor/dist/components/menu/menu.js b/packages/editor/dist/components/menu/menu.js
index 7138d7ace..b472a1533 100644
--- a/packages/editor/dist/components/menu/menu.js
+++ b/packages/editor/dist/components/menu/menu.js
@@ -204,26 +204,33 @@ function MenuContainer(props) {
}
export function MenuPresenter(props) {
var className = props.className, options = props.options, items = props.items, isOpen = props.isOpen, onClose = props.onClose, children = props.children, containerProps = __rest(props, ["className", "options", "items", "isOpen", "onClose", "children"]);
- // const { isOpen, closeMenu } = useMenuTrigger();
- // const { items, } = useMenu();
var position = options.position, type = options.type;
var isAutocomplete = type === "autocomplete";
var contentRef = useRef();
- useEffect(function () {
+ var repositionMenu = useCallback(function (position) {
if (!contentRef.current || !position)
return;
var menu = contentRef.current;
var menuPosition = getPosition(menu, position);
menu.style.top = menuPosition.top + "px";
menu.style.left = menuPosition.left + "px";
+ }, []);
+ useEffect(function () {
+ repositionMenu(position);
+ }, [position]);
+ useEffect(function () {
+ function onWindowResize() {
+ repositionMenu(position);
+ }
+ window.addEventListener("resize", onWindowResize);
+ return function () {
+ window.removeEventListener("resize", onWindowResize);
+ };
}, [position]);
return (_jsx(Modal, __assign({ contentRef: function (ref) { return (contentRef.current = ref); }, className: className || "menuContainer", role: "menu", isOpen: isOpen, appElement: document.body, shouldCloseOnEsc: true, shouldReturnFocusAfterClose: true, shouldCloseOnOverlayClick: true, shouldFocusAfterRender: !isAutocomplete, ariaHideApp: !isAutocomplete, preventScroll: !isAutocomplete, onRequestClose: onClose, portalClassName: className || "menuPresenter", onAfterOpen: function (obj) {
if (!obj || !position)
return;
- var menu = obj.contentEl;
- var menuPosition = getPosition(menu, position);
- menu.style.top = menuPosition.top + "px";
- menu.style.left = menuPosition.left + "px";
+ repositionMenu(position);
}, overlayElement: function (props, contentEl) {
return (_jsx(Box, __assign({}, props, { style: __assign(__assign({}, props.style), { position: isAutocomplete ? "initial" : "fixed", zIndex: 1000, backgroundColor: isAutocomplete ? "transparent" : "unset" }) }, { children: contentEl })));
}, contentElement: function (props, children) { return (_jsx(Box, __assign({}, props, { style: {}, sx: {
diff --git a/packages/editor/dist/components/menu/menuitem.js b/packages/editor/dist/components/menu/menuitem.js
index b85598746..87a06f5ec 100644
--- a/packages/editor/dist/components/menu/menuitem.js
+++ b/packages/editor/dist/components/menu/menuitem.js
@@ -36,11 +36,7 @@ function MenuItem(props) {
display: "flex",
alignItems: "center",
justifyContent: "space-between",
- } }, { children: [_jsxs(Flex, { children: [icon && (_jsx(Icon, { path: Icons[icon], color: "text", size: 15, sx: { mr: 2 } })), _jsx(Text, __assign({ as: "span", sx: {
- fontFamily: "body",
- fontSize: "menu",
- color: "text",
- } }, { children: title }))] }), _jsxs(Flex, { children: [isChecked && _jsx(Icon, { path: Icons.check, size: 14 }), (items === null || items === void 0 ? void 0 : items.length) && _jsx(Icon, { path: Icons.chevronRight, size: 14 }), modifier && (_jsx(Text, __assign({ as: "span", sx: {
+ } }, { children: [_jsxs(Flex, { children: [icon && (_jsx(Icon, { path: Icons[icon], color: "text", size: 15, sx: { mr: 2 } })), _jsx(Text, __assign({ as: "span", variant: "body" }, { children: title }))] }), _jsxs(Flex, { children: [isChecked && _jsx(Icon, { path: Icons.check, size: 14 }), (items === null || items === void 0 ? void 0 : items.length) && _jsx(Icon, { path: Icons.chevronRight, size: 14 }), modifier && (_jsx(Text, __assign({ as: "span", sx: {
fontFamily: "body",
fontSize: "menu",
color: "fontTertiary",
diff --git a/packages/editor/dist/components/menu/useMenu.js b/packages/editor/dist/components/menu/useMenu.js
index 761b7bd29..b4db2445e 100644
--- a/packages/editor/dist/components/menu/useMenu.js
+++ b/packages/editor/dist/components/menu/useMenu.js
@@ -14,7 +14,7 @@ export function getPosition(element, options) {
var elementWidth = element.offsetWidth;
var elementHeight = element.offsetHeight;
var windowWidth = parent.clientWidth;
- var windowHeight = parent.clientHeight - 20;
+ var windowHeight = parent.clientHeight;
var position = { top: 0, left: 0 };
if (windowWidth - actualX < elementWidth) {
var xDiff = actualX - x;
@@ -30,10 +30,8 @@ export function getPosition(element, options) {
else if (location === "left")
position.left -= elementWidth;
}
- if (windowHeight - actualY < elementHeight) {
- var yDiff = actualY - y;
+ if (actualY + elementHeight > windowHeight) {
position.top = windowHeight - elementHeight;
- position.top -= yDiff;
}
else {
position.top = y;
@@ -42,7 +40,7 @@ export function getPosition(element, options) {
if (location === "below")
position.top += height;
else if (location === "top")
- position.top -= height;
+ position.top = y - elementHeight;
}
if (target !== "mouse" && align === "center" && elementWidth > 0) {
position.left -= elementWidth / 2 - target.clientWidth / 2;
@@ -60,7 +58,7 @@ export function getPosition(element, options) {
}
position.top = position.top < 0 ? 0 : position.top;
position.left = position.left < 0 ? 0 : position.left;
- position.top += yOffset;
+ position.top += location === "below" ? yOffset : -yOffset;
position.left += xOffset;
return position;
}
diff --git a/packages/editor/dist/toolbar/components/dropdown.js b/packages/editor/dist/toolbar/components/dropdown.js
index 0041fee59..9540437d7 100644
--- a/packages/editor/dist/toolbar/components/dropdown.js
+++ b/packages/editor/dist/toolbar/components/dropdown.js
@@ -31,10 +31,12 @@ import { Button, Text } from "rebass";
import { Icon } from "./icon";
import { Icons } from "../icons";
import { MenuPresenter } from "../../components/menu/menu";
+import { useToolbarContext } from "../hooks/useToolbarContext";
export function Dropdown(props) {
var items = props.items, selectedItem = props.selectedItem, buttonRef = props.buttonRef, menuWidth = props.menuWidth;
var internalRef = useRef();
var _a = __read(useState(false), 2), isOpen = _a[0], setIsOpen = _a[1];
+ var toolbarLocation = useToolbarContext().toolbarLocation;
return (_jsxs(_Fragment, { children: [_jsxs(Button, __assign({ ref: function (ref) {
internalRef.current = ref;
if (buttonRef)
@@ -50,12 +52,12 @@ export function Dropdown(props) {
":last-of-type": {
mr: 0,
},
- }, onClick: function () { return setIsOpen(function (s) { return !s; }); } }, { children: [typeof selectedItem === "string" ? (_jsx(Text, __assign({ sx: { fontSize: 12, mr: 1, color: "text" } }, { children: selectedItem }))) : (selectedItem), _jsx(Icon, { path: Icons.chevronDown, size: 14, color: "text" })] })), _jsx(MenuPresenter, { options: {
+ }, onClick: function () { return setIsOpen(function (s) { return !s; }); }, onMouseDown: function (e) { return e.preventDefault(); } }, { children: [typeof selectedItem === "string" ? (_jsx(Text, __assign({ sx: { fontSize: 12, mr: 1, color: "text" } }, { children: selectedItem }))) : (selectedItem), _jsx(Icon, { path: toolbarLocation === "bottom" ? Icons.chevronUp : Icons.chevronDown, size: 14, color: "text" })] })), _jsx(MenuPresenter, { options: {
type: "menu",
position: {
target: internalRef.current || undefined,
isTargetAbsolute: true,
- location: "below",
+ location: toolbarLocation === "bottom" ? "top" : "below",
yOffset: 5,
},
}, isOpen: isOpen, items: items, onClose: function () { return setIsOpen(false); }, sx: { minWidth: menuWidth } })] }));
diff --git a/packages/editor/dist/toolbar/components/splitbutton.js b/packages/editor/dist/toolbar/components/splitbutton.js
index cb9ed8fcb..f5e204173 100644
--- a/packages/editor/dist/toolbar/components/splitbutton.js
+++ b/packages/editor/dist/toolbar/components/splitbutton.js
@@ -43,10 +43,12 @@ import { Icons } from "../icons";
import { Icon } from "./icon";
import { ToolButton } from "./tool-button";
import { MenuPresenter } from "../../components/menu/menu";
+import { useToolbarContext } from "../hooks/useToolbarContext";
export function SplitButton(props) {
var menuPresenterProps = props.menuPresenterProps, children = props.children, toolButtonProps = __rest(props, ["menuPresenterProps", "children"]);
var _a = __read(useState(false), 2), isOpen = _a[0], setIsOpen = _a[1];
var ref = useRef(null);
+ var toolbarLocation = useToolbarContext().toolbarLocation;
return (_jsxs(_Fragment, { children: [_jsxs(Flex, __assign({ ref: ref, sx: {
borderRadius: "default",
bg: isOpen ? "hover" : "transparent",
@@ -59,13 +61,14 @@ export function SplitButton(props) {
":last-of-type": {
mr: 0,
},
- }, onClick: function () { return setIsOpen(function (s) { return !s; }); } }, { children: _jsx(Icon, { path: Icons.chevronDown, color: "text", size: 14 }) }))] })), _jsx(MenuPresenter, __assign({ isOpen: isOpen, onClose: function () { return setIsOpen(false); }, options: {
+ }, onClick: function () { return setIsOpen(function (s) { return !s; }); }, onMouseDown: function (e) { return e.preventDefault(); } }, { children: _jsx(Icon, { path: toolbarLocation === "bottom" ? Icons.chevronUp : Icons.chevronDown, color: "text", size: 14 }) }))] })), _jsx(MenuPresenter, __assign({ isOpen: isOpen, onClose: function () { return setIsOpen(false); }, options: {
type: "menu",
position: {
target: ref.current || undefined,
isTargetAbsolute: true,
- location: "below",
+ location: toolbarLocation === "bottom" ? "top" : "below",
yOffset: 5,
+ align: "center",
},
}, items: [] }, menuPresenterProps, { children: children }))] }));
}
diff --git a/packages/editor/dist/toolbar/hooks/useToolbarContext.d.ts b/packages/editor/dist/toolbar/hooks/useToolbarContext.d.ts
new file mode 100644
index 000000000..1900e527e
--- /dev/null
+++ b/packages/editor/dist/toolbar/hooks/useToolbarContext.d.ts
@@ -0,0 +1,12 @@
+import React from "react";
+export declare type ToolbarLocation = "top" | "bottom";
+export declare const ToolbarContext: React.Context<{
+ currentPopup?: string | undefined;
+ setCurrentPopup?: React.Dispatch<React.SetStateAction<string | undefined>> | undefined;
+ toolbarLocation?: ToolbarLocation | undefined;
+}>;
+export declare function useToolbarContext(): {
+ currentPopup?: string | undefined;
+ setCurrentPopup?: React.Dispatch<React.SetStateAction<string | undefined>> | undefined;
+ toolbarLocation?: ToolbarLocation | undefined;
+};
diff --git a/packages/editor/dist/toolbar/hooks/useToolbarContext.js b/packages/editor/dist/toolbar/hooks/useToolbarContext.js
new file mode 100644
index 000000000..e94a87fa6
--- /dev/null
+++ b/packages/editor/dist/toolbar/hooks/useToolbarContext.js
@@ -0,0 +1,5 @@
+import React, { useContext } from "react";
+export var ToolbarContext = React.createContext({});
+export function useToolbarContext() {
+ return useContext(ToolbarContext);
+}
diff --git a/packages/editor/dist/toolbar/toolbar.d.ts b/packages/editor/dist/toolbar/toolbar.d.ts
index 50fc87fa2..507c2b74e 100644
--- a/packages/editor/dist/toolbar/toolbar.d.ts
+++ b/packages/editor/dist/toolbar/toolbar.d.ts
@@ -1,8 +1,10 @@
/// <reference types="react" />
import { ThemeConfig } from "@notesnook/theme/dist/theme/types";
import { Editor } from "@tiptap/core";
+import { ToolbarLocation } from "./hooks/useToolbarContext";
declare type ToolbarProps = ThemeConfig & {
editor: Editor | null;
+ location: ToolbarLocation;
};
export declare function Toolbar(props: ToolbarProps): JSX.Element | null;
export {};
diff --git a/packages/editor/dist/toolbar/toolbar.js b/packages/editor/dist/toolbar/toolbar.js
index 98c352ced..7e5e0f045 100644
--- a/packages/editor/dist/toolbar/toolbar.js
+++ b/packages/editor/dist/toolbar/toolbar.js
@@ -44,13 +44,12 @@ import { ThemeProvider } from "emotion-theming";
import { EditorFloatingMenus } from "./floating-menus";
import { getToolDefinition } from "./tool-definitions";
import { ToolButton } from "./components/tool-button";
-import { useContext, useRef, useState } from "react";
+import { useRef, useState } from "react";
import { MenuPresenter } from "../components/menu";
import { Popup } from "./components/popup";
-import React from "react";
-var ToolbarContext = React.createContext({});
+import { ToolbarContext, useToolbarContext, } from "./hooks/useToolbarContext";
export function Toolbar(props) {
- var editor = props.editor, theme = props.theme, accent = props.accent, scale = props.scale;
+ var editor = props.editor, theme = props.theme, accent = props.accent, scale = props.scale, location = props.location;
var themeProperties = useTheme({ accent: accent, theme: theme, scale: scale });
var _a = __read(useState(), 2), currentPopup = _a[0], setCurrentPopup = _a[1];
var tools = [
@@ -68,7 +67,7 @@ export function Toolbar(props) {
"textColor",
],
],
- ["fontSize", "headings", ["fontFamily"]],
+ ["fontSize", "headings", "fontFamily"],
["numberedList", "bulletList"],
["link"],
["alignCenter", ["alignLeft", "alignRight", "alignJustify", "ltr", "rtl"]],
@@ -76,8 +75,9 @@ export function Toolbar(props) {
];
if (!editor)
return null;
- return (_jsxs(ThemeProvider, __assign({ theme: themeProperties }, { children: [_jsx(ToolbarContext.Provider, __assign({ value: { setCurrentPopup: setCurrentPopup, currentPopup: currentPopup } }, { children: _jsx(Flex, __assign({ className: "editor-toolbar", sx: { flexWrap: "wrap" } }, { children: tools.map(function (tools) {
+ return (_jsxs(ThemeProvider, __assign({ theme: themeProperties }, { children: [_jsx(ToolbarContext.Provider, __assign({ value: { setCurrentPopup: setCurrentPopup, currentPopup: currentPopup, toolbarLocation: location } }, { children: _jsx(Flex, __assign({ className: "editor-toolbar", sx: { flexWrap: ["nowrap", "wrap"], overflowX: ["auto", "hidden"] } }, { children: tools.map(function (tools) {
return (_jsx(ToolbarGroup, { tools: tools, editor: editor, sx: {
+ flexShrink: 0,
pr: 2,
mr: 2,
borderRight: "1px solid var(--border)",
@@ -100,19 +100,19 @@ function ToolbarGroup(props) {
}
function MoreTools(props) {
var popupId = props.popupId;
- var _a = useContext(ToolbarContext), currentPopup = _a.currentPopup, setCurrentPopup = _a.setCurrentPopup;
+ var _a = useToolbarContext(), currentPopup = _a.currentPopup, setCurrentPopup = _a.setCurrentPopup, toolbarLocation = _a.toolbarLocation;
var buttonRef = useRef();
var show = popupId === currentPopup;
var setShow = function (state) {
return setCurrentPopup === null || setCurrentPopup === void 0 ? void 0 : setCurrentPopup(state ? popupId : undefined);
};
- return (_jsxs(_Fragment, { children: [_jsx(ToolButton, { icon: "more", title: "More", toggled: show, buttonRef: buttonRef, onClick: function () { return setShow(!show); } }), _jsx(MenuPresenter, __assign({ isOpen: show, onClose: function () { return setShow(false); }, items: [], options: {
+ return (_jsxs(_Fragment, { children: [_jsx(ToolButton, { icon: "more", title: "More", toggled: show, buttonRef: buttonRef, onMouseDown: function (e) { return e.preventDefault(); }, onClick: function () { return setShow(!show); } }), _jsx(MenuPresenter, __assign({ isOpen: show, onClose: function () { return setShow(false); }, items: [], options: {
type: "autocomplete",
position: {
isTargetAbsolute: true,
target: buttonRef.current || "mouse",
align: "center",
- location: "below",
+ location: toolbarLocation === "bottom" ? "top" : "below",
yOffset: 5,
},
} }, { children: _jsx(Popup, { children: _jsx(ToolbarGroup, { tools: props.tools, editor: props.editor, sx: {
diff --git a/packages/editor/dist/toolbar/tools/block.js b/packages/editor/dist/toolbar/tools/block.js
index 58fd1543f..79bc1a664 100644
--- a/packages/editor/dist/toolbar/tools/block.js
+++ b/packages/editor/dist/toolbar/tools/block.js
@@ -32,9 +32,11 @@ import { useRef, useState } from "react";
import { Icon } from "../components/icon";
import { Button } from "rebass";
import { TablePopup } from "../popups/table-popup";
+import { useToolbarContext } from "../hooks/useToolbarContext";
export function InsertBlock(props) {
var buttonRef = useRef();
var _a = __read(useState(false), 2), isOpen = _a[0], setIsOpen = _a[1];
+ var toolbarLocation = useToolbarContext().toolbarLocation;
return (_jsxs(_Fragment, { children: [_jsx(Button, __assign({ ref: buttonRef, sx: {
p: 1,
m: 0,
@@ -46,12 +48,12 @@ export function InsertBlock(props) {
":last-of-type": {
mr: 0,
},
- }, onClick: function () { return setIsOpen(function (s) { return !s; }); } }, { children: _jsx(Icon, { path: Icons.plus, size: 18, color: "primary" }) })), _jsx(MenuPresenter, { options: {
+ }, onMouseDown: function (e) { return e.preventDefault(); }, onClick: function () { return setIsOpen(function (s) { return !s; }); } }, { children: _jsx(Icon, { path: Icons.plus, size: 18, color: "primary" }) })), _jsx(MenuPresenter, { options: {
type: "menu",
position: {
target: buttonRef.current || undefined,
isTargetAbsolute: true,
- location: "below",
+ location: toolbarLocation === "bottom" ? "top" : "below",
yOffset: 5,
},
}, isOpen: isOpen, items: [
diff --git a/packages/editor/src/components/menu/menu.tsx b/packages/editor/src/components/menu/menu.tsx
index 47810d28a..dad833bc6 100644
--- a/packages/editor/src/components/menu/menu.tsx
+++ b/packages/editor/src/components/menu/menu.tsx
@@ -275,18 +275,30 @@ export function MenuPresenter(props: PropsWithChildren<MenuPresenterProps>) {
children,
...containerProps
} = props;
- // const { isOpen, closeMenu } = useMenuTrigger();
- // const { items, } = useMenu();
const { position, type } = options;
const isAutocomplete = type === "autocomplete";
const contentRef = useRef<HTMLDivElement>();
- useEffect(() => {
+ const repositionMenu = useCallback((position: MenuOptions["position"]) => {
if (!contentRef.current || !position) return;
const menu = contentRef.current;
const menuPosition = getPosition(menu, position);
menu.style.top = menuPosition.top + "px";
menu.style.left = menuPosition.left + "px";
+ }, []);
+
+ useEffect(() => {
+ repositionMenu(position);
+ }, [position]);
+
+ useEffect(() => {
+ function onWindowResize() {
+ repositionMenu(position);
+ }
+ window.addEventListener("resize", onWindowResize);
+ return () => {
+ window.removeEventListener("resize", onWindowResize);
+ };
}, [position]);
return (
@@ -306,10 +318,7 @@ export function MenuPresenter(props: PropsWithChildren<MenuPresenterProps>) {
portalClassName={className || "menuPresenter"}
onAfterOpen={(obj) => {
if (!obj || !position) return;
- const { contentEl: menu } = obj;
- const menuPosition = getPosition(menu, position);
- menu.style.top = menuPosition.top + "px";
- menu.style.left = menuPosition.left + "px";
+ repositionMenu(position);
}}
overlayElement={(props, contentEl) => {
return (
diff --git a/packages/editor/src/components/menu/menuitem.tsx b/packages/editor/src/components/menu/menuitem.tsx
index f6c54963b..f2c5ee8b0 100644
--- a/packages/editor/src/components/menu/menuitem.tsx
+++ b/packages/editor/src/components/menu/menuitem.tsx
@@ -83,14 +83,7 @@ function MenuItem(props: MenuItemProps) {
sx={{ mr: 2 }}
/>
)}
- <Text
- as="span"
- sx={{
- fontFamily: "body",
- fontSize: "menu",
- color: "text",
- }}
- >
+ <Text as="span" variant={"body"}>
{title}
</Text>
</Flex>
diff --git a/packages/editor/src/components/menu/useMenu.ts b/packages/editor/src/components/menu/useMenu.ts
index 0cc10ec8b..a1db444d8 100644
--- a/packages/editor/src/components/menu/useMenu.ts
+++ b/packages/editor/src/components/menu/useMenu.ts
@@ -127,7 +127,7 @@ export function getPosition(
const elementHeight = element.offsetHeight;
const windowWidth = parent.clientWidth;
- const windowHeight = parent.clientHeight - 20;
+ const windowHeight = parent.clientHeight;
let position = { top: 0, left: 0 };
@@ -144,17 +144,15 @@ export function getPosition(
else if (location === "left") position.left -= elementWidth;
}
- if (windowHeight - actualY < elementHeight) {
- const yDiff = actualY - y;
+ if (actualY + elementHeight > windowHeight) {
position.top = windowHeight - elementHeight;
- position.top -= yDiff;
} else {
position.top = y;
}
if (height) {
if (location === "below") position.top += height;
- else if (location === "top") position.top -= height;
+ else if (location === "top") position.top = y - elementHeight;
}
if (target !== "mouse" && align === "center" && elementWidth > 0) {
@@ -175,7 +173,7 @@ export function getPosition(
position.top = position.top < 0 ? 0 : position.top;
position.left = position.left < 0 ? 0 : position.left;
- position.top += yOffset;
+ position.top += location === "below" ? yOffset : -yOffset;
position.left += xOffset;
return position;
@@ -224,6 +222,7 @@ export function getElementPosition(
position.x = position.actualX;
position.y = position.actualY;
}
+
return position;
}
diff --git a/packages/editor/src/toolbar/components/dropdown.tsx b/packages/editor/src/toolbar/components/dropdown.tsx
index 85500dd74..0dc728cf4 100644
--- a/packages/editor/src/toolbar/components/dropdown.tsx
+++ b/packages/editor/src/toolbar/components/dropdown.tsx
@@ -4,6 +4,7 @@ import { Icon } from "./icon";
import { Icons } from "../icons";
import { MenuPresenter, MenuPresenterProps } from "../../components/menu/menu";
import { MenuItem } from "../../components/menu/types";
+import { useToolbarContext } from "../hooks/useToolbarContext";
type DropdownProps = {
selectedItem: string | JSX.Element;
@@ -15,6 +16,7 @@ export function Dropdown(props: DropdownProps) {
const { items, selectedItem, buttonRef, menuWidth } = props;
const internalRef = useRef<any>();
const [isOpen, setIsOpen] = useState(false);
+ const { toolbarLocation } = useToolbarContext();
return (
<>
@@ -36,6 +38,7 @@ export function Dropdown(props: DropdownProps) {
},
}}
onClick={() => setIsOpen((s) => !s)}
+ onMouseDown={(e) => e.preventDefault()}
>
{typeof selectedItem === "string" ? (
<Text sx={{ fontSize: 12, mr: 1, color: "text" }}>
@@ -44,7 +47,13 @@ export function Dropdown(props: DropdownProps) {
) : (
selectedItem
)}
- <Icon path={Icons.chevronDown} size={14} color={"text"} />
+ <Icon
+ path={
+ toolbarLocation === "bottom" ? Icons.chevronUp : Icons.chevronDown
+ }
+ size={14}
+ color={"text"}
+ />
</Button>
<MenuPresenter
options={{
@@ -52,7 +61,7 @@ export function Dropdown(props: DropdownProps) {
position: {
target: internalRef.current || undefined,
isTargetAbsolute: true,
- location: "below",
+ location: toolbarLocation === "bottom" ? "top" : "below",
yOffset: 5,
},
}}
diff --git a/packages/editor/src/toolbar/components/split-button.tsx b/packages/editor/src/toolbar/components/split-button.tsx
index 1863e236f..d2155592c 100644
--- a/packages/editor/src/toolbar/components/split-button.tsx
+++ b/packages/editor/src/toolbar/components/split-button.tsx
@@ -5,6 +5,7 @@ import { IconNames, Icons } from "../icons";
import { Icon } from "./icon";
import { ToolButton, ToolButtonProps } from "./tool-button";
import { MenuPresenter, MenuPresenterProps } from "../../components/menu/menu";
+import { useToolbarContext } from "../hooks/useToolbarContext";
type SplitButtonProps = ToolButtonProps & {
menuPresenterProps?: Partial<MenuPresenterProps>;
@@ -14,6 +15,7 @@ export function SplitButton(props: PropsWithChildren<SplitButtonProps>) {
const [isOpen, setIsOpen] = useState(false);
const ref = useRef<HTMLDivElement>(null);
+ const { toolbarLocation } = useToolbarContext();
return (
<>
@@ -37,8 +39,15 @@ export function SplitButton(props: PropsWithChildren<SplitButtonProps>) {
},
}}
onClick={() => setIsOpen((s) => !s)}
+ onMouseDown={(e) => e.preventDefault()}
>
- <Icon path={Icons.chevronDown} color="text" size={14} />
+ <Icon
+ path={
+ toolbarLocation === "bottom" ? Icons.chevronUp : Icons.chevronDown
+ }
+ color="text"
+ size={14}
+ />
</Button>
</Flex>
<MenuPresenter
@@ -49,8 +58,9 @@ export function SplitButton(props: PropsWithChildren<SplitButtonProps>) {
position: {
target: ref.current || undefined,
isTargetAbsolute: true,
- location: "below",
+ location: toolbarLocation === "bottom" ? "top" : "below",
yOffset: 5,
+ align: "center",
},
}}
items={[]}
diff --git a/packages/editor/src/toolbar/hooks/useToolbarContext.ts b/packages/editor/src/toolbar/hooks/useToolbarContext.ts
new file mode 100644
index 000000000..68b7642e8
--- /dev/null
+++ b/packages/editor/src/toolbar/hooks/useToolbarContext.ts
@@ -0,0 +1,13 @@
+import React, { useContext } from "react";
+
+export type ToolbarLocation = "top" | "bottom";
+
+export const ToolbarContext = React.createContext<{
+ currentPopup?: string;
+ setCurrentPopup?: React.Dispatch<React.SetStateAction<string | undefined>>;
+ toolbarLocation?: ToolbarLocation;
+}>({});
+
+export function useToolbarContext() {
+ return useContext(ToolbarContext);
+}
diff --git a/packages/editor/src/toolbar/toolbar.tsx b/packages/editor/src/toolbar/toolbar.tsx
index c65ed08c9..a211e500d 100644
--- a/packages/editor/src/toolbar/toolbar.tsx
+++ b/packages/editor/src/toolbar/toolbar.tsx
@@ -11,13 +11,11 @@ import { ToolButton } from "./components/tool-button";
import { useContext, useRef, useState } from "react";
import { MenuPresenter } from "../components/menu";
import { Popup } from "./components/popup";
-import React from "react";
-
-const ToolbarContext = React.createContext<{
- currentPopup?: string;
- setCurrentPopup?: React.Dispatch<React.SetStateAction<string | undefined>>;
-}>({});
-
+import {
+ ToolbarContext,
+ ToolbarLocation,
+ useToolbarContext,
+} from "./hooks/useToolbarContext";
// type Colors = {
// text: string;
// background: string;
@@ -32,11 +30,10 @@ type ToolbarDefinition = ToolbarGroupDefinition[];
type ToolbarProps = ThemeConfig & {
editor: Editor | null;
- // tools: ToolbarDefinition;
- // colors: Colors;
+ location: ToolbarLocation;
};
export function Toolbar(props: ToolbarProps) {
- const { editor, theme, accent, scale } = props;
+ const { editor, theme, accent, scale, location } = props;
const themeProperties = useTheme({ accent, theme, scale });
const [currentPopup, setCurrentPopup] = useState<string | undefined>();
@@ -55,7 +52,7 @@ export function Toolbar(props: ToolbarProps) {
"textColor",
],
],
- ["fontSize", "headings", ["fontFamily"]],
+ ["fontSize", "headings", "fontFamily"],
["numberedList", "bulletList"],
["link"],
["alignCenter", ["alignLeft", "alignRight", "alignJustify", "ltr", "rtl"]],
@@ -65,14 +62,20 @@ export function Toolbar(props: ToolbarProps) {
if (!editor) return null;
return (
<ThemeProvider theme={themeProperties}>
- <ToolbarContext.Provider value={{ setCurrentPopup, currentPopup }}>
- <Flex className="editor-toolbar" sx={{ flexWrap: "wrap" }}>
+ <ToolbarContext.Provider
+ value={{ setCurrentPopup, currentPopup, toolbarLocation: location }}
+ >
+ <Flex
+ className="editor-toolbar"
+ sx={{ flexWrap: ["nowrap", "wrap"], overflowX: ["auto", "hidden"] }}
+ >
{tools.map((tools) => {
return (
<ToolbarGroup
tools={tools}
editor={editor}
sx={{
+ flexShrink: 0,
pr: 2,
mr: 2,
borderRight: "1px solid var(--border)",
@@ -116,10 +119,15 @@ function ToolbarGroup(props: ToolbarGroupProps) {
);
}
-type MoreToolsProps = { popupId: string; tools: ToolId[]; editor: Editor };
+type MoreToolsProps = {
+ popupId: string;
+ tools: ToolId[];
+ editor: Editor;
+};
function MoreTools(props: MoreToolsProps) {
const { popupId } = props;
- const { currentPopup, setCurrentPopup } = useContext(ToolbarContext);
+ const { currentPopup, setCurrentPopup, toolbarLocation } =
+ useToolbarContext();
const buttonRef = useRef<HTMLButtonElement | null>();
const show = popupId === currentPopup;
@@ -133,6 +141,7 @@ function MoreTools(props: MoreToolsProps) {
title="More"
toggled={show}
buttonRef={buttonRef}
+ onMouseDown={(e) => e.preventDefault()}
onClick={() => setShow(!show)}
/>
<MenuPresenter
@@ -145,7 +154,7 @@ function MoreTools(props: MoreToolsProps) {
isTargetAbsolute: true,
target: buttonRef.current || "mouse",
align: "center",
- location: "below",
+ location: toolbarLocation === "bottom" ? "top" : "below",
yOffset: 5,
},
}}
diff --git a/packages/editor/src/toolbar/tools/block.tsx b/packages/editor/src/toolbar/tools/block.tsx
index 5877ad169..61c67ea87 100644
--- a/packages/editor/src/toolbar/tools/block.tsx
+++ b/packages/editor/src/toolbar/tools/block.tsx
@@ -12,10 +12,13 @@ import { Popup } from "../components/popup";
import { EmbedPopup } from "../popups/embed-popup";
import { TablePopup } from "../popups/table-popup";
import { MenuItem } from "../../components/menu/types";
+import { useToolbarContext } from "../hooks/useToolbarContext";
export function InsertBlock(props: ToolProps) {
const buttonRef = useRef<HTMLButtonElement | null>();
const [isOpen, setIsOpen] = useState(false);
+ const { toolbarLocation } = useToolbarContext();
+
return (
<>
<Button
@@ -32,6 +35,7 @@ export function InsertBlock(props: ToolProps) {
mr: 0,
},
}}
+ onMouseDown={(e) => e.preventDefault()}
onClick={() => setIsOpen((s) => !s)}
>
<Icon path={Icons.plus} size={18} color={"primary"} />
@@ -42,7 +46,7 @@ export function InsertBlock(props: ToolProps) {
position: {
target: buttonRef.current || undefined,
isTargetAbsolute: true,
- location: "below",
+ location: toolbarLocation === "bottom" ? "top" : "below",
yOffset: 5,
},
}}