Skip to content

Commit

Permalink
after the search bar is focused and the screen height is reduced, let…
Browse files Browse the repository at this point in the history
… the sheet scroll to top
  • Loading branch information
mayswind committed Feb 11, 2025
1 parent 13cc6a2 commit 2e01e55
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
14 changes: 10 additions & 4 deletions src/components/mobile/TransactionTagSelectionSheet.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<f7-sheet swipe-to-close swipe-handler=".swipe-handler"
<f7-sheet ref="sheet" swipe-to-close swipe-handler=".swipe-handler"
style="height: auto" :opened="show"
@sheet:open="onSheetOpen" @sheet:closed="onSheetClosed">
<f7-toolbar>
Expand All @@ -18,7 +18,8 @@
:placeholder="tt('Find tag')"
:disable-button="false"
v-if="enableFilter"
@input="filterContent = $event.target.value">
@input="filterContent = $event.target.value"
@focus="onSearchBarFocus">
</f7-searchbar>
<f7-page-content :class="'no-padding-top ' + heightClass">
<f7-list class="no-margin-top no-margin-bottom" v-if="(!allTags || !allTags.length || noAvailableTag) && !newTag">
Expand Down Expand Up @@ -84,7 +85,7 @@

<script setup lang="ts">
import { ref, computed, useTemplateRef } from 'vue';
import type { Searchbar } from 'framework7/types';
import type { Sheet, Searchbar } from 'framework7/types';
import { useI18n } from '@/locales/helpers.ts';
import { useI18nUIComponents, showLoading, hideLoading } from '@/lib/ui/mobile.ts';
Expand All @@ -93,7 +94,7 @@ import { TransactionTag } from '@/models/transaction_tag.ts';
import { useTransactionTagsStore } from '@/stores/transactionTag.ts';
import { copyArrayTo } from '@/lib/common.ts';
import { type Framework7Dom, scrollToSelectedItem } from '@/lib/ui/mobile.ts';
import { type Framework7Dom, scrollToSelectedItem, scrollSheetToTop } from '@/lib/ui/mobile.ts';
const props = defineProps<{
modelValue: string[];
Expand All @@ -112,6 +113,7 @@ const { showToast } = useI18nUIComponents();
const transactionTagsStore = useTransactionTagsStore();
const sheet = useTemplateRef<Sheet.Sheet>('sheet');
const searchbar = useTemplateRef<Searchbar.Searchbar>('searchbar');
const filterContent = ref<string>('');
Expand Down Expand Up @@ -220,6 +222,10 @@ function cancelSaveNewTag(): void {
newTag.value = null;
}
function onSearchBarFocus(): void {
scrollSheetToTop(sheet.value?.$el as HTMLElement, window.innerHeight); // $el is not Framework7 Dom
}
function onSheetOpen(event: { $el: Framework7Dom }): void {
selectedItemIds.value = copyArrayTo(props.modelValue, []);
newTag.value = null;
Expand Down
14 changes: 10 additions & 4 deletions src/components/mobile/TreeViewSelectionSheet.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<f7-sheet swipe-to-close swipe-handler=".swipe-handler"
<f7-sheet ref="sheet" swipe-to-close swipe-handler=".swipe-handler"
style="height: auto" :opened="show" @sheet:open="onSheetOpen" @sheet:closed="onSheetClosed">
<f7-toolbar>
<div class="swipe-handler"></div>
Expand All @@ -13,7 +13,8 @@
:placeholder="filterPlaceholder"
:disable-button="false"
v-if="enableFilter"
@input="filterContent = $event.target.value">
@input="filterContent = $event.target.value"
@focus="onSearchBarFocus">
</f7-searchbar>
<f7-page-content :class="'no-padding-top ' + heightClass">
<f7-list class="no-margin-top no-margin-bottom" v-if="!filteredItems || !filteredItems.length">
Expand Down Expand Up @@ -49,12 +50,12 @@

<script setup lang="ts">
import { ref, computed, useTemplateRef } from 'vue';
import type { Searchbar } from 'framework7/types';
import type { Sheet, Searchbar } from 'framework7/types';
import { useI18n } from '@/locales/helpers.ts';
import { type TwoLevelItemSelectionBaseProps, useTwoLevelItemSelectionBase } from '@/components/base/TwoLevelItemSelectionBase.ts';
import { type Framework7Dom, scrollToSelectedItem } from '@/lib/ui/mobile.ts';
import { type Framework7Dom, scrollToSelectedItem, scrollSheetToTop } from '@/lib/ui/mobile.ts';
interface MobileTwoLevelItemSelectionBaseProps extends TwoLevelItemSelectionBaseProps {
show: boolean;
Expand All @@ -78,6 +79,7 @@ const {
updateCurrentSecondaryValue
} = useTwoLevelItemSelectionBase(props);
const sheet = useTemplateRef<Sheet.Sheet>('sheet');
const searchbar = useTemplateRef<Searchbar.Searchbar>('searchbar');
const currentValue = ref<unknown>(props.modelValue);
Expand Down Expand Up @@ -132,6 +134,10 @@ function onSecondaryItemClicked(subItem: unknown): void {
emit('update:show', false);
}
function onSearchBarFocus(): void {
scrollSheetToTop(sheet.value?.$el as HTMLElement, window.innerHeight); // $el is not Framework7 Dom
}
function onSheetOpen(event: { $el: Framework7Dom }): void {
currentValue.value = props.modelValue;
scrollToSelectedItem(event.$el, '.page-content', '.treeview-item .treeview-item-selected');
Expand Down
14 changes: 10 additions & 4 deletions src/components/mobile/TwoColumnListItemSelectionSheet.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<f7-sheet swipe-to-close swipe-handler=".swipe-handler"
<f7-sheet ref="sheet" swipe-to-close swipe-handler=".swipe-handler"
style="height: auto" :opened="show" @sheet:open="onSheetOpen" @sheet:closed="onSheetClosed">
<f7-toolbar>
<div class="swipe-handler"></div>
Expand All @@ -13,7 +13,8 @@
:placeholder="filterPlaceholder"
:disable-button="false"
v-if="enableFilter"
@input="filterContent = $event.target.value">
@input="filterContent = $event.target.value"
@focus="onSearchBarFocus">
</f7-searchbar>
<f7-page-content class="no-padding-top">
<div class="grid grid-gap" :class="{ 'grid-cols-2': filteredItems && filteredItems.length }">
Expand Down Expand Up @@ -70,12 +71,12 @@

<script setup lang="ts">
import { ref, computed, useTemplateRef } from 'vue';
import type { Searchbar } from 'framework7/types';
import type { Sheet, Searchbar } from 'framework7/types';
import { useI18n } from '@/locales/helpers.ts';
import { type CommonTwoColumnListItemSelectionProps, useTwoColumnListItemSelectionBase } from '@/components/base/TwoColumnListItemSelectionBase.ts';
import { type Framework7Dom, scrollToSelectedItem } from '@/lib/ui/mobile.ts';
import { type Framework7Dom, scrollToSelectedItem, scrollSheetToTop } from '@/lib/ui/mobile.ts';
interface MobileTwoColumnListItemSelectionProps extends CommonTwoColumnListItemSelectionProps {
show: boolean;
Expand All @@ -101,6 +102,7 @@ const {
updateCurrentSecondaryValue
} = useTwoColumnListItemSelectionBase(props);
const sheet = useTemplateRef<Sheet.Sheet>('sheet');
const searchbar = useTemplateRef<Searchbar.Searchbar>('searchbar');
const currentPrimaryValue = ref<unknown>(getCurrentPrimaryValueBySecondaryValue(props.modelValue));
Expand All @@ -127,6 +129,10 @@ function onSecondaryItemClicked(subItem: unknown): void {
close();
}
function onSearchBarFocus(): void {
scrollSheetToTop(sheet.value?.$el as HTMLElement, window.innerHeight); // $el is not Framework7 Dom
}
function onSheetOpen(event: { $el: Framework7Dom }): void {
currentPrimaryValue.value = getCurrentPrimaryValueBySecondaryValue(props.modelValue);
currentSecondaryValue.value = props.modelValue;
Expand Down
18 changes: 18 additions & 0 deletions src/lib/ui/mobile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ export function scrollToSelectedItem(parentEl: Framework7Dom, containerSelector:
container.scrollTop(targetPos);
}

export function scrollSheetToTop(sheetElement: HTMLElement | undefined, windowNormalInnerHeight: number): void {
if (!sheetElement) {
return;
}

const sheetHeight = sheetElement.offsetHeight;

if (sheetHeight < windowNormalInnerHeight) {
setTimeout(() => {
const windowNewInnerHeight = window.innerHeight;

if (windowNewInnerHeight < windowNormalInnerHeight && sheetHeight < windowNewInnerHeight) {
window.scrollTo({ top: windowNormalInnerHeight - sheetHeight - 24, behavior: "smooth" });
}
}, 300);
}
}

export function useI18nUIComponents() {
const { tt, te } = useI18n();

Expand Down

0 comments on commit 2e01e55

Please sign in to comment.