Created
March 9, 2019 09:53
-
-
Save asci/ac4016cf4fda564f12feb1ff6e12c3a2 to your computer and use it in GitHub Desktop.
Image Shuffle component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
import { ControlType, PropertyControls, Size } from 'framer'; | |
interface Props extends Size { | |
images: string[]; | |
} | |
interface State { | |
index: number; | |
} | |
export class ShuffleImage extends React.Component<Props, State> { | |
constructor(props: Props) { | |
super(props); | |
this.state = { | |
index: this.generateIndex(), | |
}; | |
} | |
generateIndex() { | |
return Math.floor(Math.random() * this.props.images.length); | |
} | |
render() { | |
if (!this.props.images.length) | |
return ( | |
<div style={placeholderStyle}>Add images on property panel...</div> | |
); | |
return ( | |
<div | |
onClick={() => this.setState({ index: this.generateIndex() })} | |
style={{ | |
width: this.props.width, | |
height: this.props.height, | |
backgroundImage: `url("${this.props.images[this.state.index]}")`, | |
backgroundRepeat: 'no-repeat', | |
backgroundSize: 'contain', | |
backgroundPosition: 'center', | |
}} | |
/> | |
); | |
} | |
static propertyControls: PropertyControls = { | |
images: { | |
title: 'Image', | |
type: ControlType.Array, | |
propertyControl: { type: ControlType.Image }, | |
}, | |
}; | |
static defaultProps = { | |
images: [], | |
}; | |
} | |
const placeholderStyle: React.CSSProperties = { | |
width: '100%', | |
height: '100%', | |
display: 'flex', | |
justifyContent: 'center', | |
alignItems: 'center', | |
textAlign: 'center', | |
backgroundColor: 'rgba(0, 159, 255, 0.3)', | |
color: '#09f', | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment