Skip to content
This repository was archived by the owner on Jan 19, 2023. It is now read-only.

Commit e0448c5

Browse files
committed
Add the ability to display image captions
Add the optional imageCaption property of type node. Use exactly the same way as imageTitle propery. Use 'caption' and 'captionContent' classes for default style. Style wrapper with 'caption' and 'ril-caption' classes. Style content with 'caption-content' and 'ril-caption-content' classes.
1 parent 437c904 commit e0448c5

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ animationOnKeyInput | bool | `false` | | Disable animation on
9696
animationDuration | number | `300` | | Animation duration (ms)
9797
keyRepeatLimit | number | `180` | | Required interval of time (ms) between key actions (prevents excessively fast navigation of images)
9898
keyRepeatKeyupBonus | number | `40` | | Amount of time (ms) restored after each keyup (makes rapid key presses slightly faster than holding down the key to navigate images)
99-
imageTitle | string | | | Image title
99+
imageTitle | node | | | Image title
100+
imageCaption | node | | | Image caption - a small piece of text that describes the image
100101
toolbarButtons | node[] | | | Array of custom toolbar buttons
101102
reactModalStyle | Object | `{}` | | Set z-index style, etc., for the parent react-modal (format: https://github.com/reactjs/react-modal#styles )
102103
imagePadding | number | `10` | | Padding (px) between the edge of the window and the lightbox

src/examples/cats/app.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,33 @@ const titles = [
6161
</span>),
6262
];
6363

64+
const captions = [
65+
'',
66+
'Sad kitten wants a hug',
67+
(<p>
68+
.. not in the&nbsp;
69+
<em>
70+
mood
71+
</em>
72+
&nbsp;for games right now
73+
</p>),
74+
(<p>
75+
Meow!? What do you mean by «Earth is&nbsp;
76+
<strong>
77+
round
78+
</strong>
79+
»? Are you&nbsp;
80+
<em>
81+
absolutely
82+
</em>
83+
&nbsp;
84+
<strong>
85+
sure
86+
</strong>
87+
&nbsp;you know what you&#39;re talking about?
88+
</p>),
89+
];
90+
6491
const App = React.createClass({
6592
getInitialState() {
6693
return {
@@ -98,6 +125,7 @@ const App = React.createClass({
98125
onMoveNextRequest={this.moveNext}
99126

100127
imageTitle={titles[this.state.index]}
128+
imageCaption={captions[this.state.index]}
101129
/>
102130
);
103131
}

src/react-image-lightbox.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ if (_ieVersion < 10) {
3636
};
3737
}
3838

39+
function handleCaptionMousewheel(event) {
40+
event.stopPropagation();
41+
}
42+
43+
function handleCaptionMouseDown(event) {
44+
event.stopPropagation();
45+
}
46+
3947
class ReactImageLightbox extends Component {
4048
constructor(props) {
4149
super(props);
@@ -84,6 +92,8 @@ class ReactImageLightbox extends Component {
8492
this.requestClose = this.requestClose.bind(this);
8593
this.requestMoveNext = this.requestMoveNext.bind(this);
8694
this.requestMovePrev = this.requestMovePrev.bind(this);
95+
this.handleCaptionMousewheel = handleCaptionMousewheel;
96+
this.handleCaptionMouseDown = handleCaptionMouseDown;
8797
}
8898

8999
componentWillMount() {
@@ -1147,6 +1157,21 @@ class ReactImageLightbox extends Component {
11471157
</li>
11481158
</ul>
11491159
</div>
1160+
1161+
{!this.props.imageCaption ? '' :
1162+
<div // Image caption
1163+
onWheel={this.handleCaptionMousewheel}
1164+
onMouseDown={this.handleCaptionMouseDown}
1165+
className={`ril-caption ${styles.caption}`}
1166+
>
1167+
<div
1168+
className={`ril-caption-content ${styles.captionContent}`}
1169+
>
1170+
{this.props.imageCaption}
1171+
</div>
1172+
</div>
1173+
}
1174+
11501175
</div>
11511176
</Modal>
11521177
);
@@ -1239,6 +1264,9 @@ ReactImageLightbox.propTypes = {
12391264
// Image title
12401265
imageTitle: PropTypes.node,
12411266

1267+
// Image caption
1268+
imageCaption: PropTypes.node,
1269+
12421270
//-----------------------------
12431271
// Lightbox style
12441272
//-----------------------------

src/style.scss

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
$toolbarHeight: 50px;
2+
$maxCaptionHeight: 150px;
23

34
@keyframes closeWindow {
45
0% { opacity: 1; }
@@ -94,17 +95,32 @@ $toolbarHeight: 50px;
9495
background-size: cover;
9596
}
9697

98+
.caption,
9799
.toolbar {
98100
background-color: rgba(0, 0, 0, 0.5);
99101
position: absolute;
100102
left: 0;
101-
top: 0;
102103
right: 0;
103-
height: $toolbarHeight;
104104
display: flex;
105105
justify-content: space-between;
106106
}
107107

108+
.caption {
109+
bottom: 0;
110+
max-height: $maxCaptionHeight;
111+
overflow: auto;
112+
113+
.captionContent {
114+
padding: 10px 20px;
115+
color: #FFF;
116+
}
117+
}
118+
119+
.toolbar {
120+
top: 0;
121+
height: $toolbarHeight;
122+
}
123+
108124
.toolbarSide {
109125
height: $toolbarHeight;
110126
margin: 0;

0 commit comments

Comments
 (0)