Skip to content

Commit db22b1f

Browse files
author
Carlos Paelinck
authored
Added support for a slide onActive function prop (#395)
1 parent 4de1b2d commit db22b1f

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ The slide tag represents each slide in the presentation. Giving a slide tag an `
345345
|maxHeight| PropTypes.number | Used to set max dimensions of the Slide.
346346
|maxWidth| PropTypes.number | Used to set max dimensions of the Slide.
347347
|notes| PropTypes.string| Text which will appear in the presenter mode. Can be HTML.
348+
|onActive|PropTypes.func| Optional function that is called with the slide index when the slide comes into view.
348349
|transition|PropTypes.array|Accepts `slide`, `zoom`, `fade`, `spin`, or a [function](#transition-function), and can be combined. Sets the slide transition. This will affect both enter and exit transitions. **Note: If you use the 'scale' transition, fitted text won't work in Safari.**|
349350
|transitionIn|PropTypes.array|Specifies the slide transition when the slide comes into view. Accepts the same values as transition.|
350351
|transitionOut|PropTypes.array|Specifies the slide transition when the slide exits. Accepts the same values as transition.|

example/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export default class Presentation extends React.Component {
5858
<Text textSize="1.5em" margin="20px 0px 0px" bold>Hit Your Right Arrow To Begin!</Text>
5959
</Slide>
6060
<Slide
61+
onActive={slideIndex => {
62+
console.info(`Viewing slide index: ${slideIndex}.`); // eslint-disable-line no-console
63+
}}
6164
id="wait-what"
6265
goTo={4}
6366
transition={[

src/components/slide.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class Slide extends React.PureComponent {
4949
}
5050
window.addEventListener('load', this.setZoom);
5151
window.addEventListener('resize', this.setZoom);
52+
53+
if (isFunction(this.props.onActive)) {
54+
this.props.onActive(this.props.slideIndex);
55+
}
5256
}
5357

5458
componentDidUpdate() {
@@ -253,6 +257,7 @@ Slide.propTypes = {
253257
lastSlideIndex: PropTypes.number,
254258
margin: PropTypes.number,
255259
notes: PropTypes.any,
260+
onActive: PropTypes.func,
256261
presenterStyle: PropTypes.object,
257262
print: PropTypes.bool,
258263
slideIndex: PropTypes.number,

src/components/slide.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,16 @@ describe('<Slide />', () => {
6363
wrapper.setState({ reverse: true });
6464
expect(wrapper.instance().getTransitionKeys()).toEqual(['fade']);
6565
});
66+
67+
test('should call optional callback when slide becomes active', () => {
68+
const spy = jest.fn();
69+
mount(
70+
<Slide onActive={spy} slideIndex={5}>
71+
<div>Slide Content</div>
72+
</Slide>,
73+
{ context: _mockContext() }
74+
);
75+
expect(spy).toHaveBeenCalledTimes(1);
76+
expect(spy).toBeCalledWith(5);
77+
});
6678
});

0 commit comments

Comments
 (0)