@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,18 @@
1
+ import type { ReactNode } from "react";
2
+ import type { ViewStyle } from "react-native";
3
+
4
+ export interface SquircleNativeProps {
5
+ children?: ReactNode;
6
+ cornerRadius: number;
7
+ cornerSmoothing?: number;
8
+ preserveSmoothing?: boolean;
9
+ squircleBorder?: boolean;
10
+ borderColor?: string;
11
+ borderWidth?: number;
12
+ backgroundColor?: string;
13
+ style?: ViewStyle;
14
+ [key: string]: any;
15
+ }
16
+
17
+ export declare function SquircleNative(props: SquircleNativeProps): JSX.Element;
18
+ export default SquircleNative;
@@ -0,0 +1,360 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/react-native/index.tsx
31
+ var react_native_exports = {};
32
+ __export(react_native_exports, {
33
+ SquircleNative: () => SquircleNative,
34
+ default: () => react_native_default
35
+ });
36
+ module.exports = __toCommonJS(react_native_exports);
37
+ var import_react = require("react");
38
+ var import_react_native = require("react-native");
39
+ var import_react_native_svg = __toESM(require("react-native-svg"));
40
+
41
+ // src/core/squircle-path.ts
42
+ function toRadians(deg) {
43
+ return deg * Math.PI / 180;
44
+ }
45
+ function rounded(strings, ...values) {
46
+ return strings.reduce((acc, str, i) => {
47
+ const value = values[i];
48
+ return typeof value === "number" ? acc + str + value.toFixed(4) : acc + str + (value ?? "");
49
+ }, "");
50
+ }
51
+ function getPathParamsForCorner({
52
+ cornerRadius,
53
+ cornerSmoothing,
54
+ preserveSmoothing,
55
+ roundingAndSmoothingBudget
56
+ }) {
57
+ if (cornerRadius <= 0 || roundingAndSmoothingBudget <= 0) {
58
+ return { a: 0, b: 0, c: 0, d: 0, p: 0, cornerRadius: 0, arcSectionLength: 0 };
59
+ }
60
+ let p = (1 + cornerSmoothing) * cornerRadius;
61
+ if (!preserveSmoothing) {
62
+ const maxCornerSmoothing = roundingAndSmoothingBudget / cornerRadius - 1;
63
+ cornerSmoothing = Math.min(cornerSmoothing, maxCornerSmoothing);
64
+ p = Math.min(p, roundingAndSmoothingBudget);
65
+ }
66
+ const arcMeasure = 90 * (1 - cornerSmoothing);
67
+ const arcSectionLength = Math.sin(toRadians(arcMeasure / 2)) * cornerRadius * Math.sqrt(2);
68
+ const angleAlpha = (90 - arcMeasure) / 2;
69
+ const p3ToP4Distance = cornerRadius * Math.tan(toRadians(angleAlpha / 2));
70
+ const angleBeta = 45 * cornerSmoothing;
71
+ const c = p3ToP4Distance * Math.cos(toRadians(angleBeta));
72
+ const d = c * Math.tan(toRadians(angleBeta));
73
+ let b = (p - arcSectionLength - c - d) / 3;
74
+ let a = 2 * b;
75
+ if (preserveSmoothing && p > roundingAndSmoothingBudget) {
76
+ const p1ToP3MaxDistance = roundingAndSmoothingBudget - d - arcSectionLength - c;
77
+ const minA = p1ToP3MaxDistance / 6;
78
+ const maxB = p1ToP3MaxDistance - minA;
79
+ b = Math.min(b, maxB);
80
+ a = p1ToP3MaxDistance - b;
81
+ p = Math.min(p, roundingAndSmoothingBudget);
82
+ }
83
+ return { a, b, c, d, p, arcSectionLength, cornerRadius };
84
+ }
85
+ function drawTopRightPath({ cornerRadius, a, b, c, d, p, arcSectionLength }) {
86
+ if (cornerRadius) {
87
+ 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}`;
88
+ }
89
+ return rounded`l ${p} 0`;
90
+ }
91
+ function drawBottomRightPath({ cornerRadius, a, b, c, d, p, arcSectionLength }) {
92
+ if (cornerRadius) {
93
+ 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}`;
94
+ }
95
+ return rounded`l 0 ${p}`;
96
+ }
97
+ function drawBottomLeftPath({ cornerRadius, a, b, c, d, p, arcSectionLength }) {
98
+ if (cornerRadius) {
99
+ 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)}`;
100
+ }
101
+ return rounded`l ${-p} 0`;
102
+ }
103
+ function drawTopLeftPath({ cornerRadius, a, b, c, d, p, arcSectionLength }) {
104
+ if (cornerRadius) {
105
+ 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}`;
106
+ }
107
+ return rounded`l 0 ${-p}`;
108
+ }
109
+ function getSVGPathFromPathParams({
110
+ width,
111
+ height,
112
+ topLeftPathParams,
113
+ topRightPathParams,
114
+ bottomRightPathParams,
115
+ bottomLeftPathParams
116
+ }) {
117
+ return `
118
+ M ${width - topRightPathParams.p} 0
119
+ ${drawTopRightPath(topRightPathParams)}
120
+ L ${width} ${height - bottomRightPathParams.p}
121
+ ${drawBottomRightPath(bottomRightPathParams)}
122
+ L ${bottomLeftPathParams.p} ${height}
123
+ ${drawBottomLeftPath(bottomLeftPathParams)}
124
+ L 0 ${topLeftPathParams.p}
125
+ ${drawTopLeftPath(topLeftPathParams)}
126
+ Z
127
+ `.replace(/[\t\s\n]+/g, " ").trim();
128
+ }
129
+ var adjacentsByCorner = {
130
+ topLeft: [
131
+ { corner: "topRight", side: "top" },
132
+ { corner: "bottomLeft", side: "left" }
133
+ ],
134
+ topRight: [
135
+ { corner: "topLeft", side: "top" },
136
+ { corner: "bottomRight", side: "right" }
137
+ ],
138
+ bottomLeft: [
139
+ { corner: "bottomRight", side: "bottom" },
140
+ { corner: "topLeft", side: "left" }
141
+ ],
142
+ bottomRight: [
143
+ { corner: "bottomLeft", side: "bottom" },
144
+ { corner: "topRight", side: "right" }
145
+ ]
146
+ };
147
+ function distributeAndNormalize({
148
+ topLeftCornerRadius,
149
+ topRightCornerRadius,
150
+ bottomRightCornerRadius,
151
+ bottomLeftCornerRadius,
152
+ width,
153
+ height
154
+ }) {
155
+ const roundingAndSmoothingBudgetMap = {
156
+ topLeft: -1,
157
+ topRight: -1,
158
+ bottomLeft: -1,
159
+ bottomRight: -1
160
+ };
161
+ const cornerRadiusMap = {
162
+ topLeft: topLeftCornerRadius,
163
+ topRight: topRightCornerRadius,
164
+ bottomLeft: bottomLeftCornerRadius,
165
+ bottomRight: bottomRightCornerRadius
166
+ };
167
+ Object.entries(cornerRadiusMap).sort(([, r1], [, r2]) => r2 - r1).forEach(([corner, radius]) => {
168
+ const adjacents = adjacentsByCorner[corner];
169
+ const budget = Math.min(
170
+ ...adjacents.map((adj) => {
171
+ const adjRadius = cornerRadiusMap[adj.corner];
172
+ if (radius === 0 && adjRadius === 0) return 0;
173
+ const adjBudget = roundingAndSmoothingBudgetMap[adj.corner];
174
+ const sideLen = adj.side === "top" || adj.side === "bottom" ? width : height;
175
+ return adjBudget >= 0 ? sideLen - roundingAndSmoothingBudgetMap[adj.corner] : radius / (radius + adjRadius) * sideLen;
176
+ })
177
+ );
178
+ roundingAndSmoothingBudgetMap[corner] = budget;
179
+ cornerRadiusMap[corner] = Math.min(radius, budget);
180
+ });
181
+ return {
182
+ topLeft: { radius: cornerRadiusMap.topLeft, roundingAndSmoothingBudget: roundingAndSmoothingBudgetMap.topLeft },
183
+ topRight: { radius: cornerRadiusMap.topRight, roundingAndSmoothingBudget: roundingAndSmoothingBudgetMap.topRight },
184
+ bottomLeft: { radius: cornerRadiusMap.bottomLeft, roundingAndSmoothingBudget: roundingAndSmoothingBudgetMap.bottomLeft },
185
+ bottomRight: { radius: cornerRadiusMap.bottomRight, roundingAndSmoothingBudget: roundingAndSmoothingBudgetMap.bottomRight }
186
+ };
187
+ }
188
+ function drawPureBezierTopRight(r) {
189
+ if (r <= 0) return "";
190
+ 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}`;
191
+ }
192
+ function drawPureBezierBottomRight(r) {
193
+ if (r <= 0) return "";
194
+ 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}`;
195
+ }
196
+ function drawPureBezierBottomLeft(r) {
197
+ if (r <= 0) return "";
198
+ 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}`;
199
+ }
200
+ function drawPureBezierTopLeft(r) {
201
+ if (r <= 0) return "";
202
+ 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}`;
203
+ }
204
+ function getPureBezierSVGPath(width, height, tl, tr, br, bl) {
205
+ return `
206
+ M ${width - tr} 0
207
+ ${drawPureBezierTopRight(tr)}
208
+ L ${width} ${height - br}
209
+ ${drawPureBezierBottomRight(br)}
210
+ L ${bl} ${height}
211
+ ${drawPureBezierBottomLeft(bl)}
212
+ L 0 ${tl}
213
+ ${drawPureBezierTopLeft(tl)}
214
+ Z
215
+ `.replace(/[\t\s\n]+/g, " ").trim();
216
+ }
217
+ function getSvgPath({
218
+ cornerRadius = 0,
219
+ topLeftCornerRadius,
220
+ topRightCornerRadius,
221
+ bottomRightCornerRadius,
222
+ bottomLeftCornerRadius,
223
+ cornerSmoothing = 1,
224
+ width,
225
+ height,
226
+ preserveSmoothing = false,
227
+ g2Continuous = false
228
+ }) {
229
+ const tl = topLeftCornerRadius ?? cornerRadius;
230
+ const tr = topRightCornerRadius ?? cornerRadius;
231
+ const bl = bottomLeftCornerRadius ?? cornerRadius;
232
+ const br = bottomRightCornerRadius ?? cornerRadius;
233
+ if (g2Continuous) {
234
+ const { topLeft: topLeft2, topRight: topRight2, bottomLeft: bottomLeft2, bottomRight: bottomRight2 } = distributeAndNormalize({
235
+ topLeftCornerRadius: tl,
236
+ topRightCornerRadius: tr,
237
+ bottomRightCornerRadius: br,
238
+ bottomLeftCornerRadius: bl,
239
+ width,
240
+ height
241
+ });
242
+ return getPureBezierSVGPath(
243
+ width,
244
+ height,
245
+ topLeft2.radius,
246
+ topRight2.radius,
247
+ bottomRight2.radius,
248
+ bottomLeft2.radius
249
+ );
250
+ }
251
+ if (tl === tr && tr === br && br === bl) {
252
+ const roundingAndSmoothingBudget = Math.min(width, height) / 2;
253
+ const clampedRadius = Math.min(tl, roundingAndSmoothingBudget);
254
+ const params = getPathParamsForCorner({
255
+ cornerRadius: clampedRadius,
256
+ cornerSmoothing,
257
+ preserveSmoothing,
258
+ roundingAndSmoothingBudget
259
+ });
260
+ return getSVGPathFromPathParams({
261
+ width,
262
+ height,
263
+ topLeftPathParams: params,
264
+ topRightPathParams: params,
265
+ bottomLeftPathParams: params,
266
+ bottomRightPathParams: params
267
+ });
268
+ }
269
+ const { topLeft, topRight, bottomLeft, bottomRight } = distributeAndNormalize({
270
+ topLeftCornerRadius: tl,
271
+ topRightCornerRadius: tr,
272
+ bottomRightCornerRadius: br,
273
+ bottomLeftCornerRadius: bl,
274
+ width,
275
+ height
276
+ });
277
+ return getSVGPathFromPathParams({
278
+ width,
279
+ height,
280
+ topLeftPathParams: getPathParamsForCorner({ cornerSmoothing, preserveSmoothing, cornerRadius: topLeft.radius, roundingAndSmoothingBudget: topLeft.roundingAndSmoothingBudget }),
281
+ topRightPathParams: getPathParamsForCorner({ cornerSmoothing, preserveSmoothing, cornerRadius: topRight.radius, roundingAndSmoothingBudget: topRight.roundingAndSmoothingBudget }),
282
+ bottomRightPathParams: getPathParamsForCorner({ cornerSmoothing, preserveSmoothing, cornerRadius: bottomRight.radius, roundingAndSmoothingBudget: bottomRight.roundingAndSmoothingBudget }),
283
+ bottomLeftPathParams: getPathParamsForCorner({ cornerSmoothing, preserveSmoothing, cornerRadius: bottomLeft.radius, roundingAndSmoothingBudget: bottomLeft.roundingAndSmoothingBudget })
284
+ });
285
+ }
286
+ function computeSquirclePath(width, height, cornerRadius, cornerSmoothing = 1, preserveSmoothing = false, g2Continuous = false) {
287
+ if (width <= 0 || height <= 0 || cornerRadius <= 0) return "";
288
+ return getSvgPath({ width, height, cornerRadius, cornerSmoothing, preserveSmoothing, g2Continuous });
289
+ }
290
+
291
+ // src/react-native/index.tsx
292
+ var import_jsx_runtime = require("react/jsx-runtime");
293
+ function SquircleNative({
294
+ children,
295
+ cornerRadius,
296
+ cornerSmoothing = 1,
297
+ squircleBorder = false,
298
+ borderColor = "transparent",
299
+ borderWidth = 1,
300
+ backgroundColor,
301
+ style,
302
+ ...rest
303
+ }) {
304
+ const [size, setSize] = (0, import_react.useState)({ width: 0, height: 0 });
305
+ const onLayout = (0, import_react.useCallback)((event) => {
306
+ const { width: width2, height: height2 } = event.nativeEvent.layout;
307
+ setSize({ width: width2, height: height2 });
308
+ }, []);
309
+ const { width, height } = size;
310
+ let squirclePath = "";
311
+ if (width > 0 && height > 0 && cornerRadius > 0) {
312
+ squirclePath = computeSquirclePath(width, height, cornerRadius, cornerSmoothing);
313
+ }
314
+ const clipId = `sq-${Math.random().toString(36).slice(2, 9)}`;
315
+ const hasClip = squirclePath.length > 0;
316
+ const hasBorder = squircleBorder && borderWidth > 0 && borderColor !== "transparent";
317
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { onLayout, style: [{ position: "relative" }, style], ...rest, children: [
318
+ hasClip && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { style: { position: "absolute", inset: 0 }, pointerEvents: "none", children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native_svg.default, { width, height, viewBox: `0 0 ${width} ${height}`, children: [
319
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native_svg.Defs, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native_svg.ClipPath, { id: clipId, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native_svg.Path, { d: squirclePath }) }) }),
320
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
321
+ import_react_native_svg.Rect,
322
+ {
323
+ width,
324
+ height,
325
+ fill: backgroundColor || "transparent",
326
+ clipPath: `url(#${clipId})`
327
+ }
328
+ ),
329
+ hasBorder && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
330
+ import_react_native_svg.Rect,
331
+ {
332
+ width,
333
+ height,
334
+ fill: "none",
335
+ stroke: borderColor,
336
+ strokeWidth: borderWidth,
337
+ clipPath: `url(#${clipId})`
338
+ }
339
+ )
340
+ ] }) }),
341
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
342
+ import_react_native.View,
343
+ {
344
+ style: {
345
+ position: "relative",
346
+ zIndex: 1,
347
+ borderRadius: hasClip ? void 0 : cornerRadius,
348
+ backgroundColor: hasClip ? "transparent" : backgroundColor,
349
+ overflow: "hidden"
350
+ },
351
+ children
352
+ }
353
+ )
354
+ ] });
355
+ }
356
+ var react_native_default = SquircleNative;
357
+ // Annotate the CommonJS export names for ESM import in node:
358
+ 0 && (module.exports = {
359
+ SquircleNative
360
+ });