Skip to content
This repository was archived by the owner on Oct 19, 2021. It is now read-only.

Commit c56a6ca

Browse files
committed
Ensure attached transitions trigger properly
1 parent a29b09e commit c56a6ca

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

Diff for: dist/inline-transitions.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ module.exports = function (_ref) {
2323
var addTransitionState = _ref.addTransitionState;
2424
var removeTransitionState = _ref.removeTransitionState;
2525

26+
var attached = function attached(element) {
27+
if (element.attached) {
28+
return element.attached(element, element);
29+
}
30+
};
31+
2632
// Monitors whenever an element changes an attribute, if the attribute
2733
// is a valid state name, add this element into the related Set.
2834
var attributeChanged = function attributeChanged(element, name, oldVal, newVal) {
@@ -41,6 +47,7 @@ module.exports = function (_ref) {
4147
// This will unbind any internally bound transition states.
4248
var unsubscribe = function unsubscribe() {
4349
// Unbind all the transition states.
50+
removeTransitionState('attached', attached);
4451
removeTransitionState('attributeChanged', attributeChanged);
4552

4653
// Remove all elements from the internal cache.
@@ -63,6 +70,7 @@ module.exports = function (_ref) {
6370

6471
// Set a "global" `attributeChanged` to monitor all elements for transition
6572
// states being attached.
73+
addTransitionState('attached', attached);
6674
addTransitionState('attributeChanged', attributeChanged);
6775

6876
// Add a transition for every type.
@@ -81,7 +89,10 @@ module.exports = function (_ref) {
8189
// If the child element triggered in the transition is the root element,
8290
// this is an easy lookup for the handler.
8391
else if (map.has(child)) {
84-
return map.get(child).apply(child, [child].concat(rest));
92+
// Attached is handled special by the separate global attached handler.
93+
if (name !== 'attached') {
94+
return map.get(child).apply(child, [child].concat(rest));
95+
}
8596
}
8697
// The last resort is looping through all the registered elements to see
8798
// if the child is contained within. If so, it aggregates all the valid

Diff for: index.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ const boundHandlers = [];
1515
* nested children.
1616
*/
1717
module.exports = function({ addTransitionState, removeTransitionState }) {
18+
const attached = function(element) {
19+
if (element.attached) {
20+
return element.attached(element, element);
21+
}
22+
};
23+
1824
// Monitors whenever an element changes an attribute, if the attribute
1925
// is a valid state name, add this element into the related Set.
2026
const attributeChanged = function(element, name, oldVal, newVal) {
@@ -33,6 +39,7 @@ module.exports = function({ addTransitionState, removeTransitionState }) {
3339
// This will unbind any internally bound transition states.
3440
const unsubscribe = () => {
3541
// Unbind all the transition states.
42+
removeTransitionState('attached', attached);
3643
removeTransitionState('attributeChanged', attributeChanged);
3744

3845
// Remove all elements from the internal cache.
@@ -55,6 +62,7 @@ module.exports = function({ addTransitionState, removeTransitionState }) {
5562

5663
// Set a "global" `attributeChanged` to monitor all elements for transition
5764
// states being attached.
65+
addTransitionState('attached', attached);
5866
addTransitionState('attributeChanged', attributeChanged);
5967

6068
// Add a transition for every type.
@@ -69,7 +77,10 @@ module.exports = function({ addTransitionState, removeTransitionState }) {
6977
// If the child element triggered in the transition is the root element,
7078
// this is an easy lookup for the handler.
7179
else if (map.has(child)) {
72-
return map.get(child).apply(child, [child].concat(rest));
80+
// Attached is handled special by the separate global attached handler.
81+
if (name !== 'attached') {
82+
return map.get(child).apply(child, [child].concat(rest));
83+
}
7384
}
7485
// The last resort is looping through all the registered elements to see
7586
// if the child is contained within. If so, it aggregates all the valid

Diff for: test/basics.js

+10
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,14 @@ describe('Basics', function() {
6666
done();
6767
}, 20);
6868
});
69+
70+
it('supports detached transitions on the root element', (done) => {
71+
const detached = el => {
72+
assert.equal(el.nodeName, 'p')
73+
done();
74+
};
75+
76+
innerHTML(this.fixture, html`<div><p detached=${detached}/></div>`);
77+
innerHTML(this.fixture, html`<div></div>`);
78+
});
6979
});

Diff for: test/regressions.js

+5
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ describe('Regressions', function() {
1515
});
1616

1717
it('does not error when non-Promises are returned', () => {
18+
var count = 0;
19+
1820
const attached = el => {
21+
count++;
1922
return undefined;
2023
};
2124

2225
innerHTML(this.fixture, html`<div attached=${attached}>
2326
<div></div>
2427
</div>`);
28+
29+
assert.equal(count, 2);
2530
});
2631
});

0 commit comments

Comments
 (0)