@umber-style/rtl 0.11.0-alpha.0 → 0.11.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.
package/README.md CHANGED
@@ -139,7 +139,8 @@ From the passing test [`rtl.react-native.test.tsx`](https://github.com/soroush-t
139
139
  | Export | What it is |
140
140
  | ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
141
141
  | `rtl(options?)` | The plugin of `configure({ plugins })`, one object for every binding: each declaration that has a side is written under `:dir(ltr)` as it is and under `:dir(rtl)` flipped. `options` are the switches that turn each kind of flip off |
142
- | `dir({ ltr, rtl })` | A value of its own for each direction, written as given and never flipped; the plugin reads it |
142
+ | `dir({ ltr, rtl })` | A value of its own for each direction, written as given and never flipped; with `rtl()` or without it |
143
+ | `keep(value)` | A value that is never flipped: one plain declaration as it is given, with `rtl()` or without it |
143
144
  | `RtlOptions`, `DirValues` | The types of the options of `rtl()` and of the argument of `dir()` |
144
145
  | `Direction` | Not in this package: `@umber-style/react`, `@umber-style/vue` and `@umber-style/react-native` export it |
145
146
 
package/dist/index.cjs CHANGED
@@ -95,6 +95,176 @@ function negateLength(word) {
95
95
  if (!isNumberStart(first)) return `calc(-1 * ${word})`;
96
96
  return Number.parseFloat(word) === 0 ? word : `-${word}`;
97
97
  }
98
+ // src/translate.ts
99
+ /** The functions of `transform` whose first argument is the horizontal translation. */
100
+ const TRANSLATIONS = /* @__PURE__ */ new Set([
101
+ "translatex",
102
+ "translate",
103
+ "translate3d"
104
+ ]);
105
+ /** The function `word` (`translateX(...)`, `translate(...)`, `translate3d(...)`) with its first argument, the horizontal one, of the other sign. */
106
+ function flipFirstArgument(word, open) {
107
+ const inner = splitWords(word.slice(open + 1, -1));
108
+ if (inner.words.length === 0) return word;
109
+ inner.words[0] = negateLength(inner.words[0]);
110
+ return `${word.slice(0, open + 1)}${joinWords(inner)})`;
111
+ }
112
+ /**
113
+ * The `transform` value `value` for the other side: the argument of each `translateX()` and the first
114
+ * argument of each `translate()` and `translate3d()` take the other sign. The other functions stay as they
115
+ * are.
116
+ */
117
+ function flipTranslate(value) {
118
+ return mapWords(value, (word) => {
119
+ const open = word.indexOf("(");
120
+ const name = word.slice(0, Math.max(open, 0)).toLowerCase();
121
+ return TRANSLATIONS.has(name) && word.endsWith(")") ? flipFirstArgument(word, open) : word;
122
+ });
123
+ }
124
+ /** The keywords that the `translate` property takes in place of its values. */
125
+ const KEYWORDS = /* @__PURE__ */ new Set([
126
+ "none",
127
+ "inherit",
128
+ "initial",
129
+ "unset",
130
+ "revert",
131
+ "revert-layer"
132
+ ]);
133
+ /**
134
+ * The value of the `translate` property for the other side: its first value, the horizontal one, takes the
135
+ * other sign (`none` and the CSS-wide keywords stay).
136
+ */
137
+ function flipTranslateProperty(value) {
138
+ return mapWords(value, (word, index) => index === 0 && !KEYWORDS.has(word) ? negateLength(word) : word);
139
+ }
140
+ // src/nativeFlips.ts
141
+ /** Whether `value` is the marked value of `dir()` or `keep()`: its mark and the values of its sides. */
142
+ function isDirected(value) {
143
+ if (typeof value !== "object" || value === null) return false;
144
+ const { mark, values } = value;
145
+ return (mark === "dir" || mark === "keep") && typeof values === "object" && values !== null;
146
+ }
147
+ /** The number or length `value` with the other sign; anything that is not a number or starts with one stays. */
148
+ function negate(value) {
149
+ if (typeof value === "number") return 0 - value;
150
+ if (typeof value !== "string" || Number.isNaN(Number.parseFloat(value))) return value;
151
+ return value.startsWith("-") ? value.slice(1) : `-${value}`;
152
+ }
153
+ /** One function of a `transform` list (`{ translateX: 8 }`, `{ translate: [8, 2] }`) for the other side. */
154
+ function flipTransformEntry(entry) {
155
+ if (typeof entry !== "object" || entry === null) return entry;
156
+ const flipped = { ...entry };
157
+ if ("translateX" in flipped) flipped.translateX = negate(flipped.translateX);
158
+ const { translate } = flipped;
159
+ if (Array.isArray(translate)) flipped.translate = [negate(translate[0]), ...translate.slice(1)];
160
+ return "translateX" in flipped || Array.isArray(translate) ? flipped : entry;
161
+ }
162
+ /** `transform`, a list of functions or a string, for the other side: only the horizontal translation changes. */
163
+ function flipTransform(value) {
164
+ if (typeof value === "string") return flipTranslate(value);
165
+ if (!Array.isArray(value)) return value;
166
+ const entries = value.map(flipTransformEntry);
167
+ return entries.every((entry, index) => entry === value[index]) ? value : entries;
168
+ }
169
+ /** A `{ width, height }` offset for the other side: the width takes the other sign. */
170
+ function flipOffset(value) {
171
+ if (typeof value !== "object" || value === null || !("width" in value)) return value;
172
+ return {
173
+ ...value,
174
+ width: negate(value.width)
175
+ };
176
+ }
177
+ /** `left` and `right` swapped; any other alignment stays. */
178
+ function flipAlign(value) {
179
+ if (value === "left") return "right";
180
+ return value === "right" ? "left" : value;
181
+ }
182
+ /** The flip of each key the platform does not mirror, with the switch that turns it on. */
183
+ const FLIPS = /* @__PURE__ */ new Map([
184
+ ["transform", ["translateX", flipTransform]],
185
+ ["shadowOffset", ["boxShadow", flipOffset]],
186
+ ["textShadowOffset", ["textShadow", flipOffset]],
187
+ ["textAlign", ["textAlign", flipAlign]]
188
+ ]);
189
+ /**
190
+ * `style` for `direction`: a `dir()` value is replaced by its side of `direction` (a side that is left out
191
+ * removes the key), a `keep()` value by the value it holds, and, with `switches`, under `rtl` the keys the
192
+ * platform does not mirror are flipped (never a key that `dir()` or `keep()` gave). Every other key stays,
193
+ * and the same object comes back when nothing changed.
194
+ */
195
+ function transformNativeStyle(given, direction, switches) {
196
+ const style = given;
197
+ let result;
198
+ for (const key of Object.keys(style)) {
199
+ const value = style[key];
200
+ if (isDirected(value)) {
201
+ result ??= { ...style };
202
+ const side = readSide(value, direction);
203
+ if (side === void 0) delete result[key];
204
+ else result[key] = side;
205
+ continue;
206
+ }
207
+ const flipped = flipValue$1(key, value, direction, switches);
208
+ if (flipped === value) continue;
209
+ result ??= { ...style };
210
+ result[key] = flipped;
211
+ }
212
+ return result ?? given;
213
+ }
214
+ /** What a `dir()` value gives for `direction`, or what a `keep()` value holds; `undefined` for a side left out. */
215
+ function readSide(value, direction) {
216
+ return value.mark === "keep" ? value.values.value : value.values[direction];
217
+ }
218
+ /** `value` of `key` flipped under `rtl` when its kind is switched on, else `value` itself. */
219
+ function flipValue$1(key, value, direction, switches) {
220
+ const flip = direction === "rtl" ? FLIPS.get(key) : void 0;
221
+ return flip !== void 0 && switches?.[flip[0]] ? flip[1](value) : value;
222
+ }
223
+ // src/dir.ts
224
+ /** The condition of a declaration as it is written, for left to right. */
225
+ const LTR = ":dir(ltr)";
226
+ /** The condition of a declaration flipped, for right to left. */
227
+ const RTL = ":dir(rtl)";
228
+ /** The declarations of a `dir()` value: each side it has, under its own condition, as it was given. */
229
+ function toSides(property, value) {
230
+ const { ltr, rtl } = value.values;
231
+ const sides = [];
232
+ if (ltr !== void 0) sides.push([
233
+ property,
234
+ ltr,
235
+ LTR
236
+ ]);
237
+ if (rtl !== void 0) sides.push([
238
+ property,
239
+ rtl,
240
+ RTL
241
+ ]);
242
+ return sides;
243
+ }
244
+ /** What writes a `dir()` value when no plugin reads it: each side under its `:dir()`, or one plain declaration when both sides are the same. */
245
+ const DIR_HANDLER = {
246
+ name: "rtl-dir",
247
+ conditions: [LTR, RTL],
248
+ fallback: `@supports not selector(${LTR})`,
249
+ declaration(property, value) {
250
+ const sides = value;
251
+ const { ltr, rtl } = sides.values;
252
+ return ltr !== void 0 && ltr === rtl ? [property, ltr] : toSides(property, sides);
253
+ },
254
+ native: (style, { dir }) => transformNativeStyle(style, dir)
255
+ };
256
+ /**
257
+ * A value of a property for each direction (`marginTop: dir({ ltr: 2, rtl: 4 })`): each side is written as
258
+ * it is given under its `:dir()`, and never flipped; a side that is left out writes nothing for that
259
+ * direction. It is the same with the plugin `rtl()` and without it; with no plugin, sides that are equal
260
+ * are written as one plain declaration. Typed as the values it is given, so it goes wherever they go.
261
+ */
262
+ function dir(values) {
263
+ const given = {};
264
+ if (values.ltr !== void 0) given.ltr = values.ltr;
265
+ if (values.rtl !== void 0) given.rtl = values.rtl;
266
+ return (0, _umber_style_core.markValue)("dir", given, DIR_HANDLER);
267
+ }
98
268
  // src/background.ts
99
269
  /** Whether `text` is a number as CSS writes one: digits, a sign, a decimal point. */
100
270
  function isNumber(text) {
@@ -243,48 +413,6 @@ function flipShadow(value) {
243
413
  gaps
244
414
  });
245
415
  }
