Skip to content

Commit

Permalink
Merge pull request #62 from kristerkari/feature/merge-styles
Browse files Browse the repository at this point in the history
Implement merging of default styles
  • Loading branch information
kristerkari authored Jul 11, 2021
2 parents 2d7942c + 7f6b58f commit 29802f5
Show file tree
Hide file tree
Showing 11 changed files with 2,555 additions and 12 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ If you don't want to run the app, you can have a look at the [components folder]
| `accessibility` | true | `boolean` | Set to `false` to disable accessibility features (not recommended). |
| `accessibilityLabelPrev` | Previous | `string` | Accessibility label for the prev button. |
| `accessibilityLabelNext` | Next | `string` | Accessibility label for the next button. |
| `mergeStyles` | false | `boolean` | Set to `true` to merge your custom styles with the default styles. |

#### Callbacks

Expand Down Expand Up @@ -160,7 +161,11 @@ You can then call the method from outside the carousel:

#### Custom styling properties

Use these properties to customize how the carousel is styled. Have a look at the [custom styles component](/examples/components/CustomStyles.tsx) for an example.
Use these properties to customize how the carousel is styled.

Set the `mergeStyles` property to `true` if you want to merge your custom styles with the default ones instead of having define all the needed styles. You can also `import { defaulStyles } from "pinar"` to get access to the default styles and use them as defaults.

Have a look at the ["custom styles" component](/examples/components/CustomStyles.tsx) and ["custom styles with merge" component](/examples/components/CustomMergeStyles.tsx) for an example.

