fix: reposition dropdowns when resizing window
This commit is contained in:
@@ -2,13 +2,19 @@ import { AppState } from '@/ui_models/app_state';
|
|||||||
import { toDirective, useCloseOnBlur, useCloseOnClickOutside } from './utils';
|
import { toDirective, useCloseOnBlur, useCloseOnClickOutside } from './utils';
|
||||||
import { observer } from 'mobx-react-lite';
|
import { observer } from 'mobx-react-lite';
|
||||||
import { NotesOptions } from './NotesOptions';
|
import { NotesOptions } from './NotesOptions';
|
||||||
import { useRef } from 'preact/hooks';
|
import { useCallback, useEffect, useRef } from 'preact/hooks';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
appState: AppState;
|
appState: AppState;
|
||||||
};
|
};
|
||||||
|
|
||||||
const NotesContextMenu = observer(({ appState }: Props) => {
|
const NotesContextMenu = observer(({ appState }: Props) => {
|
||||||
|
const {
|
||||||
|
contextMenuOpen,
|
||||||
|
contextMenuPosition,
|
||||||
|
contextMenuMaxHeight,
|
||||||
|
} = appState.notes;
|
||||||
|
|
||||||
const contextMenuRef = useRef<HTMLDivElement>();
|
const contextMenuRef = useRef<HTMLDivElement>();
|
||||||
const [closeOnBlur] = useCloseOnBlur(
|
const [closeOnBlur] = useCloseOnBlur(
|
||||||
contextMenuRef,
|
contextMenuRef,
|
||||||
@@ -20,13 +26,24 @@ const NotesContextMenu = observer(({ appState }: Props) => {
|
|||||||
(open: boolean) => appState.notes.setContextMenuOpen(open)
|
(open: boolean) => appState.notes.setContextMenuOpen(open)
|
||||||
);
|
);
|
||||||
|
|
||||||
return appState.notes.contextMenuOpen ? (
|
const reloadContextMenuLayout = useCallback(() => {
|
||||||
|
appState.notes.reloadContextMenuLayout();
|
||||||
|
}, [appState.notes]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
window.addEventListener('resize', reloadContextMenuLayout);
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('resize', reloadContextMenuLayout);
|
||||||
|
};
|
||||||
|
}, [reloadContextMenuLayout]);
|
||||||
|
|
||||||
|
return contextMenuOpen ? (
|
||||||
<div
|
<div
|
||||||
ref={contextMenuRef}
|
ref={contextMenuRef}
|
||||||
className="sn-dropdown min-w-80 max-h-120 max-w-xs flex flex-col py-2 overflow-y-auto fixed"
|
className="sn-dropdown min-w-80 max-h-120 max-w-xs flex flex-col py-2 overflow-y-auto fixed"
|
||||||
style={{
|
style={{
|
||||||
...appState.notes.contextMenuPosition,
|
...contextMenuPosition,
|
||||||
maxHeight: appState.notes.contextMenuMaxHeight,
|
maxHeight: contextMenuMaxHeight,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<NotesOptions appState={appState} closeOnBlur={closeOnBlur} />
|
<NotesOptions appState={appState} closeOnBlur={closeOnBlur} />
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import {
|
|||||||
} from '@reach/disclosure';
|
} from '@reach/disclosure';
|
||||||
import { Switch } from './Switch';
|
import { Switch } from './Switch';
|
||||||
import { observer } from 'mobx-react-lite';
|
import { observer } from 'mobx-react-lite';
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
appState: AppState;
|
appState: AppState;
|
||||||
@@ -45,16 +46,27 @@ const SearchOptions = observer(({ appState }: Props) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const updateWidthAndPosition = () => {
|
||||||
|
const rect = buttonRef.current.getBoundingClientRect();
|
||||||
|
setMaxWidth(rect.right - 16);
|
||||||
|
setPosition({
|
||||||
|
top: rect.bottom,
|
||||||
|
right: document.body.clientWidth - rect.right,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
window.addEventListener('resize', updateWidthAndPosition);
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('resize', updateWidthAndPosition);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Disclosure
|
<Disclosure
|
||||||
open={open}
|
open={open}
|
||||||
onChange={() => {
|
onChange={() => {
|
||||||
const rect = buttonRef.current.getBoundingClientRect();
|
updateWidthAndPosition();
|
||||||
setMaxWidth(rect.right - 16);
|
|
||||||
setPosition({
|
|
||||||
top: rect.bottom,
|
|
||||||
right: document.body.clientWidth - rect.right,
|
|
||||||
});
|
|
||||||
setOpen(!open);
|
setOpen(!open);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ export class NotesState {
|
|||||||
top: 0,
|
top: 0,
|
||||||
left: 0,
|
left: 0,
|
||||||
};
|
};
|
||||||
|
contextMenuClickLocation: { x: number, y: number } = { x: 0, y: 0 };
|
||||||
contextMenuMaxHeight: number | 'auto' = 'auto';
|
contextMenuMaxHeight: number | 'auto' = 'auto';
|
||||||
showProtectedWarning = false;
|
showProtectedWarning = false;
|
||||||
|
|
||||||
@@ -47,6 +48,7 @@ export class NotesState {
|
|||||||
trashedNotesCount: computed,
|
trashedNotesCount: computed,
|
||||||
|
|
||||||
setContextMenuOpen: action,
|
setContextMenuOpen: action,
|
||||||
|
setContextMenuClickLocation: action,
|
||||||
setContextMenuPosition: action,
|
setContextMenuPosition: action,
|
||||||
setContextMenuMaxHeight: action,
|
setContextMenuMaxHeight: action,
|
||||||
setShowProtectedWarning: action,
|
setShowProtectedWarning: action,
|
||||||
@@ -183,6 +185,10 @@ export class NotesState {
|
|||||||
this.contextMenuOpen = open;
|
this.contextMenuOpen = open;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setContextMenuClickLocation(location: { x: number, y: number }): void {
|
||||||
|
this.contextMenuClickLocation = location;
|
||||||
|
}
|
||||||
|
|
||||||
setContextMenuPosition(position: {
|
setContextMenuPosition(position: {
|
||||||
top?: number;
|
top?: number;
|
||||||
left: number;
|
left: number;
|
||||||
@@ -195,6 +201,60 @@ export class NotesState {
|
|||||||
this.contextMenuMaxHeight = maxHeight;
|
this.contextMenuMaxHeight = maxHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reloadContextMenuLayout(): void {
|
||||||
|
const { clientHeight } = document.documentElement;
|
||||||
|
const defaultFontSize = window.getComputedStyle(
|
||||||
|
document.documentElement
|
||||||
|
).fontSize;
|
||||||
|
const maxContextMenuHeight = parseFloat(defaultFontSize) * 30;
|
||||||
|
const footerHeight = 32;
|
||||||
|
|
||||||
|
// Open up-bottom is default behavior
|
||||||
|
let openUpBottom = true;
|
||||||
|
|
||||||
|
const bottomSpace = clientHeight - footerHeight - this.contextMenuClickLocation.y;
|
||||||
|
const upSpace = this.contextMenuClickLocation.y;
|
||||||
|
|
||||||
|
// If not enough space to open up-bottom
|
||||||
|
if (maxContextMenuHeight > bottomSpace) {
|
||||||
|
// If there's enough space, open bottom-up
|
||||||
|
if (upSpace > maxContextMenuHeight) {
|
||||||
|
openUpBottom = false;
|
||||||
|
this.setContextMenuMaxHeight(
|
||||||
|
'auto'
|
||||||
|
);
|
||||||
|
// Else, reduce max height (menu will be scrollable) and open in whichever direction there's more space
|
||||||
|
} else {
|
||||||
|
if (upSpace > bottomSpace) {
|
||||||
|
this.setContextMenuMaxHeight(
|
||||||
|
upSpace - 2
|
||||||
|
);
|
||||||
|
openUpBottom = false;
|
||||||
|
} else {
|
||||||
|
this.setContextMenuMaxHeight(
|
||||||
|
bottomSpace - 2
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.setContextMenuMaxHeight(
|
||||||
|
'auto'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (openUpBottom) {
|
||||||
|
this.setContextMenuPosition({
|
||||||
|
top: this.contextMenuClickLocation.y,
|
||||||
|
left: this.contextMenuClickLocation.x,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.setContextMenuPosition({
|
||||||
|
bottom: clientHeight - this.contextMenuClickLocation.y,
|
||||||
|
left: this.contextMenuClickLocation.x,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async changeSelectedNotes(
|
async changeSelectedNotes(
|
||||||
mutate: (mutator: NoteMutator) => void
|
mutate: (mutator: NoteMutator) => void
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
|||||||
@@ -310,58 +310,11 @@ class NotesViewCtrl extends PureViewCtrl<unknown, NotesCtrlState> {
|
|||||||
await this.selectNote(note, true);
|
await this.selectNote(note, true);
|
||||||
}
|
}
|
||||||
if (this.state.selectedNotes[note.uuid]) {
|
if (this.state.selectedNotes[note.uuid]) {
|
||||||
const { clientHeight } = document.documentElement;
|
this.appState.notes.setContextMenuClickLocation({
|
||||||
const defaultFontSize = window.getComputedStyle(
|
x: e.clientX,
|
||||||
document.documentElement
|
y: e.clientY,
|
||||||
).fontSize;
|
});
|
||||||
const maxContextMenuHeight = parseFloat(defaultFontSize) * 30;
|
this.appState.notes.reloadContextMenuLayout();
|
||||||
const footerHeight = 32;
|
|
||||||
|
|
||||||
// Open up-bottom is default behavior
|
|
||||||
let openUpBottom = true;
|
|
||||||
|
|
||||||
const bottomSpace = clientHeight - footerHeight - e.clientY;
|
|
||||||
const upSpace = e.clientY;
|
|
||||||
|
|
||||||
// If not enough space to open up-bottom
|
|
||||||
if (maxContextMenuHeight > bottomSpace) {
|
|
||||||
// If there's enough space, open bottom-up
|
|
||||||
if (upSpace > maxContextMenuHeight) {
|
|
||||||
openUpBottom = false;
|
|
||||||
this.appState.notes.setContextMenuMaxHeight(
|
|
||||||
'auto'
|
|
||||||
);
|
|
||||||
// Else, reduce max height (menu will be scrollable) and open in whichever direction there's more space
|
|
||||||
} else {
|
|
||||||
if (upSpace > bottomSpace) {
|
|
||||||
this.appState.notes.setContextMenuMaxHeight(
|
|
||||||
upSpace - 2
|
|
||||||
);
|
|
||||||
openUpBottom = false;
|
|
||||||
} else {
|
|
||||||
this.appState.notes.setContextMenuMaxHeight(
|
|
||||||
bottomSpace - 2
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this.appState.notes.setContextMenuMaxHeight(
|
|
||||||
'auto'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (openUpBottom) {
|
|
||||||
this.appState.notes.setContextMenuPosition({
|
|
||||||
top: e.clientY,
|
|
||||||
left: e.clientX,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.appState.notes.setContextMenuPosition({
|
|
||||||
bottom: clientHeight - e.clientY,
|
|
||||||
left: e.clientX,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.appState.notes.setContextMenuOpen(true);
|
this.appState.notes.setContextMenuOpen(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user