246
- // src/translate.ts
247
- /** The functions of `transform` whose first argument is the horizontal translation. */
248
- const TRANSLATIONS = /* @__PURE__ */ new Set([
249
- "translatex",
250
- "translate",
251
- "translate3d"
252
- ]);
253
- /** The function `word` (`translateX(...)`, `translate(...)`, `translate3d(...)`) with its first argument, the horizontal one, of the other sign. */
254
- function flipFirstArgument(word, open) {
255
- const inner = splitWords(word.slice(open + 1, -1));
256
- if (inner.words.length === 0) return word;
257
- inner.words[0] = negateLength(inner.words[0]);
258
- return `${word.slice(0, open + 1)}${joinWords(inner)})`;
259
- }
260
- /**
261
- * The `transform` value `value` for the other side: the argument of each `translateX()` and the first
262
- * argument of each `translate()` and `translate3d()` take the other sign. The other functions stay as they
263
- * are.
264
- */
265
- function flipTranslate(value) {
266
- return mapWords(value, (word) => {
267
- const open = word.indexOf("(");
268
- const name = word.slice(0, Math.max(open, 0)).toLowerCase();
269
- return TRANSLATIONS.has(name) && word.endsWith(")") ? flipFirstArgument(word, open) : word;
270
- });
271
- }
272
- /** The keywords that the `translate` property takes in place of its values. */
273
- const KEYWORDS = /* @__PURE__ */ new Set([
274
- "none",
275
- "inherit",
276
- "initial",
277
- "unset",
278
- "revert",
279
- "revert-layer"
280
- ]);
281
- /**
282
- * The value of the `translate` property for the other side: its first value, the horizontal one, takes the
283
- * other sign (`none` and the CSS-wide keywords stay).
284
- */
285
- function flipTranslateProperty(value) {
286
- return mapWords(value, (word, index) => index === 0 && !KEYWORDS.has(word) ? negateLength(word) : word);
287
- }
288
416
  // src/urls.ts
