Skip to content

Commit

Permalink
fix: (attempt) new toolbar item scrollIntoView not working on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgurney committed May 13, 2024
1 parent 133fa24 commit 6ade527
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
10 changes: 7 additions & 3 deletions src/Settings/ToolbarSettingsModal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { App, ButtonComponent, Menu, Modal, Setting, TFile, debounce, normalizePath, setIcon } from 'obsidian';
import { arraymove, debugLog, emptyMessageFr, getPosition, hasVars, removeComponentVisibility, addComponentVisibility, learnMoreFr, scrollElementIntoView } from 'src/Utils/Utils';
import { App, ButtonComponent, Menu, Modal, Platform, Setting, TFile, debounce, normalizePath, setIcon } from 'obsidian';
import { arraymove, debugLog, emptyMessageFr, getPosition, hasVars, removeComponentVisibility, addComponentVisibility, learnMoreFr } from 'src/Utils/Utils';
import NoteToolbarPlugin from 'src/main';
import { DEFAULT_STYLE_OPTIONS, LinkType, MOBILE_STYLE_OPTIONS, POSITION_OPTIONS, PlatformType, PositionType, ToolbarItemSettings, ToolbarSettings } from './NoteToolbarSettings';
import { NoteToolbarSettingTab } from './NoteToolbarSettingTab';
Expand Down Expand Up @@ -73,8 +73,12 @@ export default class ToolbarSettingsModal extends Modal {
let inputToFocus = this.contentEl.querySelector('#note-toolbar-setting-item-field-' + (this.toolbar.items.length - 1) + ' input[type="text"]') as HTMLInputElement;
let inputContainer = inputToFocus.closest('.note-toolbar-setting-item') as HTMLElement;
if (inputToFocus?.value.length === 0) {
scrollElementIntoView(this.modalEl, inputContainer);
inputToFocus.focus();
if (Platform.isMobile) {
setTimeout(() => {
inputContainer.scrollIntoView(true);
}, 100);
}
}
}

Expand Down
8 changes: 0 additions & 8 deletions src/Utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,4 @@ export function isValidUri(u: string): boolean {
else {
return false
}
}

/**
* Scrolls the element into view. Implemented as scrollIntoView doesn't work consistently on mobile (iOS, at least).
*/
export function scrollElementIntoView(parent: HTMLElement, element: HTMLElement) {
const scrollOffset = element.getBoundingClientRect().top + element.scrollTop;
parent.scrollTo({ top: scrollOffset, behavior: 'auto' });
}

0 comments on commit 6ade527

Please sign in to comment.