Skip to content

Commit c20226e

Browse files
author
Gökcan Değirmenci
committed
Introduce fallback loader prop with v2
1 parent 453e491 commit c20226e

File tree

3 files changed

+56
-36
lines changed

3 files changed

+56
-36
lines changed

README.md

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,47 @@
3333
npm i react-shimmer
3434
```
3535

36+
or
37+
38+
```bash
39+
yarn add react-shimmer
40+
```
41+
3642
## Usage
3743

3844
```jsx
39-
import React, { Component } from 'react'
40-
45+
import React from 'react'
4146
import Image from 'react-shimmer'
4247

43-
export default class App extends Component {
44-
render () {
45-
return (
46-
<div>
47-
<Image
48-
src={'https://example.com/test.jpg'}
49-
width={120} height={120}
50-
style={{objectFit: 'cover'}} // Style your <img>
51-
delay={25}
52-
duration={0.9} // Customize the animation duration (s).
53-
/>
54-
</div>
55-
)
56-
}
48+
function App(props) {
49+
return (
50+
<div>
51+
<Image
52+
src="https://example.com/test.jpg"
53+
width={640} height={480}
54+
style={{ objectFit: 'cover' }}
55+
/>
56+
</div>
57+
)
58+
}
59+
```
60+
61+
or you can use the `fallback` prop:
62+
63+
```jsx
64+
import React from 'react'
65+
import Image from 'react-shimmer'
66+
import Spinner from './Spinner'
67+
68+
function App(props) {
69+
return (
70+
<div>
71+
<Image
72+
src="https://example.com/test.jpg"
73+
fallback={<Spinner />}
74+
/>
75+
</div>
76+
)
5777
}
5878
```
5979

@@ -63,19 +83,20 @@ Property | Type | Required | Default value | Description
6383
:--- | :--- | :--- | :--- | :---
6484
`src`|string|yes||
6585
`color`|string|no|`#f6f7f8`| Background color of the loader.
66-
`duration`|number|no|`1.6`| Animation duration (s) Higher value == slower animation.
67-
`width`|number|yes||
68-
`height`|number|yes||
86+
`duration`|number|no|`1600`| Animation duration (ms) Higher value == slower animation.
87+
`width`|number|yes (no if `fallback` is present)||
88+
`height`|number|yes (no if `fallback` is present)||
6989
`style`|object|no||
7090
`onError`|func|no||
7191
`onLoad`|func|no||
72-
`loadingIndicatorSource`|string|no||
92+
`fallback`|React.Element|no||
7393
`delay`|number|no|| Delay the starting time of the animation. (ms)
7494
-----
7595

7696
## Contributing
7797
---
78-
Feel free to send PRs.
98+
99+
Feel free to send PRs.
79100

80101
## License
81102

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
{
22
"name": "react-shimmer",
3-
"version": "1.2.1",
3+
"version": "2.0.0",
44
"description": "A shared Image placeholder component that has a loading shimmer effect.",
55
"author": "gokcan",
66
"keywords": [
77
"react",
8-
"react-component",
98
"reactjs",
109
"loader",
1110
"loading-indicator",

src/web/index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// @flow
22
/**
33
* @class ShimmerImage
4-
* @version 1.2.0
4+
* @version 2.0.0
55
* @author github.com/gokcan
66
*/
77

8-
import React, { Component } from 'react'
8+
import * as React from 'react'
99
import * as PropTypes from 'prop-types'
1010
import cl from './styles.css'
1111
import 'regenerator-runtime/runtime'
@@ -21,7 +21,7 @@ type Props = {
2121
style?: Object,
2222
onError?: (err: string) => void,
2323
onLoad?: (image: Image) => void,
24-
loadingIndicatorSource?: string,
24+
fallback?: React.Element<*>,
2525
delay?: number,
2626
}
2727

@@ -31,17 +31,17 @@ type State = {
3131
error?: string
3232
}
3333

34-
export default class ShimmerImage extends Component<Props, State> {
34+
export default class ShimmerImage extends React.Component<Props, State> {
3535
static propTypes = {
3636
src: PropTypes.string.isRequired,
3737
color: PropTypes.string,
3838
duration: PropTypes.number,
39-
width: PropTypes.number.isRequired,
40-
height: PropTypes.number.isRequired,
39+
width: PropTypes.number,
40+
height: PropTypes.number,
4141
style: PropTypes.object,
4242
onError: PropTypes.func,
4343
onLoad: PropTypes.func,
44-
loadingIndicatorSource: PropTypes.string,
44+
fallback: PropTypes.element,
4545
delay: PropTypes.number
4646
}
4747

@@ -75,8 +75,8 @@ export default class ShimmerImage extends Component<Props, State> {
7575
}
7676

7777
startImageLoadingProcess = async () => {
78-
const { src, delay, width, height } = this.props
79-
if (!(width && height)) {
78+
const { src, delay, width, height, fallback } = this.props
79+
if (!fallback && !(width & height)) {
8080
this.setState({ error: 'Height and Width props must be provided!' })
8181
}
8282
/*
@@ -147,7 +147,7 @@ export default class ShimmerImage extends Component<Props, State> {
147147

148148
render() {
149149
const { src, error, isLoading } = this.state
150-
const { width, height, color, duration, style, onError, loadingIndicatorSource, ...passed } = this.props
150+
const { width, height, color, duration, style, onError, fallback, ...passed } = this.props
151151
const { ...passedStyles } = style
152152
const passedProps = { ...passed, ...{ src, width, height } }
153153
const backgroundSize = `${width * 10}px ${height}px`
@@ -156,15 +156,15 @@ export default class ShimmerImage extends Component<Props, State> {
156156
backgroundColor: color,
157157
backgroundSize,
158158
// $FlowFixMe
159-
animationDuration: `${duration}s`
159+
animationDuration: `${(duration / 1000).toFixed(1)}s`
160160
}
161161

162162
if (error) {
163163
// $FlowFixMe
164164
return onError ? onError(error) : null
165165
} else if (isLoading) {
166-
if (loadingIndicatorSource) {
167-
return <img src={loadingIndicatorSource} />
166+
if (fallback) {
167+
return fallback
168168
} else {
169169
return (
170170
<div className={cl.shimmerdiv} style={{ ...passedStyles }}>

0 commit comments

Comments
 (0)