Skip to content

Commit 785504f

Browse files
committed
feat(rebound): add flip option
Flip mirrors the current divider vertically.
1 parent 8429552 commit 785504f

File tree

4 files changed

+47
-58
lines changed

4 files changed

+47
-58
lines changed

src/components/ReboundDivider.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@ import { CustomSVGElement } from '../elements/CustomSVGElement';
44
type Props = {
55
rebound: number;
66
height: number;
7+
flip?: boolean;
78
primaryColor: string;
89
secondaryColor?: string;
910
};
1011

11-
export function ReboundDivider({ rebound, height, primaryColor, secondaryColor }: Props) {
12+
export function ReboundDivider({ rebound, height, flip, primaryColor, secondaryColor }: Props) {
1213
const viewBox = `0 0 100 ${height}`;
13-
const points = `0,${height} ${rebound},0 100,${height}`;
14+
const points = flip ? `0,0 ${rebound},${height} 100,0` : `0,${height} ${rebound},0 100,${height}`;
1415

1516
return (
1617
<CustomSVGElement
1718
css={css`
18-
background-color: ${secondaryColor || 'transparent'};
19+
background-color: ${flip ? primaryColor : secondaryColor || 'transparent'};
1920
width: 100%;
2021
height: ${height}px;
2122
`}
2223
viewBox={viewBox}
2324
>
24-
<polygon points={points} fill={primaryColor} />
25+
<polygon points={points} fill={flip ? secondaryColor : primaryColor} />
2526
</CustomSVGElement>
2627
);
2728
}

src/components/ReboundSection.tsx

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { css, SerializedStyles } from '@emotion/react';
1+
/* eslint-disable no-nested-ternary */
2+
import { css } from '@emotion/react';
23
import { ElementType, HTMLProps } from 'react';
34

45
type Props = {
56
rebound: number;
6-
as?: ElementType;
77
bottomRebound?: number;
88
height: number;
99
bottomHeight?: number;
10-
primaryColor?: string;
1110
position?: 'top' | 'bottom' | 'both';
11+
flip?: 'top' | 'bottom' | 'both' | 'none';
12+
primaryColor?: string;
13+
as?: ElementType;
1214
};
1315

1416
export function ReboundSection({
@@ -19,38 +21,32 @@ export function ReboundSection({
1921
height,
2022
bottomHeight,
2123
primaryColor,
22-
position,
24+
position = 'top',
25+
flip = 'none',
2326
...props
2427
}: Props & HTMLProps<HTMLElement>) {
2528
const baseStyles = css`
2629
background-color: ${primaryColor ?? '#000000'};
2730
`;
28-
let styles: SerializedStyles;
31+
const polygon = [
32+
position === 'top' ? '0 100%' : flip !== 'none' && flip !== 'bottom' ? `0 100%` : `0 calc(100% - ${bottomHeight ?? height}px)`,
33+
position === 'top'
34+
? '0 100%'
35+
: flip !== 'none' && flip !== 'bottom'
36+
? `${`${bottomRebound ?? rebound}%`} calc(100% - ${bottomHeight ?? height}px)`
37+
: `${`${bottomRebound ?? rebound}%`} 100%`,
38+
position === 'top' ? '100% 100%' : flip !== 'none' && flip !== 'bottom' ? '100% 100%' : `100% calc(100% - ${bottomHeight ?? height}px)`,
39+
position === 'bottom' ? '100% 0' : flip !== 'none' && flip !== 'top' ? '100% 0' : `100% ${height}px`,
40+
position === 'bottom' ? '100% 0' : flip !== 'none' && flip !== 'top' ? `${rebound}% ${height}px` : `${rebound}% 0`,
41+
position === 'bottom' ? '0 0' : flip !== 'none' && flip !== 'top' ? `0 0` : `0 ${height}px`,
42+
];
43+
44+
const styles = css`
45+
${position !== 'top' ? `border-bottom: ${bottomHeight ?? height}px solid transparent;` : ''}
46+
${position !== 'bottom' ? `border-top: ${height}px solid transparent;` : ''}
47+
clip-path: polygon(${polygon.join(',')});
48+
`;
2949

30-
if (position === 'bottom') {
31-
styles = css`
32-
border-bottom: ${height}px solid transparent;
33-
clip-path: polygon(0 0, 0 calc(100% - ${height}px), ${rebound}% 100%, 100% calc(100% - ${height}px), 100% 0);
34-
`;
35-
} else if (position === 'both') {
36-
styles = css`
37-
border-top: ${height}px solid transparent;
38-
border-bottom: ${bottomHeight ?? height}px solid transparent;
39-
clip-path: polygon(
40-
0 ${height}px,
41-
0 calc(100% - ${bottomHeight ?? height}px),
42-
${`${bottomRebound ?? rebound}%`} 100%,
43-
100% calc(100% - ${bottomHeight ?? height}px),
44-
100% ${height}px,
45-
${`${rebound}%`} 0
46-
);
47-
`;
48-
} else {
49-
styles = css`
50-
border-top: ${height}px solid transparent;
51-
clip-path: polygon(0 ${height}px, 0 100%, 100% 100%, 100% ${height}px, ${rebound}% 0);
52-
`;
53-
}
5450
// eslint-disable-next-line react/jsx-props-no-spreading
5551
return <Section css={[baseStyles, styles, externalStyles]} {...props} />;
5652
}

stories/ReboundDivider.stories.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ export default {
3434
export function Basic({
3535
rebound,
3636
height,
37+
flip,
3738
primaryColor,
3839
secondaryColor,
3940
}: {
4041
rebound: number;
4142
height: number;
43+
flip: boolean;
4244
primaryColor: string;
4345
secondaryColor: string;
4446
}) {
@@ -50,7 +52,7 @@ export function Basic({
5052
color: #000;
5153
`}
5254
/>
53-
<ReboundDivider rebound={rebound} height={height} primaryColor={primaryColor} secondaryColor={secondaryColor} />
55+
<ReboundDivider rebound={rebound} height={height} flip={flip} primaryColor={primaryColor} secondaryColor={secondaryColor} />
5456
<SectionDummy
5557
css={css`
5658
background-color: ${primaryColor};

stories/ReboundSection.stories.tsx

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,18 @@ export default {
3939
},
4040
};
4141

42-
export function Basic({
43-
rebound,
44-
bottomRebound,
45-
height,
46-
bottomHeight,
47-
primaryColor,
48-
secondaryColor,
49-
position,
50-
}: {
42+
type StoryProps = {
5143
rebound: number;
52-
bottomRebound: number;
44+
bottomRebound?: number;
5345
height: number;
54-
bottomHeight: number;
55-
primaryColor: string;
56-
secondaryColor: string;
57-
position?: 'bottom' | 'top';
58-
}) {
46+
bottomHeight?: number;
47+
position?: 'top' | 'bottom' | 'both';
48+
flip?: 'top' | 'bottom' | 'both' | 'none';
49+
primaryColor?: string;
50+
secondaryColor?: string;
51+
};
52+
53+
export function Basic({ rebound, bottomRebound, height, bottomHeight, primaryColor, secondaryColor, position, flip }: StoryProps) {
5954
return (
6055
<Fragment>
6156
<SectionDummy
@@ -72,6 +67,7 @@ export function Basic({
7267
bottomHeight={bottomHeight}
7368
primaryColor={primaryColor}
7469
position={position}
70+
flip={flip}
7571
css={css`
7672
color: #fff;
7773
margin-top: -${height}px;
@@ -108,15 +104,8 @@ export function BackgroundImages({
108104
primaryColor,
109105
secondaryColor,
110106
position,
111-
}: {
112-
rebound: number;
113-
bottomRebound: number;
114-
height: number;
115-
bottomHeight: number;
116-
primaryColor: string;
117-
secondaryColor: string;
118-
position?: 'bottom' | 'top';
119-
}) {
107+
flip,
108+
}: StoryProps) {
120109
return (
121110
<Fragment>
122111
<SectionDummy
@@ -137,6 +126,7 @@ export function BackgroundImages({
137126
bottomHeight={bottomHeight}
138127
primaryColor={primaryColor}
139128
position={position}
129+
flip={flip}
140130
css={css`
141131
color: #fff;
142132
background-image: url(./res/photo.jpg);

0 commit comments

Comments
 (0)