Skip to content

Commit 9571b5f

Browse files
authored
Merge pull request #83 from sag1v/items-css-api
resolve #82 + public css classes for items
2 parents e072866 + 55af4f3 commit 9571b5f

File tree

10 files changed

+123
-18
lines changed

10 files changed

+123
-18
lines changed

doczrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export default {
3535
'onPrevEnd',
3636
'onResize',
3737
'slideNext / slidePrev',
38-
'goTo'
38+
'goTo',
39+
'Styling'
3940
]
4041
}
4142
],

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"babel-eslint": "^9.0.0",
6262
"concurrently": "^4.1.0",
6363
"cross-env": "^5.1.4",
64-
"docz": "^2.3.0-alpha.6",
64+
"docz": "^2.3.1",
6565
"enzyme": "^3.6.0",
6666
"enzyme-adapter-react-16": "^1.5.0",
6767
"eslint": "5.12.0",

src/docs/mdx/index.mdx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import Item from '../components/SimpleItem';
1010
# Getting started
1111

1212
## install
13-
```
13+
14+
```bash
1415
npm install react-elastic-carousel
1516
```
1617

1718
## Import
18-
```
19+
20+
```js
1921
import Carousel from 'react-elastic-carousel'
2022
```
2123

@@ -34,4 +36,11 @@ import Carousel from 'react-elastic-carousel'
3436

3537
## Props
3638

37-
<Props of={Carousel} />
39+
<Props of={Carousel} />
40+
41+
## Styling
42+
43+
Almost every element in `react-elastic-carousel` has a css class with the `rec-` prefix (rec is short React Elastic Carousel).
44+
For example: `rec-arrow` for both arrow buttons or `rec-arrow-left` just for the left one.
45+
46+
You can see a code example [here](/styling).

src/docs/mdx/styling.mdx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
name: Styling
3+
route: /styling
4+
menu: Examples
5+
---
6+
7+
import {Playground, Props } from 'docz';
8+
import Carousel from 'react-elastic-carousel';
9+
import Item from '../components/SimpleItem';
10+
import '../styles/styling.css';
11+
12+
# Styling
13+
#### Almost every element in `react-elastic-carousel` has a css class with the `rec-` prefix (rec is short React Elastic Carousel).
14+
15+
```css
16+
/* square buttons */
17+
.rec.rec-arrow {
18+
border-radius: 0;
19+
}
20+
21+
/* round buttons on hover */
22+
.rec.rec-arrow:hover {
23+
border-radius: 50%;
24+
}
25+
26+
/* hide disabled buttons */
27+
.rec.rec-arrow:disabled {
28+
visibility: hidden;
29+
}
30+
31+
/* disable default outline on focused items */
32+
/* add custom outline on focused items */
33+
.rec-carousel-item:focus {
34+
outline: none;
35+
box-shadow: inset 0 0 1px 1px lightgrey;
36+
}
37+
```
38+
39+
40+
<Playground>
41+
<div className="styling-example">
42+
<Carousel itemsToShow={2}>
43+
<Item>1</Item>
44+
<Item>2</Item>
45+
<Item>3</Item>
46+
<Item>4</Item>
47+
<Item>5</Item>
48+
<Item>6</Item>
49+
</Carousel>
50+
</div>
51+
</Playground>

src/docs/styles/styling.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* square buttons */
2+
.styling-example .rec.rec-arrow {
3+
border-radius: 0;
4+
}
5+
6+
/* round buttons on hover */
7+
.styling-example .rec.rec-arrow:hover {
8+
border-radius: 50%;
9+
}
10+
/* hide disabled buttons */
11+
.styling-example .rec.rec-arrow:disabled {
12+
visibility: hidden;
13+
}
14+
/* disable default outline on focused items */
15+
/* add custom outline on focused items */
16+
.styling-example .rec-carousel-item:focus {
17+
outline: none;
18+
box-shadow: inset 0 0 1px 1px lightgrey;
19+
}

