ng-bootstrap Modal size

感情迁移 提交于 2019-11-29 10:39:08

问题


What is the best way to set/override a custom width for a modal?

It seems ng-bootstrap currently supports

size: 'sm' | 'lg'

but Bootstrap 4 supports sm, md and lg.

Ideally I would like the modal to be responsive and adjust similar to container sizes. And on mobile have it go full screen.

EDIT: Bootstrap 4 _variables.scss seems to have a $modal-md setting but appears to be unused.

$modal-lg:                    800px !default;
$modal-md:                    500px !default;
$modal-sm:                    300px !default;

回答1:


The cleanest solution that I could find is to use the windowClass property.

I.e.:

this.modalService.open(MyModalComponent,  { windowClass : "myCustomModalClass"});

Then add the following to your global CSS styles. This is important as the style won't be picked up from your components CSS.

.myCustomModalClass .modal-dialog {
  max-width: 565px;
}



回答2:


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' });
    }

}



回答3:


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%;
}



回答4:


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"});



回答5:


In addition to djpalme and Kapil Thakkar answers, you will need to set the 'encapsulation' of your Component to None as mentioned here

@Component({
    selector: 'app-generic-view',
    templateUrl: './generic-view.component.html',
    styleUrls: ['./generic-view.component.scss'],
    encapsulation: ViewEncapsulation.None
})



回答6:


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.




回答7:


Add size xl on interface NgbModalOptions.

export interface NgbModalOptions {
   size?: 'sm' | 'lg' | 'xl';
}

Now use it while opening the modal this.modalService.open(content, { size: 'xl' });




回答8:


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'


来源:https://stackoverflow.com/questions/46977398/ng-bootstrap-modal-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!