@umber-style/rtl 0.0.0 → 0.10.0-alpha.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,72 @@
1
+ import { CachePlugin, CachePlugin as CachePlugin$1 } from "@umber-style/core";
2
+ /** Which kinds of declaration `rtl` flips: each switch is on by default, except `urls`. */
3
+ interface RtlOptions {
4
+ /** `margin-left` and `margin-right`, and the second and fourth of four values of `margin`. Default true. */
5
+ margin?: boolean;
6
+ /** `padding-left` and `padding-right`, and the second and fourth of four values of `padding`. Default true. */
7
+ padding?: boolean;
8
+ /** The properties `left` and `right`, and the second and fourth of four values of `inset`. Default true. */
9
+ inset?: boolean;
10
+ /** `border-left` and `border-right` with their `-width`, `-color` and `-style`, and four values of `border-width`, `border-color` and `border-style`. Default true. */
11
+ border?: boolean;
12
+ /** The corners `border-top-left-radius` and the like, and the corners of `border-radius`, both axes of the `a / b` form. Default true. */
13
+ borderRadius?: boolean;
14
+ /** `left` and `right` in `float` and `clear`. Default true. */
15
+ float?: boolean;
16
+ /** `left` and `right` in `text-align`. Default true. */
17
+ textAlign?: boolean;
18
+ /** `ltr` and `rtl` in `direction`. Default true. */
19
+ direction?: boolean;
20
+ /** The horizontal offset of each shadow of `box-shadow`. Default true. */
21
+ boxShadow?: boolean;
22
+ /** The horizontal offset of each shadow of `text-shadow`. Default true. */
23
+ textShadow?: boolean;
24
+ /** The argument of `translateX()` and the horizontal argument of `translate()` and `translate3d()` in `transform`, and the first value of the `translate` property. Default true. */
25
+ translateX?: boolean;
26
+ /** `left`, `right` and the horizontal percentage of `background-position` and `background-position-x`. Default true. */
27
+ backgroundPosition?: boolean;
28
+ /** `e-resize` and `w-resize`, `ne-resize` and `nw-resize`, `se-resize` and `sw-resize`, `nesw-resize` and `nwse-resize` in `cursor`. Default true. */
29
+ cursor?: boolean;
30
+ /** The words `left`, `right`, `ltr` and `rtl` in the address of a `url()`, in any declaration. Default false. */
31
+ urls?: boolean;
32
+ }
33
+ /** The direction a style is made for. */
34
+ type NativeDirectionName = 'ltr' | 'rtl';
35
+ /** The value of a property for each direction: either side may be left out. */
36
+ interface DirValues<T> {
37
+ /** The value for left to right, written under `:dir(ltr)`, and for a browser without `:dir()`. */
38
+ ltr?: T;
39
+ /** The value for right to left, written under `:dir(rtl)`. */
40
+ rtl?: T;
41
+ }
42
+ /**
43
+ * A value of a property for each direction (`marginTop: dir({ ltr: 2, rtl: 4 })`): with the plugin
44
+ * `rtl()`, each side is written as it is given under its `:dir()`, and never flipped; a side that is left
45
+ * out writes nothing for that direction. Typed as the values it is given, so it goes wherever they go.
46
+ */
47
+ export declare function dir<T extends string | number>(values: DirValues<T>): T;
48
+ /**
49
+ * The plugin `rtl()` returns: a `CachePlugin` for the web, which also has `native`, the member that the
50
+ * React Native binding runs over its styles. One object serves every platform.
51
+ */
52
+ export interface RtlPlugin extends CachePlugin$1 {
53
+ /**
54
+ * The native style `style` for the direction of `context`: under `rtl`, what the platform does not mirror
55
+ * (the `translateX` of `transform`, `shadowOffset.width`, `textShadowOffset.width`, `textAlign`) is
56
+ * flipped; a `dir()` value is its side of the direction. The same object when nothing changes.
57
+ */
58
+ native<S extends object>(style: S, context: {
59
+ readonly dir: NativeDirectionName;
60
+ }): S;
61
+ }
62
+ /**
63
+ * The plugin of the default style (`configure({ plugins: [rtl()] })`) that writes both directions of a
64
+ * style in one class: a declaration that has a side is written as it is under `:dir(ltr)` and for the
65
+ * other side under `:dir(rtl)` (`margin-left` is `margin-right`, never a logical property), and a value
66
+ * that names or measures a side is written for the other one; `@supports not selector(:dir(ltr))` gives
67
+ * the left-to-right declarations to a browser without `:dir()`. A `dir()` value is written as it is given,
68
+ * each side under its own condition. Logical properties, the values `start` and `end`, vertical values and
69
+ * unknown properties are left as they are. `options` turns kinds off (or `urls` on).
70
+ */
71
+ export declare function rtl(options?: RtlOptions): RtlPlugin;
72
+ export type { CachePlugin, DirValues, RtlOptions };
package/dist/index.mjs ADDED
@@ -0,0 +1,569 @@
1
+ import { markValue } from "@umber-style/core";
2
+ // src/values.ts
3
+ /** The characters that end a word outside parentheses and quotes. */
4
+ const SEPARATORS = " \n\r\f,/";
5
+ /** Whether `char` ends a word: a separator that is outside parentheses and quotes. */
6
+ function isSeparatorAt(char, { depth, quote }) {
7
+ return depth === 0 && quote === "" && SEPARATORS.includes(char);
8
+ }
9
+ /** Moves `scope` over `char`: a quote opens or closes, a parenthesis opens or closes outside a quote. */
10
+ function moveScope(scope, char) {
11
+ if (scope.quote !== "") {
12
+ if (char === scope.quote) scope.quote = "";
13
+ } else if (char === "\"" || char === "'") scope.quote = char;
14
+ else if (char === "(") scope.depth++;
15
+ else if (char === ")" && scope.depth > 0) scope.depth--;
16
+ }
17
+ /** Cuts `value` into its words at white space, commas and slashes, outside parentheses and quotes. */
18
+ function splitWords(value) {
19
+ const words = [];
20
+ const gaps = [];
21
+ const scope = {
22
+ depth: 0,
23
+ quote: ""
24
+ };
25
+ let wordStart = -1;
26
+ let gapStart = 0;
27
+ for (let index = 0; index < value.length; index++) {
28
+ const char = value[index];
29
+ if (isSeparatorAt(char, scope)) {
30
+ if (wordStart !== -1) {
31
+ words.push(value.slice(wordStart, index));
32
+ gapStart = index;
33
+ wordStart = -1;
34
+ }
35
+ continue;
36
+ }
37
+ if (wordStart === -1) {
38
+ gaps.push(value.slice(gapStart, index));
39
+ wordStart = index;
40
+ }
41
+ moveScope(scope, char);
42
+ }
43
+ if (wordStart === -1) gaps.push(value.slice(gapStart));
44
+ else {
45
+ words.push(value.slice(wordStart));
46
+ gaps.push("");
47
+ }
48
+ return {
49
+ words,
50
+ gaps
51
+ };
52
+ }
53
+ /** The value that `split` stands for: each word between its gaps. */
54
+ function joinWords({ words, gaps }) {
55
+ let text = gaps[0];
56
+ for (let index = 0; index < words.length; index++) text += words[index] + gaps[index + 1];
57
+ return text;
58
+ }
59
+ /** `value` with each word replaced by what `replace` returns for it, the text around the words kept. */
60
+ function mapWords(value, replace) {
61
+ const split = splitWords(value);
62
+ return joinWords({
63
+ words: split.words.map((word, index) => replace(word, index)),
64
+ gaps: split.gaps
65
+ });
66
+ }
67
+ /** Whether `char` is a digit or a decimal point: how a number starts after its sign. */
68
+ function isNumberStart(char) {
69
+ return char === "." || char !== void 0 && char >= "0" && char <= "9";
70
+ }
71
+ /** The functions of CSS whose result is a number, a length or a percentage. */
72
+ const MATH_FUNCTIONS = /* @__PURE__ */ new Set([
73
+ "calc",
74
+ "min",
75
+ "max",
76
+ "clamp"
77
+ ]);
78
+ /** Whether `word` is a number, with or without a unit, or a math function (`calc()`, `min()`, `max()`, `clamp()`). */
79
+ function isLength(word) {
80
+ const [first, second] = word;
81
+ if (isNumberStart(first)) return true;
82
+ if ((first === "-" || first === "+") && isNumberStart(second)) return true;
83
+ const open = word.indexOf("(");
84
+ return open > 0 && MATH_FUNCTIONS.has(word.slice(0, open).toLowerCase());
85
+ }
86
+ /**
87
+ * The length `word` with the other sign: `4px` is `-4px`, `-4px` is `4px`, a zero stays as it is, and
88
+ * anything else (a reference, a function) is multiplied by -1 in a `calc()`.
89
+ */
90
+ function negateLength(word) {
91
+ const [first, second] = word;
92
+ if (first === "-" && isNumberStart(second)) return word.slice(1);
93
+ if (first === "+" && isNumberStart(second)) return `-${word.slice(1)}`;
94
+ if (!isNumberStart(first)) return `calc(-1 * ${word})`;
95
+ return Number.parseFloat(word) === 0 ? word : `-${word}`;
96
+ }
97
+ // src/background.ts
98
+ /** Whether `text` is a number as CSS writes one: digits, a sign, a decimal point. */
99
+ function isNumber(text) {
100
+ return text !== "" && text.trim() === text && Number.isFinite(Number(text));
101
+ }
102
+ /** The percentage `word` measured from the other side (`20%` is `80%`, with as many decimals), or `word` when it is none. */
103
+ function flipPercentage(word) {
104
+ const number = word.slice(0, -1);
105
+ if (!word.endsWith("%") || !isNumber(number)) return word;
106
+ const point = number.indexOf(".");
107
+ const decimals = point === -1 ? 0 : number.length - point - 1;
108
+ return `${(100 - Number(number)).toFixed(decimals)}%`;
109
+ }
110
+ /** `word` for the other side when it is `left` or `right`. */
111
+ function swapSide(word) {
112
+ if (word === "left") return "right";
113
+ return word === "right" ? "left" : word;
114
+ }
115
+ /**
116
+ * The `background-position` or `background-position-x` value `value` for the other side, each layer of
117
+ * the list on its own: `left` and `right` swap, and in a layer of one or two words the first word, when it
118
+ * is a percentage, is measured from the other side. In a layer of three or four words each offset belongs
119
+ * to the keyword before it, so swapping the keyword is enough. A percentage inside a function (`calc()`)
120
+ * is not read.
121
+ */
122
+ function flipBackgroundPosition(value) {
123
+ const { words, gaps } = splitWords(value);
124
+ const count = words.at(-1) === "!important" ? words.length - 1 : words.length;
125
+ const flipped = words.map(swapSide);
126
+ let start = 0;
127
+ for (let end = 1; end <= count; end++) {
128
+ if (end < count && !gaps[end].includes(",")) continue;
129
+ if (end - start <= 2) flipped[start] = flipPercentage(flipped[start]);
130
+ start = end;
131
+ }
132
+ return joinWords({
133
+ words: flipped,
134
+ gaps
135
+ });
136
+ }
137
+ // src/cursor.ts
138
+ /** Each cursor keyword that points to a side, by the keyword that points to the other side. */
139
+ const MIRRORED$1 = {
140
+ "e-resize": "w-resize",
141
+ "w-resize": "e-resize",
142
+ "ne-resize": "nw-resize",
143
+ "nw-resize": "ne-resize",
144
+ "se-resize": "sw-resize",
145
+ "sw-resize": "se-resize",
146
+ "nesw-resize": "nwse-resize",
147
+ "nwse-resize": "nesw-resize"
148
+ };
149
+ /** The `cursor` value `value` for the other side: an east keyword becomes the west one, and the other way round. */
150
+ function flipCursor(value) {
151
+ return mapWords(value, (word) => Object.hasOwn(MIRRORED$1, word) ? MIRRORED$1[word] : word);
152
+ }
153
+ // src/edges.ts
154
+ /** The property `property` for the other side: each `left` part of its name becomes `right`, and each `right` part `left`. */
155
+ function flipEdge(property) {
156
+ return property.split("-").map((part) => {
157
+ if (part === "left") return "right";
158
+ return part === "right" ? "left" : part;
159
+ }).join("-");
160
+ }
161
+ // src/fourValues.ts
162
+ /**
163
+ * The value of a shorthand of the four sides (`margin`, `padding`, `border-width`, `border-color`,
164
+ * `border-style`) for the other side: with four values (top, right, bottom, left), the second and the
165
+ * fourth swap places. One, two or three values name the left side as the right one, and stay.
166
+ */
167
+ function flipFourValues(value) {
168
+ const { words, gaps } = splitWords(value);
169
+ if ((words.at(-1) === "!important" ? words.length - 1 : words.length) !== 4) return value;
170
+ const [top, right, bottom, left] = words;
171
+ return joinWords({
172
+ words: [
173
+ top,
174
+ left,
175
+ bottom,
176
+ right,
177
+ ...words.slice(4)
178
+ ],
179
+ gaps
180
+ });
181
+ }
182
+ // src/keywords.ts
183
+ /** `value` with each word that is `first` written as `second`, and each that is `second` as `first`. */
184
+ function swapKeywords(value, first, second) {
185
+ return mapWords(value, (word) => {
186
+ if (word === first) return second;
187
+ return word === second ? first : word;
188
+ });
189
+ }
190
+ // src/radius.ts
191
+ /**
192
+ * The radii of one axis of `border-radius` (top-left, top-right, bottom-right, bottom-left, with the
193
+ * shorter forms of CSS) for the other side: each corner takes the radius of its mirror.
194
+ */
195
+ function mirrorCorners(radii) {
196
+ if (radii.length === 2) return [radii[1], radii[0]];
197
+ if (radii.length === 3) return [
198
+ radii[1],
199
+ radii[0],
200
+ radii[1],
201
+ radii[2]
202
+ ];
203
+ if (radii.length === 4) return [
204
+ radii[1],
205
+ radii[0],
206
+ radii[3],
207
+ radii[2]
208
+ ];
209
+ return radii;
210
+ }
211
+ /**
212
+ * The `border-radius` value `value` for the other side: the radii of each axis, before and after a `/`,
213
+ * mirrored. A value that changes is written again with one space between its words.
214
+ */
215
+ function flipRadius(value) {
216
+ const { words, gaps } = splitWords(value);
217
+ const isImportant = words.at(-1) === "!important";
218
+ const radii = isImportant ? words.slice(0, -1) : words;
219
+ const slash = gaps.findIndex((gap) => gap.includes("/"));
220
+ const axes = slash === -1 ? [radii] : [radii.slice(0, slash), radii.slice(slash)];
221
+ const mirrored = axes.map(mirrorCorners);
222
+ if (mirrored.every((axis, index) => axis.join(" ") === axes[index].join(" "))) return value;
223
+ const text = mirrored.map((axis) => axis.join(" ")).join(" / ");
224
+ return isImportant ? `${text} !important` : text;
225
+ }
226
+ // src/shadows.ts
227
+ /**
228
+ * The `box-shadow` or `text-shadow` value `value` for the other side: in each shadow of the list, the first
229
+ * length, its horizontal offset, takes the other sign. A shadow whose offsets are no numbers or math
230
+ * functions (a bare `var()`) is left as it is.
231
+ */
232
+ function flipShadow(value) {
233
+ const { words, gaps } = splitWords(value);
234
+ let isFlipped = false;
235
+ return joinWords({
236
+ words: words.map((word, index) => {
237
+ if (gaps[index].includes(",")) isFlipped = false;
238
+ if (isFlipped || !isLength(word)) return word;
239
+ isFlipped = true;
240
+ return negateLength(word);
241
+ }),
242
+ gaps
243
+ });
244
+ }
245
+ // src/translate.ts
246
+ /** The functions of `transform` whose first argument is the horizontal translation. */
247
+ const TRANSLATIONS = /* @__PURE__ */ new Set([
248
+ "translatex",
249
+ "translate",
250
+ "translate3d"
251
+ ]);
252
+ /** The function `word` (`translateX(...)`, `translate(...)`, `translate3d(...)`) with its first argument, the horizontal one, of the other sign. */
253
+ function flipFirstArgument(word, open) {
254
+ const inner = splitWords(word.slice(open + 1, -1));
255
+ if (inner.words.length === 0) return word;
256
+ inner.words[0] = negateLength(inner.words[0]);
257
+ return `${word.slice(0, open + 1)}${joinWords(inner)})`;
258
+ }
259
+ /**
260
+ * The `transform` value `value` for the other side: the argument of each `translateX()` and the first
261
+ * argument of each `translate()` and `translate3d()` take the other sign. The other functions stay as they
262
+ * are.
263
+ */
264
+ function flipTranslate(value) {
265
+ return mapWords(value, (word) => {
266
+ const open = word.indexOf("(");
267
+ const name = word.slice(0, Math.max(open, 0)).toLowerCase();
268
+ return TRANSLATIONS.has(name) && word.endsWith(")") ? flipFirstArgument(word, open) : word;
269
+ });
270
+ }
271
+ /** The keywords that the `translate` property takes in place of its values. */
272
+ const KEYWORDS = /* @__PURE__ */ new Set([
273
+ "none",
274
+ "inherit",
275
+ "initial",
276
+ "unset",
277
+ "revert",
278
+ "revert-layer"
279
+ ]);
280
+ /**
281
+ * The value of the `translate` property for the other side: its first value, the horizontal one, takes the
282
+ * other sign (`none` and the CSS-wide keywords stay).
283
+ */
284
+ function flipTranslateProperty(value) {
285
+ return mapWords(value, (word, index) => index === 0 && !KEYWORDS.has(word) ? negateLength(word) : word);
286
+ }
287
+ // src/urls.ts
288
+ /** Each word of a file name that names a side or a direction, by the word for the other one. */
289
+ const MIRRORED = {
290
+ left: "right",
291
+ right: "left",
292
+ ltr: "rtl",
293
+ rtl: "ltr"
294
+ };
295
+ /** Whether `char` is an ASCII letter: the words of a name are the runs of letters. */
296
+ function isLetter(char) {
297
+ const lower = char.toLowerCase();
298
+ return lower >= "a" && lower <= "z";
299
+ }
300
+ /** The address `address` with each run of letters that is `left`, `right`, `ltr` or `rtl` written for the other side. */
301
+ function flipAddress(address) {
302
+ let text = "";
303
+ let index = 0;
304
+ while (index < address.length) {
305
+ let end = index;
306
+ while (end < address.length && isLetter(address[end])) end++;
307
+ if (end === index) {
308
+ text += address[index];
309
+ index++;
310
+ continue;
311
+ }
312
+ const word = address.slice(index, end);
313
+ text += Object.hasOwn(MIRRORED, word) ? MIRRORED[word] : word;
314
+ index = end;
315
+ }
316
+ return text;
317
+ }
318
+ /** Where the `url(` that starts at `open` ends: the index of its closing parenthesis, quotes skipped, or -1. */
319
+ function findClose(value, open) {
320
+ let quote = "";
321
+ for (let index = open + 4; index < value.length; index++) {
322
+ const char = value[index];
323
+ if (quote !== "") {
324
+ if (char === quote) quote = "";
325
+ } else if (char === "\"" || char === "'") quote = char;
326
+ else if (char === ")") return index;
327
+ }
328
+ return -1;
329
+ }
330
+ /**
331
+ * `value` with the address of each `url()` written for the other side: a word of the address that is
332
+ * `left`, `right`, `ltr` or `rtl`, between characters that are not letters (`arrow-left.png`,
333
+ * `icon_rtl.svg`), becomes its opposite; `bright.png` stays. Nothing outside `url()` is read.
334
+ */
335
+ function flipUrls(value) {
336
+ let text = "";
337
+ let from = 0;
338
+ let open = value.indexOf("url(");
339
+ while (open !== -1) {
340
+ const close = findClose(value, open);
341
+ if (close === -1) break;
342
+ const isFunction = open === 0 || !isLetter(value[open - 1]);
343
+ const address = value.slice(open + 4, close);
344
+ text += value.slice(from, open + 4) + (isFunction ? flipAddress(address) : address);
345
+ from = close;
346
+ open = value.indexOf("url(", close);
347
+ }
348
+ return text + value.slice(from);
349
+ }
350
+ // src/nativeFlips.ts
351
+ /** Whether `value` is the marked value of `dir()`: a mark `dir` and the values of its sides. */
352
+ function isDirValue(value) {
353
+ if (typeof value !== "object" || value === null) return false;
354
+ const { mark, values } = value;
355
+ return mark === "dir" && typeof values === "object" && values !== null;
356
+ }
357
+ /** The number or length `value` with the other sign; anything that is not a number or starts with one stays. */
358
+ function negate(value) {
359
+ if (typeof value === "number") return 0 - value;
360
+ if (typeof value !== "string" || Number.isNaN(Number.parseFloat(value))) return value;
361
+ return value.startsWith("-") ? value.slice(1) : `-${value}`;
362
+ }
363
+ /** One function of a `transform` list (`{ translateX: 8 }`, `{ translate: [8, 2] }`) for the other side. */
364
+ function flipTransformEntry(entry) {
365
+ if (typeof entry !== "object" || entry === null) return entry;
366
+ const flipped = { ...entry };
367
+ if ("translateX" in flipped) flipped.translateX = negate(flipped.translateX);
368
+ const { translate } = flipped;
369
+ if (Array.isArray(translate)) flipped.translate = [negate(translate[0]), ...translate.slice(1)];
370
+ return "translateX" in flipped || Array.isArray(translate) ? flipped : entry;
371
+ }
372
+ /** `transform`, a list of functions or a string, for the other side: only the horizontal translation changes. */
373
+ function flipTransform(value) {
374
+ if (typeof value === "string") return flipTranslate(value);
375
+ if (!Array.isArray(value)) return value;
376
+ const entries = value.map(flipTransformEntry);
377
+ return entries.every((entry, index) => entry === value[index]) ? value : entries;
378
+ }
379
+ /** A `{ width, height }` offset for the other side: the width takes the other sign. */
380
+ function flipOffset(value) {
381
+ if (typeof value !== "object" || value === null || !("width" in value)) return value;
382
+ return {
383
+ ...value,
384
+ width: negate(value.width)
385
+ };
386
+ }
387
+ /** `left` and `right` swapped; any other alignment stays. */
388
+ function flipAlign(value) {
389
+ if (value === "left") return "right";
390
+ return value === "right" ? "left" : value;
391
+ }
392
+ /** The flip of each key the platform does not mirror, with the switch that turns it on. */
393
+ const FLIPS = /* @__PURE__ */ new Map([
394
+ ["transform", ["translateX", flipTransform]],
395
+ ["shadowOffset", ["boxShadow", flipOffset]],
396
+ ["textShadowOffset", ["textShadow", flipOffset]],
397
+ ["textAlign", ["textAlign", flipAlign]]
398
+ ]);
399
+ /**
400
+ * `style` for `direction`: a `dir()` value is replaced by its side of `direction` (a side that is left out
401
+ * removes the key), and under `rtl` the keys the platform does not mirror are flipped (never a key that
402
+ * `dir()` gave). Every other key stays, and the same object comes back when nothing changed.
403
+ */
404
+ function transformNativeStyle(given, direction, switches) {
405
+ const style = given;
406
+ let result;
407
+ for (const key of Object.keys(style)) {
408
+ const value = style[key];
409
+ if (isDirValue(value)) {
410
+ result ??= { ...style };
411
+ const side = value.values[direction];
412
+ if (side === void 0) delete result[key];
413
+ else result[key] = side;
414
+ continue;
415
+ }
416
+ const flip = direction === "rtl" ? FLIPS.get(key) : void 0;
417
+ if (flip === void 0 || !switches[flip[0]]) continue;
418
+ const flipped = flip[1](value);
419
+ if (flipped === value) continue;
420
+ result ??= { ...style };
421
+ result[key] = flipped;
422
+ }
423
+ return result ?? given;
424
+ }
425
+ // src/switches.ts
426
+ /** The value of each switch when it is not given. */
427
+ const DEFAULTS = {
428
+ margin: true,
429
+ padding: true,
430
+ inset: true,
431
+ border: true,
432
+ borderRadius: true,
433
+ float: true,
434
+ textAlign: true,
435
+ direction: true,
436
+ boxShadow: true,
437
+ textShadow: true,
438
+ translateX: true,
439
+ backgroundPosition: true,
440
+ cursor: true,
441
+ urls: false
442
+ };
443
+ /** The switches of `options`: each one given, or its default. */
444
+ function readSwitches(options) {
445
+ const switches = { ...DEFAULTS };
446
+ for (const key of Object.keys(DEFAULTS)) switches[key] = options[key] ?? DEFAULTS[key];
447
+ return switches;
448
+ }
449
+ /** The name of a plugin: `base`, followed by each switch that differs from its default, so that a stamp tells two settings apart. */
450
+ function createName(base, switches) {
451
+ const changed = Object.keys(DEFAULTS).filter((key) => switches[key] !== DEFAULTS[key]).map((key) => `${key}=${switches[key]}`);
452
+ return changed.length === 0 ? base : `${base}:${changed.join(",")}`;
453
+ }
454
+ // src/dir.ts
455
+ /**
456
+ * A value of a property for each direction (`marginTop: dir({ ltr: 2, rtl: 4 })`): with the plugin
457
+ * `rtl()`, each side is written as it is given under its `:dir()`, and never flipped; a side that is left
458
+ * out writes nothing for that direction. Typed as the values it is given, so it goes wherever they go.
459
+ */
460
+ function dir(values) {
461
+ const given = {};
462
+ if (values.ltr !== void 0) given.ltr = values.ltr;
463
+ if (values.rtl !== void 0) given.rtl = values.rtl;
464
+ return markValue("dir", given);
465
+ }
466
+ // src/index.ts
467
+ /** The condition of a declaration as it is written, for left to right. */
468
+ const LTR = ":dir(ltr)";
469
+ /** The condition of a declaration flipped, for right to left. */
470
+ const RTL = ":dir(rtl)";
471
+ /** The declaration of the property of the other side, with the same value. */
472
+ const renameEdge = (property, value) => [flipEdge(property), value];
473
+ /** A rule that flips the value of a declaration with `flip`, and keeps its property. */
474
+ const flipValue = (flip) => (property, value) => [property, flip(value)];
475
+ const fourValues = flipValue(flipFourValues);
476
+ const leftRight = flipValue((value) => swapKeywords(value, "left", "right"));
477
+ const shadow = flipValue(flipShadow);
478
+ const backgroundPosition = flipValue(flipBackgroundPosition);
479
+ /** The rule of each property that has one, and the switch that turns it on. */
480
+ const RULES = /* @__PURE__ */ new Map([
481
+ ["margin-left", ["margin", renameEdge]],
482
+ ["margin-right", ["margin", renameEdge]],
483
+ ["margin", ["margin", fourValues]],
484
+ ["padding-left", ["padding", renameEdge]],
485
+ ["padding-right", ["padding", renameEdge]],
486
+ ["padding", ["padding", fourValues]],
487
+ ["left", ["inset", renameEdge]],
488
+ ["right", ["inset", renameEdge]],
489
+ ["inset", ["inset", fourValues]],
490
+ ["border-left", ["border", renameEdge]],
491
+ ["border-right", ["border", renameEdge]],
492
+ ["border-left-width", ["border", renameEdge]],
493
+ ["border-right-width", ["border", renameEdge]],
494
+ ["border-left-color", ["border", renameEdge]],
495
+ ["border-right-color", ["border", renameEdge]],
496
+ ["border-left-style", ["border", renameEdge]],
497
+ ["border-right-style", ["border", renameEdge]],
498
+ ["border-width", ["border", fourValues]],
499
+ ["border-color", ["border", fourValues]],
500
+ ["border-style", ["border", fourValues]],
501
+ ["border-top-left-radius", ["borderRadius", renameEdge]],
502
+ ["border-top-right-radius", ["borderRadius", renameEdge]],
503
+ ["border-bottom-left-radius", ["borderRadius", renameEdge]],
504
+ ["border-bottom-right-radius", ["borderRadius", renameEdge]],
505
+ ["border-radius", ["borderRadius", flipValue(flipRadius)]],
506
+ ["float", ["float", leftRight]],
507
+ ["clear", ["float", leftRight]],
508
+ ["text-align", ["textAlign", leftRight]],
509
+ ["direction", ["direction", flipValue((value) => swapKeywords(value, "ltr", "rtl"))]],
510
+ ["box-shadow", ["boxShadow", shadow]],
511
+ ["text-shadow", ["textShadow", shadow]],
512
+ ["transform", ["translateX", flipValue(flipTranslate)]],
513
+ ["translate", ["translateX", flipValue(flipTranslateProperty)]],
514
+ ["background-position", ["backgroundPosition", backgroundPosition]],
515
+ ["background-position-x", ["backgroundPosition", backgroundPosition]],
516
+ ["cursor", ["cursor", flipValue(flipCursor)]]
517
+ ]);
518
+ /** The declarations of a `dir()` value: each side it has, under its own condition, as it was given. */
519
+ function toSides(property, value) {
520
+ const { ltr, rtl: right } = value.values;
521
+ const sides = [];
522
+ if (ltr !== void 0) sides.push([
523
+ property,
524
+ ltr,
525
+ LTR
526
+ ]);
527
+ if (right !== void 0) sides.push([
528
+ property,
529
+ right,
530
+ RTL
531
+ ]);
532
+ return sides;
533
+ }
534
+ /**
535
+ * The plugin of the default style (`configure({ plugins: [rtl()] })`) that writes both directions of a
536
+ * style in one class: a declaration that has a side is written as it is under `:dir(ltr)` and for the
537
+ * other side under `:dir(rtl)` (`margin-left` is `margin-right`, never a logical property), and a value
538
+ * that names or measures a side is written for the other one; `@supports not selector(:dir(ltr))` gives
539
+ * the left-to-right declarations to a browser without `:dir()`. A `dir()` value is written as it is given,
540
+ * each side under its own condition. Logical properties, the values `start` and `end`, vertical values and
541
+ * unknown properties are left as they are. `options` turns kinds off (or `urls` on).
542
+ */
543
+ function rtl(options = {}) {
544
+ const switches = readSwitches(options);
545
+ return {
546
+ name: createName("rtl", switches),
547
+ native: (style, { dir }) => transformNativeStyle(style, dir, switches),
548
+ conditions: [LTR, RTL],
549
+ fallback: `@supports not selector(${LTR})`,
550
+ declaration(property, value) {
551
+ if (typeof value !== "string") return value.mark === "dir" ? toSides(property, value) : void 0;
552
+ const rule = RULES.get(property);
553
+ const isRuled = rule !== void 0 && switches[rule[0]];
554
+ const [name, flipped] = isRuled ? rule[1](property, value) : [property, value];
555
+ const text = switches.urls && flipped.includes("url(") ? flipUrls(flipped) : flipped;
556
+ if (!isRuled && text === value) return void 0;
557
+ return [[
558
+ property,
559
+ value,
560
+ LTR
561
+ ], [
562
+ name,
563
+ text,
564
+ RTL
565
+ ]];
566
+ }
567
+ };
568
+ }
569
+ export { dir, rtl };