Skip to content

Commit

Permalink
Add option to configure content rendering in block form
Browse files Browse the repository at this point in the history
`yieldedContentRendering`:
- 'always': render always (by default, old behaviour);
- 'lazy': render since the first opening;
- 'open': render only when modal is open, content is destroyed on closing.
  • Loading branch information
nag5000 committed Mar 27, 2017
1 parent 808234a commit e8e8b72
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
25 changes: 23 additions & 2 deletions addon/components/ember-remodal.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export default Component.extend({
modifier: '',
modal: null,
options: null,
yieldedContentRendering: 'always',
isOpen: false,
closeOnEscape: true,
closeOnCancel: true,
closeOnConfirm: true,
Expand All @@ -46,8 +48,8 @@ export default Component.extend({
},

willDestroyElement() {
scheduleOnce('destroy', this, '_destroyDomElements');
scheduleOnce('destroy', this, '_deregisterObservers');
this._destroyDomElements();
this._deregisterObservers();
},

modalId: computed('elementId', {
Expand All @@ -66,11 +68,29 @@ export default Component.extend({
}
}),

shouldRenderBlock: computed('yieldedContentRendering', 'isOpen', 'modal', function() {
let rendering = this.get('yieldedContentRendering');
switch (rendering) {
case 'always':
return true;

case 'lazy':
return !!this.get('modal');

case 'open':
return this.get('isOpen');

default:
throw new Error(`Invalid value of "yieldedContentRendering": ${rendering}`);
}
}),

openDidFire: on('opened', function() {
this.sendAction('onOpen');
}),

closeDidFire: on('closed', function() {
this.set('isOpen', false);
this.sendAction('onClose');
}),

Expand Down Expand Up @@ -209,6 +229,7 @@ export default Component.extend({
},

open() {
this.set('isOpen', true);
if (this.get('modal')) {
scheduleOnce('afterRender', this, '_openModal');
} else {
Expand Down
16 changes: 9 additions & 7 deletions addon/templates/components/ember-remodal.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
{{/if}}

{{#if hasBlock}}
<div class="ember-remodal inner yielded content" data-test-id="yielded">
{{yield (hash
open=(component 'ember-remodal/er-button' modalId=elementId)
confirm=(component 'ember-remodal/er-button' action=(action 'confirm'))
cancel=(component 'ember-remodal/er-button' action=(action 'cancel'))
)}}
</div>
{{#if shouldRenderBlock}}
<div class="ember-remodal inner yielded content" data-test-id="yielded">
{{yield (hash
open=(component 'ember-remodal/er-button' modalId=elementId)
confirm=(component 'ember-remodal/er-button' action=(action 'confirm'))
cancel=(component 'ember-remodal/er-button' action=(action 'cancel'))
)}}
</div>
{{/if}}
{{/if}}

{{#if cancelButton}}
Expand Down

0 comments on commit e8e8b72

Please sign in to comment.