-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE] Add support for public in-element
#18867
Merged
+326
−19
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b666561
[FEATURE in-element] Add support for public `in-element` as per RFC287
simonihmig 2de45d6
[FEATURE in-element] Add deprecation for `-in-element`
simonihmig 7143578
Fix in-element transform for IE
simonihmig 48112f9
[DOC] Add API docs for `in-element`
simonihmig 457b5a1
FEATURE in-element] Add EMBER_GLIMMER_IN_ELEMENT feature flag
simonihmig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
packages/@ember/-internals/glimmer/lib/syntax/in-element.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
@module ember | ||
*/ | ||
|
||
/** | ||
The `in-element` helper renders its block content outside of the regular flow, | ||
into a DOM element given by its `destinationElement` positional argument. | ||
|
||
Common use cases - often referred to as "portals" or "wormholes" - are rendering | ||
dropdowns, modals or tooltips close to the root of the page to bypass CSS overflow | ||
rules, or to render content to parts of the page that are outside of the control | ||
of the Ember app itself (e.g. embedded into a static or server rendered HTML page). | ||
|
||
```handlebars | ||
{{#in-element this.destinationElement}} | ||
<div>Some content</div> | ||
{{/in-element}} | ||
``` | ||
|
||
### Arguments | ||
|
||
`{{in-element}}` requires a single positional argument: | ||
|
||
- `destinationElement` -- the DOM element to render into. It must exist at the time | ||
of rendering. | ||
|
||
It also supports an optional named argument: | ||
|
||
- `insertBefore` -- by default the DOM element's content is replaced when used as | ||
`destinationElement`. Passing `null` changes the behaviour to appended at the end | ||
of any existing content. Any other value than `null` is currently not supported. | ||
|
||
``` | ||
|
||
@method in-element | ||
@for Ember.Templates.helpers | ||
@public | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
216 changes: 216 additions & 0 deletions
216
packages/@ember/-internals/glimmer/tests/integration/syntax/public-in-element-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
import { moduleFor, RenderingTestCase, strip, equalTokens, runTask } from 'internal-test-helpers'; | ||
|
||
import { Component } from '@ember/-internals/glimmer'; | ||
import { set } from '@ember/-internals/metal'; | ||
|
||
moduleFor( | ||
'{{in-element}}', | ||
class extends RenderingTestCase { | ||
['@feature(EMBER_GLIMMER_IN_ELEMENT) allows rendering into an external element']() { | ||
let someElement = document.createElement('div'); | ||
|
||
this.render( | ||
strip` | ||
{{#in-element someElement}} | ||
{{text}} | ||
{{/in-element}} | ||
`, | ||
{ | ||
someElement, | ||
text: 'Whoop!', | ||
} | ||
); | ||
|
||
equalTokens(this.element, '<!---->'); | ||
equalTokens(someElement, 'Whoop!'); | ||
|
||
this.assertStableRerender(); | ||
|
||
runTask(() => set(this.context, 'text', 'Huzzah!!')); | ||
|
||
equalTokens(this.element, '<!---->'); | ||
equalTokens(someElement, 'Huzzah!!'); | ||
|
||
runTask(() => set(this.context, 'text', 'Whoop!')); | ||
|
||
equalTokens(this.element, '<!---->'); | ||
equalTokens(someElement, 'Whoop!'); | ||
} | ||
|
||
["@feature(EMBER_GLIMMER_IN_ELEMENT) it replaces the external element's content by default"]() { | ||
let someElement = document.createElement('div'); | ||
someElement.appendChild(document.createTextNode('foo ')); | ||
|
||
this.render( | ||
strip` | ||
{{#in-element someElement insertBefore=undefined}} | ||
{{text}} | ||
{{/in-element}} | ||
`, | ||
{ | ||
someElement, | ||
text: 'bar', | ||
} | ||
); | ||
|
||
equalTokens(this.element, '<!---->'); | ||
equalTokens(someElement, 'bar'); | ||
|
||
this.assertStableRerender(); | ||
|
||
runTask(() => set(this.context, 'text', 'bar!!')); | ||
|
||
equalTokens(this.element, '<!---->'); | ||
equalTokens(someElement, 'bar!!'); | ||
|
||
runTask(() => set(this.context, 'text', 'bar')); | ||
|
||
equalTokens(this.element, '<!---->'); | ||
equalTokens(someElement, 'bar'); | ||
} | ||
|
||
['@feature(EMBER_GLIMMER_IN_ELEMENT) allows appending to the external element with insertBefore=null']() { | ||
let someElement = document.createElement('div'); | ||
someElement.appendChild(document.createTextNode('foo ')); | ||
|
||
this.render( | ||
strip` | ||
{{#in-element someElement insertBefore=null}} | ||
{{text}} | ||
{{/in-element}} | ||
`, | ||
{ | ||
someElement, | ||
text: 'bar', | ||
} | ||
); | ||
|
||
equalTokens(this.element, '<!---->'); | ||
equalTokens(someElement, 'foo bar'); | ||
|
||
this.assertStableRerender(); | ||
|
||
runTask(() => set(this.context, 'text', 'bar!!')); | ||
|
||
equalTokens(this.element, '<!---->'); | ||
equalTokens(someElement, 'foo bar!!'); | ||
|
||
runTask(() => set(this.context, 'text', 'bar')); | ||
|
||
equalTokens(this.element, '<!---->'); | ||
equalTokens(someElement, 'foo bar'); | ||
} | ||
|
||
['@feature(EMBER_GLIMMER_IN_ELEMENT) does not allow insertBefore=non-null-value']() { | ||
let someElement = document.createElement('div'); | ||
|
||
expectAssertion(() => { | ||
this.render( | ||
strip` | ||
{{#in-element someElement insertBefore=".foo"}} | ||
{{text}} | ||
{{/in-element}} | ||
`, | ||
{ | ||
someElement, | ||
text: 'Whoop!', | ||
} | ||
); | ||
}, /Can only pass null to insertBefore in in-element, received:/); | ||
} | ||
|
||
['@feature(EMBER_GLIMMER_IN_ELEMENT) components are cleaned up properly'](assert) { | ||
let hooks = []; | ||
|
||
let someElement = document.createElement('div'); | ||
|
||
this.registerComponent('modal-display', { | ||
ComponentClass: Component.extend({ | ||
didInsertElement() { | ||
hooks.push('didInsertElement'); | ||
}, | ||
|
||
willDestroyElement() { | ||
hooks.push('willDestroyElement'); | ||
}, | ||
}), | ||
|
||
template: `{{text}}`, | ||
}); | ||
|
||
this.render( | ||
strip` | ||
{{#if showModal}} | ||
{{#in-element someElement}} | ||
{{modal-display text=text}} | ||
{{/in-element}} | ||
{{/if}} | ||
`, | ||
{ | ||
someElement, | ||
text: 'Whoop!', | ||
showModal: false, | ||
} | ||
); | ||
|
||
equalTokens(this.element, '<!---->'); | ||
equalTokens(someElement, ''); | ||
|
||
this.assertStableRerender(); | ||
|
||
runTask(() => set(this.context, 'showModal', true)); | ||
|
||
equalTokens(this.element, '<!---->'); | ||
this.assertComponentElement(someElement.firstChild, { | ||
content: 'Whoop!', | ||
}); | ||
|
||
runTask(() => set(this.context, 'text', 'Huzzah!')); | ||
|
||
equalTokens(this.element, '<!---->'); | ||
this.assertComponentElement(someElement.firstChild, { | ||
content: 'Huzzah!', | ||
}); | ||
|
||
runTask(() => set(this.context, 'text', 'Whoop!')); | ||
|
||
equalTokens(this.element, '<!---->'); | ||
this.assertComponentElement(someElement.firstChild, { | ||
content: 'Whoop!', | ||
}); | ||
|
||
runTask(() => set(this.context, 'showModal', false)); | ||
|
||
equalTokens(this.element, '<!---->'); | ||
equalTokens(someElement, ''); | ||
|
||
assert.deepEqual(hooks, ['didInsertElement', 'willDestroyElement']); | ||
} | ||
|
||
['@feature(EMBER_GLIMMER_IN_ELEMENT) appending to the root element should not cause double clearing']() { | ||
this.render( | ||
strip` | ||
Before | ||
{{#in-element this.rootElement insertBefore=null}} | ||
{{this.text}} | ||
{{/in-element}} | ||
After | ||
`, | ||
{ | ||
rootElement: this.element, | ||
text: 'Whoop!', | ||
} | ||
); | ||
|
||
equalTokens(this.element, 'BeforeWhoop!<!---->After'); | ||
|
||
this.assertStableRerender(); | ||
|
||
runTask(() => set(this.context, 'text', 'Huzzah!')); | ||
|
||
equalTokens(this.element, 'BeforeHuzzah!<!---->After'); | ||
|
||
// teardown happens in afterEach and should not cause double-clearing error | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's probably fine to enable by default in this PR too, since it's not a major refactor. The feature flag is mostly to give us a way to turn it off in case we do run into major issues duringbeta
. Will confirm thoughScratch that, we'll bring it up in the next core team meeting to enable, going through the standard process (checked with @rwjblue)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right. Now that you are mentioning this, I remember that I wanted to ask about this 😝
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, seems I was mistaken. Assuming travis passes I think this is good to go!