src/react-elastic-carousel/components/ItemWrapperContainer.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@ class ItemWrapperContainer extends React.Component {
1010
onClick(id);
1111
};
1212
render() {
13-
const { child, style, itemPosition } = this.props;
13+
const { itemPosition, ...restOfProps } = this.props;
1414
return (
1515
<ItemWrapper
1616
onClick={this.onClick}
1717
className={cssPrefix("item-wrapper")}
1818
itemPosition={itemPosition}
19-
style={style}
20-
>
21-
{child}
22-
</ItemWrapper>
19+
{...restOfProps}
20+
/>
2321
);
2422
}
2523
}
@@ -31,10 +29,9 @@ ItemWrapperContainer.defaultProps = {
3129
};
3230

3331
ItemWrapperContainer.propTypes = {
34-
child: PropTypes.element.isRequired,
32+
children: PropTypes.element.isRequired,
3533
itemPosition: PropTypes.oneOf([consts.START, consts.CENTER, consts.END]),
3634
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
37-
style: PropTypes.object,
3835
onClick: PropTypes.func
3936
};
4037

src/react-elastic-carousel/components/Track.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ const Track = ({
2626
const min = currentItem;
2727
const max = currentItem + itemsToShow;
2828
const isVisible = idx >= min && idx < max;
29+
// we can't fix the typo (swipable -> swipeable) without a major bump
30+
const swipeableClassName = cssPrefix("swipable", `swipable-${idx}`);
2931
const item = (
3032
<ItemWrapperContainer
3133
id={idx}
3234
itemPosition={itemPosition}
33-
child={child}
3435
style={{ width, padding: paddingStyle }}
3536
key={idx}
3637
onClick={onItemClick}
37-
/>
38+
>
39+
{child}
40+
</ItemWrapperContainer>
3841
);
3942
const toRender = enableSwipe ? (
4043
<Swipeable
@@ -45,14 +48,21 @@ const Track = ({
4548
onSwipedRight={onSwipedRight}
4649
onSwipedUp={onSwipedUp}
4750
onSwipedDown={onSwipedDown}
48-
className={cssPrefix(`swipable-${idx}`)}
51+
className={swipeableClassName}
4952
>
5053
{item}
5154
</Swipeable>
5255
) : (
5356
item
5457
);
55-
return <div tabIndex={isVisible ? 0 : -1}>{toRender}</div>;
58+
return (
59+
<div
60+
className={cssPrefix("carousel-item", `carousel-item-${idx}`)}
61+
tabIndex={isVisible ? 0 : -1}
62+
>
63+
{toRender}
64+
</div>
65+
);
5666
});
5767
return <React.Fragment>{originalChildren}</React.Fragment>;
5868
};

src/react-elastic-carousel/utils/__tests__/helpers.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ describe("helpers", () => {
1313
expect(css).toEqual("rec rec-test");
1414
});
1515

16+
it("cssPrefix multi keys", () => {
17+
const css = helpers.cssPrefix("test", "test2");
18+
expect(css).toEqual("rec rec-test rec-test2");
19+
});
20+
1621
it("pipe", () => {
1722
const inc = num => num + 1;
1823
const double = num => num * 2;

src/react-elastic-carousel/utils/helpers.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@ export const noop = () => {};
22

33
export const numberToArray = n => [...Array(n).keys()];
44

5-
export const cssPrefix = className => `rec rec-${className}`;
5+
export const cssPrefix = (...classNames) => {
6+
const prefix = "rec";
7+
const space = " ";
8+
let result = `${prefix}`; // initial it with global prefix;
9+
10+
// in case of an array we add the class prefix per item;
11+
const chainedClasses = classNames.reduce((acc, current) => {
12+
acc += `${space}${prefix}-${current}`; // we must keep spaces between class names
13+
return acc;
14+
}, "");
15+
result += chainedClasses;
16+
17+
return result;
18+
};
619

720
export const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);
821

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6224,7 +6224,7 @@ docz-utils@^2.3.0:
62246224
unist-util-is "^3.0.0"
62256225
unist-util-visit "^1.4.1"
62266226

6227-
docz@^2.3.0-alpha.6:
6227+
docz@^2.3.1:
62286228
version "2.3.1"
62296229
resolved "https://registry.yarnpkg.com/docz/-/docz-2.3.1.tgz#931cd840e2f9a25691fb7c67fa48d4a6ef1476a3"
62306230
integrity sha512-HlbLqpizhieqvjD6xxNr6Nj8sk00vnHbLh4wHabXUpOi8ZkILBDSLmnfGzozAgmT+HN18CbszYMXCOlbcr4MXQ==

0 commit comments

Comments
 (0)