adobe / react-webcomponent

This projects automates the wrapping of a React component in a CustomElement.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DOM content retrieved by @byContent still renders with the react component mounted

visomar opened this issue · comments

Expected Behaviour

As stated in the documentation:

The DOM content will be moved when the React component is mounted. And, the content will be put back in its original location if the React component is later unmounted.

Actual Behaviour

After some tests, if the property holding the content (inside the react component) is not used, even if the react component is still mounted, the DOM content is still put back in its original location.

Sample Code that illustrates the problem

React component:
import React from 'react';
import PropTypes from 'prop-types';

const Example = ({
  // products,
  title,
  content,
}) => (
  <div className="example" style={{ backgroundColor: 'lightgrey' }}>
    { title === 'yepe' && content }
    { title !== 'yepe' && <p>Not the title I expected</p> }
  </div>
);

Example.propTypes = {
  // products: PropTypes.arrayOf(PropTypes.object).isRequired,
  title: PropTypes.string,
  content: PropTypes.oneOfType([
    PropTypes.arrayOf(PropTypes.node),
    PropTypes.node,
  ]).isRequired,
};

Example.defaultProps = {
  title: 'no',
};

export default Example;
React container:
import {
  DOMModel,
  byAttrVal,
  byContent,
} from '@adobe/react-webcomponent';
import { connect } from 'react-redux';
import Example from '../example/example';
import { action } from 'actions';

const mapStateToProps = state => ({
  products: state.products,
});

const ReduxedExample = connect(
  mapStateToProps,
  { action },
)(Example);


class ExampleModel extends DOMModel {
  @byAttrVal('title') title;

  @byContent('.content') content;
}

// Register web component "example-component"
// using above Model
});
Html example:
<example-component title="yepe">
    <div class="content">
        This is the correct title!
    </div>
</example-component>

2018-12-19 13_15_51-example template localhost

Other html example:
<example-component title="other title">
    <div class="content">
        This is the correct title!
    </div>
</example-component>

2018-12-19 13_16_42-example template localhost

There is the workaround of controlling the content visibility (class hierarchy, styling), but the behaviour should match the description, IMO.