Skip to content

Commit

Permalink
Merge pull request #22 from ValYouW/add-custom-overlay
Browse files Browse the repository at this point in the history
Add option to render a custom thumbnail overlay on hover
  • Loading branch information
benhowell authored Apr 22, 2017
2 parents 6fa7a55 + 235636a commit f9e62a5
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ require.js
*.bundle.js
examples/dist
.publish
.idea
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ thumbnailHeight | number | undefined | Required. Height of th
tags | array | undefined | Optional. An array of objects containing tag attributes (value and title). e.g. `{value: "foo", title: "bar"}`
isSelected | bool | undefined | Optional. The selected state of the image.
caption | string | undefined | Optional. Image caption.
srcset | array | undefined | Optional. Array of srcsets for lightbox.

srcset | array | undefined | Optional. Array of srcsets for lightbox.
customOverlay | element | undefined | Optional. A custom element to be rendered as tile overlay when hovering.

## Gallery Options

Expand Down
129 changes: 129 additions & 0 deletions examples/demo4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import React from 'react';
import ReactDOM from 'react-dom';
import Gallery from '../src/Gallery';


class Demo4 extends React.Component {
constructor(props){
super(props);

this.state = {
images: this.props.images
};

}

render () {
var captionStyle = {
backgroundColor: 'rgba(0, 0, 0, 0.8)',
maxHeight: '240px',
overflow: 'auto',
position: 'absolute',
bottom: '0',
width: '100%',
color: 'white',
padding: '3px',
fontSize: '0.8em'
};

var imageTitleStyle = {
fontSize: '1.5em'
};

var chipStyle = {
display: 'inline-block',
backgroundColor: '#fff',
height: 'auto',
lineHeight: 'inherit',
padding: '2px 2px',
borderRadius: '2px',
color: 'black',
marginRight: '3px'
};

var images = this.state.images.map(i => {
i.customOverlay = (
<div style={captionStyle}>
<div style={imageTitleStyle}>
<span>{i.name}</span>
</div>
<div>
by: {i.artist}
</div>
<p>{i.caption}</p>
<div className="tags">
{i.tags ? i.tags.map(t => {
return (<div key={t.value} style={chipStyle}>{t.title}</div>);
}) : ''}
</div>
</div>
);
i.tags = [];
return i;
});

return (
<div style={{
display: "block",
minHeight: "1px",
width: "100%",
border: "1px solid #ddd",
overflow: "auto"}}>
<Gallery
images={images}
rowHeight={240}
enableImageSelection={false}/>
</div>
);
}
}

Demo4.propTypes = {
images: React.PropTypes.arrayOf(
React.PropTypes.shape({
src: React.PropTypes.string.isRequired,
thumbnail: React.PropTypes.string.isRequired,
srcset: React.PropTypes.array,
caption: React.PropTypes.string,
thumbnailWidth: React.PropTypes.number.isRequired,
thumbnailHeight: React.PropTypes.number.isRequired
})
).isRequired
};

Demo4.defaultProps = {
images: shuffleArray([
{
src: "https://c2.staticflickr.com/9/8817/28973449265_07e3aa5d2e_b.jpg",
thumbnail: "https://c2.staticflickr.com/9/8817/28973449265_07e3aa5d2e_n.jpg",
thumbnailWidth: 320,
thumbnailHeight: 174,
tags: [{value: "Nature", title: "Nature"}, {value: "Flora", title: "Flora"}],
name: 'After Rain',
caption: "After Rain (Jeshu John - designerspics.com)",
artist: 'Jeshu John'
},
{
src: "https://c5.staticflickr.com/9/8768/28941110956_b05ab588c1_b.jpg",
thumbnail: "https://c5.staticflickr.com/9/8768/28941110956_b05ab588c1_n.jpg",
thumbnailWidth: 240,
thumbnailHeight: 320,
tags: [{value: "Nature", title: "Nature"}, {value: "Water", title: "Water"}],
name: '8H',
caption: "8H (gratisography.com)",
artist: 'gratisography.com'
},
{
src: "https://c7.staticflickr.com/9/8569/28941134686_d57273d933_b.jpg",
thumbnail: "https://c7.staticflickr.com/9/8569/28941134686_d57273d933_n.jpg",
thumbnailWidth: 320,
thumbnailHeight: 148,
tags: [{value: "People", title: "People"}],
name: '315H',
caption: "315H (gratisography.com)",
artist: 'gratisography.com'
}
])
};

ReactDOM.render(<Demo4 />, document.getElementById('demo4'));
8 changes: 8 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ <h2><a id="user-content-image-options" class="anchor" href="#image-options" aria
<td align="left">undefined</td>
<td align="left">Optional. Array of srcsets for lightbox.</td>
</tr>
<tr>
<td align="left">customOverlay</td>
<td align="left">element</td>
<td align="left">undefined</td>
<td align="left">Optional. A custom element to be rendered as tile overlay when hovering.</td>
</tr>

</tbody></table>

<h2><a id="user-content-gallery-options" class="anchor" href="#gallery-options" aria-hidden="true"></a>Gallery Options</h2>
Expand Down Expand Up @@ -380,6 +387,7 @@ <h4><a href="https://github.com/benhowell/react-grid-gallery/blob/master/example




<footer class="site-footer">
<span class="site-footer-owner"><a href="https://github.com/benhowell/react-grid-gallery">react-grid-gallery</a> is maintained by <a href="https://github.com/benhowell">Ben Howell</a>.</span>

Expand Down
3 changes: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ gulp.task('browserify', function() {
browserify(['./examples/app.js',
'./examples/demo1.js',
'./examples/demo2.js',
'./examples/demo3.js'], {
'./examples/demo3.js',
'./examples/demo4.js'], {
extensions: ['.js', '.jsx']
})
//)
Expand Down
15 changes: 14 additions & 1 deletion src/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ class Image extends Component {
</div>;
});

var customOverlay = typeof this.props.item.customOverlay === 'undefined' ? <noscript/> :
<div style={{
pointerEvents: "none",
opacity: this.state.hover ? 1 : 0,
position: "absolute",
height: "100%",
width: "100%"}}>
{this.props.item.customOverlay}
</div>;

return (
<div className="tile"
key={"tile-"+this.props.index}
Expand Down Expand Up @@ -127,6 +137,8 @@ class Image extends Component {
{tags}
</div>

{customOverlay}

<div className="tile-overlay"
key={"tile-overlay-"+this.props.index}
style={{
Expand Down Expand Up @@ -165,7 +177,8 @@ Image.propTypes = {
height: PropTypes.number,
isSelectable: PropTypes.bool,
onClick: PropTypes.func,
onSelectImage: PropTypes.func
onSelectImage: PropTypes.func,
customOverlay: PropTypes.element
};

Image.defaultProps = {
Expand Down

0 comments on commit f9e62a5

Please sign in to comment.