modal-service.mdx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ---
  2. title: "ModalService"
  3. generated: true
  4. ---
  5. <GenerationInfo sourceFile="packages/admin-ui/src/lib/core/src/providers/modal/modal.service.ts" sourceLine="21" packageName="@vendure/admin-ui" />
  6. This service is responsible for instantiating a ModalDialog component and
  7. embedding the specified component within.
  8. ```ts title="Signature"
  9. class ModalService {
  10. constructor(overlayHostService: OverlayHostService)
  11. fromComponent(component: Type<T> & Type<Dialog<R>>, options?: ModalOptions<T>) => Observable<R | undefined>;
  12. dialog(config: DialogConfig<T>) => Observable<T | undefined>;
  13. }
  14. ```
  15. <div className="members-wrapper">
  16. ### constructor
  17. <MemberInfo kind="method" type={`(overlayHostService: OverlayHostService) => ModalService`} />
  18. ### fromComponent
  19. <MemberInfo kind="method" type={`(component: Type<T> &#38; Type<<a href='/reference/admin-ui-api/services/modal-service#dialog'>Dialog</a><R>>, options?: <a href='/reference/admin-ui-api/services/modal-service#modaloptions'>ModalOptions</a><T>) => Observable<R | undefined>`} />
  20. Create a modal from a component. The component must implement the <DocsLink href="/reference/admin-ui-api/services/modal-service#dialog">Dialog</DocsLink> interface.
  21. Additionally, the component should include templates for the title and the buttons to be
  22. displayed in the modal dialog. See example:
  23. *Example*
  24. ```ts
  25. class MyDialog implements Dialog {
  26. resolveWith: (result?: any) => void;
  27. okay() {
  28. doSomeWork().subscribe(result => {
  29. this.resolveWith(result);
  30. })
  31. }
  32. cancel() {
  33. this.resolveWith(false);
  34. }
  35. }
  36. ```
  37. *Example*
  38. ```html
  39. <ng-template vdrDialogTitle>Title of the modal</ng-template>
  40. <p>
  41. My Content
  42. </p>
  43. <ng-template vdrDialogButtons>
  44. <button type="button"
  45. class="btn"
  46. (click)="cancel()">Cancel</button>
  47. <button type="button"
  48. class="btn btn-primary"
  49. (click)="okay()">Okay</button>
  50. </ng-template>
  51. ```
  52. ### dialog
  53. <MemberInfo kind="method" type={`(config: <a href='/reference/admin-ui-api/services/modal-service#dialogconfig'>DialogConfig</a><T>) => Observable<T | undefined>`} />
  54. Displays a modal dialog with the provided title, body and buttons.
  55. </div>
  56. <GenerationInfo sourceFile="packages/admin-ui/src/lib/core/src/providers/modal/modal.types.ts" sourceLine="9" packageName="@vendure/admin-ui" />
  57. Any component intended to be used with the ModalService.fromComponent() method must implement
  58. this interface.
  59. ```ts title="Signature"
  60. interface Dialog<R = any> {
  61. resolveWith: (result?: R) => void;
  62. }
  63. ```
  64. <div className="members-wrapper">
  65. ### resolveWith
  66. <MemberInfo kind="property" type={`(result?: R) => void`} />
  67. Function to be invoked in order to close the dialog when the action is complete.
  68. The Observable returned from the .fromComponent() method will emit the value passed
  69. to this method and then complete.
  70. </div>
  71. <GenerationInfo sourceFile="packages/admin-ui/src/lib/core/src/providers/modal/modal.types.ts" sourceLine="33" packageName="@vendure/admin-ui" />
  72. Configures a generic modal dialog.
  73. ```ts title="Signature"
  74. interface DialogConfig<T> {
  75. title: string;
  76. body?: string;
  77. translationVars?: { [key: string]: string | number };
  78. buttons: Array<DialogButtonConfig<T>>;
  79. size?: 'sm' | 'md' | 'lg' | 'xl';
  80. }
  81. ```
  82. <div className="members-wrapper">
  83. ### title
  84. <MemberInfo kind="property" type={`string`} />
  85. ### body
  86. <MemberInfo kind="property" type={`string`} />
  87. ### translationVars
  88. <MemberInfo kind="property" type={`{ [key: string]: string | number }`} />
  89. ### buttons
  90. <MemberInfo kind="property" type={`Array<DialogButtonConfig<T>>`} />
  91. ### size
  92. <MemberInfo kind="property" type={`'sm' | 'md' | 'lg' | 'xl'`} />
  93. </div>
  94. <GenerationInfo sourceFile="packages/admin-ui/src/lib/core/src/providers/modal/modal.types.ts" sourceLine="48" packageName="@vendure/admin-ui" />
  95. Options to configure the behaviour of the modal.
  96. ```ts title="Signature"
  97. interface ModalOptions<T> {
  98. size?: 'sm' | 'md' | 'lg' | 'xl';
  99. verticalAlign?: 'top' | 'center' | 'bottom';
  100. closable?: boolean;
  101. locals?: Partial<T>;
  102. }
  103. ```
  104. <div className="members-wrapper">
  105. ### size
  106. <MemberInfo kind="property" type={`'sm' | 'md' | 'lg' | 'xl'`} />
  107. Sets the width of the dialog
  108. ### verticalAlign
  109. <MemberInfo kind="property" type={`'top' | 'center' | 'bottom'`} />
  110. Sets the vertical alignment of the dialog
  111. ### closable
  112. <MemberInfo kind="property" type={`boolean`} />
  113. When true, the "x" icon is shown
  114. and clicking it or the mask will close the dialog
  115. ### locals
  116. <MemberInfo kind="property" type={`Partial<T>`} />
  117. Values to be passed directly to the component being instantiated inside the dialog.
  118. </div>