| Property | Default | Type | Description |
| :----------------------- | :-----: | :--------------------: | :------------------------------------------------ |
Expand Down
3 changes: 3 additions & 0 deletions e2e/loop.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ describe("loop", () => {
await device.reloadReactNative();
});
it("should be able to go to Page 1 -> Page 2 -> Page 3 -> Page 1", async () => {
await element(by.id("scrollview")).scrollTo("bottom");
await expect(element(by.id("loop"))).toBeVisible();
await element(by.id("loop")).tap();

Expand Down Expand Up @@ -44,6 +45,7 @@ describe("loop", () => {
});

it("should be able to go to Page 1 -> Page 3 -> Page 2 -> Page 1", async () => {
await element(by.id("scrollview")).scrollTo("bottom");
await expect(element(by.id("loop"))).toBeVisible();
await element(by.id("loop")).tap();

Expand Down Expand Up @@ -83,6 +85,7 @@ describe("loop", () => {
});

it("should be able to go to Page 1 -> Page 3 -> Page 1", async () => {
await element(by.id("scrollview")).scrollTo("bottom");
await expect(element(by.id("loop"))).toBeVisible();
await element(by.id("loop")).tap();

Expand Down
13 changes: 13 additions & 0 deletions examples/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
CustomRender,
CustomSize,
CustomStyles,
CustomMergeStyles,
CustomWidth,
DisabledControls,
Loop,
Expand Down Expand Up @@ -97,6 +98,12 @@ class HomeScreen extends React.Component<Props, State> {
}}
title="Custom styling"
/>
<Button
onPress={(): void => {
navigation.navigate("CustomMergeStyles");
}}
title="Custom styling merging with defaults"
/>
<Button
testID="custom-render"
onPress={(): void => {
Expand Down Expand Up @@ -237,6 +244,12 @@ const AppNavigator = createStackNavigator({
title: "Custom styling",
},
},
CustomMergeStyles: {
screen: CustomMergeStyles,
navigationOptions: {
title: "Custom styling merging with defaults",
},
},
CustomWidth: {
screen: CustomWidth,
navigationOptions: {
Expand Down
94 changes: 94 additions & 0 deletions examples/components/CustomMergeStyles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import React from "react";
import {
AccessibilityInfo,
Platform,
Text,
TextStyle,
View,
ViewStyle,
} from "react-native";
import Carousel from "../../src/index";

const styles = {
slide1: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#a3c9a8",
} as ViewStyle,
slide2: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#84b59f",
} as ViewStyle,
slide3: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#69a297",
} as ViewStyle,
text: {
color: "#1f2d3d",
opacity: 0.7,
fontSize: 48,
fontWeight: "bold",
} as TextStyle,
};

export const CustomMergeStyles = (): JSX.Element => (
<Carousel
mergeStyles={true}
dotsContainerStyle={{
position: "absolute",
top: 25,
left: 0,
right: 0,
alignItems: "flex-start",
}}
activeDotStyle={{
backgroundColor: "#eace15",
}}
dotStyle={{
backgroundColor: "rgba(0,0,0,.2)",
}}
controlsContainerStyle={{
position: "absolute",
top: 30,
left: 0,
right: 0,
flexDirection: "row",
justifyContent: "space-between",
alignItems: "flex-start",
}}
controlsButtonStyle={{
backgroundColor: "black",
borderRadius: 60,
opacity: 0.5,
width: 80,
height: 80,
flexDirection: "row",
justifyContent: "center",
alignItems: "center",
}}
controlsTextStyle={{ fontSize: 50, color: "#eace15", padding: 0 }}
onIndexChanged={({ index, total }): void => {
if (Platform.OS === "ios") {
const page = index + 1;
AccessibilityInfo.announceForAccessibility(
`Changed to page ${page}/${total}`
);
}
}}
>
<View style={styles.slide1}>
<Text style={styles.text}>1</Text>
</View>
<View style={styles.slide2}>
<Text style={styles.text}>2</Text>
</View>
<View style={styles.slide3}>
<Text style={styles.text}>3</Text>
</View>
</Carousel>
);
6 changes: 2 additions & 4 deletions examples/components/CustomStyles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
View,
ViewStyle
} from "react-native";
import Carousel from "../../src/index";
import Carousel, { defaultStyles } from "../../src/index";

const styles = {
slide1: {
Expand Down Expand Up @@ -67,15 +67,13 @@ export const CustomStyles = (): JSX.Element => (
marginBottom: 3
}}
controlsContainerStyle={{
flexDirection: "row",
...defaultStyles.controlsContainer,
position: "absolute",
top: 30,
left: 0,
right: 0,
paddingHorizontal: 10,
paddingVertical: 10,
justifyContent: "space-between",
alignItems: "center"
}}
controlsButtonStyle={{
display: "flex",
Expand Down
1 change: 1 addition & 0 deletions examples/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export { CustomIndex } from "./CustomIndex";
export { CustomRender } from "./CustomRender";
export { CustomSize } from "./CustomSize";
export { CustomStyles } from "./CustomStyles";
export { CustomMergeStyles } from "./CustomMergeStyles";
export { CustomWidth } from "./CustomWidth";
export { DisabledControls } from "./DisabledControls";
export { Loop } from "./Loop";
Expand Down
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export interface Props
accessibilityLabelPrev: string;
accessibilityLabelNext: string;
children: JSX.Element | JSX.Element[];
mergeStyles: boolean;
}

export interface State {
Expand All @@ -110,6 +111,8 @@ export interface State {
};
}

export { defaultStyles } from "./src/styles"

declare module "pinar" {
export default class Pinar extends PureComponent<Props, State> {
static defaultProps: {
Expand All @@ -131,6 +134,7 @@ declare module "pinar" {
accessibilityLabelPrev: string;
accessibilityLabelNext: string;
index: number;
mergeStyles: boolean;
};
public scrollBy(options: ScrollByOptions): void;
public scrollToIndex(options: ScrollByOptions): void;
Expand Down
37 changes: 31 additions & 6 deletions src/Pinar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const defaultCarouselProps = {
accessibilityLabelPrev: "Previous",
accessibilityLabelNext: "Next",
index: 0,
mergeStyles: false,
};

const styles = StyleSheet.create(defaultStyles);
Expand Down Expand Up @@ -429,6 +430,7 @@ export class Pinar extends React.PureComponent<Props, State> {
accessibilityLabelNext,
controlsButtonStyle,
controlsTextStyle,
mergeStyles,
} = this.props;
return (
<TouchableOpacity
Expand All @@ -442,7 +444,11 @@ export class Pinar extends React.PureComponent<Props, State> {
<Text
accessibilityLabel={accessibilityLabelNext}
accessible={accessibility}
style={controlsTextStyle || styles.buttonText}
style={
mergeStyles
? [styles.buttonText, controlsTextStyle]
: controlsTextStyle || styles.buttonText
}
>
</Text>
Expand All @@ -468,6 +474,7 @@ export class Pinar extends React.PureComponent<Props, State> {
accessibilityLabelPrev,
controlsButtonStyle,
controlsTextStyle,
mergeStyles,
} = this.props;
return (
<TouchableOpacity
Expand All @@ -481,7 +488,11 @@ export class Pinar extends React.PureComponent<Props, State> {
<Text
accessibilityLabel={accessibilityLabelPrev}
accessible={accessibility}
style={controlsTextStyle || styles.buttonText}
style={
mergeStyles
? [styles.buttonText, controlsTextStyle]
: controlsTextStyle || styles.buttonText
}
>
</Text>
Expand Down Expand Up @@ -509,7 +520,7 @@ export class Pinar extends React.PureComponent<Props, State> {
}

const { height, width } = this.state;
const { controlsContainerStyle } = this.props;
const { controlsContainerStyle, mergeStyles } = this.props;

const defaultControlsContainerStyle = [
styles.controlsContainer,
Expand All @@ -518,7 +529,11 @@ export class Pinar extends React.PureComponent<Props, State> {
return (
<View
pointerEvents="box-none"
style={controlsContainerStyle || defaultControlsContainerStyle}
style={
mergeStyles
? [defaultControlsContainerStyle, controlsContainerStyle]
: controlsContainerStyle || defaultControlsContainerStyle
}
>
{this.renderPrev()}
{this.renderNext()}
Expand All @@ -544,13 +559,20 @@ export class Pinar extends React.PureComponent<Props, State> {
horizontal,
renderActiveDot,
renderDot,
mergeStyles,
} = this.props;
const defaultDotsContainerStyle = horizontal
? styles.dotsContainerHorizontal
: styles.dotsContainerVertical;

return (
<View style={dotsContainerStyle || defaultDotsContainerStyle}>
<View
style={
mergeStyles
? [defaultDotsContainerStyle, dotsContainerStyle]
: dotsContainerStyle || defaultDotsContainerStyle
}
>
{React.Children.map(
children,
(_: JSX.Element, i: number): JSX.Element => {
Expand All @@ -566,7 +588,10 @@ export class Pinar extends React.PureComponent<Props, State> {
const style = isActive
? activeDotStyle || styles.dotActive
: dotStyle || styles.dot;
return <View key={i} style={style} />;
const mergeStyle = isActive
? [styles.dotActive, activeDotStyle]
: [styles.dot, dotStyle];
return <View key={i} style={mergeStyles ? mergeStyle : style} />;
/* eslint-enable react/no-array-index-key */
}
)}
Expand Down
Loading

0 comments on commit 29802f5

Please sign in to comment.