Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(select): disable layout scrolling #1012

Merged
merged 6 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/gulp/tasks/bundle/rollup-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const ROLLUP_GLOBALS = {
'@angular/cdk/portal': 'ng.cdk.portal',
'@angular/cdk/a11y': 'ng.cdk.a11y',
'@angular/cdk/bidi': 'ng.cdk.bidi',
'@angular/cdk/scrolling': 'ng.cdk.scrolling',


// RxJS dependencies
Expand Down
7 changes: 4 additions & 3 deletions src/framework/theme/components/cdk/adapter/adapter.module.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { ModuleWithProviders, NgModule } from '@angular/core';
import { OverlayContainer, ScrollDispatcher } from '@angular/cdk/overlay';
import { OverlayContainer, ScrollDispatcher, ScrollStrategyOptions } from '@angular/cdk/overlay';

import { NbOverlayContainerAdapter } from './overlay-container-adapter';
import { NbScrollDispatcherAdapter } from './scroll-dispatcher-adapter';
import { NbViewportRulerAdapter } from './viewport-ruler-adapter';
import { NbBlockScrollStrategyAdapter } from './block-scroll-strategy-adapter';
import { NbBlockScrollStrategyAdapter, NbScrollStrategyOptions } from './block-scroll-strategy-adapter';


@NgModule({})
export class NbCdkAdapterModule {
static forRoot(): ModuleWithProviders {
return <ModuleWithProviders> {
return <ModuleWithProviders>{
ngModule: NbCdkAdapterModule,
providers: [
NbViewportRulerAdapter,
NbOverlayContainerAdapter,
NbBlockScrollStrategyAdapter,
{ provide: OverlayContainer, useExisting: NbOverlayContainerAdapter },
{ provide: ScrollDispatcher, useClass: NbScrollDispatcherAdapter },
{ provide: ScrollStrategyOptions, useClass: NbScrollStrategyOptions },
],
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
import { Injectable, Inject } from '@angular/core';
import { BlockScrollStrategy } from '@angular/cdk/overlay';
import { Inject, Injectable, NgZone } from '@angular/core';
import { BlockScrollStrategy, ScrollDispatcher, ScrollStrategyOptions } from '@angular/cdk/overlay';

import { NbLayoutScrollService } from '../../../services/scroll.service';
import { NB_DOCUMENT } from '../../../theme.options';
import { NbViewportRulerAdapter } from './viewport-ruler-adapter';


/**
* Overrides default block scroll strategy because default strategy blocks scrolling on the body only.
* But Nebular has its own scrollable container - nb-layout. So, we need to block scrolling in it to.
* */
@Injectable()
export class NbBlockScrollStrategyAdapter extends BlockScrollStrategy {
constructor(ruler: NbViewportRulerAdapter, @Inject(NB_DOCUMENT) document) {
super(ruler, document);
constructor(@Inject(NB_DOCUMENT) document: any,
viewportRuler: NbViewportRulerAdapter,
protected scrollService: NbLayoutScrollService) {
super(viewportRuler, document);
}

enable() {
super.enable();
this.scrollService.scrollable(false);
}

disable() {
super.disable();
this.scrollService.scrollable(true);
}
}

export class NbScrollStrategyOptions extends ScrollStrategyOptions {
constructor(protected scrollService: NbLayoutScrollService,
protected scrollDispatcher: ScrollDispatcher,
protected viewportRuler: NbViewportRulerAdapter,
protected ngZone: NgZone,
@Inject(NB_DOCUMENT) protected document) {
super(scrollDispatcher, viewportRuler, ngZone, document);
}

block = () => new NbBlockScrollStrategyAdapter(this.document, this.viewportRuler, this.scrollService);
}
9 changes: 8 additions & 1 deletion src/framework/theme/components/dialog/dialog.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { Component, NgModule } from '@angular/core';
import { TestBed } from '@angular/core/testing';

import { NbOverlayContainerAdapter, NbOverlayService } from '../cdk';
import { NbOverlayContainerAdapter, NbOverlayService, NbViewportRulerAdapter } from '../cdk';
import { NbDialogService } from './dialog.service';
import { NbDialogModule } from './dialog.module';
import { NbThemeModule } from '../../theme.module';
import { NB_DOCUMENT } from '../../theme.options';


export class NbViewportRulerMockAdapter extends NbViewportRulerAdapter {
getViewportSize(): Readonly<{ width: number; height: number; }> {
return { width: 1600, height: 900 };
}
}

@Component({ selector: 'nb-test-dialog', template: '<button class="test-focusable-button"></button>' })
class NbTestDialogComponent {
}
Expand Down Expand Up @@ -36,6 +42,7 @@ describe('dialog-service', () => {
NbThemeModule.forRoot(),
NbDialogModule.forRoot(),
],
providers: [{ provide: NbViewportRulerAdapter, useClass: NbViewportRulerMockAdapter }],
});

dialog = TestBed.get(NbDialogService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
}
}

nb-layout.overlay-scroll-block .scrollable-container {
overflow: hidden;
}

.layout {
// TODO: check this prop name
min-width: nb-theme(layout-window-mode-min-width);
Expand Down
10 changes: 10 additions & 0 deletions src/framework/theme/components/layout/layout.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ export class NbLayoutComponent implements AfterViewInit, OnDestroy {
@HostBinding('class.window-mode') windowModeValue: boolean = false;
@HostBinding('class.with-scroll') withScrollValue: boolean = false;
@HostBinding('class.with-subheader') withSubheader: boolean = false;
@HostBinding('class.overlay-scroll-block') overlayScrollBlock: boolean = false;

/**
* Defines whether the layout columns will be centered after some width
Expand Down Expand Up @@ -436,6 +437,15 @@ export class NbLayoutComponent implements AfterViewInit, OnDestroy {
this.scroll(0, 0);
});

this.scrollService
.onScrollableChange()
.pipe(
filter(() => this.withScrollValue),
)
.subscribe((scrollable: boolean) => {
this.overlayScrollBlock = !scrollable;
});

if (isPlatformBrowser(this.platformId)) {
// trigger first time so that after the change we have the initial value
this.themeService.changeWindowWidth(this.window.innerWidth);
Expand Down
9 changes: 9 additions & 0 deletions src/framework/theme/services/scroll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class NbLayoutScrollService {
private scrollPositionReq$ = new Subject<any>();
private manualScroll$ = new Subject<NbScrollPosition>();
private scroll$ = new Subject<any>();
private scrollable$ = new Subject<boolean>();

/**
* Returns scroll position
Expand Down Expand Up @@ -86,11 +87,19 @@ export class NbLayoutScrollService {
return this.scrollPositionReq$;
}

onScrollableChange(): Observable<boolean> {
return this.scrollable$.pipe(share());
}

/**
* @private
* @param {any} event
*/
fireScrollChange(event: any) {
this.scroll$.next(event);
}

scrollable(scrollable: boolean) {
this.scrollable$.next(scrollable);
}
}