Skip to content

Commit

Permalink
feat: remove pipe rules when node is unmounted
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 27, 2018
1 parent f2a9087 commit d278cec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .storybook/ref.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ class RefExample extends Component {
super(props);
this.state = {
color: 'red',
show: true
};
}

render () {
var data = nano.ref({'&': {color: this.state.color}, '&:hover': {color: 'blue'}});

return h('div', {},
h('div', data, 'Hello world'),
this.state.show && h('div', data, 'Hello world'),

h('br'),

h('button', {onClick: () => this.setState({color: 'red'})}, 'red'),
h('button', {onClick: () => this.setState({color: 'blue'})}, 'blue'),
h('button', {onClick: () => this.setState({show: !this.state.show})}, 'show/hide'),
);
}
}
Expand Down
23 changes: 23 additions & 0 deletions addon/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ exports.addon = function (renderer) {
if (!pipe) {
el[sNano] = pipe = renderer.pipe();
el.setAttribute(pipe.attr, '');

// Add unmount logic

var observer = new MutationObserver(function (mutations) {
for (var i = 0; i < mutations.length; i++) {
var mutation = mutations[i];

if (mutation.removedNodes.length) {
var nodes = mutation.removedNodes;

for (var j = 0; j < nodes.length; j++) {
if (nodes[j] === el) {
pipe.remove();
delete el[sNano];
observer.disconnect();
return;
}
}
}
}
});

observer.observe(el.parentNode, {childList: true});
}

pipe.css(css);
Expand Down

0 comments on commit d278cec

Please sign in to comment.