Skip to content
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

Create the property holdPlaceholder to keep the placeholder for some … #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ color: PropTypes.string,
customPlaceholder: PropTypes.oneOfType([
PropTypes.node,
PropTypes.element
])
]),
holdPlaceholder: PropTypes.number,
```

### Customization
Expand Down Expand Up @@ -79,6 +80,9 @@ You can pass an optional `delay` prop which specifies the time (in milliseconds)

Note that this delay will __not__ affect the initial render, only subsequent "ready" state changes. Setting the `firsLaunchOnly` prop to `true` will also disable this feature.

### Hold Placeholder
You can pass an optional `holdPlaceholder` prop which specifies the time (in milliseconds) `react-placeholder` should keep displaying the placeholder before displaying the content element even if the `ready` prop is `true`. This is useful if you want to keep displaying the placeholder for slower connections while avoiding a brief "flash" on faster connections.

### Animation
`react-placeholder` already comes with one default pulse animation to better tell the user that the page is loading.
The animation is defined in a separate CSS file so, in order to enable it, you should import that style in your project like this:
Expand Down
31 changes: 30 additions & 1 deletion examples/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class Example extends React.Component {
readyCustom: false,
readyFirstLaunch: false,
textBlockRows: 6,
mediaBlockRows: 4
mediaBlockRows: 4,
holdPlaceholder: 2000
};

onChange = (key) => ({ target: { value } }) => {
Expand Down Expand Up @@ -176,6 +177,34 @@ class Example extends React.Component {
</ReactPlaceholder>
</div>

<h1>Using ReactPlaceholder with a hold placeholder time</h1>
<button onClick={this.toggleReadyFirstLaunch} style={buttonStyle}>
{this.state.readyFirstLaunch ? 'set loading' : 'set ready'}
</button>
<p>Will show the content only after specified time and it's ready</p>
<div className='ui input'>
<span style={{ lineHeight: '40px' }}>
holdPlaceholder (ms):
</span>
<input
type='number'
value={this.state.holdPlaceholder}
onChange={this.onChange('holdPlaceholder')}
style={{ width: 80, marginLeft: 5 }}
/>
</div>
<div className='ui segment'>
<ReactPlaceholder
ready={this.state.readyFirstLaunch}
holdPlaceholder={this.state.holdPlaceholder}
rows={4}
color='#E0E0E0'
className='my-text-block'
>
{realComponent}
</ReactPlaceholder>
</div>

<h1>Using ReactPlaceholder with a delay</h1>
<button onClick={this.toggleReadyFirstLaunch} style={buttonStyle}>
{this.state.readyFirstLaunch ? 'set loading' : 'set ready'}
Expand Down
31 changes: 24 additions & 7 deletions src/ReactPlaceholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,25 @@ export default class ReactPlaceholder extends React.Component {
customPlaceholder: PropTypes.oneOfType([
PropTypes.node,
PropTypes.element
])
]),
holdPlaceholder: PropTypes.number
}

static defaultProps = {
delay: 0,
type: 'text',
color: '#CDCDCD'
color: '#CDCDCD',
holdPlaceholder: 0
}

state = {
ready: this.props.ready
ready: this.props.ready,
holdPlaceholder: this.props.holdPlaceholder > 0
}

getFiller = () => {
const {
firstLaunchOnly, children, ready, className, // eslint-disable-line no-unused-vars
firstLaunchOnly, children, ready, className, holdPlaceholder, // eslint-disable-line no-unused-vars
type, customPlaceholder, showLoadingAnimation, ...rest
} = this.props;

Expand Down Expand Up @@ -80,14 +83,28 @@ export default class ReactPlaceholder extends React.Component {
}

render() {
return this.state.ready ? this.props.children : this.getFiller();
const { ready, holdPlaceholder } = this.state;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have ready, can't see the need for another variable that does the same thing

return ready && !holdPlaceholder ? this.props.children : this.getFiller();
}

componentWillReceiveProps(nextProps) {
if (!this.props.firstLaunchOnly && this.state.ready && !nextProps.ready) {
this.setNotReady();
if (!this.props.firstLaunchOnly && !nextProps.ready) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this new code over complicated and difficult to read...
Why didn't you use the two functions setReady and setNotReady already implemented?
we already have a state variable called ready to decide if we should render the placeholder or the content => I don't see the need of the new this.state.holdPlaceholder variable

if (this.state.ready) {
this.setNotReady();
}
if (this.props.holdPlaceholder) {
clearTimeout(this.holdPlaceholderTimeout);
this.setState({ holdPlaceholder: true });
this.holdPlaceholderTimeout = setTimeout(() => this.setState({ holdPlaceholder: false }), nextProps.holdPlaceholder);
}
} else if (nextProps.ready) {
this.setReady();
}
}

componentWillMount() {
if (this.props.holdPlaceholder > 0) {
this.holdPlaceholderTimeout = setTimeout(() => this.setState({ holdPlaceholder: false }), this.props.holdPlaceholder);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you always setting a timeout? shouldn't it be added only if this.state.ready (which is initialized to this.props.ready) is true?

}
}
}
3 changes: 2 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ interface ReactPlaceholderProps {
rows?: number,
color?: string,
showLoadingAnimation?: boolean,
customPlaceholder?: ReactNode | ReactElement<any>
customPlaceholder?: ReactNode | ReactElement<any>,
holdPlaceholder?: number,
}

interface ReactPlaceholderState {
Expand Down
47 changes: 47 additions & 0 deletions tests/ReactPlaceholder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,51 @@ describe('ReactPlaceholder', () => {
expect(tree.getElements()).toMatchSnapshot();
});

it('renders the placeholder for a while even if the content is ready', () => {
const content = <div>Some ready content</div>;
const tree = shallow(
<ReactPlaceholder
ready
type='text'
rows={2}
holdPlaceholder={10000}
>
{content}
</ReactPlaceholder>
);
expect(tree.contains(content)).toBe(false);
jest.runAllTimers();
tree.update();
expect(tree.contains(content)).toBe(true);
expect(tree.getElements()).toMatchSnapshot();
});

it('renders the placeholder again for a while even if the content is ready', () => {
const content = <div>Some ready content</div>;
const tree = shallow(
<ReactPlaceholder
ready
type='text'
rows={2}
holdPlaceholder={10000}
>
{content}
</ReactPlaceholder>
);
expect(tree.contains(content)).toBe(false);
jest.runAllTimers();
tree.update();
expect(tree.contains(content)).toBe(true);
tree.setProps({ ready: false });
tree.update();
expect(tree.contains(content)).toBe(false);
tree.setProps({ ready: true });
tree.update();
expect(tree.contains(content)).toBe(false);
jest.runAllTimers();
tree.update();
expect(tree.contains(content)).toBe(true);
expect(tree.getElements()).toMatchSnapshot();
});

});
16 changes: 16 additions & 0 deletions tests/__snapshots__/ReactPlaceholder.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ Array [
]
`;

exports[`ReactPlaceholder renders the placeholder again for a while even if the content is ready 1`] = `
Array [
<div>
Some ready content
</div>,
]
`;

exports[`ReactPlaceholder renders the placeholder for a while even if the content is ready 1`] = `
Array [
<div>
Some ready content
</div>,
]
`;

exports[`ReactPlaceholder renders the placeholder only after the specified delay 1`] = `
Array [
<TextBlock
Expand Down