What is the best way to set/override a custom width for a modal?
It seems ng-bootstrap currently supports
size: \'sm\' | \'lg\'
add following css in style.css file.
.xlModal > .modal-dialog {
max-width: 1490px !important;
min-width: 971px !important;
width: 95% !important;
}
Note : we can replace .xlModal
with any name.
open modal with new created style.
this.modalService.open(content,{windowClass:"xlModal"});
I found this as a good solution.
this.modalService.open(MyComponent, { windowClass: 'my-class'});
ng-deep “shadow-piercing” descendant combinator can be used to force a style down through the child component tree into all the child component views. In this case modal-dialog class.
::ng-deep .my-class .modal-dialog {
max-width: 80%;
width: 80%;
}
You could wrap it into a function like
component.ts
// accepts 'sm', 'md' or 'lg' as argument.
// default argument 'md'
public openModal( dialogSize: 'sm' | 'md' | 'lg' = 'md'): void {
let modalOptions = {};
if (dialogSize === 'sm' || dialogSize === 'lg') {
modalOptions = { size: dialogSize };
} else {
modalOptions = { windowClass: 'md-class' };
}
this.modalService.open(ConfirmationDialogComponent, modalOptions);
}
component.css
::ng-deep .md-class .modal-dialog {
max-width: 80%;
width: 80%;
}
And call it like,
openModal(); //argument 'md'
openModal('sm'); //argument 'sm'
openModal('md'); //argument 'md'
openModal('lg'); //argument 'lg'
When you don't specify the size
, the default value use $modal-md
(500px).
All the 3 modals sm, md and lg are responsive and will display on full width (with borders) on mobile.
If you really want a new size, you can use a custom size:
this.modalService.open(MyModalComponent, { size: <any>'xl' });
With a css for this class:
.modal-xl {
max-width: 1000px;
}
EDIT: the xl size is now a standard size so the CSS.
Add a argument class: 'modal-lg'
open() {
const modalRef = this.modalService.open(ModelComponent, { class: 'modal-lg', backdrop: 'static' });
}
}
Try like this :
import { NgbModal, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { ModelComponent } from './model.component';
export class Component {
constructor(private modalService: NgbModal) {}
open() {
const modalRef = this.modalService.open(ModelComponent, { size: 'lg', backdrop: 'static' });
}
}