Download wp-calypso/apps/notifications/src/panel/templates/image-loader.jsx from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.44 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/apps/notifications/src/panel/templates/image-loader.jsx
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/apps/notifications/src/panel/templates/image-loader.jsx
-
curl -L -o image-loader.jsx https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/apps/notifications/src/panel/templates/image-loader.jsx
1.44 kB
| import PropTypes from 'prop-types'; | |
| import { Component } from 'react'; | |
| /** | |
| * Module variables | |
| */ | |
| const LoadStatus = { | |
| PENDING: 'PENDING', | |
| LOADING: 'LOADING', | |
| LOADED: 'LOADED', | |
| FAILED: 'FAILED', | |
| }; | |
| const noop = () => {}; | |
| export class ImageLoader extends Component { | |
| static propTypes = { | |
| src: PropTypes.string.isRequired, | |
| placeholder: PropTypes.element.isRequired, | |
| children: PropTypes.node, | |
| }; | |
| constructor( props ) { | |
| super( props ); | |
| this.image = new Image(); | |
| this.image.src = props.src; | |
| this.image.onload = this.onLoadComplete; | |
| this.image.onerror = this.onLoadComplete; | |
| this.state = { | |
| status: LoadStatus.LOADING, | |
| }; | |
| } | |
| componentWillUnmount() { | |
| this.destroyLoader(); | |
| } | |
| destroyLoader = () => { | |
| if ( ! this.image ) { | |
| return; | |
| } | |
| this.image.onload = noop; | |
| this.image.onerror = noop; | |
| delete this.image; | |
| }; | |
| onLoadComplete = ( event ) => { | |
| this.destroyLoader(); | |
| this.setState( { | |
| status: 'load' === event.type ? LoadStatus.LOADED : LoadStatus.FAILED, | |
| } ); | |
| }; | |
| render() { | |
| const { children, placeholder, src } = this.props; | |
| const { status } = this.state; | |
| return ( | |
| <div className="image-preloader"> | |
| { status === LoadStatus.LOADING && placeholder } | |
| { /* eslint-disable-next-line jsx-a11y/alt-text */ } | |
| { status === LoadStatus.LOADED && <img src={ src } /> } | |
| { status === LoadStatus.FAILED && children } | |
| </div> | |
| ); | |
| } | |
| } | |
| export default ImageLoader; | |