-
-
Notifications
You must be signed in to change notification settings - Fork 765
feat: Mobile formatting toolbar (BLO-1292) #2939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
03e86dd
e45f5a1
d9902bf
af12646
1d4fbe3
07d9ff7
faa72b5
d070f14
ece1f83
e23b2f3
57b68bf
dfb89ac
5a5fcd1
4b920a1
52933c2
8a03207
f779a4b
19428d4
a174fef
51aa828
9c748cc
3a95797
69c6a71
a42e93c
1d0ef95
e441113
09692f4
d4079d4
b05f7ab
cef137d
be8838e
51b89d0
83c2a00
22c7645
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,3 +38,60 @@ The first element in the default Formatting Toolbar is the Block Type Select, an | |
| <Example name="ui-components/formatting-toolbar-block-type-items" /> | ||
|
|
||
| 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, `<html>` and `<body>` 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. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. duplicate
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed - removed the duplicated clause. |
||
|
|
||
| To set this up, add the `bn-scroll-host` class to your scroll container: | ||
|
|
||
| ```tsx | ||
| <div className="bn-scroll-host">{/* nav, editor, page content... */}</div> | ||
| ``` | ||
|
|
||
| <Callout type="warning"> | ||
| 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. | ||
| </Callout> | ||
|
|
||
| That's all the setup needed. BlockNote injects the following styles for you: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated - the CSS rules (the scroll lock + |
||
|
|
||
| ```css | ||
| html:has(.bn-scroll-host), | ||
| body:has(.bn-scroll-host) { | ||
| overflow: hidden; | ||
| } | ||
| ``` | ||
|
|
||
| This locks scrolling on `<html>` and `<body>` 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 `<html>`: | ||
|
|
||
| ```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. | ||
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "playground": true, | ||
| "docs": true, | ||
| "docs": false, | ||
| "author": "areknawo", | ||
| "tags": [ | ||
| "Intermediate", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 ( | ||
| <div | ||
| className={ | ||
| scrollMode === "scroll-container" ? "bn-scroll-host" : undefined | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's unify scroll-host and scroll-container between code and documentation? Can we think of a single term?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unified everything on scroll host (matching the public |
||
| } | ||
| > | ||
| <NavBar scrollMode={scrollMode} onScrollModeChange={setScrollMode} /> | ||
| <main className="app-main"> | ||
| <StaticText /> | ||
| {/* On mobile, the default UI automatically shows the mobile formatting | ||
| toolbar above the keyboard - no extra setup needed. */} | ||
| <BlockNoteView editor={editor} /> | ||
| <StaticText /> | ||
| <BlockNoteView editor={secondEditor} /> | ||
| <StaticText /> | ||
| </main> | ||
| </div> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { useState } from "react"; | ||
|
|
||
| function HamburgerMenu() { | ||
| const [open, setOpen] = useState(false); | ||
|
|
||
| return ( | ||
| <div className="dummy-hamburger"> | ||
| <button | ||
| type="button" | ||
| className="dummy-hamburger-button" | ||
| aria-label="Menu" | ||
| aria-expanded={open} | ||
| onClick={() => setOpen((v) => !v)} | ||
| > | ||
| <span /> | ||
| <span /> | ||
| <span /> | ||
| </button> | ||
| {open && ( | ||
| <nav className="dummy-hamburger-menu"> | ||
| <a href="#">Home</a> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the hamburger menu required for testing / demonstrational purposes? if so, let's change to "some", "dummy", "buttons" or similar to clarify these menus are fake and people don't think they're broken
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the text of the buttons hasn't been updated. It's nice that the code is now clearer, but a user just playing with the example could still think "why doesn't this button do anything?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed the hamburger links to "Dummy link 1-4" so they clearly read as placeholders. |
||
| <a href="#">Documents</a> | ||
| <a href="#">Shared with me</a> | ||
| <a href="#">Settings</a> | ||
| </nav> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export function NavBar(props: { | ||
| scrollMode: "scrolling-document" | "scroll-container"; | ||
| onScrollModeChange: ( | ||
| scrollContainer: "scrolling-document" | "scroll-container", | ||
| ) => void; | ||
| }) { | ||
| return ( | ||
| <header className="dummy-top-nav"> | ||
| <HamburgerMenu /> | ||
| <span className="dummy-top-nav-title">Lorem Ipsum</span> | ||
| {/* Switches between the default "scrolling document" layout and the | ||
| "pinned scroll container" layout, to compare the toolbar in both. */} | ||
| <button | ||
| type="button" | ||
| className="dummy-layout-toggle" | ||
| aria-pressed={props.scrollMode === "scroll-container"} | ||
| onClick={() => | ||
| props.onScrollModeChange( | ||
| props.scrollMode === "scroll-container" | ||
| ? "scrolling-document" | ||
| : "scroll-container", | ||
| ) | ||
| } | ||
| > | ||
| Pinned scroll container | ||
| <span className="dummy-layout-toggle-track" aria-hidden="true" /> | ||
| </button> | ||
| </header> | ||
| ); | ||
| } | ||
|
|
||
| /** A block of static page text, to sit around the editor. */ | ||
| export function StaticText() { | ||
| return ( | ||
| <section className="dummy-prose"> | ||
| <h2>Lorem Ipsum</h2> | ||
| <p> | ||
| 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. | ||
| </p> | ||
| <p> | ||
| 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. | ||
| </p> | ||
| <p> | ||
| 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. | ||
| </p> | ||
| <p> | ||
| 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. | ||
| </p> | ||
| </section> | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which one you get is decided purely by your app's CSS:obsolete and incorrectThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reworded to "Which one you get depends on your app's layout:".