Skip to content

Commit ff7ac7c

Browse files
Merge pull request #67 from eddiemcleanlux/slide-alignment
Add slide alignment prop
2 parents 774a68c + 51aee4c commit ff7ac7c

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ render(
2929
Running test page:
3030
----------------
3131

32-
Clone the repository, run `npm i` and then run `npm start` and point your browser to
33-
`localhost:8080/webpack-dev-server/`
32+
Clone the repository, run `npm i` and then run `npm run storybook`. The Storybook should open in your browser automatically.
3433

3534
Available props:
3635
----------------
@@ -112,6 +111,11 @@ Used to specify a fixed width for all slides. Without specifying this, slides wi
112111
Used to specify a fixed height for all slides. Without specifying this, slides will simply be the height of their
113112
content.
114113

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

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,
@@ -134,7 +136,7 @@ export default class Carousel extends Component {
134136
}
135137

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

@@ -143,7 +145,8 @@ export default class Carousel extends Component {
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)