289
417
  /** Each word of a file name that names a side or a direction, by the word for the other one. */
290
418
  const MIRRORED = {
@@ -348,81 +476,6 @@ function flipUrls(value) {
348
476
  }
349
477
  return text + value.slice(from);
350
478
  }
351
- // src/nativeFlips.ts
352
- /** Whether `value` is the marked value of `dir()`: a mark `dir` and the values of its sides. */
353
- function isDirValue(value) {
354
- if (typeof value !== "object" || value === null) return false;
355
- const { mark, values } = value;
356
- return mark === "dir" && typeof values === "object" && values !== null;
357
- }
358
- /** The number or length `value` with the other sign; anything that is not a number or starts with one stays. */
359
- function negate(value) {
360
- if (typeof value === "number") return 0 - value;
361
- if (typeof value !== "string" || Number.isNaN(Number.parseFloat(value))) return value;
362
- return value.startsWith("-") ? value.slice(1) : `-${value}`;
363
- }
364
- /** One function of a `transform` list (`{ translateX: 8 }`, `{ translate: [8, 2] }`) for the other side. */
365
- function flipTransformEntry(entry) {
366
- if (typeof entry !== "object" || entry === null) return entry;
367
- const flipped = { ...entry };
368
- if ("translateX" in flipped) flipped.translateX = negate(flipped.translateX);
369
- const { translate } = flipped;
370
- if (Array.isArray(translate)) flipped.translate = [negate(translate[0]), ...translate.slice(1)];
371
- return "translateX" in flipped || Array.isArray(translate) ? flipped : entry;
372
- }
373
- /** `transform`, a list of functions or a string, for the other side: only the horizontal translation changes. */
374
- function flipTransform(value) {
375
- if (typeof value === "string") return flipTranslate(value);
376
- if (!Array.isArray(value)) return value;
377
- const entries = value.map(flipTransformEntry);
378
- return entries.every((entry, index) => entry === value[index]) ? value : entries;
379
- }
380
- /** A `{ width, height }` offset for the other side: the width takes the other sign. */
381
- function flipOffset(value) {
382
- if (typeof value !== "object" || value === null || !("width" in value)) return value;
383
- return {
384
- ...value,
385
- width: negate(value.width)
386
- };
387
- }
388
- /** `left` and `right` swapped; any other alignment stays. */
389
- function flipAlign(value) {
390
- if (value === "left") return "right";
391
- return value === "right" ? "left" : value;
392
- }
393
- /** The flip of each key the platform does not mirror, with the switch that turns it on. */
394
- const FLIPS = /* @__PURE__ */ new Map([
395
- ["transform", ["translateX", flipTransform]],
396
- ["shadowOffset", ["boxShadow", flipOffset]],
397
- ["textShadowOffset", ["textShadow", flipOffset]],
398
- ["textAlign", ["textAlign", flipAlign]]
399
- ]);
400
- /**
401
- * `style` for `direction`: a `dir()` value is replaced by its side of `direction` (a side that is left out
402
- * removes the key), and under `rtl` the keys the platform does not mirror are flipped (never a key that
403
- * `dir()` gave). Every other key stays, and the same object comes back when nothing changed.
404
- */
405
- function transformNativeStyle(given, direction, switches) {
406
- const style = given;
407
- let result;
408
- for (const key of Object.keys(style)) {
409
- const value = style[key];
410
- if (isDirValue(value)) {
411
- result ??= { ...style };
412
- const side = value.values[direction];
413
- if (side === void 0) delete result[key];
414
- else result[key] = side;
415
- continue;
416
- }
417
- const flip = direction === "rtl" ? FLIPS.get(key) : void 0;
418
- if (flip === void 0 || !switches[flip[0]]) continue;
419
- const flipped = flip[1](value);
420
- if (flipped === value) continue;
421
- result ??= { ...style };
422
- result[key] = flipped;
423
- }
424
- return result ?? given;
425
- }
426
479
  // src/switches.ts
