diff --git a/docs/content/docs/react/components/formatting-toolbar.mdx b/docs/content/docs/react/components/formatting-toolbar.mdx
index 962035ba57..1ef97afac1 100644
--- a/docs/content/docs/react/components/formatting-toolbar.mdx
+++ b/docs/content/docs/react/components/formatting-toolbar.mdx
@@ -38,3 +38,60 @@ The first element in the default Formatting Toolbar is the Block Type Select, an
Here, we use the `FormattingToolbar` component but keep the default buttons (we don't pass any children). Instead, we pass our customized Block Type Select items using the `blockTypeSelectItems` prop.
+
+## Mobile Formatting Toolbar
+
+On touch devices, BlockNote's default UI replaces the floating Formatting Toolbar with a mobile Formatting Toolbar that sits just above the on-screen keyboard. It shows the same items as the regular Formatting Toolbar and is enabled by default - there's nothing to set up. Open any of the examples above on a phone to see it.
+
+The mobile Formatting Toolbar works with two page layouts. Which one you get is decided purely by your app's CSS:
+
+- **Scrolling document** (the default): the page scrolls as usual and BlockNote repositions the toolbar as you scroll.
+- **Scroll container**: the document itself doesn't scroll; a container pinned to the visual viewport scrolls instead, and the toolbar never has to move.
+
+### Scrolling document
+
+This is what you get without any changes to your app. The toolbar follows the visible area above the keyboard as the page scrolls. Mobile browsers only report visual viewport changes after the fact, so the toolbar can lag or jitter slightly while the page is scrolling. If that matters for your app, switch to a scroll container.
+
+### Scroll container
+
+In this layout, `` and `
` are locked and all page content lives inside a single scroll container that BlockNote keeps aligned with the visual viewport. Since the document never scrolls, the toolbar can stay at a truly fixed position and the lag/jitter disappears. Since the document no longer scrolls, this comes with some potential trade-offs. Browser gestures that rely on document scrolling, like pull-to-refresh, may stop working and browser UI elements like the address bar, which normally hides and reappears as you scroll, may stay fixed. Note that these trade-offs are browser-dependent - some will have neither, while others will have both.
+
+To set this up, add the `bn-scroll-host` class to your scroll container:
+
+```tsx
+
{/* nav, editor, page content... */}
+```
+
+
+ Your app should only ever have a single `bn-scroll-host` element. It's pinned
+ to the visual viewport with `position: fixed`, so multiple hosts would overlap
+ each other. Wrap all your scrollable page content in one host.
+
+
+That's all the setup needed. BlockNote injects the following styles for you:
+
+```css
+html:has(.bn-scroll-host),
+body:has(.bn-scroll-host) {
+ overflow: hidden;
+}
+```
+
+This locks scrolling on `` and `` whenever a `bn-scroll-host` element is present. It's then pinned to the visual viewport using the `--bn-vv-*` CSS variables that BlockNote publishes on ``:
+
+```css
+.bn-scroll-host {
+ position: fixed;
+ top: var(--bn-vv-top, 0px);
+ left: var(--bn-vv-left, 0px);
+ width: var(--bn-vv-width, 100vw);
+ height: var(--bn-vv-height, 100dvh);
+ overflow-y: auto;
+ -webkit-overflow-scrolling: touch;
+ overscroll-behavior: contain;
+}
+```
+
+These variables track the [visual viewport](https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport) - the part of the page actually visible above the keyboard. BlockNote keeps `--bn-vv-top`, `--bn-vv-left`, `--bn-vv-width`, and `--bn-vv-height` (plus `--bn-vv-scale`, the pinch-zoom factor) up to date as the keyboard opens and closes and as the user pans or zooms, so the scroll container always lines up with the visible area above the keyboard without any JavaScript on your end.
+
+Because this layout changes how the whole page scrolls, the example can't be embedded here - open the [standalone example](https://playground.blocknotejs.org/ui-components/mobile-formatting-toolbar?hideMenu=true) on a phone instead. It puts a navigation bar, some static text, and the editor inside a container with the `bn-scroll-host` class, and the switch in the navigation bar toggles the pinned scroll container layout on and off so you can compare it with the default scrolling document. Select some text and scroll in each layout to see the difference.
diff --git a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/README.md b/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/README.md
deleted file mode 100644
index 02eaf7673f..0000000000
--- a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/README.md
+++ /dev/null
@@ -1,10 +0,0 @@
-# Experimental Mobile Formatting Toolbar
-
-This example shows how to use the experimental mobile formatting toolbar, which uses [Visual Viewport API](https://developer.mozilla.org/en-US/docs/Web/API/Visual_Viewport_API) to position the toolbar right above the virtual keyboard on mobile devices.
-
-Controller is currently marked **experimental** due to the flickering issue with positioning (caused by delays of the Visual Viewport API)
-
-**Relevant Docs:**
-
-- [Changing the Formatting Toolbar](/docs/react/components/formatting-toolbar)
-- [Editor Setup](/docs/getting-started/editor-setup)
diff --git a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/src/App.tsx b/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/src/App.tsx
deleted file mode 100644
index 47d59e453c..0000000000
--- a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/src/App.tsx
+++ /dev/null
@@ -1,39 +0,0 @@
-import "@blocknote/core/fonts/inter.css";
-import {
- ExperimentalMobileFormattingToolbarController,
- useCreateBlockNote,
-} from "@blocknote/react";
-import { BlockNoteView } from "@blocknote/mantine";
-import "@blocknote/mantine/style.css";
-
-import "./style.css";
-
-export default function App() {
- // Creates a new editor instance.
- const editor = useCreateBlockNote({
- initialContent: [
- {
- type: "paragraph",
- content: "Welcome to this demo!",
- },
- {
- type: "paragraph",
- content:
- "Check out the experimental mobile formatting toolbar by selecting some text (best experienced on a mobile device).",
- },
- ],
- });
-
- // Renders the editor instance using a React component.
- return (
- // Disables the default formatting toolbar and re-adds it without the
- // `FormattingToolbarController` component. You may have seen
- // `FormattingToolbarController` used in other examples, but we omit it here
- // as we want to control the position and visibility ourselves. BlockNote
- // also uses the `FormattingToolbarController` when displaying the
- // Formatting Toolbar by default.
-
-
-
- );
-}
diff --git a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/src/style.css b/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/src/style.css
deleted file mode 100644
index 98e93611cd..0000000000
--- a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/src/style.css
+++ /dev/null
@@ -1,9 +0,0 @@
-.bn-container {
- display: flex;
- flex-direction: column-reverse;
- gap: 8px;
-}
-
-.bn-formatting-toolbar {
- margin-inline: auto;
-}
diff --git a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/.bnexample.json b/examples/03-ui-components/14-mobile-formatting-toolbar/.bnexample.json
similarity index 90%
rename from examples/03-ui-components/14-experimental-mobile-formatting-toolbar/.bnexample.json
rename to examples/03-ui-components/14-mobile-formatting-toolbar/.bnexample.json
index 16f9aea065..2d14483537 100644
--- a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/.bnexample.json
+++ b/examples/03-ui-components/14-mobile-formatting-toolbar/.bnexample.json
@@ -1,6 +1,6 @@
{
"playground": true,
- "docs": true,
+ "docs": false,
"author": "areknawo",
"tags": [
"Intermediate",
diff --git a/examples/03-ui-components/14-mobile-formatting-toolbar/README.md b/examples/03-ui-components/14-mobile-formatting-toolbar/README.md
new file mode 100644
index 0000000000..5b5ef133d7
--- /dev/null
+++ b/examples/03-ui-components/14-mobile-formatting-toolbar/README.md
@@ -0,0 +1,8 @@
+# Mobile Formatting Toolbar
+
+This example demos the opt-in **scroll container** layout: adding the `bn-scroll-host` class to your scroll container locks `html`/`body` scrolling and pins the container to the visual viewport (BlockNote injects the styles), so the toolbar stays perfectly in place while scrolling and zooming. Use the switch in the nav bar to toggle it off and compare it with the default scrolling document layout.
+
+**Relevant Docs:**
+
+- [Mobile Formatting Toolbar](/docs/react/components/formatting-toolbar#mobile-formatting-toolbar)
+- [Editor Setup](/docs/getting-started/editor-setup)
diff --git a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/index.html b/examples/03-ui-components/14-mobile-formatting-toolbar/index.html
similarity index 85%
rename from examples/03-ui-components/14-experimental-mobile-formatting-toolbar/index.html
rename to examples/03-ui-components/14-mobile-formatting-toolbar/index.html
index 69b3583594..edd82eaea0 100644
--- a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/index.html
+++ b/examples/03-ui-components/14-mobile-formatting-toolbar/index.html
@@ -2,7 +2,7 @@
- Experimental Mobile Formatting Toolbar
+ Mobile Formatting Toolbar
diff --git a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/main.tsx b/examples/03-ui-components/14-mobile-formatting-toolbar/main.tsx
similarity index 100%
rename from examples/03-ui-components/14-experimental-mobile-formatting-toolbar/main.tsx
rename to examples/03-ui-components/14-mobile-formatting-toolbar/main.tsx
diff --git a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/package.json b/examples/03-ui-components/14-mobile-formatting-toolbar/package.json
similarity index 89%
rename from examples/03-ui-components/14-experimental-mobile-formatting-toolbar/package.json
rename to examples/03-ui-components/14-mobile-formatting-toolbar/package.json
index c0843c027a..79453826e2 100644
--- a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/package.json
+++ b/examples/03-ui-components/14-mobile-formatting-toolbar/package.json
@@ -1,5 +1,5 @@
{
- "name": "@blocknote/example-ui-components-experimental-mobile-formatting-toolbar",
+ "name": "@blocknote/example-ui-components-mobile-formatting-toolbar",
"description": "AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY",
"type": "module",
"private": true,
diff --git a/examples/03-ui-components/14-mobile-formatting-toolbar/src/App.tsx b/examples/03-ui-components/14-mobile-formatting-toolbar/src/App.tsx
new file mode 100644
index 0000000000..88afbfdb7d
--- /dev/null
+++ b/examples/03-ui-components/14-mobile-formatting-toolbar/src/App.tsx
@@ -0,0 +1,63 @@
+import "@blocknote/core/fonts/inter.css";
+import { useCreateBlockNote } from "@blocknote/react";
+import { BlockNoteView } from "@blocknote/mantine";
+import "@blocknote/mantine/style.css";
+import { useState } from "react";
+
+import "./style.css";
+import { StaticText, NavBar } from "./DummyUI";
+
+// Enough content that the editor actually overflows, so scrolling is testable.
+const initialContent = [
+ { type: "paragraph" as const, content: "Welcome to this demo!" },
+ {
+ type: "paragraph" as const,
+ content:
+ "Select some text to bring up the toolbar, then scroll. With the pinned " +
+ "scroll container layout on, it stays put because the document itself " +
+ "doesn't scroll. Toggle it off in the nav bar to compare.",
+ },
+ ...Array.from({ length: 20 }, (_, i) => ({
+ type: "paragraph" as const,
+ content:
+ `Filler paragraph ${i + 1}. Select some text here and bring up the ` +
+ "keyboard to see the toolbar sit above it.",
+ })),
+];
+
+export default function App() {
+ const editor = useCreateBlockNote({ initialContent });
+ // A second editor, to check the mobile toolbar still works with multiple
+ // editors on a page: the scroll-host styles are injected only once and each
+ // editor tracks the shared visual viewport independently.
+ const secondEditor = useCreateBlockNote({ initialContent });
+
+ // Which element scrolls the page. The "pinned scroll container" layout is
+ // opt-in via a single class: adding `bn-scroll-host` to the scroll container
+ // makes BlockNote's injected styles lock document scroll and pin the container
+ // to the visual viewport. Switching layouts is therefore just adding/removing
+ // the class - a real app would apply it unconditionally, the switch is only
+ // here so you can compare both.
+ const [scrollMode, setScrollMode] = useState<
+ "scrolling-document" | "scroll-container"
+ >("scroll-container");
+
+ return (
+
+
+
+
+ {/* On mobile, the default UI automatically shows the mobile formatting
+ toolbar above the keyboard - no extra setup needed. */}
+
+
+
+
+
+
+ );
+}
+
+export function NavBar(props: {
+ scrollMode: "scrolling-document" | "scroll-container";
+ onScrollModeChange: (
+ scrollContainer: "scrolling-document" | "scroll-container",
+ ) => void;
+}) {
+ return (
+
+
+ Lorem Ipsum
+ {/* Switches between the default "scrolling document" layout and the
+ "pinned scroll container" layout, to compare the toolbar in both. */}
+
+
+ );
+}
+
+/** A block of static page text, to sit around the editor. */
+export function StaticText() {
+ return (
+
+
Lorem Ipsum
+
+ Elit ipsum qui deserunt deserunt. Qui labore eu esse veniam excepteur.
+ Aute ipsum qui dolore in ipsum commodo adipisicing velit. Qui
+ consectetur et cupidatat consectetur sunt anim excepteur reprehenderit
+ sunt quis magna aliqua laborum. Lorem irure est ipsum ea nisi incididunt
+ culpa qui consequat eiusmod deserunt ipsum nostrud velit laboris.
+
+
+ Culpa quis id ipsum enim proident dolore non. Ad occaecat nostrud
+ eiusmod pariatur occaecat nisi voluptate nulla. Nisi quis ut esse ex
+ reprehenderit Lorem tempor ex tempor id sit officia. Commodo sunt sint
+ aliqua quis reprehenderit. Occaecat id ad dolor officia qui sunt dolor.
+ Consectetur magna excepteur in minim pariatur qui elit in sit consequat
+ aliquip voluptate laboris. Reprehenderit et eu dolor ex cupidatat aliqua
+ in elit anim eiusmod et adipisicing. Cupidatat fugiat fugiat amet duis.
+
+
+ Voluptate quis dolor ipsum commodo fugiat sit tempor tempor non aliqua
+ qui. Veniam consectetur mollit consequat exercitation sit ad. Lorem amet
+ deserunt qui sint et. Sint aute cillum aliqua pariatur cillum id.
+ Consectetur proident Lorem qui laborum id in sit. Aute aute irure nisi
+ est veniam Lorem. Anim labore irure ut sit mollit velit et duis veniam
+ ipsum aliquip.
+
+
+ Occaecat dolore excepteur qui proident laborum. Dolor deserunt cillum
+ veniam nulla minim eu in est aute nulla anim incididunt ea. Anim aliquip
+ aute duis aliqua eu pariatur est dolor magna Lorem dolore do sunt
+ aliquip est. Laborum pariatur fugiat do reprehenderit tempor cupidatat
+ proident ipsum ad dolor laboris.
+
+
+ );
+}
diff --git a/examples/03-ui-components/14-mobile-formatting-toolbar/src/style.css b/examples/03-ui-components/14-mobile-formatting-toolbar/src/style.css
new file mode 100644
index 0000000000..5d8a2e4110
--- /dev/null
+++ b/examples/03-ui-components/14-mobile-formatting-toolbar/src/style.css
@@ -0,0 +1,155 @@
+html,
+body {
+ margin: 0;
+}
+
+/* Fixed-height, internally scrollable editor — a nested scroll container inside
+ the page's `.bn-scroll-host`, to check nested scrolling works. */
+.bn-container {
+ height: 300px;
+ border: 1px solid #e0e0e0;
+ border-radius: 8px;
+}
+
+.bn-editor {
+ height: 100%;
+ overflow: auto;
+}
+
+/* --- Dummy app UI (see DummyUI.tsx) --- */
+
+.dummy-top-nav {
+ position: sticky;
+ top: 0;
+ z-index: 20;
+ display: flex;
+ align-items: center;
+ gap: 12px;
+ height: 48px;
+ padding: 0 12px;
+ background: #1a1a1a;
+ color: #fff;
+}
+
+.dummy-top-nav-title {
+ font: 600 15px/1 sans-serif;
+}
+
+/* Switch for the pinned scroll container layout, pushed to the right edge. */
+.dummy-layout-toggle {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ height: 44px;
+ margin-left: auto;
+ padding: 0 4px 0 10px;
+ background: none;
+ border: none;
+ color: inherit;
+ font: 13px/1 sans-serif;
+ cursor: pointer;
+}
+
+.dummy-layout-toggle-track {
+ position: relative;
+ width: 36px;
+ height: 20px;
+ border-radius: 10px;
+ background: #555;
+ transition: background 0.15s;
+}
+
+.dummy-layout-toggle[aria-pressed="true"] .dummy-layout-toggle-track {
+ background: #4caf50;
+}
+
+.dummy-layout-toggle-track::after {
+ content: "";
+ position: absolute;
+ top: 2px;
+ left: 2px;
+ width: 16px;
+ height: 16px;
+ border-radius: 50%;
+ background: #fff;
+ transition: transform 0.15s;
+}
+
+.dummy-layout-toggle[aria-pressed="true"] .dummy-layout-toggle-track::after {
+ transform: translateX(16px);
+}
+
+.dummy-hamburger {
+ position: relative;
+}
+
+.dummy-hamburger-button {
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
+ align-items: center;
+ gap: 5px;
+ width: 44px;
+ height: 44px;
+ margin: -10px;
+ padding: 0;
+ background: none;
+ border: none;
+ cursor: pointer;
+}
+
+.dummy-hamburger-button span {
+ display: block;
+ width: 22px;
+ height: 2px;
+ border-radius: 1px;
+ background: #fff;
+}
+
+.dummy-hamburger-menu {
+ position: absolute;
+ top: calc(100% + 8px);
+ left: 0;
+ display: flex;
+ flex-direction: column;
+ min-width: 180px;
+ padding: 8px;
+ background: #fff;
+ color: #111;
+ border-radius: 8px;
+ box-shadow: 0 6px 20px rgb(0 0 0 / 0.15);
+}
+
+.dummy-hamburger-menu a {
+ display: flex;
+ align-items: center;
+ min-height: 44px;
+ padding: 8px 10px;
+ color: inherit;
+ text-decoration: none;
+ border-radius: 6px;
+}
+
+.dummy-hamburger-menu a:hover {
+ background: #f0f0f0;
+}
+
+.app-main {
+ display: flex;
+ flex-direction: column;
+ gap: 16px;
+ max-width: 720px;
+ margin: 0 auto;
+ padding: 16px;
+}
+
+.dummy-prose h2 {
+ margin: 0 0 8px;
+ font: 600 18px/1.2 sans-serif;
+}
+
+.dummy-prose p {
+ margin: 0 0 8px;
+ font: 14px/1.6 sans-serif;
+ color: #333;
+}
diff --git a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/vite-env.d.ts b/examples/03-ui-components/14-mobile-formatting-toolbar/src/vite-env.d.ts
similarity index 100%
rename from examples/03-ui-components/14-experimental-mobile-formatting-toolbar/vite-env.d.ts
rename to examples/03-ui-components/14-mobile-formatting-toolbar/src/vite-env.d.ts
diff --git a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/tsconfig.json b/examples/03-ui-components/14-mobile-formatting-toolbar/tsconfig.json
similarity index 100%
rename from examples/03-ui-components/14-experimental-mobile-formatting-toolbar/tsconfig.json
rename to examples/03-ui-components/14-mobile-formatting-toolbar/tsconfig.json
diff --git a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/src/vite-env.d.ts b/examples/03-ui-components/14-mobile-formatting-toolbar/vite-env.d.ts
similarity index 100%
rename from examples/03-ui-components/14-experimental-mobile-formatting-toolbar/src/vite-env.d.ts
rename to examples/03-ui-components/14-mobile-formatting-toolbar/vite-env.d.ts
diff --git a/examples/03-ui-components/14-experimental-mobile-formatting-toolbar/vite.config.ts b/examples/03-ui-components/14-mobile-formatting-toolbar/vite.config.ts
similarity index 100%
rename from examples/03-ui-components/14-experimental-mobile-formatting-toolbar/vite.config.ts
rename to examples/03-ui-components/14-mobile-formatting-toolbar/vite.config.ts
diff --git a/packages/ariakit/src/menu/Menu.tsx b/packages/ariakit/src/menu/Menu.tsx
index c2a401204a..177dc37f73 100644
--- a/packages/ariakit/src/menu/Menu.tsx
+++ b/packages/ariakit/src/menu/Menu.tsx
@@ -11,13 +11,20 @@ import {
import { assertEmpty, mergeCSSClasses } from "@blocknote/core";
import { ComponentProps } from "@blocknote/react";
-import { forwardRef } from "react";
+import { createContext, forwardRef, useContext } from "react";
+
+// Threads the `portalRoot` override from `Menu` (the provider) down to
+// `MenuDropdown`, where ariakit's `portalElement` prop actually lives.
+const PortalRootContext = createContext(
+ undefined,
+);
export const Menu = (props: ComponentProps["Generic"]["Menu"]["Root"]) => {
const {
children,
onOpenChange,
position,
+ portalRoot,
sub: _sub, // unused
...rest
} = props;
@@ -30,7 +37,9 @@ export const Menu = (props: ComponentProps["Generic"]["Menu"]["Root"]) => {
setOpen={onOpenChange}
virtualFocus={true}
>
- {children}
+
+ {children}
+
);
};
@@ -48,10 +57,13 @@ export const MenuDropdown = forwardRef<
assertEmpty(rest);
+ const portalRoot = useContext(PortalRootContext);
+
return (
{children}
diff --git a/packages/ariakit/src/popover/Popover.tsx b/packages/ariakit/src/popover/Popover.tsx
index df8e01128b..29e662e5a6 100644
--- a/packages/ariakit/src/popover/Popover.tsx
+++ b/packages/ariakit/src/popover/Popover.tsx
@@ -8,6 +8,8 @@ import { assertEmpty, mergeCSSClasses } from "@blocknote/core";
import { ComponentProps } from "@blocknote/react";
import { createContext, forwardRef, useContext } from "react";
+// Threads the `portalRoot` override from `Popover` (the provider) down to
+// `PopoverContent`, where ariakit's `portalElement` prop actually lives.
const PortalRootContext = createContext(
undefined,
);
diff --git a/packages/ariakit/src/toolbar/ToolbarSelect.tsx b/packages/ariakit/src/toolbar/ToolbarSelect.tsx
index f596cbbae6..26d817976e 100644
--- a/packages/ariakit/src/toolbar/ToolbarSelect.tsx
+++ b/packages/ariakit/src/toolbar/ToolbarSelect.tsx
@@ -16,7 +16,7 @@ export const ToolbarSelect = forwardRef<
HTMLDivElement,
ComponentProps["FormattingToolbar"]["Select"]
>((props, ref) => {
- const { className, items, isDisabled, ...rest } = props;
+ const { className, items, isDisabled, portalRoot, ...rest } = props;
assertEmpty(rest);
@@ -40,6 +40,7 @@ export const ToolbarSelect = forwardRef<
className={mergeCSSClasses("bn-ak-popover", className || "")}
ref={ref}
gutter={4}
+ portalElement={portalRoot ?? undefined}
>
{items.map((option) => (
{
const styles: Styles = {};
- const marks = tr.selection.$to.marks();
+ const marks =
+ (tr.selection.empty && tr.storedMarks) || tr.selection.$to.marks();
for (const mark of marks) {
const config = this.editor.schema.styleSchema[mark.type.name];
diff --git a/packages/core/src/util/browser.ts b/packages/core/src/util/browser.ts
index 118c138a48..d070115c2a 100644
--- a/packages/core/src/util/browser.ts
+++ b/packages/core/src/util/browser.ts
@@ -28,3 +28,24 @@ export function mergeCSSClasses(...classes: (string | false | undefined)[]) {
export const isSafari = () =>
/^((?!chrome|android).)*safari/i.test(navigator.userAgent);
+
+// Cached lazily on first call in a browser environment. Touch capability
+// doesn't change during a session, so there's no need to re-run `matchMedia` on
+// every call. We only cache once `navigator`/`window` are available, so a
+// `false` computed during SSR isn't frozen and carried onto the client.
+let isTouchDeviceCache: boolean | undefined;
+
+export const isTouchDevice = () => {
+ if (typeof navigator === "undefined" || typeof window === "undefined") {
+ return false;
+ }
+
+ if (isTouchDeviceCache === undefined) {
+ isTouchDeviceCache =
+ navigator.maxTouchPoints > 0 &&
+ typeof window.matchMedia === "function" &&
+ window.matchMedia("(pointer: coarse)").matches;
+ }
+
+ return isTouchDeviceCache;
+};
diff --git a/packages/mantine/src/blocknoteStyles.css b/packages/mantine/src/blocknoteStyles.css
index accb33f62a..70c179e26a 100644
--- a/packages/mantine/src/blocknoteStyles.css
+++ b/packages/mantine/src/blocknoteStyles.css
@@ -155,10 +155,6 @@
overflow: auto;
}
-.bn-mantine .mantine-Button-root[aria-controls*="dropdown"] {
- min-width: fit-content;
-}
-
/* Toolbar styling */
.bn-mantine .bn-toolbar {
background-color: var(--bn-colors-menu-background);
@@ -170,7 +166,7 @@
padding: 2px;
width: fit-content;
overflow-x: auto;
- max-width: 100vw;
+ max-width: var(--bn-vv-width, 100vw);
}
.bn-mantine .bn-toolbar:empty {
@@ -183,13 +179,18 @@
border: none;
border-radius: var(--bn-border-radius-small);
color: var(--bn-colors-menu-text);
+ flex-shrink: 0;
}
-.bn-toolbar .mantine-Button-root:hover,
-.bn-toolbar .mantine-ActionIcon-root:hover {
- background-color: var(--bn-colors-hovered-background);
- border: none;
- color: var(--bn-colors-hovered-text);
+/* Hover styles are gated behind `hover: hover` so they don't stick after a tap
+on touch devices (e.g. the mobile formatting toolbar). */
+@media (hover: hover) {
+ .bn-toolbar .mantine-Button-root:hover,
+ .bn-toolbar .mantine-ActionIcon-root:hover {
+ background-color: var(--bn-colors-hovered-background);
+ border: none;
+ color: var(--bn-colors-hovered-text);
+ }
}
.bn-toolbar .mantine-Button-root[data-selected],
@@ -206,6 +207,16 @@
color: var(--bn-colors-disabled-text);
}
+.bn-mobile-formatting-toolbar .bn-toolbar .mantine-Button-root {
+ height: 40px;
+ padding-inline: 12px;
+}
+
+.bn-mobile-formatting-toolbar .bn-toolbar .mantine-ActionIcon-root {
+ width: 40px;
+ height: 40px;
+}
+
.bn-toolbar .mantine-Menu-item {
font-size: 12px;
height: 30px;
diff --git a/packages/mantine/src/menu/Menu.tsx b/packages/mantine/src/menu/Menu.tsx
index c81ed870d7..14e08bbdd1 100644
--- a/packages/mantine/src/menu/Menu.tsx
+++ b/packages/mantine/src/menu/Menu.tsx
@@ -16,16 +16,20 @@ const SubMenuContext = createContext<
>(undefined);
export const Menu = (props: ComponentProps["Generic"]["Menu"]["Root"]) => {
- const { children, onOpenChange, position, sub, ...rest } = props;
+ const { children, onOpenChange, position, portalRoot, sub, ...rest } = props;
assertEmpty(rest);
+ // When explicitly positioned to a `top` placement (e.g. the mobile toolbar's
+ // color menu, opening above the keyboard) don't let `flip` send it back down.
+ const flip = !position?.startsWith("top");
+
if (sub) {
return (
@@ -36,11 +40,14 @@ export const Menu = (props: ComponentProps["Generic"]["Menu"]["Root"]) => {
return (
{children}
diff --git a/packages/mantine/src/popover/Popover.tsx b/packages/mantine/src/popover/Popover.tsx
index a96c1f284f..e965800251 100644
--- a/packages/mantine/src/popover/Popover.tsx
+++ b/packages/mantine/src/popover/Popover.tsx
@@ -20,6 +20,9 @@ export const Popover = (
middlewares={{ size: { padding: 20 } }}
withinPortal={!!portalRoot}
portalProps={portalRoot ? { target: portalRoot } : undefined}
+ // Do not move focus to dropdown when portaled (mobile), as it blurs the
+ // editor's contentEditable and dismisses the on-screen keyboard.
+ trapFocus={portalRoot ? false : undefined}
opened={open}
onChange={onOpenChange}
position={position}
diff --git a/packages/mantine/src/toolbar/ToolbarButton.tsx b/packages/mantine/src/toolbar/ToolbarButton.tsx
index 179b08b03c..678d025c96 100644
--- a/packages/mantine/src/toolbar/ToolbarButton.tsx
+++ b/packages/mantine/src/toolbar/ToolbarButton.tsx
@@ -6,7 +6,7 @@ import {
Tooltip as MantineTooltip,
} from "@mantine/core";
-import { assertEmpty, isSafari } from "@blocknote/core";
+import { assertEmpty, isSafari, isTouchDevice } from "@blocknote/core";
import { ComponentProps } from "@blocknote/react";
import { forwardRef, useState } from "react";
@@ -57,11 +57,17 @@ export const ToolbarButton = forwardRef(
{
+ onPointerDown={(event) => {
+ // Prevents focus shift on mobile.
+ if (isTouchDevice()) {
+ event.preventDefault();
+ return;
+ }
+
+ // Needed as Safari doesn't focus button elements on mouse down
+ // unlike other browsers.
if (isSafari()) {
- (e.currentTarget as HTMLButtonElement).focus();
+ (event.currentTarget as HTMLButtonElement).focus();
}
}}
onClick={(event) => {
@@ -90,11 +96,17 @@ export const ToolbarButton = forwardRef(
{
+ onPointerDown={(event) => {
+ // Prevents focus shift on mobile.
+ if (isTouchDevice()) {
+ event.preventDefault();
+ return;
+ }
+
+ // Needed as Safari doesn't focus button elements on mouse down
+ // unlike other browsers.
if (isSafari()) {
- (e.currentTarget as HTMLButtonElement).focus();
+ (event.currentTarget as HTMLButtonElement).focus();
}
}}
onClick={(event) => {
diff --git a/packages/mantine/src/toolbar/ToolbarSelect.tsx b/packages/mantine/src/toolbar/ToolbarSelect.tsx
index 21cee2a1fd..5c627ef928 100644
--- a/packages/mantine/src/toolbar/ToolbarSelect.tsx
+++ b/packages/mantine/src/toolbar/ToolbarSelect.tsx
@@ -4,7 +4,7 @@ import {
Menu as MantineMenu,
} from "@mantine/core";
-import { assertEmpty, isSafari } from "@blocknote/core";
+import { assertEmpty, isSafari, isTouchDevice } from "@blocknote/core";
import { ComponentProps } from "@blocknote/react";
import { forwardRef } from "react";
import { HiChevronDown } from "react-icons/hi";
@@ -14,7 +14,7 @@ export const ToolbarSelect = forwardRef<
HTMLDivElement,
ComponentProps["FormattingToolbar"]["Select"]
>((props, ref) => {
- const { className, items, isDisabled, ...rest } = props;
+ const { className, items, isDisabled, portalRoot, ...rest } = props;
assertEmpty(rest);
@@ -26,18 +26,33 @@ export const ToolbarSelect = forwardRef<
return (
{
+ onPointerDown={(e) => {
+ // Prevents focus shift on mo
+ if (isTouchDevice()) {
+ e.preventDefault();
+ return;
+ }
+
+ // Needed as Safari doesn't focus button elements on mouse down
+ // unlike other browsers.
if (isSafari()) {
(e.currentTarget as HTMLButtonElement).focus();
}
diff --git a/packages/react/src/components/Comments/EmojiPicker.tsx b/packages/react/src/components/Comments/EmojiPicker.tsx
index db078703f2..f029146942 100644
--- a/packages/react/src/components/Comments/EmojiPicker.tsx
+++ b/packages/react/src/components/Comments/EmojiPicker.tsx
@@ -20,6 +20,8 @@ export const EmojiPicker = (props: {
}
return (
+ // Portal into the editor's portal element (which carries the color-scheme
+ // class) so the picker inherits light/dark mode instead of the body's.