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

let user pass extra attributes #16

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ yarn add react-worker-image

## Usage

`react-worker-image` exports one react component which takes `src` as a prop, and an optional prop of `placeholder`, `style` and `imageClass` which are applied to the img tag.
`react-worker-image` exports one react component which takes `src` as a prop, and an optional prop of `placeholder`.

```js
const ImageWorker = require('react-worker-image').default;
Expand All @@ -37,11 +37,8 @@ usage in code
| ------------- |:-------------:| --------------:|
| src | yes | string |
| placeholder | optional | string or Component|
| style | optional | Object |
|imageClass | optional | string
|containerClass | optional | string

The above props are applied to the img tag.
Any other attribute that is passed to the component will be applied to the img tag.

Found a bug file them [here](https://github.com/nitish24p/react-worker-image/issues).

Expand Down
71 changes: 36 additions & 35 deletions lib/ImageWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,33 @@ const webWorkerScript = `
})
`;


type ImageWorkerProps = {
src: string,
placeholder?: string | Function,
style?: Object,
imageClass?: string,
containerClass?: string,
}
src: string
};

type ImageWorkerState = {
isLoading: boolean,
imgSrc: string
}


const wrappedComponent = WrappedComponent => props => {
return <WrappedComponent {...props} />;
imgSrc: string,
isLoading: boolean
};

class ImageWorker extends Component<ImageWorkerProps, ImageWorkerState> {
image: HTMLImageElement;
worker = new Worker(URL.createObjectURL(
new Blob([webWorkerScript], { type: 'application/javascript' })
))

worker = new Worker(
URL.createObjectURL(
new Blob([webWorkerScript], { type: 'application/javascript' })
)
);

state = {
isLoading: true,
imgSrc: ''
}
};

constructor(props: ImageWorkerProps) {
super(props);

this.worker.onmessage = (event: Object) => {
this.loadImage(event.data);
};
Expand All @@ -60,46 +56,51 @@ class ImageWorker extends Component<ImageWorkerProps, ImageWorkerState> {
this.image.onload = null;
this.image.onerror = null;
}

this.worker.terminate();
}

renderPlaceholder() {
const { placeholder, style } = this.props;
const { placeholder, src, ...attributes } = this.props; // eslint-disable-line no-unused-vars

if (typeof placeholder === 'function') {
const PlaceholderComponent = wrappedComponent(placeholder);
const PlaceholderComponent = placeholder;

return <PlaceholderComponent />;
} else if (typeof placeholder === 'string') {
return <img src={placeholder} style={{ ...style }} alt='placeholder' />;
} else {
return null;
return <img {...attributes} src={placeholder} />;
}

return null;
}

loadImage = (url: string) => {
const image = new Image();
this.image = image;

image.src = url;
image.decode !== undefined ? image.decode().then(this.onLoad).catch(this.onLoad) : image.onload = this.onLoad;
}
image.decode !== undefined
? image
.decode()
.then(this.onLoad)
.catch(this.onLoad)
: (image.onload = this.onLoad);
};

onLoad = () => {
this.setState({
imgSrc: this.image.src,
isLoading: false
});
}
};

render() {
const { style, imageClass, containerClass } = this.props;
return (
<div className={containerClass}>
{
this.state.isLoading ? this.renderPlaceholder() :
<img src={this.state.imgSrc}
style={{ ...style }} className={imageClass} alt='worker' />
}
</div>
const { placeholder, src, ...attributes } = this.props; // eslint-disable-line no-unused-vars

return this.state.isLoading ? (
this.renderPlaceholder()
) : (
<img {...attributes} src={this.state.imgSrc} />
);
}
}
Expand Down