Skip to content

Commit 69f4d5e

Browse files
Add slide alignment prop
1 parent 774a68c commit 69f4d5e

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ Used to specify a fixed width for all slides. Without specifying this, slides wi
112112
Used to specify a fixed height for all slides. Without specifying this, slides will simply be the height of their
113113
content.
114114

115+
#### slideAlignment
116+
`PropTypes.oneOf(['left', 'center', 'right'])`
117+
118+
Used to set the alignment of the currently selected slide in the carousel's viewport. Defaults to `center`.
119+
115120
#### beforeChange
116121
`PropTypes.func`
117122

src/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export default class Carousel extends Component {
4646
cellPadding: PropTypes.number,
4747
slideWidth: PropTypes.string,
4848
slideHeight: PropTypes.string,
49+
slideAlignment: PropTypes.oneOf(['left', 'center', 'right']),
4950
beforeChange: PropTypes.func,
5051
afterChange: PropTypes.func,
5152
transitionDuration: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
@@ -92,6 +93,7 @@ export default class Carousel extends Component {
9293
imagesToPrefetch: 5,
9394
maxRenderedSlides: 5,
9495
cellPadding: 0,
96+
slideAlignment: 'center',
9597
transitionDuration: 500,
9698
autoplay: false,
9799
autoplaySpeed: 4000,
@@ -135,15 +137,16 @@ export default class Carousel extends Component {
135137

136138
componentDidUpdate(prevProps, prevState) {
137139
const { children, autoplay, slideWidth } = this.props;
138-
const { currentSlide, loadedImages, direction, loading, slideDimensions } = this.state;
140+
const { currentSlide, loadedImages, direction, loading, slideDimensions, slideAlignment } = this.state;
139141
const oldChildren = prevProps.children;
140142

141143
if (direction !== prevState.direction ||
142144
currentSlide !== prevState.currentSlide ||
143145
loadedImages !== prevState.loadedImages ||
144146
slideWidth !== prevProps.slideWidth ||
145147
slideDimensions.width !== prevState.slideDimensions.width ||
146-
slideDimensions.height !== prevState.slideDimensions.height) {
148+
slideDimensions.height !== prevState.slideDimensions.height ||
149+
slideAlignment !== prevProps.slideAlignment) {
147150
// Whenever new images are loaded, the current slide index changes, the transition direction changes, or the
148151
// slide width changes, we need to recalculate the left offset positioning of the slides.
149152
this.calcLeftOffset();
@@ -691,7 +694,7 @@ export default class Carousel extends Component {
691694
return;
692695
}
693696

694-
const { infinite, children, cellPadding } = this.props;
697+
const { infinite, children, cellPadding, slideAlignment } = this.props;
695698
let { currentSlide } = this.state;
696699
const slides = this._track.childNodes;
697700
const numChildren = Children.count(children);
@@ -721,8 +724,13 @@ export default class Carousel extends Component {
721724
leftOffset -= currentSlideWidth;
722725
}
723726

724-
// Center the current slide within the viewport
725-
leftOffset += (viewportWidth - currentSlideWidth) / 2;
727+
// Adjust the offset to get the correct alignment of current slide within the viewport
728+
if (slideAlignment === 'center') {
729+
leftOffset += (viewportWidth - currentSlideWidth) / 2;
730+
} else if (slideAlignment === 'right') {
731+
leftOffset += (viewportWidth - currentSlideWidth);
732+
}
733+
726734
const shouldRetry = foundZeroWidthSlide && retryCount < MAX_LOAD_RETRIES;
727735

728736
if (leftOffset !== this.state.leftOffset) {

src/stories/index.stories.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,13 @@ export const addImages = () => {
227227
</Fragment>
228228
);
229229
};
230+
231+
export const leftAlignedSlides = () =>
232+
<Carousel width='450px' cellPadding={ 5 } slideAlignment='left'>
233+
{ imgElements }
234+
</Carousel>;
235+
236+
export const rightAlignedSlides = () =>
237+
<Carousel width='450px' cellPadding={ 5 } slideAlignment='right'>
238+
{ imgElements }
239+
</Carousel>;

0 commit comments

Comments
 (0)