Skip to content

Instantly share code, notes, and snippets.

@SebastianHGonzalez
Last active January 3, 2020 19:19
Show Gist options
  • Save SebastianHGonzalez/6dfeaa2973891361d7d019c0a07e3d6b to your computer and use it in GitHub Desktop.
Save SebastianHGonzalez/6dfeaa2973891361d7d019c0a07e3d6b to your computer and use it in GitHub Desktop.
Carousel component with seamless infinite scroll
import React from 'react';
import styled from 'styled-components';
export const ChevronShadowPath = styled.path.attrs({ d: 'M4,70l-3.8,0L34,36L0,1.8h3.8L38,36L4,70z' })`
fill: white;
`;
export const ChevronPath = styled.path.attrs({ d: 'M6.7,72l-4.7,0l35.8-36L2.1,0h4.7l35.9,36L6.7,72z' })``;
export default styled.svg.attrs({
height: '100%',
width: '100%',
viewBox: '0 0 42.7 72',
xmlns: 'http://www.w3.org/2000/svg',
children: [
<ChevronPath />,
<ChevronShadowPath />,
],
})``;
import React from 'react';
import { number, func, elementType, string } from 'prop-types';
import styled from 'styled-components';
export const DefaultStepperWrapper = styled.ul`
display: flex;
justify-content: center;
font-size: 18px;
line-height: 1;
text-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.19);
list-style: none;
padding: 0;
margin: 0;
`;
const Button = styled.button.attrs({ type: 'button', tabIndex: -1 })`
margin: 0;
padding: 0;
border: 0;
cursor: pointer;
height: 6px;
width: 6px;
border-radius: 6px;
margin: 4px;
background-color: ${({ selected }) => (selected ? '#807F80' : '#C9C7C7')};
/* is touch screen */
@media (pointer:coarse) {
pointer-events: none;
}
`;
export const DefaultStep = styled.li.attrs(({ selected, onClick }) => ({
children: <Button selected={selected} onClick={onClick} />,
}))``;
export default function Stepper ({
className,
total,
current,
onChange,
wrapperComponent: Wrapper,
stepComponent: Step,
}) {
return (
<Wrapper className={className}>
{Array(total)
.fill()
.map((_, index) => index)
.map(step => (
<Step selected={current === step} onClick={() => onChange(step)} />
))}
</Wrapper>
);
}
function noop () {}
Stepper.propTypes = {
className: string,
total: number.isRequired,
current: number.isRequired,
onChange: func,
wrapperComponent: elementType,
stepComponent: elementType,
};
Stepper.defaultProps = {
className: undefined,
onChange: noop,
wrapperComponent: DefaultStepperWrapper,
stepComponent: DefaultStep,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment