From 630d6cd4543529f0f650ff99ad4a99803e85843b Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sat, 4 Mar 2023 02:27:27 +0330 Subject: [PATCH 1/2] feat(pwa-helper): scrollToTopCommand --- ui/pwa-helper/src/context.ts | 6 ++++-- ui/pwa-helper/src/pwa-element.ts | 37 +++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/ui/pwa-helper/src/context.ts b/ui/pwa-helper/src/context.ts index 8e36ac26a..08edc348b 100644 --- a/ui/pwa-helper/src/context.ts +++ b/ui/pwa-helper/src/context.ts @@ -1,6 +1,8 @@ -import {contextProvider} from '@alwatr/signal'; +import {contextProvider, commandTrigger} from '@alwatr/signal'; import type {TopAppBarContent} from '@alwatr/ui-kit/top-app-bar/top-app-bar.js'; -export const topAppBarContextProvider = contextProvider.bind('top-app-bar-context'); +export const topAppBarContextProvider = contextProvider.bind('top_app_bar_context'); // export const topAppBarContextConsumer = contextConsumer.bind(topAppBarContextProvider.id); + +export const scrollToTopCommand = commandTrigger.bind<{smooth?: boolean}, undefined>('command_scroll_to_top'); diff --git a/ui/pwa-helper/src/pwa-element.ts b/ui/pwa-helper/src/pwa-element.ts index 590c3b515..e0cc02a87 100644 --- a/ui/pwa-helper/src/pwa-element.ts +++ b/ui/pwa-helper/src/pwa-element.ts @@ -10,14 +10,15 @@ import { type CSSResultGroup, } from '@alwatr/element'; import {localeContextConsumer, setLocale} from '@alwatr/i18n'; -import {routerOutlet, type RoutesConfig} from '@alwatr/router'; -import {commandTrigger} from '@alwatr/signal'; +import {RouteContext, routerOutlet, type RoutesConfig} from '@alwatr/router'; +import {commandHandler, commandTrigger} from '@alwatr/signal'; import '@alwatr/ui-kit/snackbar/controller.js'; import '@alwatr/ui-kit/style/pwa.css'; import '@alwatr/ui-kit/style/token.css'; import '@alwatr/ui-kit/top-app-bar/top-app-bar.js'; import {untilNextFrame} from '@alwatr/util'; +import {scrollToTopCommand, topAppBarContextProvider} from './context.js'; import './signal/back-to-home-click.js'; import './signal/browser-back-click.js'; import './signal/register-service-worker-command.js'; @@ -46,6 +47,9 @@ export class AlwatrPwaElement extends RouterMixin(SignalMixin(UnresolvedMixin(Al contain: size layout paint style; flex-grow: 1; flex-shrink: 0; + } + + .scroll-area { overflow-y: auto; } @@ -62,7 +66,19 @@ export class AlwatrPwaElement extends RouterMixin(SignalMixin(UnresolvedMixin(Al override connectedCallback(): void { super.connectedCallback(); - if (!localeContextConsumer.getValue()) {setLocale();} + if (!localeContextConsumer.getValue()) { + setLocale(); + } + this._signalListenerList.push( + commandHandler.define<{smooth?: boolean}, undefined>(scrollToTopCommand.id, (option): undefined => { + this.renderRoot.querySelector('.scroll-area')?.scrollTo({ + top: 0, + left: 0, + behavior: option.smooth ? 'smooth' : 'auto', + }); + return; + }), + ); } protected _routesConfig: RoutesConfig = { @@ -73,6 +89,11 @@ export class AlwatrPwaElement extends RouterMixin(SignalMixin(UnresolvedMixin(Al }, }; + protected override _routeContextUpdated(routeContext: RouteContext): void { + super._routeContextUpdated(routeContext); + scrollToTopCommand.request({}); + } + protected override async scheduleUpdate(): Promise { await untilNextFrame(); super.scheduleUpdate(); @@ -80,11 +101,7 @@ export class AlwatrPwaElement extends RouterMixin(SignalMixin(UnresolvedMixin(Al override render(): unknown { this._logger.logMethod('render'); - return [ - this._topAppBarTemplate(), - this._mainTemplate(), - this._navigationBarTemplate(), - ]; + return [this._topAppBarTemplate(), this._mainTemplate(), this._navigationBarTemplate()]; } protected override firstUpdated(changedProperties: PropertyValues): void { @@ -93,11 +110,11 @@ export class AlwatrPwaElement extends RouterMixin(SignalMixin(UnresolvedMixin(Al } protected _topAppBarTemplate(): unknown { - return html``; + return html``; } protected _mainTemplate(): unknown { - return html`
${cache(routerOutlet(this._routesConfig))}
`; + return html`
${cache(routerOutlet(this._routesConfig))}
`; } protected _navigationBarTemplate(): unknown { From e74b6dfa98530ea6331a3ab1a764aba10eb0b64b Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Sun, 5 Mar 2023 19:03:44 +0330 Subject: [PATCH 2/2] chore(pwa-helper): new version --- ui/pwa-helper/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/pwa-helper/package.json b/ui/pwa-helper/package.json index ae770e20c..17bcfafd0 100644 --- a/ui/pwa-helper/package.json +++ b/ui/pwa-helper/package.json @@ -1,7 +1,7 @@ { "name": "@alwatr/pwa-helper", "version": "0.29.0", - "pwaVersion": "0.30.0-beta3", + "pwaVersion": "0.30.0-beta4", "description": "Alwatr PWA Helper", "keywords": [ "element",