427
480
  /** The value of each switch when it is not given. */
428
481
  const DEFAULTS = {
@@ -452,25 +505,22 @@ function createName(base, switches) {
452
505
  const changed = Object.keys(DEFAULTS).filter((key) => switches[key] !== DEFAULTS[key]).map((key) => `${key}=${switches[key]}`);
453
506
  return changed.length === 0 ? base : `${base}:${changed.join(",")}`;
454
507
  }
455
- // src/dir.ts
508
+ // src/keep.ts
509
+ /** What writes a `keep()` value, with the plugin `rtl()` or without: one plain declaration, as it was given. */
510
+ const KEEP_HANDLER = {
511
+ name: "rtl-keep",
512
+ declaration: (property, value) => [property, value.values.value],
513
+ native: (style, { dir }) => transformNativeStyle(style, dir)
514
+ };
456
515
  /**
457
- * A value of a property for each direction (`marginTop: dir({ ltr: 2, rtl: 4 })`): with the plugin
458
- * `rtl()`, each side is written as it is given under its `:dir()`, and never flipped; a side that is left
459
- * out writes nothing for that direction. With no plugin that reads it, nothing is written, and in
460
- * development the engine reports it with `console.error`. Typed as the values it is given, so it goes
461
- * wherever they go.
516
+ * A value that is never flipped (`left: keep(0)`): one plain declaration, written as it is given in both
517
+ * directions, with the plugin `rtl()` and without it. Typed as the value it is given, so it goes wherever
518
+ * that goes.
462
519
  */
463
- function dir(values) {
464
- const given = {};
465
- if (values.ltr !== void 0) given.ltr = values.ltr;
466
- if (values.rtl !== void 0) given.rtl = values.rtl;
467
- return (0, _umber_style_core.markValue)("dir", given);
520
+ function keep(value) {
521
+ return (0, _umber_style_core.markValue)("keep", { value }, KEEP_HANDLER);
468
522
  }
469
523
  // src/index.ts
470
- /** The condition of a declaration as it is written, for left to right. */
471
- const LTR = ":dir(ltr)";
472
- /** The condition of a declaration flipped, for right to left. */
473
- const RTL = ":dir(rtl)";
474
524
  /** A selector that already names a direction: `[dir="rtl"] .c`, `.c:dir(rtl)`. */
475
525
  const NAMES_DIRECTION = /\[dir|:dir\(/;
476
526
  /** Whether this is a production build: `process.env.NODE_ENV` is `production`. Where `process` does not exist it is taken as one. */
@@ -482,8 +532,8 @@ function isProduction() {
482
532
  }
483
533
  }
484
534
  /**
485
- * In development, warns once for each selector that names a direction: with `rtl()` its rule is flipped
486
- * too. The selector of the compile that only names a class (`&`) is not the rule's, so it is not warned
535
+ * In development, warns once for each selector that names a direction, when a declaration under it is changed
536
+ * by `rtl()`: its rule is flipped too. The selector of the compile that only names a class (`&`) is not the rule's, so it is not warned
487
537
  * about; nothing is warned about in production.
488
538
  */
489
539
  function warnAboutDirection(selector, warned) {
@@ -538,22 +588,6 @@ const RULES = /* @__PURE__ */ new Map([
538
588
  ["background-position-x", ["backgroundPosition", backgroundPosition]],
539
589
  ["cursor", ["cursor", flipValue(flipCursor)]]
540
590
  ]);
541
- /** The declarations of a `dir()` value: each side it has, under its own condition, as it was given. */
542
- function toSides(property, value) {
543
- const { ltr, rtl: right } = value.values;
544
- const sides = [];
545
- if (ltr !== void 0) sides.push([
546
- property,
547
- ltr,
548
- LTR
549
- ]);
550
- if (right !== void 0) sides.push([
551
- property,
552
- right,
553
- RTL
554
- ]);
555
- return sides;
556
- }
557
591
  /**
558
592
  * The plugin of the default style (`configure({ plugins: [rtl()] })`) that writes both directions of a
559
593
  * style in one class: a declaration that has a side is written as it is under `:dir(ltr)` and for the
@@ -573,13 +607,14 @@ function rtl(options = {}) {
573
607
  conditions: [LTR, RTL],
574
608
  fallback: `@supports not selector(${LTR})`,
575
609
  declaration(property, value, selector) {
576
- warnAboutDirection(selector, warned);
577
610
  if (typeof value !== "string") return value.mark === "dir" ? toSides(property, value) : void 0;
578
611
  const rule = RULES.get(property);
579
612
  const isRuled = rule !== void 0 && switches[rule[0]];
580
613
  const [name, flipped] = isRuled ? rule[1](property, value) : [property, value];
581
614
  const text = switches.urls && flipped.includes("url(") ? flipUrls(flipped) : flipped;
582
- if (!isRuled && text === value) return void 0;
615
+ const isChanged = name !== property || text !== value;
616
+ if (isChanged) warnAboutDirection(selector, warned);
617
+ if (!isRuled && !isChanged) return void 0;
583
618
  return [[
584
619
  property,
585
620
  value,
@@ -593,4 +628,5 @@ function rtl(options = {}) {
593
628
  };
594
629
  }
595
630
  exports.dir = dir;
631
+ exports.keep = keep;
596
632
  exports.rtl = rtl;
package/dist/index.d.cts CHANGED
@@ -40,13 +40,18 @@ interface DirValues<T> {
40
40
  rtl?: T;
41
41
  }
42
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. With no plugin that reads it, nothing is written, and in
46
- * development the engine reports it with `console.error`. Typed as the values it is given, so it goes
47
- * wherever they go.
43
+ * A value of a property for each direction (`marginTop: dir({ ltr: 2, rtl: 4 })`): each side is written as
44
+ * it is given under its `:dir()`, and never flipped; a side that is left out writes nothing for that
45
+ * direction. It is the same with the plugin `rtl()` and without it; with no plugin, sides that are equal
46
+ * are written as one plain declaration. Typed as the values it is given, so it goes wherever they go.
48
47
  */
49
48
  export declare function dir<T extends string | number>(values: DirValues<T>): T;
49
+ /**
50
+ * A value that is never flipped (`left: keep(0)`): one plain declaration, written as it is given in both
51
+ * directions, with the plugin `rtl()` and without it. Typed as the value it is given, so it goes wherever
52
+ * that goes.
53
+ */
54
+ export declare function keep<T extends string | number>(value: T): T;
50
55
  /**
51
56
  * The plugin `rtl()` returns: a `CachePlugin` for the web, which also has `native`, the member that the
52
57
  * React Native binding runs over its styles. One object serves every platform.
package/dist/index.d.mts CHANGED
@@ -40,13 +40,18 @@ interface DirValues<T> {
40
40
  rtl?: T;
41
41
  }
42
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. With no plugin that reads it, nothing is written, and in
46
- * development the engine reports it with `console.error`. Typed as the values it is given, so it goes
47
- * wherever they go.
43
+ * A value of a property for each direction (`marginTop: dir({ ltr: 2, rtl: 4 })`): each side is written as
44
+ * it is given under its `:dir()`, and never flipped; a side that is left out writes nothing for that
45
+ * direction. It is the same with the plugin `rtl()` and without it; with no plugin, sides that are equal
46
+ * are written as one plain declaration. Typed as the values it is given, so it goes wherever they go.
48
47
  */
49
48
  export declare function dir<T extends string | number>(values: DirValues<T>): T;
49
+ /**
50
+ * A value that is never flipped (`left: keep(0)`): one plain declaration, written as it is given in both
51
+ * directions, with the plugin `rtl()` and without it. Typed as the value it is given, so it goes wherever
52
+ * that goes.
53
+ */
54
+ export declare function keep<T extends string | number>(value: T): T;
50
55
  /**
51
56
  * The plugin `rtl()` returns: a `CachePlugin` for the web, which also has `native`, the member that the
52
57
  * React Native binding runs over its styles. One object serves every platform.
package/dist/index.mjs CHANGED
@@ -94,6 +94,176 @@ function negateLength(word) {
94
94
  if (!isNumberStart(first)) return `calc(-1 * ${word})`;
95
95
  return Number.parseFloat(word) === 0 ? word : `-${word}`;
96
96
  }
97
+ // src/translate.ts
98
+ /** The functions of `transform` whose first argument is the horizontal translation. */
99
+ const TRANSLATIONS = /* @__PURE__ */ new Set([
100
+ "translatex",
101
+ "translate",
102
+ "translate3d"
103
+ ]);
104
+ /** The function `word` (`translateX(...)`, `translate(...)`, `translate3d(...)`) with its first argument, the horizontal one, of the other sign. */
105
+ function flipFirstArgument(word, open) {
106
+ const inner = splitWords(word.slice(open + 1, -1));
107
+ if (inner.words.length === 0) return word;
108
+ inner.words[0] = negateLength(inner.words[0]);
109
+ return `${word.slice(0, open + 1)}${joinWords(inner)})`;
110
+ }
111
+ /**
112
+ * The `transform` value `value` for the other side: the argument of each `translateX()` and the first
113
+ * argument of each `translate()` and `translate3d()` take the other sign. The other functions stay as they
114
+ * are.
115
+ */
116
+ function flipTranslate(value) {
117
+ return mapWords(value, (word) => {
118
+ const open = word.indexOf("(");
119
+ const name = word.slice(0, Math.max(open, 0)).toLowerCase();
120
+ return TRANSLATIONS.has(name) && word.endsWith(")") ? flipFirstArgument(word, open) : word;
121
+ });
122
+ }
123
+ /** The keywords that the `translate` property takes in place of its values. */
124
+ const KEYWORDS = /* @__PURE__ */ new Set([
125
+ "none",
126
+ "inherit",
127
+ "initial",
128
+ "unset",
129
+ "revert",
130
+ "revert-layer"
131
+ ]);
132
+ /**
133
+ * The value of the `translate` property for the other side: its first value, the horizontal one, takes the
134
+ * other sign (`none` and the CSS-wide keywords stay).
135
+ */
136
+ function flipTranslateProperty(value) {
137
+ return mapWords(value, (word, index) => index === 0 && !KEYWORDS.has(word) ? negateLength(word) : word);
138
+ }
139
+ // src/nativeFlips.ts
140
+ /** Whether `value` is the marked value of `dir()` or `keep()`: its mark and the values of its sides. */
141
+ function isDirected(value) {
142
+ if (typeof value !== "object" || value === null) return false;
143
+ const { mark, values } = value;
144
+ return (mark === "dir" || mark === "keep") && typeof values === "object" && values !== null;
145
+ }
146
+ /** The number or length `value` with the other sign; anything that is not a number or starts with one stays. */
147
+ function negate(value) {
148
+ if (typeof value === "number") return 0 - value;
149
+ if (typeof value !== "string" || Number.isNaN(Number.parseFloat(value))) return value;
150
+ return value.startsWith("-") ? value.slice(1) : `-${value}`;
151
+ }
152
+ /** One function of a `transform` list (`{ translateX: 8 }`, `{ translate: [8, 2] }`) for the other side. */
153
+ function flipTransformEntry(entry) {
154
+ if (typeof entry !== "object" || entry === null) return entry;
155
+ const flipped = { ...entry };
156
+ if ("translateX" in flipped) flipped.translateX = negate(flipped.translateX);
157
+ const { translate } = flipped;
158
+ if (Array.isArray(translate)) flipped.translate = [negate(translate[0]), ...translate.slice(1)];
159
+ return "translateX" in flipped || Array.isArray(translate) ? flipped : entry;
160
+ }
161
+ /** `transform`, a list of functions or a string, for the other side: only the horizontal translation changes. */
162
+ function flipTransform(value) {
163
+ if (typeof value === "string") return flipTranslate(value);
164
+ if (!Array.isArray(value)) return value;
165
+ const entries = value.map(flipTransformEntry);
166
+ return entries.every((entry, index) => entry === value[index]) ? value : entries;
167
+ }
168
+ /** A `{ width, height }` offset for the other side: the width takes the other sign. */
169
+ function flipOffset(value) {
170
+ if (typeof value !== "object" || value === null || !("width" in value)) return value;
171
+ return {
172
+ ...value,
173
+ width: negate(value.width)
174
+ };
175
+ }
176
+ /** `left` and `right` swapped; any other alignment stays. */
177
+ function flipAlign(value) {
178
+ if (value === "left") return "right";
179
+ return value === "right" ? "left" : value;
180
+ }
181
+ /** The flip of each key the platform does not mirror, with the switch that turns it on. */
182
+ const FLIPS = /* @__PURE__ */ new Map([
183
+ ["transform", ["translateX", flipTransform]],
184
+ ["shadowOffset", ["boxShadow", flipOffset]],
185
+ ["textShadowOffset", ["textShadow", flipOffset]],
186
+ ["textAlign", ["textAlign", flipAlign]]
187
+ ]);
188
+ /**
189
+ * `style` for `direction`: a `dir()` value is replaced by its side of `direction` (a side that is left out
190
+ * removes the key), a `keep()` value by the value it holds, and, with `switches`, under `rtl` the keys the
191
+ * platform does not mirror are flipped (never a key that `dir()` or `keep()` gave). Every other key stays,
192
+ * and the same object comes back when nothing changed.
193
+ */
194
+ function transformNativeStyle(given, direction, switches) {
195
+ const style = given;
196
+ let result;
197
+ for (const key of Object.keys(style)) {
198
+ const value = style[key];
199
+ if (isDirected(value)) {
200
+ result ??= { ...style };
201
+ const side = readSide(value, direction);
202
+ if (side === void 0) delete result[key];
203
+ else result[key] = side;
204
+ continue;
205
+ }
206
+ const flipped = flipValue$1(key, value, direction, switches);
207
+ if (flipped === value) continue;
208
+ result ??= { ...style };
209
+ result[key] = flipped;
210
+ }
211
+ return result ?? given;
212
+ }
213
+ /** What a `dir()` value gives for `direction`, or what a `keep()` value holds; `undefined` for a side left out. */
214
+ function readSide(value, direction) {
215
+ return value.mark === "keep" ? value.values.value : value.values[direction];
216
+ }
217
+ /** `value` of `key` flipped under `rtl` when its kind is switched on, else `value` itself. */
218
+ function flipValue$1(key, value, direction, switches) {
219
+ const flip = direction === "rtl" ? FLIPS.get(key) : void 0;
220
+ return flip !== void 0 && switches?.[flip[0]] ? flip[1](value) : value;
221
+ }
222
+ // src/dir.ts
223
+ /** The condition of a declaration as it is written, for left to right. */
224
+ const LTR = ":dir(ltr)";
225
+ /** The condition of a declaration flipped, for right to left. */
226
+ const RTL = ":dir(rtl)";
227
+ /** The declarations of a `dir()` value: each side it has, under its own condition, as it was given. */
228
+ function toSides(property, value) {
229
+ const { ltr, rtl } = value.values;
230
+ const sides = [];
231
+ if (ltr !== void 0) sides.push([
232
+ property,
233
+ ltr,
234
+ LTR
235
+ ]);
236
+ if (rtl !== void 0) sides.push([
237
+ property,
238
+ rtl,
239
+ RTL
240
+ ]);
241
+ return sides;
242
+ }
243
+ /** What writes a `dir()` value when no plugin reads it: each side under its `:dir()`, or one plain declaration when both sides are the same. */
244
+ const DIR_HANDLER = {
245
+ name: "rtl-dir",
246
+ conditions: [LTR, RTL],
247
+ fallback: `@supports not selector(${LTR})`,
248
+ declaration(property, value) {
249
+ const sides = value;
250
+ const { ltr, rtl } = sides.values;
251
+ return ltr !== void 0 && ltr === rtl ? [property, ltr] : toSides(property, sides);
252
+ },
253
+ native: (style, { dir }) => transformNativeStyle(style, dir)
254
+ };
255
+ /**
256
+ * A value of a property for each direction (`marginTop: dir({ ltr: 2, rtl: 4 })`): each side is written as
257
+ * it is given under its `:dir()`, and never flipped; a side that is left out writes nothing for that
258
+ * direction. It is the same with the plugin `rtl()` and without it; with no plugin, sides that are equal
259
+ * are written as one plain declaration. Typed as the values it is given, so it goes wherever they go.
260
+ */
261
+ function dir(values) {
262
+ const given = {};
263
+ if (values.ltr !== void 0) given.ltr = values.ltr;
264
+ if (values.rtl !== void 0) given.rtl = values.rtl;
265
+ return markValue("dir", given, DIR_HANDLER);
266
+ }
97
267
  // src/background.ts
98
268
  /** Whether `text` is a number as CSS writes one: digits, a sign, a decimal point. */
99
269
  function isNumber(text) {
@@ -242,48 +412,6 @@ function flipShadow(value) {
242
412
  gaps
243
413
  });
244
414
  }
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
415
  // src/urls.ts
288
416
  /** Each word of a file name that names a side or a direction, by the word for the other one. */
289
417
  const MIRRORED = {
@@ -347,81 +475,6 @@ function flipUrls(value) {
347
475
  }
348
476
  return text + value.slice(from);
349
477
  }
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
478
  // src/switches.ts
426
479
  /** The value of each switch when it is not given. */
427
480
  const DEFAULTS = {
@@ -451,25 +504,22 @@ function createName(base, switches) {
451
504
  const changed = Object.keys(DEFAULTS).filter((key) => switches[key] !== DEFAULTS[key]).map((key) => `${key}=${switches[key]}`);
452
505
  return changed.length === 0 ? base : `${base}:${changed.join(",")}`;
453
506
  }
454
- // src/dir.ts
507
+ // src/keep.ts
508
+ /** What writes a `keep()` value, with the plugin `rtl()` or without: one plain declaration, as it was given. */
509
+ const KEEP_HANDLER = {
510
+ name: "rtl-keep",
511
+ declaration: (property, value) => [property, value.values.value],
512
+ native: (style, { dir }) => transformNativeStyle(style, dir)
513
+ };
455
514
  /**
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. With no plugin that reads it, nothing is written, and in
459
- * development the engine reports it with `console.error`. Typed as the values it is given, so it goes
460
- * wherever they go.
515
+ * A value that is never flipped (`left: keep(0)`): one plain declaration, written as it is given in both
516
+ * directions, with the plugin `rtl()` and without it. Typed as the value it is given, so it goes wherever
517
+ * that goes.
461
518
  */
462
- function dir(values) {
463
- const given = {};
464
- if (values.ltr !== void 0) given.ltr = values.ltr;
465
- if (values.rtl !== void 0) given.rtl = values.rtl;
466
- return markValue("dir", given);
519
+ function keep(value) {
520
+ return markValue("keep", { value }, KEEP_HANDLER);
467
521
  }
468
522
  // src/index.ts
469
- /** The condition of a declaration as it is written, for left to right. */
470
- const LTR = ":dir(ltr)";
471
- /** The condition of a declaration flipped, for right to left. */
472
- const RTL = ":dir(rtl)";
473
523
  /** A selector that already names a direction: `[dir="rtl"] .c`, `.c:dir(rtl)`. */
474
524
  const NAMES_DIRECTION = /\[dir|:dir\(/;
475
525
  /** Whether this is a production build: `process.env.NODE_ENV` is `production`. Where `process` does not exist it is taken as one. */
@@ -481,8 +531,8 @@ function isProduction() {
481
531
  }
482
532
  }
483
533
  /**
484
- * In development, warns once for each selector that names a direction: with `rtl()` its rule is flipped
485
- * too. The selector of the compile that only names a class (`&`) is not the rule's, so it is not warned
534
+ * In development, warns once for each selector that names a direction, when a declaration under it is changed
535
+ * by `rtl()`: its rule is flipped too. The selector of the compile that only names a class (`&`) is not the rule's, so it is not warned
486
536
  * about; nothing is warned about in production.
487
537
  */
488
538
  function warnAboutDirection(selector, warned) {
@@ -537,22 +587,6 @@ const RULES = /* @__PURE__ */ new Map([
537
587
  ["background-position-x", ["backgroundPosition", backgroundPosition]],
538
588
  ["cursor", ["cursor", flipValue(flipCursor)]]
539
589
  ]);
540
- /** The declarations of a `dir()` value: each side it has, under its own condition, as it was given. */
541
- function toSides(property, value) {
542
- const { ltr, rtl: right } = value.values;
543
- const sides = [];
544
- if (ltr !== void 0) sides.push([
545
- property,
546
- ltr,
547
- LTR
548
- ]);
549
- if (right !== void 0) sides.push([
550
- property,
551
- right,
552
- RTL
553
- ]);
554
- return sides;
555
- }
556
590
  /**
557
591
  * The plugin of the default style (`configure({ plugins: [rtl()] })`) that writes both directions of a
558
592
  * style in one class: a declaration that has a side is written as it is under `:dir(ltr)` and for the
@@ -572,13 +606,14 @@ function rtl(options = {}) {
572
606
  conditions: [LTR, RTL],
573
607
  fallback: `@supports not selector(${LTR})`,
574
608
  declaration(property, value, selector) {
575
- warnAboutDirection(selector, warned);
576
609
  if (typeof value !== "string") return value.mark === "dir" ? toSides(property, value) : void 0;
577
610
  const rule = RULES.get(property);
578
611
  const isRuled = rule !== void 0 && switches[rule[0]];
579
612
  const [name, flipped] = isRuled ? rule[1](property, value) : [property, value];
580
613
  const text = switches.urls && flipped.includes("url(") ? flipUrls(flipped) : flipped;
581
- if (!isRuled && text === value) return void 0;
614
+ const isChanged = name !== property || text !== value;
615
+ if (isChanged) warnAboutDirection(selector, warned);
616
+ if (!isRuled && !isChanged) return void 0;
582
617
  return [[
583
618
  property,
584
619
  value,
@@ -591,4 +626,4 @@ function rtl(options = {}) {
591
626
  }
592
627
  };
593
628
  }
594
- export { dir, rtl };
629
+ export { dir, keep, rtl };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umber-style/rtl",
3
- "version": "0.11.0-alpha.0",
3
+ "version": "0.11.0",
4
4
  "description": "The plugin of the UmberStyle default style that writes both directions of a style in one class: each declaration that has a side under :dir(ltr) as written and under :dir(rtl) flipped.",
5
5
  "keywords": [
6
6
  "styling",
@@ -42,7 +42,7 @@
42
42
  "access": "public"
43
43
  },
44
44
  "dependencies": {
45
- "@umber-style/core": "0.11.0-alpha.0"
45
+ "@umber-style/core": "^0.11.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@types/node": "^26.6.6",