@tetrax/squircle 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,325 @@
1
+ // src/react-native/index.tsx
2
+ import { useState, useCallback } from "react";
3
+ import { View } from "react-native";
4
+ import Svg, { Defs, ClipPath, Path, Rect } from "react-native-svg";
5
+
6
+ // src/core/squircle-path.ts
7
+ function toRadians(deg) {
8
+ return deg * Math.PI / 180;
9
+ }
10
+ function rounded(strings, ...values) {
11
+ return strings.reduce((acc, str, i) => {
12
+ const value = values[i];
13
+ return typeof value === "number" ? acc + str + value.toFixed(4) : acc + str + (value ?? "");
14
+ }, "");
15
+ }
16
+ function getPathParamsForCorner({
17
+ cornerRadius,
18
+ cornerSmoothing,
19
+ preserveSmoothing,
20
+ roundingAndSmoothingBudget
21
+ }) {
22
+ if (cornerRadius <= 0 || roundingAndSmoothingBudget <= 0) {
23
+ return { a: 0, b: 0, c: 0, d: 0, p: 0, cornerRadius: 0, arcSectionLength: 0 };
24
+ }
25
+ let p = (1 + cornerSmoothing) * cornerRadius;
26
+ if (!preserveSmoothing) {
27
+ const maxCornerSmoothing = roundingAndSmoothingBudget / cornerRadius - 1;
28
+ cornerSmoothing = Math.min(cornerSmoothing, maxCornerSmoothing);
29
+ p = Math.min(p, roundingAndSmoothingBudget);
30
+ }
31
+ const arcMeasure = 90 * (1 - cornerSmoothing);
32
+ const arcSectionLength = Math.sin(toRadians(arcMeasure / 2)) * cornerRadius * Math.sqrt(2);
33
+ const angleAlpha = (90 - arcMeasure) / 2;
34
+ const p3ToP4Distance = cornerRadius * Math.tan(toRadians(angleAlpha / 2));
35
+ const angleBeta = 45 * cornerSmoothing;
36
+ const c = p3ToP4Distance * Math.cos(toRadians(angleBeta));
37
+ const d = c * Math.tan(toRadians(angleBeta));
38
+ let b = (p - arcSectionLength - c - d) / 3;
39
+ let a = 2 * b;
40
+ if (preserveSmoothing && p > roundingAndSmoothingBudget) {
41
+ const p1ToP3MaxDistance = roundingAndSmoothingBudget - d - arcSectionLength - c;
42
+ const minA = p1ToP3MaxDistance / 6;
43
+ const maxB = p1ToP3MaxDistance - minA;
44
+ b = Math.min(b, maxB);
45
+ a = p1ToP3MaxDistance - b;
46
+ p = Math.min(p, roundingAndSmoothingBudget);
47
+ }
48
+ return { a, b, c, d, p, arcSectionLength, cornerRadius };
49
+ }
50
+ function drawTopRightPath({ cornerRadius, a, b, c, d, p, arcSectionLength }) {
51
+ if (cornerRadius) {
52
+ return rounded`c ${a} 0 ${a + b} 0 ${a + b + c} ${d} a ${cornerRadius} ${cornerRadius} 0 0 1 ${arcSectionLength} ${arcSectionLength} c ${d} ${c} ${d} ${b + c} ${d} ${a + b + c}`;
53
+ }
54
+ return rounded`l ${p} 0`;
55
+ }
56
+ function drawBottomRightPath({ cornerRadius, a, b, c, d, p, arcSectionLength }) {
57
+ if (cornerRadius) {
58
+ return rounded`c 0 ${a} 0 ${a + b} ${-d} ${a + b + c} a ${cornerRadius} ${cornerRadius} 0 0 1 ${-arcSectionLength} ${arcSectionLength} c ${-c} ${d} ${-(b + c)} ${d} ${-(a + b + c)} ${d}`;
59
+ }
60
+ return rounded`l 0 ${p}`;
61
+ }
62
+ function drawBottomLeftPath({ cornerRadius, a, b, c, d, p, arcSectionLength }) {
63
+ if (cornerRadius) {
64
+ return rounded`c ${-a} 0 ${-(a + b)} 0 ${-(a + b + c)} ${-d} a ${cornerRadius} ${cornerRadius} 0 0 1 ${-arcSectionLength} ${-arcSectionLength} c ${-d} ${-c} ${-d} ${-(b + c)} ${-d} ${-(a + b + c)}`;
65
+ }
66
+ return rounded`l ${-p} 0`;
67
+ }
68
+ function drawTopLeftPath({ cornerRadius, a, b, c, d, p, arcSectionLength }) {
69
+ if (cornerRadius) {
70
+ return rounded`c 0 ${-a} 0 ${-(a + b)} ${d} ${-(a + b + c)} a ${cornerRadius} ${cornerRadius} 0 0 1 ${arcSectionLength} ${-arcSectionLength} c ${c} ${-d} ${b + c} ${-d} ${a + b + c} ${-d}`;
71
+ }
72
+ return rounded`l 0 ${-p}`;
73
+ }
74
+ function getSVGPathFromPathParams({
75
+ width,
76
+ height,
77
+ topLeftPathParams,
78
+ topRightPathParams,
79
+ bottomRightPathParams,
80
+ bottomLeftPathParams
81
+ }) {
82
+ return `
83
+ M ${width - topRightPathParams.p} 0
84
+ ${drawTopRightPath(topRightPathParams)}
85
+ L ${width} ${height - bottomRightPathParams.p}
86
+ ${drawBottomRightPath(bottomRightPathParams)}
87
+ L ${bottomLeftPathParams.p} ${height}
88
+ ${drawBottomLeftPath(bottomLeftPathParams)}
89
+ L 0 ${topLeftPathParams.p}
90
+ ${drawTopLeftPath(topLeftPathParams)}
91
+ Z
92
+ `.replace(/[\t\s\n]+/g, " ").trim();
93
+ }
94
+ var adjacentsByCorner = {
95
+ topLeft: [
96
+ { corner: "topRight", side: "top" },
97
+ { corner: "bottomLeft", side: "left" }
98
+ ],
99
+ topRight: [
100
+ { corner: "topLeft", side: "top" },
101
+ { corner: "bottomRight", side: "right" }
102
+ ],
103
+ bottomLeft: [
104
+ { corner: "bottomRight", side: "bottom" },
105
+ { corner: "topLeft", side: "left" }
106
+ ],
107
+ bottomRight: [
108
+ { corner: "bottomLeft", side: "bottom" },
109
+ { corner: "topRight", side: "right" }
110
+ ]
111
+ };
112
+ function distributeAndNormalize({
113
+ topLeftCornerRadius,
114
+ topRightCornerRadius,
115
+ bottomRightCornerRadius,
116
+ bottomLeftCornerRadius,
117
+ width,
118
+ height
119
+ }) {
120
+ const roundingAndSmoothingBudgetMap = {
121
+ topLeft: -1,
122
+ topRight: -1,
123
+ bottomLeft: -1,
124
+ bottomRight: -1
125
+ };
126
+ const cornerRadiusMap = {
127
+ topLeft: topLeftCornerRadius,
128
+ topRight: topRightCornerRadius,
129
+ bottomLeft: bottomLeftCornerRadius,
130
+ bottomRight: bottomRightCornerRadius
131
+ };
132
+ Object.entries(cornerRadiusMap).sort(([, r1], [, r2]) => r2 - r1).forEach(([corner, radius]) => {
133
+ const adjacents = adjacentsByCorner[corner];
134
+ const budget = Math.min(
135
+ ...adjacents.map((adj) => {
136
+ const adjRadius = cornerRadiusMap[adj.corner];
137
+ if (radius === 0 && adjRadius === 0) return 0;
138
+ const adjBudget = roundingAndSmoothingBudgetMap[adj.corner];
139
+ const sideLen = adj.side === "top" || adj.side === "bottom" ? width : height;
140
+ return adjBudget >= 0 ? sideLen - roundingAndSmoothingBudgetMap[adj.corner] : radius / (radius + adjRadius) * sideLen;
141
+ })
142
+ );
143
+ roundingAndSmoothingBudgetMap[corner] = budget;
144
+ cornerRadiusMap[corner] = Math.min(radius, budget);
145
+ });
146
+ return {
147
+ topLeft: { radius: cornerRadiusMap.topLeft, roundingAndSmoothingBudget: roundingAndSmoothingBudgetMap.topLeft },
148
+ topRight: { radius: cornerRadiusMap.topRight, roundingAndSmoothingBudget: roundingAndSmoothingBudgetMap.topRight },
149
+ bottomLeft: { radius: cornerRadiusMap.bottomLeft, roundingAndSmoothingBudget: roundingAndSmoothingBudgetMap.bottomLeft },
150
+ bottomRight: { radius: cornerRadiusMap.bottomRight, roundingAndSmoothingBudget: roundingAndSmoothingBudgetMap.bottomRight }
151
+ };
152
+ }
153
+ function drawPureBezierTopRight(r) {
154
+ if (r <= 0) return "";
155
+ return rounded`c ${0.3 * r} 0 ${0.473 * r} 0 ${0.619 * r} ${0.039 * r} c ${0.185 * r} ${0.049 * r} ${0.293 * r} ${0.157 * r} ${0.342 * r} ${0.342 * r} c ${0.039 * r} ${0.146 * r} ${0.039 * r} ${0.319 * r} ${0.039 * r} ${0.619 * r}`;
156
+ }
157
+ function drawPureBezierBottomRight(r) {
158
+ if (r <= 0) return "";
159
+ return rounded`c 0 ${0.3 * r} 0 ${0.473 * r} ${-0.039 * r} ${0.619 * r} c ${-0.049 * r} ${0.185 * r} ${-0.157 * r} ${0.293 * r} ${-0.342 * r} ${0.342 * r} c ${-0.146 * r} ${0.039 * r} ${-0.319 * r} ${0.039 * r} ${-0.619 * r} ${0.039 * r}`;
160
+ }
161
+ function drawPureBezierBottomLeft(r) {
162
+ if (r <= 0) return "";
163
+ return rounded`c ${-0.3 * r} 0 ${-0.473 * r} 0 ${-0.619 * r} ${-0.039 * r} c ${-0.185 * r} ${-0.049 * r} ${-0.293 * r} ${-0.157 * r} ${-0.342 * r} ${-0.342 * r} c ${-0.039 * r} ${-0.146 * r} ${-0.039 * r} ${-0.319 * r} ${-0.039 * r} ${-0.619 * r}`;
164
+ }
165
+ function drawPureBezierTopLeft(r) {
166
+ if (r <= 0) return "";
167
+ return rounded`c 0 ${-0.3 * r} 0 ${-0.473 * r} ${0.039 * r} ${-0.619 * r} c ${0.049 * r} ${-0.185 * r} ${0.157 * r} ${-0.293 * r} ${0.342 * r} ${-0.342 * r} c ${0.146 * r} ${-0.039 * r} ${0.319 * r} ${-0.039 * r} ${0.619 * r} ${-0.039 * r}`;
168
+ }
169
+ function getPureBezierSVGPath(width, height, tl, tr, br, bl) {
170
+ return `
171
+ M ${width - tr} 0
172
+ ${drawPureBezierTopRight(tr)}
173
+ L ${width} ${height - br}
174
+ ${drawPureBezierBottomRight(br)}
175
+ L ${bl} ${height}
176
+ ${drawPureBezierBottomLeft(bl)}
177
+ L 0 ${tl}
178
+ ${drawPureBezierTopLeft(tl)}
179
+ Z
180
+ `.replace(/[\t\s\n]+/g, " ").trim();
181
+ }
182
+ function getSvgPath({
183
+ cornerRadius = 0,
184
+ topLeftCornerRadius,
185
+ topRightCornerRadius,
186
+ bottomRightCornerRadius,
187
+ bottomLeftCornerRadius,
188
+ cornerSmoothing = 1,
189
+ width,
190
+ height,
191
+ preserveSmoothing = false,
192
+ g2Continuous = false
193
+ }) {
194
+ const tl = topLeftCornerRadius ?? cornerRadius;
195
+ const tr = topRightCornerRadius ?? cornerRadius;
196
+ const bl = bottomLeftCornerRadius ?? cornerRadius;
197
+ const br = bottomRightCornerRadius ?? cornerRadius;
198
+ if (g2Continuous) {
199
+ const { topLeft: topLeft2, topRight: topRight2, bottomLeft: bottomLeft2, bottomRight: bottomRight2 } = distributeAndNormalize({
200
+ topLeftCornerRadius: tl,
201
+ topRightCornerRadius: tr,
202
+ bottomRightCornerRadius: br,
203
+ bottomLeftCornerRadius: bl,
204
+ width,
205
+ height
206
+ });
207
+ return getPureBezierSVGPath(
208
+ width,
209
+ height,
210
+ topLeft2.radius,
211
+ topRight2.radius,
212
+ bottomRight2.radius,
213
+ bottomLeft2.radius
214
+ );
215
+ }
216
+ if (tl === tr && tr === br && br === bl) {
217
+ const roundingAndSmoothingBudget = Math.min(width, height) / 2;
218
+ const clampedRadius = Math.min(tl, roundingAndSmoothingBudget);
219
+ const params = getPathParamsForCorner({
220
+ cornerRadius: clampedRadius,
221
+ cornerSmoothing,
222
+ preserveSmoothing,
223
+ roundingAndSmoothingBudget
224
+ });
225
+ return getSVGPathFromPathParams({
226
+ width,
227
+ height,
228
+ topLeftPathParams: params,
229
+ topRightPathParams: params,
230
+ bottomLeftPathParams: params,
231
+ bottomRightPathParams: params
232
+ });
233
+ }
234
+ const { topLeft, topRight, bottomLeft, bottomRight } = distributeAndNormalize({
235
+ topLeftCornerRadius: tl,
236
+ topRightCornerRadius: tr,
237
+ bottomRightCornerRadius: br,
238
+ bottomLeftCornerRadius: bl,
239
+ width,
240
+ height
241
+ });
242
+ return getSVGPathFromPathParams({
243
+ width,
244
+ height,
245
+ topLeftPathParams: getPathParamsForCorner({ cornerSmoothing, preserveSmoothing, cornerRadius: topLeft.radius, roundingAndSmoothingBudget: topLeft.roundingAndSmoothingBudget }),
246
+ topRightPathParams: getPathParamsForCorner({ cornerSmoothing, preserveSmoothing, cornerRadius: topRight.radius, roundingAndSmoothingBudget: topRight.roundingAndSmoothingBudget }),
247
+ bottomRightPathParams: getPathParamsForCorner({ cornerSmoothing, preserveSmoothing, cornerRadius: bottomRight.radius, roundingAndSmoothingBudget: bottomRight.roundingAndSmoothingBudget }),
248
+ bottomLeftPathParams: getPathParamsForCorner({ cornerSmoothing, preserveSmoothing, cornerRadius: bottomLeft.radius, roundingAndSmoothingBudget: bottomLeft.roundingAndSmoothingBudget })
249
+ });
250
+ }
251
+ function computeSquirclePath(width, height, cornerRadius, cornerSmoothing = 1, preserveSmoothing = false, g2Continuous = false) {
252
+ if (width <= 0 || height <= 0 || cornerRadius <= 0) return "";
253
+ return getSvgPath({ width, height, cornerRadius, cornerSmoothing, preserveSmoothing, g2Continuous });
254
+ }
255
+
256
+ // src/react-native/index.tsx
257
+ import { jsx, jsxs } from "react/jsx-runtime";
258
+ function SquircleNative({
259
+ children,
260
+ cornerRadius,
261
+ cornerSmoothing = 1,
262
+ squircleBorder = false,
263
+ borderColor = "transparent",
264
+ borderWidth = 1,
265
+ backgroundColor,
266
+ style,
267
+ ...rest
268
+ }) {
269
+ const [size, setSize] = useState({ width: 0, height: 0 });
270
+ const onLayout = useCallback((event) => {
271
+ const { width: width2, height: height2 } = event.nativeEvent.layout;
272
+ setSize({ width: width2, height: height2 });
273
+ }, []);
274
+ const { width, height } = size;
275
+ let squirclePath = "";
276
+ if (width > 0 && height > 0 && cornerRadius > 0) {
277
+ squirclePath = computeSquirclePath(width, height, cornerRadius, cornerSmoothing);
278
+ }
279
+ const clipId = `sq-${Math.random().toString(36).slice(2, 9)}`;
280
+ const hasClip = squirclePath.length > 0;
281
+ const hasBorder = squircleBorder && borderWidth > 0 && borderColor !== "transparent";
282
+ return /* @__PURE__ */ jsxs(View, { onLayout, style: [{ position: "relative" }, style], ...rest, children: [
283
+ hasClip && /* @__PURE__ */ jsx(View, { style: { position: "absolute", inset: 0 }, pointerEvents: "none", children: /* @__PURE__ */ jsxs(Svg, { width, height, viewBox: `0 0 ${width} ${height}`, children: [
284
+ /* @__PURE__ */ jsx(Defs, { children: /* @__PURE__ */ jsx(ClipPath, { id: clipId, children: /* @__PURE__ */ jsx(Path, { d: squirclePath }) }) }),
285
+ /* @__PURE__ */ jsx(
286
+ Rect,
287
+ {
288
+ width,
289
+ height,
290
+ fill: backgroundColor || "transparent",
291
+ clipPath: `url(#${clipId})`
292
+ }
293
+ ),
294
+ hasBorder && /* @__PURE__ */ jsx(
295
+ Rect,
296
+ {
297
+ width,
298
+ height,
299
+ fill: "none",
300
+ stroke: borderColor,
301
+ strokeWidth: borderWidth,
302
+ clipPath: `url(#${clipId})`
303
+ }
304
+ )
305
+ ] }) }),
306
+ /* @__PURE__ */ jsx(
307
+ View,
308
+ {
309
+ style: {
310
+ position: "relative",
311
+ zIndex: 1,
312
+ borderRadius: hasClip ? void 0 : cornerRadius,
313
+ backgroundColor: hasClip ? "transparent" : backgroundColor,
314
+ overflow: "hidden"
315
+ },
316
+ children
317
+ }
318
+ )
319
+ ] });
320
+ }
321
+ var react_native_default = SquircleNative;
322
+ export {
323
+ SquircleNative,
324
+ react_native_default as default
325
+ };
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "@tetrax/squircle",
3
+ "version": "1.0.0",
4
+ "description": "Figma-accurate squircles for React & React Native with Tailwind CSS classes",
5
+ "source": "src/index.ts",
6
+ "main": "dist/index.js",
7
+ "module": "dist/index.mjs",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js",
13
+ "types": "./dist/index.d.ts"
14
+ },
15
+ "./react": {
16
+ "import": "./dist/react/index.mjs",
17
+ "require": "./dist/react/index.js",
18
+ "types": "./dist/react/index.d.ts"
19
+ },
20
+ "./react-native": {
21
+ "import": "./dist/react-native/index.mjs",
22
+ "require": "./dist/react-native/index.js",
23
+ "types": "./dist/react-native/index.d.ts"
24
+ },
25
+ "./core": {
26
+ "import": "./dist/core.mjs",
27
+ "require": "./dist/core.js",
28
+ "types": "./dist/core.d.ts"
29
+ }
30
+ },
31
+ "files": [
32
+ "dist"
33
+ ],
34
+ "scripts": {
35
+ "build": "tsup && node -e \"const fs = require('fs'); fs.mkdirSync('dist/react-native', { recursive: true }); fs.copyFileSync('src/react-native/index.d.ts', 'dist/react-native/index.d.ts')\"",
36
+ "dev": "tsup --watch",
37
+ "clean": "rm -rf dist",
38
+ "typecheck": "tsc --noEmit"
39
+ },
40
+ "peerDependencies": {
41
+ "react": ">=16.8.0",
42
+ "react-native": ">=0.60.0",
43
+ "react-native-svg": ">=12.0.0"
44
+ },
45
+ "peerDependenciesMeta": {
46
+ "react-native": {
47
+ "optional": true
48
+ },
49
+ "react-native-svg": {
50
+ "optional": true
51
+ }
52
+ },
53
+ "devDependencies": {
54
+ "@types/react": "^18.0.0",
55
+ "@types/react-native": "^0.72.8",
56
+ "react": "^18.0.0",
57
+ "tsup": "^8.0.0",
58
+ "typescript": "^5.0.0"
59
+ },
60
+ "keywords": [
61
+ "squircle",
62
+ "react",
63
+ "react-native",
64
+ "figma",
65
+ "tailwind",
66
+ "css",
67
+ "clip-path",
68
+ "corners"
69
+ ],
70
+ "license": "MIT"
71
+ }