Skip to content

Commit 59aa3df

Browse files
committed
support both className and inline styles override
1 parent 807e3e3 commit 59aa3df

8 files changed

Lines changed: 51 additions & 21 deletions

File tree

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"printWidth": 90,
3+
"tabWidth": 4
4+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-card-stack-carousel",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "A tiny configurable carousel component for React",
55
"main": "dist/index.js",
66
"scripts": {

src/CarouselItem.jsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from "react";
2+
import { getMergedStyles } from "./getMergedStyles";
23

34
export default React.memo(function CarouselItem(props) {
45
const {
@@ -31,10 +32,15 @@ export default React.memo(function CarouselItem(props) {
3132
transform,
3233
zIndex,
3334
};
34-
const mergedStyles = Object.assign({}, styleOverride, computedStyle);
35+
36+
const [className, inlineStyle] = getMergedStyles(
37+
"rcsc-item",
38+
computedStyle,
39+
styleOverride
40+
);
3541

3642
return (
37-
<div style={mergedStyles} className="rcsc-item">
43+
<div style={inlineStyle} className={className}>
3844
{children}
3945
</div>
4046
);

src/Navigation.jsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
import { ArrowLeft, ArrowRight } from "./Icons";
2+
import { getMergedStyles } from "./getMergedStyles";
23

34
export default function Navigation(props) {
45
const { onNext, onPrevious, styleOverrides } = props;
56
const arrowSize = 16;
67

8+
const [containerClassName, containerInlineStyle] = getMergedStyles(
9+
"rcsc-nav-container",
10+
null,
11+
styleOverrides.Navigation
12+
);
13+
const [iconClassName, iconInlineStyle] = getMergedStyles(
14+
"rcsc-nav-icon",
15+
null,
16+
styleOverrides.NavIcon
17+
);
18+
719
return (
8-
<nav style={styleOverrides.Navigation} className="rcsc-nav-container">
9-
<div
10-
style={styleOverrides.NavIcon}
11-
onClick={onPrevious}
12-
className="rcsc-nav-icon"
13-
>
20+
<nav style={containerInlineStyle} className={containerClassName}>
21+
<div style={iconInlineStyle} onClick={onPrevious} className={iconClassName}>
1422
<ArrowLeft color="#fff" size={arrowSize} />
1523
</div>
16-
<div
17-
style={styleOverrides.NavIcon}
18-
onClick={onNext}
19-
className="rcsc-nav-icon"
20-
>
24+
<div style={iconInlineStyle} onClick={onNext} className={iconClassName}>
2125
<ArrowRight color="#fff" size={arrowSize} />
2226
</div>
2327
</nav>

src/StackedCarousel.jsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import CarouselItem from "./CarouselItem";
33
import Navigation from "./Navigation";
44
import { useCardStackCarousel } from "./useCardStackCarousel";
55
import { useRootHeight } from "./useRootHeight";
6+
import { getMergedStyles } from "./getMergedStyles";
67

78
export function StackedCarousel(props) {
89
const {
@@ -50,11 +51,13 @@ export function StackedCarousel(props) {
5051
});
5152
};
5253

53-
const { Root } = styleOverrides;
54-
const styles = Object.assign({}, Root, { height: rootHeight });
55-
54+
const [className, inlineStyle] = getMergedStyles(
55+
"rcsc-container",
56+
{ height: rootHeight },
57+
styleOverrides.Root
58+
);
5659
return (
57-
<section className="rcsc-container" style={styles}>
60+
<section className={className} style={inlineStyle}>
5861
{renderCards()}
5962

6063
<Navigation

src/getMergedStyles.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const getMergedStyles = (className, inlineStyle, styleOverride) => {
2+
if (styleOverride) {
3+
if (typeof styleOverride === "string") {
4+
const mergedClassName = `${className} ${styleOverride}`;
5+
return [mergedClassName, inlineStyle];
6+
} else {
7+
const mergedInlineStyle = Object.assign({}, styleOverride, inlineStyle);
8+
return [className, mergedInlineStyle];
9+
}
10+
}
11+
12+
return [className, inlineStyle];
13+
};

styles/styles.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
.rcsc-nav-container {
1313
position: absolute;
14-
bottom: -40px;
14+
bottom: -44px;
1515
width: 100%;
1616
display: flex;
1717
justify-content: center;
@@ -25,7 +25,7 @@
2525
width: 30px;
2626
height: 30px;
2727
border-radius: 50%;
28-
margin: 0 5px;
28+
margin: 0 6px;
2929
padding: 8px;
3030
cursor: pointer;
3131
background-color: #374151;

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ declare module "react-card-stack-carousel" {
22
import { ReactNode } from "react";
33

44
type StyleKeys = "Root" | "CarouselItem" | "Navigation" | "NavIcon";
5-
type StyleOverrides = Record<StyleKeys, React.CSSProperties>;
5+
type StyleOverrides = Record<StyleKeys, React.CSSProperties | string>;
66

77
export interface StackedCarouselProps {
88
height: number | string;

0 commit comments

Comments
 (0)