@ariaui/radius 0.1.2 → 0.1.4

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,305 @@
1
+ /**
2
+ * A single radius value.
3
+ *
4
+ * The original unit is preserved; conversion to other units is on-demand
5
+ * via the format module.
6
+ */
7
+ interface RadiusValue {
8
+ /** Numeric value in the original unit */
9
+ readonly value: number;
10
+ /** CSS unit */
11
+ readonly unit: "px" | "rem" | "%";
12
+ /** Original authored string, e.g. `"8px"`, `"0.5rem"` */
13
+ readonly original: string;
14
+ }
15
+ /**
16
+ * Per-corner radii (CSS `border-radius` longhand).
17
+ */
18
+ interface CornerRadii {
19
+ readonly topLeft: RadiusValue;
20
+ readonly topRight: RadiusValue;
21
+ readonly bottomRight: RadiusValue;
22
+ readonly bottomLeft: RadiusValue;
23
+ }
24
+ /** Corner position names. */
25
+ type Corner = "topLeft" | "topRight" | "bottomRight" | "bottomLeft";
26
+ /**
27
+ * Named radius scale levels matching `@ariaui/tokens`.
28
+ */
29
+ type RadiusLevel = "none" | "xxs" | "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl" | "4xl" | "full";
30
+ /** A radius scale: level name → value mapping. */
31
+ type RadiusScale = Record<string, RadiusValue>;
32
+ /** Breakpoint name → pixel value. */
33
+ type Breakpoints = Record<string, number>;
34
+ /** Responsive radius: default + optional breakpoint overrides. */
35
+ interface ResponsiveRadius {
36
+ readonly default: RadiusValue;
37
+ readonly breakpoints: Record<string, RadiusValue>;
38
+ }
39
+
40
+ /**
41
+ * Parse a single CSS radius value string into a `RadiusValue`.
42
+ *
43
+ * Accepts `px`, `rem`, and `%` units. Plain numbers are treated as `px`.
44
+ *
45
+ * @example
46
+ * parseRadius("8px") → { value: 8, unit: "px", original: "8px" }
47
+ * parseRadius("0.5rem") → { value: 0.5, unit: "rem", original: "0.5rem" }
48
+ * parseRadius(8) → { value: 8, unit: "px", original: "8px" }
49
+ */
50
+ declare function parseRadius(input: string | number): RadiusValue;
51
+ /**
52
+ * Parse a CSS `border-radius` shorthand into per-corner `CornerRadii`.
53
+ *
54
+ * Follows CSS shorthand rules:
55
+ * - 1 value: all corners
56
+ * - 2 values: top-left/bottom-right, top-right/bottom-left
57
+ * - 3 values: top-left, top-right/bottom-left, bottom-right
58
+ * - 4 values: top-left, top-right, bottom-right, bottom-left
59
+ *
60
+ * Does **not** support the `x / y` elliptical radius syntax.
61
+ *
62
+ * @example
63
+ * parseCorners("8px") → all corners 8px
64
+ * parseCorners("8px 4px") → TL/BR=8, TR/BL=4
65
+ * parseCorners("8px 4px 2px 0px") → TL=8, TR=4, BR=2, BL=0
66
+ */
67
+ declare function parseCorners(input: string): CornerRadii;
68
+
69
+ /**
70
+ * Create a linear radius scale.
71
+ *
72
+ * Generates `count` steps starting from `base`, incrementing by `step`.
73
+ * Steps are named `"0"`, `"1"`, `"2"`, etc.
74
+ *
75
+ * @example
76
+ * createLinearScale(0, 4, 6)
77
+ * // → { "0": 0px, "1": 4px, "2": 8px, "3": 12px, "4": 16px, "5": 20px }
78
+ */
79
+ declare function createLinearScale(base?: number, step?: number, count?: number): RadiusScale;
80
+ /**
81
+ * Create a radius scale from explicit named values.
82
+ *
83
+ * @example
84
+ * createScale({ none: 0, sm: 4, md: 8, lg: 12, full: "9999px" })
85
+ */
86
+ declare function createScale(values: Record<string, number | string>): RadiusScale;
87
+ /**
88
+ * Scale all values in a radius scale by a factor.
89
+ *
90
+ * @example
91
+ * scaleBy(scale, 1.5) // 50% larger radii
92
+ */
93
+ declare function scaleBy(scale: RadiusScale, factor: number): RadiusScale;
94
+ /**
95
+ * Clamp all values in a scale to a [min, max] range.
96
+ *
97
+ * Values are clamped in their original unit.
98
+ */
99
+ declare function clampScale(scale: RadiusScale, min: number, max: number): RadiusScale;
100
+ /**
101
+ * Get level names sorted by value (ascending).
102
+ */
103
+ declare function sortedLevels(scale: RadiusScale): string[];
104
+ /**
105
+ * Step to the next or previous level in a scale.
106
+ *
107
+ * Levels are ordered by value. Returns the current level if at a boundary.
108
+ */
109
+ declare function stepRadius(scale: RadiusScale, current: string, direction: "up" | "down"): string;
110
+
111
+ /**
112
+ * Create uniform corners (all four corners the same).
113
+ *
114
+ * @example
115
+ * uniformCorners("8px")
116
+ * uniformCorners(8)
117
+ * uniformCorners({ value: 8, unit: "px", original: "8px" })
118
+ */
119
+ declare function uniformCorners(radius: RadiusValue | string | number): CornerRadii;
120
+ /**
121
+ * Set a single corner, returning new CornerRadii.
122
+ *
123
+ * @example
124
+ * setCorner(corners, "topLeft", "12px")
125
+ */
126
+ declare function setCorner(corners: CornerRadii, corner: Corner, value: RadiusValue | string | number): CornerRadii;
127
+ /**
128
+ * Set both top corners.
129
+ */
130
+ declare function setTop(corners: CornerRadii, value: RadiusValue | string | number): CornerRadii;
131
+ /**
132
+ * Set both bottom corners.
133
+ */
134
+ declare function setBottom(corners: CornerRadii, value: RadiusValue | string | number): CornerRadii;
135
+ /**
136
+ * Set both left corners.
137
+ */
138
+ declare function setLeft(corners: CornerRadii, value: RadiusValue | string | number): CornerRadii;
139
+ /**
140
+ * Set both right corners.
141
+ */
142
+ declare function setRight(corners: CornerRadii, value: RadiusValue | string | number): CornerRadii;
143
+ /**
144
+ * Round only the specified corners; all others are set to 0.
145
+ *
146
+ * @example
147
+ * roundCorners("12px", "topLeft", "topRight")
148
+ * // → TL=12px, TR=12px, BR=0px, BL=0px
149
+ */
150
+ declare function roundCorners(radius: RadiusValue | string | number, ...corners: Corner[]): CornerRadii;
151
+
152
+ /**
153
+ * Calculate the inner radius for a nested element.
154
+ *
155
+ * When a rounded parent has padding/gap, the child needs a smaller radius
156
+ * to visually match the parent's curve: `inner = outer - gap`.
157
+ *
158
+ * Result is clamped to ≥ 0 (except for `9999px` pill shapes, where
159
+ * the subtraction still yields a huge value = still pill).
160
+ *
161
+ * @param outer - Outer (parent) radius
162
+ * @param gap - Gap between parent and child edges (in the same unit as outer)
163
+ *
164
+ * @example
165
+ * innerRadius("12px", 4) → { value: 8, unit: "px", original: "8px" }
166
+ * innerRadius("9999px", 8) → { value: 9991, unit: "px", original: "9991px" }
167
+ * innerRadius("4px", 8) → { value: 0, unit: "px", original: "0px" }
168
+ */
169
+ declare function innerRadius(outer: RadiusValue | string | number, gap: number): RadiusValue;
170
+ /**
171
+ * Calculate inner corners from outer corners and a uniform gap.
172
+ *
173
+ * Each corner's radius is reduced by `gap`, clamped to ≥ 0.
174
+ *
175
+ * @example
176
+ * innerCorners(outerCorners, 8)
177
+ */
178
+ declare function innerCorners(outer: CornerRadii, gap: number): CornerRadii;
179
+ /**
180
+ * Calculate the gap needed to achieve a desired inner radius.
181
+ *
182
+ * `gap = outer.value - inner.value` (clamped to ≥ 0).
183
+ */
184
+ declare function requiredGap(outer: RadiusValue | string | number, inner: RadiusValue | string | number): number;
185
+ /**
186
+ * Suggest matching radii for a parent-child nesting pair.
187
+ *
188
+ * @param outerRadius - Desired outer radius in px
189
+ * @param gap - Padding/gap between parent and child
190
+ *
191
+ * @returns `{ outer, inner }` — both as `RadiusValue`
192
+ *
193
+ * @example
194
+ * nestingPair(12, 8) → { outer: 12px, inner: 4px }
195
+ * nestingPair(6, 8) → { outer: 6px, inner: 0px }
196
+ */
197
+ declare function nestingPair(outerRadius: number, gap: number): {
198
+ outer: RadiusValue;
199
+ inner: RadiusValue;
200
+ };
201
+
202
+ /**
203
+ * Default breakpoints matching Tailwind CSS v4 defaults.
204
+ */
205
+ declare const BREAKPOINTS: Breakpoints;
206
+ /**
207
+ * Create a responsive radius from a config map.
208
+ *
209
+ * The `"default"` key is required; other keys must match breakpoint names.
210
+ *
211
+ * @example
212
+ * responsiveRadius({ default: "4px", md: "8px", lg: "12px" })
213
+ * responsiveRadius({ default: 4, md: 8, lg: 12 })
214
+ */
215
+ declare function responsiveRadius(config: Record<string, string | number>, breakpoints?: Breakpoints): ResponsiveRadius;
216
+ /**
217
+ * Scale down a radius for mobile viewports.
218
+ *
219
+ * @param value - Original radius
220
+ * @param factor - Scale factor (default 0.75)
221
+ *
222
+ * @example
223
+ * mobileRadius(parseRadius("12px")) → { value: 9, ... }
224
+ * mobileRadius(parseRadius("12px"), 0.5) → { value: 6, ... }
225
+ */
226
+ declare function mobileRadius(value: RadiusValue, factor?: number): RadiusValue;
227
+ /**
228
+ * Get the sorted breakpoint entries from a `ResponsiveRadius`,
229
+ * ordered by viewport width (ascending).
230
+ */
231
+ declare function sortedBreakpoints(responsive: ResponsiveRadius, breakpoints?: Breakpoints): Array<[string, number, RadiusValue]>;
232
+
233
+ /**
234
+ * Format a `RadiusValue` as a CSS value string in its original unit.
235
+ *
236
+ * @example
237
+ * toCSS(parseRadius("8px")) → "8px"
238
+ * toCSS(parseRadius("0.5rem")) → "0.5rem"
239
+ */
240
+ declare function toCSS(value: RadiusValue): string;
241
+ /**
242
+ * Convert a `RadiusValue` to rem.
243
+ *
244
+ * @param base - Root font size in px (default 16)
245
+ *
246
+ * @example
247
+ * toRem(parseRadius("8px")) → "0.5rem"
248
+ * toRem(parseRadius("0.5rem")) → "0.5rem"
249
+ * toRem(parseRadius("24px"), 16) → "1.5rem"
250
+ */
251
+ declare function toRem(value: RadiusValue, base?: number): string;
252
+ /**
253
+ * Convert a `RadiusValue` to px.
254
+ *
255
+ * @param base - Root font size in px (default 16, used for rem → px)
256
+ */
257
+ declare function toPx(value: RadiusValue, base?: number): string;
258
+ /**
259
+ * Format `CornerRadii` as a CSS `border-radius` shorthand.
260
+ *
261
+ * Uses the most compact shorthand form possible.
262
+ *
263
+ * @example
264
+ * toCSSShorthand(uniformCorners(8)) → "8px"
265
+ * toCSSShorthand(corners) → "8px 4px 2px 0px"
266
+ */
267
+ declare function toCSSShorthand(corners: CornerRadii): string;
268
+ /**
269
+ * Format `CornerRadii` as React `CSSProperties`.
270
+ *
271
+ * @example
272
+ * toCSSProperties(corners)
273
+ * // → { borderTopLeftRadius: "8px", borderTopRightRadius: "4px", ... }
274
+ */
275
+ declare function toCSSProperties(corners: CornerRadii): Record<string, string>;
276
+ /**
277
+ * Format `CornerRadii` as a CSS declaration block.
278
+ *
279
+ * Uses shorthand when possible.
280
+ */
281
+ declare function toCSSDeclaration(corners: CornerRadii): string;
282
+ /**
283
+ * Format a `RadiusValue` as a Tailwind CSS `rounded-[...]` class.
284
+ *
285
+ * @example
286
+ * toTailwindClass(parseRadius("8px")) → "rounded-[8px]"
287
+ */
288
+ declare function toTailwindClass(value: RadiusValue): string;
289
+ /**
290
+ * Format `CornerRadii` as Tailwind per-corner classes.
291
+ *
292
+ * @example
293
+ * toTailwindCorners(corners)
294
+ * // → "rounded-tl-[8px] rounded-tr-[4px] rounded-br-[2px] rounded-bl-[0px]"
295
+ */
296
+ declare function toTailwindCorners(corners: CornerRadii): string;
297
+ /**
298
+ * Generate a CSS `var()` reference for a token radius level.
299
+ *
300
+ * @example
301
+ * toCSSVar("md") → "var(--radius-md)"
302
+ */
303
+ declare function toCSSVar(level: string): string;
304
+
305
+ export { BREAKPOINTS, type Breakpoints, type Corner, type CornerRadii, type RadiusLevel, type RadiusScale, type RadiusValue, type ResponsiveRadius, clampScale, createLinearScale, createScale, innerCorners, innerRadius, mobileRadius, nestingPair, parseCorners, parseRadius, requiredGap, responsiveRadius, roundCorners, scaleBy, setBottom, setCorner, setLeft, setRight, setTop, sortedBreakpoints, sortedLevels, stepRadius, toCSS, toCSSDeclaration, toCSSProperties, toCSSShorthand, toCSSVar, toPx, toRem, toTailwindClass, toTailwindCorners, uniformCorners };
package/dist/index.js ADDED
@@ -0,0 +1,68 @@
1
+ import {
2
+ BREAKPOINTS,
3
+ clampScale,
4
+ createLinearScale,
5
+ createScale,
6
+ innerCorners,
7
+ innerRadius,
8
+ mobileRadius,
9
+ nestingPair,
10
+ parseCorners,
11
+ parseRadius,
12
+ requiredGap,
13
+ responsiveRadius,
14
+ roundCorners,
15
+ scaleBy,
16
+ setBottom,
17
+ setCorner,
18
+ setLeft,
19
+ setRight,
20
+ setTop,
21
+ sortedBreakpoints,
22
+ sortedLevels,
23
+ stepRadius,
24
+ toCSS,
25
+ toCSSDeclaration,
26
+ toCSSProperties,
27
+ toCSSShorthand,
28
+ toCSSVar,
29
+ toPx,
30
+ toRem,
31
+ toTailwindClass,
32
+ toTailwindCorners,
33
+ uniformCorners
34
+ } from "./chunk-3Y6IG3UW.js";
35
+ export {
36
+ BREAKPOINTS,
37
+ clampScale,
38
+ createLinearScale,
39
+ createScale,
40
+ innerCorners,
41
+ innerRadius,
42
+ mobileRadius,
43
+ nestingPair,
44
+ parseCorners,
45
+ parseRadius,
46
+ requiredGap,
47
+ responsiveRadius,
48
+ roundCorners,
49
+ scaleBy,
50
+ setBottom,
51
+ setCorner,
52
+ setLeft,
53
+ setRight,
54
+ setTop,
55
+ sortedBreakpoints,
56
+ sortedLevels,
57
+ stepRadius,
58
+ toCSS,
59
+ toCSSDeclaration,
60
+ toCSSProperties,
61
+ toCSSShorthand,
62
+ toCSSVar,
63
+ toPx,
64
+ toRem,
65
+ toTailwindClass,
66
+ toTailwindCorners,
67
+ uniformCorners
68
+ };