Skip to content

Commit

Permalink
fix(modal): Fixed modal positioning for SUI v2.3 (edcarroll#362) (Closes
Browse files Browse the repository at this point in the history
 edcarroll#371)

* fix(dimmer): Fixed dimmer to work correctly with SUI v2.3.0

* fix(modal): Fixed modal positioning for SUI v2.3.0

* chore: Updated SUI version to 2.3.1

* chore: Fixed typescript dependency version

* feat(modal): Added isCentered property

* doc(modal): Updated API description

* docs(modal): Fixed a typo in a property description

* fix(modal): Fixed barrel reference & private `HostBinding`
  • Loading branch information
avaneev95 authored and avohra committed Oct 6, 2018
1 parent 4307585 commit e9b58a4
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 32 deletions.
9 changes: 8 additions & 1 deletion demo/src/app/pages/modules/modal/modal.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,17 @@ export class ModalPage {
description: "Sets whether the modal displays against a light background.",
defaultValue: "false"
},
{
name: "isCentered",
type: "boolean",
description: "Whether or not the modal should be placed in the center of the page",
defaultValue: "true"
},
{
name: "mustScroll",
type: "boolean",
description: "Whether or not the modal should always display a scrollbar.",
description: "Whether or not the modal should be always scrolling. " +
"Should be used when the modal content is dynamic and can exceed the height of the browser",
defaultValue: "false"
},
{
Expand Down
2 changes: 1 addition & 1 deletion demo/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<meta name="description" content="ng2-semantic-ui - Semantic UI Angular 2 Integrations">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.3.1/semantic.min.css">
<!-- Calendar Module CSS -->
<link rel="stylesheet" href="https://unpkg.com/semantic-ui-calendar/dist/calendar.min.css">

Expand Down
7 changes: 3 additions & 4 deletions src/modules/dimmer/components/dimmer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import { TransitionController, SuiTransition, TransitionDirection, Transition }
selector: "sui-dimmer",
template: `
<div [class.content]="wrapContent">
<div [class.center]="wrapContent">
<ng-content></ng-content>
</div>
<ng-content></ng-content>
</div>
`,
styles: [`
:host.dimmer {
:host.dimmer:not(.hidden) {
transition: none;
display: flex !important;
}
`]
})
Expand Down
3 changes: 3 additions & 0 deletions src/modules/modal/classes/modal-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export class ModalConfig<T, U = undefined, V = undefined> {
public isBasic:boolean;
// Whether the modal shows against a light background.
public isInverted:boolean;
// Whether or not the modal should be placed in the center of the page.
public isCentered:boolean;

// Whether or not the modal should always display a scrollbar.
public mustScroll:boolean;
Expand All @@ -47,6 +49,7 @@ export class ModalConfig<T, U = undefined, V = undefined> {
this.isFullScreen = false;
this.isBasic = false;
this.isInverted = false;
this.isCentered = true;

this.mustScroll = false;

Expand Down
26 changes: 26 additions & 0 deletions src/modules/modal/components/dimmer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Component, HostBinding, Renderer2, ElementRef, ChangeDetectorRef } from "@angular/core";
import { SuiDimmer } from "../../dimmer/internal";

@Component({
selector: "sui-modal-dimmer",
template: `<ng-content></ng-content>`,
styles: [`
:host.ui.dimmer:not(.hidden) {
transition: none;
overflow-y: auto;
display: flex !important;
}
`]
})
export class SuiModalDimmer extends SuiDimmer {

@HostBinding("class.page")
@HostBinding("class.modals")
public readonly hasClasses:boolean;

constructor(renderer:Renderer2, element:ElementRef, changeDetector:ChangeDetectorRef) {
super(renderer, element, changeDetector);
this.hasClasses = true;
this.isClickable = false;
}
}
38 changes: 13 additions & 25 deletions src/modules/modal/components/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import { ModalConfig, ModalSize } from "../classes/modal-config";
selector: "sui-modal",
template: `
<!-- Page dimmer for modal background. -->
<sui-dimmer class="page"
<sui-modal-dimmer [ngClass]="{'top aligned': !isCentered}"
[class.inverted]="isInverted"
[(isDimmed)]="dimBackground"
[isClickable]="false"
[transitionDuration]="transitionDuration"
[wrapContent]="false"
(click)="close()">
<!-- Modal component, with transition component attached -->
Expand All @@ -25,7 +23,7 @@ import { ModalConfig, ModalSize } from "../classes/modal-config";
[class.active]="transitionController?.isVisible"
[class.fullscreen]="isFullScreen"
[class.basic]="isBasic"
[class.scroll]="mustScroll"
[class.scrolling]="mustScroll"
[class.inverted]="isInverted"
[ngClass]="dynamicClasses"
(click)="onClick($event)"
Expand All @@ -38,21 +36,9 @@ import { ModalConfig, ModalSize } from "../classes/modal-config";
<!-- @ViewChild reference so we can insert elements beside this div. -->
<div #templateSibling></div>
</div>
</sui-dimmer>
</sui-modal-dimmer>
`,
styles: [`
.ui.dimmer {
overflow-y: auto;
display: flex !important;
}
/* avoid .scrolling as Semantic UI adds unwanted styles. */
.scroll {
margin-top: 3.5rem !important;
margin-bottom: 3.5rem !important;
top: 0;
}
`]
styles: [``]
})
export class SuiModal<T, U> implements OnInit, AfterViewInit {
@Input()
Expand Down Expand Up @@ -93,6 +79,9 @@ export class SuiModal<T, U> implements OnInit, AfterViewInit {
@Input()
public size:ModalSize;

@Input()
public isCentered:boolean;

// Whether the modal takes up the full width of the screen.
private _isFullScreen:boolean;

Expand Down Expand Up @@ -206,11 +195,8 @@ export class SuiModal<T, U> implements OnInit, AfterViewInit {
templateElement.parentNode.removeChild(templateElement);
}

// Update margin offset to center modal correctly on-screen.
const element = this._modalElement.nativeElement as Element;
setTimeout(() => {
this.updateScroll();
});
setTimeout(() => this.updateScroll());

// Focus any element with [autofocus] attribute.
const autoFocus = element.querySelector("[autofocus]") as HTMLElement | null;
Expand All @@ -231,6 +217,7 @@ export class SuiModal<T, U> implements OnInit, AfterViewInit {
this.isFullScreen = config.isFullScreen;
this.isBasic = config.isBasic;
this.isInverted = config.isInverted;
this.isCentered = config.isCentered;

this.mustScroll = config.mustScroll;

Expand Down Expand Up @@ -269,12 +256,13 @@ export class SuiModal<T, U> implements OnInit, AfterViewInit {

// Decides whether the modal needs to reposition to allow scrolling.
private updateScroll():void {
// Semantic UI modal margin is 3.5rem, which is relative to the global font size, so for compatibility:
const fontSize = parseFloat(window.getComputedStyle(document.documentElement).getPropertyValue("font-size"));
const margin = fontSize * 3.5;

// _mustAlwaysScroll works by stopping _mustScroll from being automatically updated, so it stays `true`.
if (!this._mustAlwaysScroll && this._modalElement) {

// Semantic UI modal margin and dimmer padding are 1rem, which is relative to the global font size, so for compatibility:
const fontSize = parseFloat(window.getComputedStyle(document.documentElement).getPropertyValue("font-size"));
const margin = fontSize * 2;
const element = this._modalElement.nativeElement as Element;

// The modal must scroll if the window height is smaller than the modal height + both margins.
Expand Down
4 changes: 3 additions & 1 deletion src/modules/modal/modal.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SuiTransitionModule } from "../transition/internal";
import { SuiUtilityModule } from "../../misc/util/internal";
import { SuiModalService } from "./services/modal.service";
import { SuiModal } from "./components/modal";
import { SuiModalDimmer } from "./components/dimmer";

@NgModule({
imports: [
Expand All @@ -14,7 +15,8 @@ import { SuiModal } from "./components/modal";
SuiUtilityModule
],
declarations: [
SuiModal
SuiModal,
SuiModalDimmer
],
exports: [
SuiModal
Expand Down

0 comments on commit e9b58a4

Please sign in to comment.