Skip to content

Commit

Permalink
Improve overlay and modal
Browse files Browse the repository at this point in the history
  • Loading branch information
vinimarcili committed Aug 30, 2023
1 parent 74323ea commit 0311006
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 131 deletions.
11 changes: 9 additions & 2 deletions src/components/sq-modal/sq-modal.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<div class="modal align-items-center {{ backdropClass }}" [ngClass]="{ open: open, 'need-priority': needPriority }" #modal>
<div
class="modal align-items-center {{ backdropClass }}"
[id]="id"
[ngClass]="{
open: open,
}"
#modal
>
<div class="modal-dialog modal-{{ modalSize }} {{ modalClass }}">
<div class="modal-content modal-sq">
<div
Expand All @@ -15,7 +22,7 @@
</button>
</div>

<div class="modal-body scrollbar p-3">
<div class="modal-body scrollbar">
<ng-content></ng-content>
</div>

Expand Down
1 change: 1 addition & 0 deletions src/components/sq-modal/sq-modal.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
border: none;
justify-content: flex-end;
button.close {
z-index: 1;
position: relative;
top: 5px;
left: -4px;
Expand Down
64 changes: 46 additions & 18 deletions src/components/sq-modal/sq-modal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ContentChild,
ElementRef,
EventEmitter,
HostListener,
Inject,
Input,
OnChanges,
Expand All @@ -19,11 +20,12 @@ import {
styleUrls: ['./sq-modal.component.scss'],
})
export class SqModalComponent implements OnChanges {
@Input() id = `random-id-${(1 + Date.now() + Math.random()).toString().replace('.', '')}`
@Input() open?: boolean
@Input() modalSize: 'sm' | 'md' | 'lg' | string = 'md'
@Input() modalClass?: string
@Input() backdropClass?: string
@Input() needPriority?: boolean
@Input() backdrop = 'static'

@Output() modalClose: EventEmitter<void> = new EventEmitter()
@Output() leftPress: EventEmitter<void> = new EventEmitter()
Expand All @@ -38,52 +40,78 @@ export class SqModalComponent implements OnChanges {
modalNumber = 0
hasHeader = false
document: Document
enableBackdropClick = false
modalsLength = 0

constructor(@Inject(DOCUMENT) public documentImported: Document) {
this.onKeydown = this.onKeydown.bind(this)
this.document = documentImported || document
}

@HostListener('document:click', ['$event'])
backdropClick(event: any) {
if (this.backdrop === 'static' || !this.modal || !this.open || !this.enableBackdropClick) {
return
}
const modalDialog = this.modal.nativeElement.firstElementChild
if (!modalDialog?.contains(event.target)) {
const body = this.document.getElementsByTagName('body')[0]
const backdrop = this.document.getElementById('modal-backdrop') || this.document.createElement('div')
this.modalClose.emit()
this.modal.nativeElement.style.display = 'none'
if (backdrop.parentNode && this.modalsLength === 1) {
backdrop.parentNode.removeChild(backdrop)
body.classList.remove('block')
}
window.removeEventListener('keydown', this.onKeydown)
this.enableBackdropClick = false
}
}

ngOnChanges(changes: SimpleChanges) {
if (changes.hasOwnProperty('open') || changes.hasOwnProperty('needPriority')) {
if (changes.hasOwnProperty('open')) {
const body = this.document.getElementsByTagName('body')[0]
const backdrop = this.document.getElementById('modal-backdrop') || this.document.createElement('div')
if (this.open && this.modal) {
const modal = this.modal
if (this.open && modal) {
this.hasHeader = !!this.headerTemplate
this.modals = this.document.getElementsByClassName('modal open')
body.classList.add('block')
this.modal.nativeElement.style.display = 'flex'
modal.nativeElement.style.display = 'flex'
window.addEventListener('keydown', this.onKeydown)
this.modals = this.document.getElementsByClassName('modal open')
setTimeout(() => {
this.modalNumber = this.modals?.length || 0
if (this.modalNumber === 1) {
this.modalsLength = this.modals?.length || 0
if (this.modalsLength === 1) {
backdrop.setAttribute('id', 'modal-backdrop')
backdrop.setAttribute('class', 'modal-backdrop show')
body.appendChild(backdrop)
} else if (this.modalsLength > 1) {
modal.nativeElement.style.zIndex = 1060 + this.modalsLength + 1
setTimeout(() => {
backdrop.setAttribute('style', `z-index: ${1060 + this.modalsLength};`)
}, 200)
}
this.enableBackdropClick = true
})
if (this.needPriority) {
backdrop.setAttribute('style', 'z-index: 1080;')
}
} else if (this.modal) {
} else if (modal) {
this.modalClose.emit()
this.modal.nativeElement.style.display = 'none'
if (backdrop.parentNode && this.modalNumber === 1) {
modal.nativeElement.style.display = 'none'
modal.nativeElement.style.zIndex = null
backdrop.removeAttribute('style')
if (backdrop.parentNode && this.modalsLength === 1) {
backdrop.parentNode.removeChild(backdrop)
body.classList.remove('block')
}
this.enableBackdropClick = false
window.removeEventListener('keydown', this.onKeydown)
if (this.needPriority) {
backdrop.removeAttribute('style')
}
}
}
}

onKeydown(event: KeyboardEvent) {
onKeydown(event: any) {
if (this.open) {
this.modals = this.document.getElementsByClassName('modal')
if (this.modals?.length === this.modalNumber) {
if (this.modals?.length === this.modalsLength) {
this.events(event.keyCode)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/sq-overlay/sq-overlay.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="modal overlay {{ overlayDirection }}" [ngClass]="{ open: open }" #overlay>
<div class="modal-dialog {{ overlayDirection }}" [ngStyle]="{ 'background-color': headerColor }" [ngClass]="{ opened: opened }">
<div [id]="id" class="modal overlay {{ overlayDirection }}" [ngClass]="{ open: open }" #overlay>
<div class="modal-dialog {{ overlayDirection }}" [ngStyle]="{ 'background-color': headerColor }" [ngClass]="{ opened: finishOpening }">
<div class="modal-content scrollbar">
<div
class="modal-header"
Expand Down
173 changes: 67 additions & 106 deletions src/components/sq-overlay/sq-overlay.component.scss
Original file line number Diff line number Diff line change
@@ -1,125 +1,86 @@

::ng-deep {
.overlay {
padding: 0;
border-radius: 0px;
&.left {
justify-content: flex-start;
}

&.right {
justify-content: flex-end;
}

.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
height: 60px;
border-radius: 0;
overflow: hidden;
padding: 1rem 2rem;
}

.modal-footer {
height: 60px;
border-radius: 0;
overflow: hidden;
padding: 1rem 2rem;
}

.borderless {
border-color: transparent;
}

.modal-body {
height: calc(100vh - 120px);
padding: 2rem;
overflow-y: auto;
overflow-x: hidden;
}

.modal-dialog {
transition: opacity 0.3s linear, left 0.3s ease-out, right 0.3s ease-out, width 0.3s ease-out, height 0.3s ease-out !important;
position: fixed;
margin: auto;
width: 320px;
max-width: 100vw;
height: 100%;
transform: translate3d(0%, 0, 0) !important;
}

.modal-content {
height: 100%;
border-radius: 0;
overflow-y: auto;
border: none;
background-color: transparent;
}

.modal-footer > * {
margin: 0;
}

&.modal-static {
.modal-dialog {
transform: scale(1.02) !important;
transition: opacity 0.3s linear, left 0.3s ease-out, right 0.3s ease-out, width 0.3s ease-out, height 0.3s ease-out,
transform 0.3s ease-out !important;
}
}
}

.modal.overlay {
padding: 0;
border-radius: 0px;

button.close {
max-height: 30px;
line-height: 30px;
transition: var(--transition);
z-index: 1;

span {
font-size: 2.1rem;
font-weight: 300;
transition: var(--transition);
}

&:hover,
&:focus {
outline: none;

span {
font-weight: 500;
.modal-content {
height: 100%;
border-radius: 0;
overflow-y: auto;
border: none;
background-color: transparent;
.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
height: 60px;
border-radius: 0;
overflow: hidden;
padding: 1rem 2rem;
&.without-header {
padding: 0 0.08rem;
border: none;
justify-content: flex-end;
.button-close {
margin: 0;
padding: 0;
}
}
.button-close {
max-height: 30px;
line-height: 30px;
opacity: 0.7 !important;
transition: var(--transition);
z-index: 1;
span {
font-size: 2.1rem;
font-weight: 300;
transition: var(--transition);
}
&:hover,
&:focus {
outline: none;

span {
font-weight: 500;
}
}
}
}
.modal-body {
height: calc(100vh - 120px);
padding: 2rem;
overflow-y: auto;
overflow-x: hidden;
&.without-footer {
height: calc(100vh - 60px);
}
}
.modal-footer {
height: 60px;
border-radius: 0;
overflow: hidden;
padding: 1rem 2rem;
}
.modal-footer > * {
margin: 0;
}
.borderless {
border-color: transparent;
}
}
}

.without-footer {
height: calc(100vh - 60px);
}

.without-header {
padding: 0 0.08rem;
border: none;
justify-content: flex-end;

button.close {
margin: 0;
padding: 0;
}
}
}
}

#close-button {
opacity: 0.7 !important;
transition: var(--transition) !important;

&:hover,
&:focus {
outline: none !important;

span {
font-weight: 500;
}
}
}
Loading

0 comments on commit 0311006

Please sign in to comment.