[feature] Only animate CardMedia GIFs if hovering #2029

This commit is contained in:
Cameron Yick 2019-10-06 01:47:08 -04:00 committed by Sean Yesmunt
parent daaa337c7f
commit fa549fcb0f
2 changed files with 43 additions and 6 deletions

View file

@ -0,0 +1,34 @@
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import Freezeframe from 'freezeframe';
const FreezeframeWrapper = props => {
const imgRef = React.useRef();
const freezeframe = React.useRef();
const { src, className, options } = props;
useEffect(() => {
freezeframe.current = new Freezeframe(imgRef.current, options);
}, [options]);
return (
<div className={className}>
<img ref={imgRef} src={src} />
</div>
);
};
FreezeframeWrapper.propTypes = {
src: PropTypes.string.isRequired,
className: PropTypes.string.isRequired,
// Docs: https://github.com/ctrl-freaks/freezeframe.js/tree/master/packages/freezeframe
options: PropTypes.shape({
selector: PropTypes.string,
trigger: PropTypes.oneOf(['hover', 'click', false]),
overlay: PropTypes.boolean,
responsive: PropTypes.boolean,
}),
};
export default FreezeframeWrapper;

View file

@ -1,21 +1,24 @@
// @flow // @flow
import React from 'react'; import React from 'react';
import FreezeframeWrapper from './FreezeframeWrapper';
import Placeholder from './placeholder.png'; import Placeholder from './placeholder.png';
type Props = { type Props = {
thumbnail: ?string, // externally sourced image thumbnail: ?string, // externally sourced image
}; };
const className = 'media__thumb';
class CardMedia extends React.PureComponent<Props> { class CardMedia extends React.PureComponent<Props> {
render() { render() {
const { thumbnail } = this.props; const { thumbnail } = this.props;
return ( if (thumbnail && thumbnail.endsWith('gif')) {
<div return <FreezeframeWrapper src={thumbnail} className={className} />;
style={thumbnail ? { backgroundImage: `url('${thumbnail}')` } : { backgroundImage: `url(${Placeholder})` }} }
className="media__thumb"
/> const url = thumbnail || Placeholder;
); return <div style={{ backgroundImage: `url('${url}')` }} className={className} />;
} }
} }