Skip to content

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

web: improve slider ux on mobile & small screens

Abdullah Atta44bb9752d2ab
diff --git a/apps/web/src/app-effects.mobile.ts b/apps/web/src/app-effects.mobile.ts
deleted file mode 100644
index 1bec0cab4..000000000
--- a/apps/web/src/app-effects.mobile.ts
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
-This file is part of the Notesnook project (https://notesnook.com/)
-
-Copyright (C) 2023 Streetwriters (Private) Limited
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-import { useEffect } from "react";
-import { useStore } from "./stores/app-store";
-import useSlider from "./hooks/use-slider";
-import useMobile from "./hooks/use-mobile";
-import useTablet from "./hooks/use-tablet";
-
-type MobileAppEffectsProps = {
- sliderId: string;
- overlayId: string;
- setShow: (show: boolean) => void;
-};
-export default function MobileAppEffects({
- sliderId,
- overlayId,
- setShow
-}: MobileAppEffectsProps) {
- const isMobile = useMobile();
- const isTablet = useTablet();
- const toggleSideMenu = useStore((store) => store.toggleSideMenu);
- const setIsEditorOpen = useStore((store) => store.setIsEditorOpen);
- const isEditorOpen = useStore((store) => store.isEditorOpen);
- const isSideMenuOpen = useStore((store) => store.isSideMenuOpen);
- const isFocusMode = useStore((store) => store.isFocusMode);
-
- const [slideToIndex] = useSlider(sliderId, {
- onSliding: (_e, { position }) => {
- if (!isMobile) return;
- const offset = 70;
- const width = 300;
-
- const percent = offset - (position / width) * offset;
- const overlay = document.getElementById("overlay");
- if (!overlay) return;
- if (percent > 0) {
- overlay.style.opacity = `${percent}%`;
- overlay.style.pointerEvents = "all";
- } else {
- overlay.style.opacity = "0%";
- overlay.style.pointerEvents = "none";
- }
- },
- onChange: (e, { slide }) => {
- toggleSideMenu(slide?.index === 0 ? true : false);
- setIsEditorOpen(slide?.index === 3 ? true : false);
- }
- });
-
- useEffect(() => {
- if (!isMobile) return;
- slideToIndex(isSideMenuOpen ? 1 : 2);
- }, [isMobile, slideToIndex, isSideMenuOpen]);
-
- useEffect(() => {
- if (!isMobile) return;
- slideToIndex(isEditorOpen ? 3 : 2);
- }, [isMobile, slideToIndex, isEditorOpen]);
-
- useEffect(() => {
- toggleSideMenu(!isMobile);
- if (!isMobile && !isTablet && !isFocusMode) setShow(true);
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [isMobile, isTablet, isFocusMode, toggleSideMenu]);
-
- useEffect(() => {
- if (!overlayId) return;
- const overlay = document.getElementById(overlayId);
- if (!overlay) return;
- overlay.onclick = () => toggleSideMenu(false);
- return () => {
- overlay.onclick = null;
- };
- }, [overlayId, toggleSideMenu]);
-
- return null;
-}
diff --git a/apps/web/src/app.tsx b/apps/web/src/app.tsx
index 8398a112c..ff8eecf2b 100644
--- a/apps/web/src/app.tsx
+++ b/apps/web/src/app.tsx
@@ -42,8 +42,6 @@ import { Global } from "@emotion/react";
new WebExtensionRelay();
-const MobileAppEffects = React.lazy(() => import("./app-effects.mobile"));
-
function App() {
const isMobile = useMobile();
const [show, setShow] = useState(true);
@@ -69,13 +67,6 @@ function App() {
<div id="menu-wrapper">
<GlobalMenuWrapper />
</div>
- {isMobile && (
- <MobileAppEffects
- sliderId="slider"
- overlayId="overlay"
- setShow={setShow}
- />
- )}
</Suspense>
<AppEffects setShow={setShow} />
@@ -210,8 +201,46 @@ function DesktopAppContents({ show, setShow }: DesktopAppContentsProps) {
}
function MobileAppContents() {
+ const { ref, slideToIndex } = useSlider({
+ onSliding: (_e, { position }) => {
+ const offset = 70;
+ const width = 300;
+
+ const percent = offset - (position / width) * offset;
+ const overlay = document.getElementById("overlay");
+ if (!overlay) return;
+ if (percent > 0) {
+ overlay.style.opacity = `${percent}%`;
+ overlay.style.pointerEvents = "all";
+ } else {
+ overlay.style.opacity = "0%";
+ overlay.style.pointerEvents = "none";
+ }
+ },
+ onChange: (e, { slide, lastSlide }) => {
+ slide.node.classList.add("pane-active");
+ lastSlide?.node.classList.remove("pane-active");
+ }
+ });
+
+ useEffect(() => {
+ const toggleSideMenuEvent = AppEventManager.subscribe(
+ AppEvents.toggleSideMenu,
+ (state) => slideToIndex(state ? 0 : 1)
+ );
+ const toggleEditorEvent = AppEventManager.subscribe(
+ AppEvents.toggleEditor,
+ (state) => slideToIndex(state ? 2 : 1)
+ );
+ return () => {
+ toggleSideMenuEvent.unsubscribe();
+ toggleEditorEvent.unsubscribe();
+ };
+ }, [slideToIndex]);
+
return (
<FlexScrollContainer
+ scrollRef={ref}
id="slider"
suppressScrollX
style={{
@@ -228,17 +257,18 @@ function MobileAppContents() {
}}
>
<Flex
+ className="mobile-nav-pane"
sx={{
scrollSnapAlign: "start",
scrollSnapStop: "always",
- width: [300, 60],
+ width: 300,
flexShrink: 0
}}
>
<NavigationMenu toggleNavigationContainer={() => { }} isTablet={false} />
</Flex>
<Flex
- className="listMenu"
+ className="mobile-list-pane"
variant="columnFill"
sx={{
position: "relative",
@@ -251,6 +281,7 @@ function MobileAppContents() {
<CachedRouter />
<Box
id="overlay"
+ onClick={() => slideToIndex(1)}
sx={{
position: "absolute",
width: "100%",
@@ -266,6 +297,7 @@ function MobileAppContents() {
/>
</Flex>
<Flex
+ className="mobile-editor-pane"
sx={{
scrollSnapAlign: "start",
scrollSnapStop: "always",
diff --git a/apps/web/src/common/app-events.ts b/apps/web/src/common/app-events.ts
index b4c790a57..bf5bf2a1b 100644
--- a/apps/web/src/common/app-events.ts
+++ b/apps/web/src/common/app-events.ts
@@ -38,5 +38,8 @@ export const AppEvents = {
changeNoteTitle: "changeNoteTitle",
- revealItemInList: "list:revealItem"
+ revealItemInList: "list:revealItem",
+
+ toggleSideMenu: "app:openSideMenu",
+ toggleEditor: "app:toggleEditor"
};
diff --git a/apps/web/src/components/editor/action-bar.tsx b/apps/web/src/components/editor/action-bar.tsx
index 312d3562a..55f94c53f 100644
--- a/apps/web/src/components/editor/action-bar.tsx
+++ b/apps/web/src/components/editor/action-bar.tsx
@@ -95,7 +95,6 @@ export function EditorActionBar() {
const isNotePublished =
activeSession && db.monographs.isPublished(activeSession.id);
const isMobile = useMobile();
- const setIsEditorOpen = useAppStore((store) => store.setIsEditorOpen);
const tools = [
{
@@ -210,7 +209,9 @@ export function EditorActionBar() {
borderRadius: 0,
flexShrink: 0
}}
- onClick={() => setIsEditorOpen(false)}
+ onClick={() =>
+ AppEventManager.publish(AppEvents.toggleEditor, false)
+ }
>
<ArrowLeft size={18} />
</Button>
diff --git a/apps/web/src/components/navigation-menu/navigation-item.tsx b/apps/web/src/components/navigation-menu/navigation-item.tsx
index 356859019..a407c8aa5 100644
--- a/apps/web/src/components/navigation-menu/navigation-item.tsx
+++ b/apps/web/src/components/navigation-menu/navigation-item.tsx
@@ -18,7 +18,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Button, Flex, FlexProps, Image, Text } from "@theme-ui/components";
-import { useStore as useAppStore } from "../../stores/app-store";
import { Menu } from "../../hooks/use-menu";
import useMobile from "../../hooks/use-mobile";
import { PropsWithChildren } from "react";
@@ -27,6 +26,7 @@ import { SchemeColors, createButtonVariant } from "@notesnook/theme";
import { MenuItem } from "@notesnook/ui";
import { useSortable } from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
+import { AppEventManager, AppEvents } from "../../common/app-events";
type NavigationItemProps = {
icon?: Icon;
@@ -66,7 +66,6 @@ function NavigationItem(
containerRef,
...restProps
} = props;
- const toggleSideMenu = useAppStore((store) => store.toggleSideMenu);
const isMobile = useMobile();
return (
@@ -120,7 +119,7 @@ function NavigationItem(
Menu.openMenu(menuItems);
}}
onClick={() => {
- if (isMobile) toggleSideMenu(false);
+ AppEventManager.publish(AppEvents.toggleSideMenu, false);
if (onClick) onClick();
}}
>
diff --git a/apps/web/src/components/route-container/index.tsx b/apps/web/src/components/route-container/index.tsx
index 2ca229b16..b5523195e 100644
--- a/apps/web/src/components/route-container/index.tsx
+++ b/apps/web/src/components/route-container/index.tsx
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { PropsWithChildren } from "react";
-import { Flex, Text } from "@theme-ui/components";
+import { Button, Flex, Text } from "@theme-ui/components";
import { ArrowLeft, Menu, Search, Plus, Close } from "../icons";
import { useStore } from "../../stores/app-store";
import { useStore as useSearchStore } from "../../stores/search-store";
@@ -26,6 +26,7 @@ import useMobile from "../../hooks/use-mobile";
import { debounce, usePromise } from "@notesnook/common";
import Field from "../field";
import { strings } from "@notesnook/intl";
+import { AppEventManager, AppEvents } from "../../common/app-events";
export type RouteContainerButtons = {
search?: {
@@ -64,7 +65,6 @@ function Header(props: RouteContainerProps) {
() => (typeof props.title === "string" ? props.title : props.title?.()),
[props.title]
);
- const toggleSideMenu = useStore((store) => store.toggleSideMenu);
const isMobile = useMobile();
const isSearching = useSearchStore((store) => store.isSearching);
const query = useSearchStore((store) => store.query);
@@ -134,17 +134,20 @@ function Header(props: RouteContainerProps) {
data-test-id="go-back"
/>
) : (
+ <Button
+ onClick={() =>
+ AppEventManager.publish(AppEvents.toggleSideMenu, true)
+ }
+ sx={{ p: 0, flexShrink: 0 }}
+ >
<Menu
- onClick={() => toggleSideMenu(true)}
sx={{
- flexShrink: 0,
- ml: 0,
- mr: 4,
- mt: 1,
- display: ["block", "none", "none"]
+ display: ["block", "none", "none"],
+ size: 23
}}
- size={30}
+ size={24}
/>
+ </Button>
)}
{titlePromise.status === "fulfilled" && titlePromise.value && (
<Text variant="heading" data-test-id="routeHeader" color="heading">
diff --git a/apps/web/src/hooks/use-slider.ts b/apps/web/src/hooks/use-slider.ts
index 2619940f5..5ad68e963 100644
--- a/apps/web/src/hooks/use-slider.ts
+++ b/apps/web/src/hooks/use-slider.ts
@@ -26,27 +26,24 @@ type Slide = {
width: number;
};
-export default function useSlider(
- sliderId: string,
- {
- onSliding,
- onChange
- }: {
- onSliding?: (
- e: Event,
- options: {
- lastSlide: Slide | null;
- lastPosition: number;
- position: number;
- }
- ) => void;
- onChange?: (
- e: Event,
- options: { position: number; slide: Slide; lastSlide: Slide | null }
- ) => void;
- } = {}
-) {
- const ref = useRef(document.getElementById(sliderId));
+export default function useSlider({
+ onSliding,
+ onChange
+}: {
+ onSliding?: (
+ e: Event,
+ options: {
+ lastSlide: Slide | null;
+ lastPosition: number;
+ position: number;
+ }
+ ) => void;
+ onChange?: (
+ e: Event,
+ options: { position: number; slide: Slide; lastSlide: Slide | null }
+ ) => void;
+} = {}) {
+ const ref = useRef<HTMLDivElement>(null);
const slides: Slide[] = useMemo(() => [], []);
useEffect(() => {
@@ -57,7 +54,11 @@ export default function useSlider(
let last = 0;
if (!slides.length) {
for (const node of slider.childNodes) {
- if (!(node instanceof HTMLElement)) continue;
+ if (
+ !(node instanceof HTMLElement) ||
+ node.classList.contains("ms-track-box")
+ )
+ continue;
slides.push({
index: slides.length,
node,
@@ -102,5 +103,5 @@ export default function useSlider(
[ref, slides]
);
- return [slideToIndex];
+ return { ref, slideToIndex };
}
diff --git a/apps/web/src/stores/app-store.ts b/apps/web/src/stores/app-store.ts
index 30fd5b582..8f1e63535 100644
--- a/apps/web/src/stores/app-store.ts
+++ b/apps/web/src/stores/app-store.ts
@@ -61,10 +61,7 @@ let syncTimeout = 0;
let pendingSync: SyncOptions | undefined = undefined;
class AppStore extends BaseStore<AppStore> {
- // default state
- isSideMenuOpen = false;
isFocusMode = false;
- isEditorOpen = false;
isVaultCreated = false;
isAutoSyncEnabled = Config.get("autoSyncEnabled", true);
isSyncEnabled = Config.get("syncEnabled", true);
@@ -199,17 +196,6 @@ class AppStore extends BaseStore<AppStore> {
);
};
- toggleSideMenu = (toggleState: boolean) => {
- console.log("toggling side menu");
- this.set(
- (state) => (state.isSideMenuOpen = toggleState ?? !state.isSideMenuOpen)
- );
- };
-
- setIsEditorOpen = (toggleState: boolean) => {
- this.set((state) => (state.isEditorOpen = toggleState));
- };
-
setIsVaultCreated = (toggleState: boolean) => {
this.set((state) => (state.isVaultCreated = toggleState));
};
diff --git a/apps/web/src/stores/editor-store.ts b/apps/web/src/stores/editor-store.ts
index 83ec32238..ad19a5e6e 100644
--- a/apps/web/src/stores/editor-store.ts
+++ b/apps/web/src/stores/editor-store.ts
@@ -518,7 +518,7 @@ class EditorStore extends BaseStore<EditorStore> {
} else setDocumentTitle();
this.set({ activeSessionId: id });
- appStore.setIsEditorOpen(!!id);
+ AppEventManager.publish(AppEvents.toggleEditor, true);
if (id) {
const { history } = this.get();