Skip to content

Commit d26f362

Browse files
authored
border-radius as prop for View #117 (#122)
* add ListView docs * Add borderRadius prop for View * update Sidebar.js * remove ListView and SectionList components from docs
1 parent e9dd41b commit d26f362

File tree

6 files changed

+98
-57
lines changed

6 files changed

+98
-57
lines changed

packages/app/src/Sidebar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const styles = StyleSheet.create({
2323
position: 'absolute',
2424
left: 40,
2525
top: 100,
26+
borderRadius: 10,
2627
lineHeight: 40,
2728
},
2829
});

packages/docs/components-sectionlist.md

Lines changed: 0 additions & 51 deletions
This file was deleted.

packages/react-ape/__tests__/layout-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,31 @@ describe('Layout test', () => {
6666
}
6767
}
6868

69+
render(<Layout />, canvas, () => testCanvasSnapshot(expect, canvas));
70+
});
71+
test('Test 2 View with BorderRadius', () => {
72+
const canvas = document.createElement('canvas');
73+
canvas.height = 600;
74+
canvas.width = 600;
75+
class Layout extends React.Component {
76+
render() {
77+
return (
78+
<View>
79+
<View
80+
style={{
81+
width: 60,
82+
left: 100,
83+
top: 0,
84+
height: 60,
85+
borderRadius: 8,
86+
backgroundColor: 'white',
87+
}}
88+
/>
89+
</View>
90+
);
91+
}
92+
}
93+
6994
render(<Layout />, canvas, () => testCanvasSnapshot(expect, canvas));
7095
});
7196
});

packages/react-ape/renderer/elements/View.js

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,33 @@ class View {
6969
if (this.previousRect) {
7070
const previousStroke = ctx.strokeStyle;
7171
ctx.beginPath();
72-
const {x, y, width, height} = this.previousRect;
73-
ctx.rect(x, y, width, height);
72+
const {x, y, width, height, cornerRadius} = this.previousRect;
73+
// ctx.rect(x, y, width, height);
74+
// Ref: https://stackoverflow.com/questions/1255512/how-to-draw-a-rounded-rectangle-using-html-canvas
75+
ctx.moveTo(x, y);
76+
/**
77+
* Top Right Radius
78+
*/
79+
ctx.lineTo(x + width - cornerRadius, y);
80+
ctx.quadraticCurveTo(x + width, y, x + width, y + cornerRadius);
81+
/**
82+
* Bottom right Radius
83+
*/
84+
ctx.lineTo(x + width, y + height - cornerRadius);
85+
ctx.quadraticCurveTo(
86+
x + width,
87+
y + height,
88+
x + width - cornerRadius,
89+
y + height
90+
);
91+
/**
92+
* Bottom Left Radius
93+
*/
94+
ctx.lineTo(x + cornerRadius, y + height);
95+
ctx.quadraticCurveTo(x, y + height, x, y + height - cornerRadius);
96+
/** Top left Radius */
97+
ctx.lineTo(x, y + cornerRadius);
98+
ctx.quadraticCurveTo(x, y, x + cornerRadius, y);
7499
ctx.strokeStyle = style.backgroundColor || 'transparent';
75100
ctx.fillStyle = style.backgroundColor || 'transparent';
76101
ctx.fill();
@@ -89,6 +114,9 @@ class View {
89114
const width = style.width || ViewDefaults.size;
90115
const height = style.height || ViewDefaults.size;
91116

117+
// Draw borderRadius
118+
const cornerRadius = style.borderRadius || 0;
119+
92120
if (!style.position || style.position === 'relative') {
93121
const surfaceHeight = getSurfaceHeight();
94122
y = surfaceHeight;
@@ -97,8 +125,33 @@ class View {
97125

98126
ctx.globalCompositeOperation = 'destination-over';
99127
ctx.beginPath();
100-
ctx.rect(x, y, width, height);
101-
this.previousRect = {x, y, width, height};
128+
// ctx.rect(x, y, width, height);
129+
// Ref: https://stackoverflow.com/questions/1255512/how-to-draw-a-rounded-rectangle-using-html-canvas
130+
ctx.moveTo(x, y);
131+
/**
132+
* Top Right Radius
133+
*/
134+
ctx.lineTo(x + width - cornerRadius, y);
135+
ctx.quadraticCurveTo(x + width, y, x + width, y + cornerRadius);
136+
/**
137+
* Bottom right Radius
138+
*/
139+
ctx.lineTo(x + width, y + height - cornerRadius);
140+
ctx.quadraticCurveTo(
141+
x + width,
142+
y + height,
143+
x + width - cornerRadius,
144+
y + height
145+
);
146+
/**
147+
* Bottom Left Radius
148+
*/
149+
ctx.lineTo(x + cornerRadius, y + height);
150+
ctx.quadraticCurveTo(x, y + height, x, y + height - cornerRadius);
151+
/** Top left Radius */
152+
ctx.lineTo(x, y + cornerRadius);
153+
ctx.quadraticCurveTo(x, y, x + cornerRadius, y);
154+
this.previousRect = {x, y, width, height, cornerRadius};
102155
ctx.strokeStyle = style.borderColor || 'transparent';
103156
ctx.fillStyle = style.backgroundColor || 'transparent';
104157
ctx.fill();

packages/react-ape/renderer/elements/__tests__/View-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('View', () => {
99
top: 10,
1010
left: 100,
1111
position: 'absolute',
12+
borderRadius: 10,
1213
backgroundColor: 'green',
1314
};
1415
const props = {style, children: 'My Amazing Text'};
@@ -33,6 +34,9 @@ describe('View', () => {
3334
stroke: jest.fn(),
3435
rect: jest.fn(),
3536
closePath: jest.fn(),
37+
moveTo: jest.fn(),
38+
lineTo: jest.fn(),
39+
quadraticCurveTo: jest.fn(),
3640
},
3741
getSurfaceHeight: () => 0,
3842
setSurfaceHeight: () => {},
@@ -63,6 +67,7 @@ describe('View', () => {
6367
style: {
6468
backgroundColor: 'green',
6569
borderColor: 'white',
70+
borderRadius: 10,
6671
left: 100,
6772
lineHeight: 24,
6873
position: 'absolute',
@@ -98,6 +103,9 @@ describe('View', () => {
98103
stroke: jest.fn(),
99104
rect: jest.fn(),
100105
closePath: jest.fn(),
106+
moveTo: jest.fn(),
107+
lineTo: jest.fn(),
108+
quadraticCurveTo: jest.fn(),
101109
},
102110
getSurfaceHeight: () => 0,
103111
setSurfaceHeight: () => {},
@@ -110,11 +118,17 @@ describe('View', () => {
110118
beginPath,
111119
closePath,
112120
fillStyle,
121+
moveTo,
122+
lineTo,
123+
quadraticCurveTo,
113124
globalCompositeOperation,
114125
} = apeContext.ctx;
115126

116127
expect(beginPath.mock.calls.length).toBe(1);
117128
expect(closePath.mock.calls.length).toBe(1);
129+
expect(moveTo.mock.calls.length).toBe(1);
130+
expect(lineTo.mock.calls.length).toBe(4);
131+
expect(quadraticCurveTo.mock.calls.length).toBe(4);
118132
expect(beginPath).toBeCalledWith();
119133
expect(closePath).toBeCalledWith();
120134

packages/website/sidebars.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
"Components": [
1717
"components-image",
1818
"components-text",
19-
"components-view",
20-
"components-sectionlist"
19+
"components-view"
2120
],
2221
"APIs": [
2322
"apis-dimensions",

0 commit comments

Comments
 (0)