@umber-style/rtl 0.10.0 → 0.11.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.
package/README.md CHANGED
@@ -1,16 +1,170 @@
1
1
  # @umber-style/rtl
2
2
 
3
- The plugin of the default style that writes both directions of a style in one class: each declaration that has a side is written as it is under `:dir(ltr)` and for the other side under `:dir(rtl)`, and the browser picks by the `dir` attribute of the page.
3
+ <!-- gen:readme description start - regenerated by `pnpm gen:readmes`; do not edit by hand -->
4
4
 
5
- Install: `pnpm add @umber-style/rtl`.
5
+ 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.
6
+
7
+ <!-- gen:readme end -->
8
+
9
+ One class holds both directions, so a server render sends the same CSS for a right-to-left page as for a left-to-right one, and switching the direction is an attribute change: no render, no new class.
10
+
11
+ ## Install
12
+
13
+ <!-- gen:readme install start - regenerated by `pnpm gen:readmes`; do not edit by hand -->
14
+
15
+ ```sh
16
+ pnpm add @umber-style/rtl
17
+ ```
18
+
19
+ <!-- gen:readme end -->
20
+
21
+ It depends on `@umber-style/core` only and knows no framework; `Direction` is in the bindings.
22
+
23
+ ## React
24
+
25
+ `rtl()` goes in `configure`; `dir()` gives a property a value of its own for each direction; `Direction` keeps an area in one direction. See [right-to-left.md](https://github.com/soroush-tech/umber-style/blob/main/docs/right-to-left.md#one-configure-both-directions).
26
+
27
+ <!-- gen:readme example rtl.react.test.tsx start - regenerated by `pnpm gen:readmes`; do not edit by hand -->
28
+
29
+ ```tsx
30
+ import { configure, Direction, styled } from '@umber-style/react'
31
+ import { dir, rtl } from '@umber-style/rtl'
32
+ import { renderToString } from 'react-dom/server'
33
+
34
+ configure({ prefix: 'app', plugins: [rtl()] })
35
+
36
+ const Item = styled('li', { name: 'Item' })({
37
+ marginLeft: 8,
38
+ textAlign: 'left',
39
+ color: 'teal',
40
+ marginTop: dir({ ltr: 2, rtl: 4 }),
41
+ })
42
+
43
+ function List() {
44
+ return (
45
+ <ul>
46
+ <Item>One</Item>
47
+ <Direction dir="ltr">
48
+ <Item>Two</Item>
49
+ </Direction>
50
+ </ul>
51
+ )
52
+ }
53
+
54
+ // Both items have one class, which holds both directions.
55
+ const html = renderToString(<List />)
56
+ const expectedClass = 'app-item-root-1wrr0da'
57
+ ```
58
+
59
+ From the passing test [`rtl.react.test.tsx`](https://github.com/soroush-tech/umber-style/blob/main/packages/umber-style/readme-examples/rtl.react.test.tsx).
60
+
61
+ <!-- gen:readme end -->
62
+
63
+ ## Vue
64
+
65
+ The same plugin, with `Direction` of `@umber-style/vue`. See [right-to-left.md](https://github.com/soroush-tech/umber-style/blob/main/docs/right-to-left.md#one-configure-both-directions).
66
+
67
+ <!-- gen:readme example rtl.vue.test.ts start - regenerated by `pnpm gen:readmes`; do not edit by hand -->
6
68
 
7
69
  ```ts
8
- import { configure } from '@umber-style/react'
9
- import { rtl } from '@umber-style/rtl'
70
+ import { configure, Direction, styled } from '@umber-style/vue'
71
+ import { dir, rtl } from '@umber-style/rtl'
72
+ import { createSSRApp, h } from 'vue'
73
+ import { renderToString } from 'vue/server-renderer'
74
+
75
+ configure({ prefix: 'app', plugins: [rtl()] })
76
+
77
+ const Item = styled('li', { name: 'Item' })({
78
+ marginLeft: 8,
79
+ textAlign: 'left',
80
+ marginTop: dir({ ltr: 2, rtl: 4 }),
81
+ })
82
+
83
+ const app = createSSRApp({
84
+ render: () =>
85
+ h('ul', [h(Item, () => 'One'), h(Direction, { dir: 'ltr' }, () => h(Item, () => 'Two'))]),
86
+ })
87
+ const html = await renderToString(app)
88
+ ```
89
+
90
+ From the passing test [`rtl.vue.test.ts`](https://github.com/soroush-tech/umber-style/blob/main/packages/umber-style/readme-examples/rtl.vue.test.ts).
91
+
92
+ <!-- gen:readme end -->
93
+
94
+ ## React Native
95
+
96
+ React Native has no CSS and the platform mirrors layout itself: the plugin flips what it does not mirror. See [right-to-left.md](https://github.com/soroush-tech/umber-style/blob/main/docs/right-to-left.md#react-native).
97
+
98
+ <!-- gen:readme example rtl.react-native.test.tsx start - regenerated by `pnpm gen:readmes`; do not edit by hand -->
99
+
100
+ ```tsx
101
+ import { configure, Direction, styled } from '@umber-style/react-native'
102
+ import { dir, rtl } from '@umber-style/rtl'
103
+ import { View } from 'react-native'
10
104
 
11
105
  configure({ plugins: [rtl()] })
106
+
107
+ const Box = styled(View, { name: 'Box' })({
108
+ marginLeft: 4,
109
+ transform: [{ translateX: 8 }],
110
+ marginTop: dir({ ltr: 2, rtl: 4 }),
111
+ })
112
+
113
+ export function Screen() {
114
+ return (
115
+ <Direction dir="rtl">
116
+ <Box />
117
+ <Direction dir="ltr">
118
+ <Box />
119
+ </Direction>
120
+ </Direction>
121
+ )
122
+ }
123
+
124
+ // In the right-to-left area the translation is flipped; the platform mirrors the margin itself.
125
+ const expected = [
126
+ { marginLeft: 4, transform: [{ translateX: -8 }], marginTop: 4 },
127
+ { marginLeft: 4, transform: [{ translateX: 8 }], marginTop: 2 },
128
+ ]
12
129
  ```
13
130
 
14
- `Direction`, which `@umber-style/react`, `@umber-style/vue` and `@umber-style/react-native` export, holds an area in one direction.
131
+ From the passing test [`rtl.react-native.test.tsx`](https://github.com/soroush-tech/umber-style/blob/main/packages/umber-style/readme-examples/rtl.react-native.test.tsx).
132
+
133
+ <!-- gen:readme end -->
134
+
135
+ ## API at a glance
136
+
137
+ <!-- gen:readme docs right-to-left.md#region:rtl-api start - regenerated by `pnpm gen:readmes`; do not edit by hand -->
138
+
139
+ | Export | What it is |
140
+ | ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
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 |
143
+ | `RtlOptions`, `DirValues` | The types of the options of `rtl()` and of the argument of `dir()` |
144
+ | `Direction` | Not in this package: `@umber-style/react`, `@umber-style/vue` and `@umber-style/react-native` export it |
145
+
146
+ <!-- gen:readme end -->
147
+
148
+ ## Limits
149
+
150
+ <!-- gen:readme docs right-to-left.md#region:rtl-limits start - regenerated by `pnpm gen:readmes`; do not edit by hand -->
151
+
152
+ - `:dir()` is supported in every major browser from late 2023 (no version was checked here: see [Browser support](https://github.com/soroush-tech/umber-style/blob/main/docs/browser-support.md)). An older browser gets the left-to-right layout through the fallback, whatever `dir` says. A browser that knows neither `:dir()` nor `@supports selector()` gets neither the rules of a direction nor the fallback: no declaration with a side applies there.
153
+ - One extra rule for each class that has a declaration with a side, and one for its fallback: a class with such declarations is four rules where it was one.
154
+ - A declaration with a side has the specificity of two classes (`.class:dir(rtl)`): against a rule of your own CSS with the specificity of one class, in the same cascade layer, it wins where the plain declaration would have lost. The layers of the engine (`app-components`, `app-overrides`) order the engine's own classes as before.
155
+
156
+ <!-- gen:readme end -->
157
+
158
+ The full list is in [Limits](https://github.com/soroush-tech/umber-style/blob/main/docs/right-to-left.md#limits).
159
+
160
+ ## Documentation
161
+
162
+ - [Right to left](https://github.com/soroush-tech/umber-style/blob/main/docs/right-to-left.md)
163
+ - [React](https://github.com/soroush-tech/umber-style/blob/main/docs/react.md)
164
+ - [Vue](https://github.com/soroush-tech/umber-style/blob/main/docs/vue.md)
165
+ - [React Native](https://github.com/soroush-tech/umber-style/blob/main/docs/react-native.md)
166
+ - [All the documentation](https://github.com/soroush-tech/umber-style/blob/main/docs/README.md)
167
+
168
+ ## License
15
169
 
16
- Documentation: [docs](../../docs/README.md), and [Right to left](../../docs/right-to-left.md) for this package.
170
+ MIT
package/dist/index.cjs CHANGED
@@ -456,7 +456,9 @@ function createName(base, switches) {
456
456
  /**
457
457
  * A value of a property for each direction (`marginTop: dir({ ltr: 2, rtl: 4 })`): with the plugin
458
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. Typed as the values it is given, so it goes wherever they go.
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.
460
462
  */
461
463
  function dir(values) {
462
464
  const given = {};
@@ -469,6 +471,26 @@ function dir(values) {
469
471
  const LTR = ":dir(ltr)";
470
472
  /** The condition of a declaration flipped, for right to left. */
471
473
  const RTL = ":dir(rtl)";
474
+ /** A selector that already names a direction: `[dir="rtl"] .c`, `.c:dir(rtl)`. */
475
+ const NAMES_DIRECTION = /\[dir|:dir\(/;
476
+ /** Whether this is a production build: `process.env.NODE_ENV` is `production`. Where `process` does not exist it is taken as one. */
477
+ function isProduction() {
478
+ try {
479
+ return process.env.NODE_ENV === "production";
480
+ } catch {
481
+ return true;
482
+ }
483
+ }
484
+ /**
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
487
+ * about; nothing is warned about in production.
488
+ */
489
+ function warnAboutDirection(selector, warned) {
490
+ if (isProduction() || selector.includes("&") || !NAMES_DIRECTION.test(selector) || warned.has(selector)) return;
491
+ warned.add(selector);
492
+ console.warn(`[rtl] The rule "${selector}" names a direction, and with rtl() it is flipped too, so its value is flipped twice. Use dir({ ltr, rtl }) for a value per direction instead.`);
493
+ }
472
494
  /** The declaration of the property of the other side, with the same value. */
473
495
  const renameEdge = (property, value) => [flipEdge(property), value];
474
496
  /** A rule that flips the value of a declaration with `flip`, and keeps its property. */
@@ -538,17 +560,20 @@ function toSides(property, value) {
538
560
  * other side under `:dir(rtl)` (`margin-left` is `margin-right`, never a logical property), and a value
539
561
  * that names or measures a side is written for the other one; `@supports not selector(:dir(ltr))` gives
540
562
  * the left-to-right declarations to a browser without `:dir()`. A `dir()` value is written as it is given,
541
- * each side under its own condition. Logical properties, the values `start` and `end`, vertical values and
563
+ * each side under its own condition. In development a rule whose selector names a direction (`[dir` or
564
+ * `:dir(`) is warned about once: it is flipped like any other. Logical properties, the values `start` and `end`, vertical values and
542
565
  * unknown properties are left as they are. `options` turns kinds off (or `urls` on).
543
566
  */
544
567
  function rtl(options = {}) {
568
+ const warned = /* @__PURE__ */ new Set();
545
569
  const switches = readSwitches(options);
546
570
  return {
547
571
  name: createName("rtl", switches),
548
572
  native: (style, { dir }) => transformNativeStyle(style, dir, switches),
549
573
  conditions: [LTR, RTL],
550
574
  fallback: `@supports not selector(${LTR})`,
551
- declaration(property, value) {
575
+ declaration(property, value, selector) {
576
+ warnAboutDirection(selector, warned);
552
577
  if (typeof value !== "string") return value.mark === "dir" ? toSides(property, value) : void 0;
553
578
  const rule = RULES.get(property);
554
579
  const isRuled = rule !== void 0 && switches[rule[0]];
package/dist/index.d.cts CHANGED
@@ -42,7 +42,9 @@ interface DirValues<T> {
42
42
  /**
43
43
  * A value of a property for each direction (`marginTop: dir({ ltr: 2, rtl: 4 })`): with the plugin
44
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.
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.
46
48
  */
47
49
  export declare function dir<T extends string | number>(values: DirValues<T>): T;
48
50
  /**
@@ -65,7 +67,8 @@ export interface RtlPlugin extends CachePlugin$1 {
65
67
  * other side under `:dir(rtl)` (`margin-left` is `margin-right`, never a logical property), and a value
66
68
  * that names or measures a side is written for the other one; `@supports not selector(:dir(ltr))` gives
67
69
  * 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
70
+ * each side under its own condition. In development a rule whose selector names a direction (`[dir` or
71
+ * `:dir(`) is warned about once: it is flipped like any other. Logical properties, the values `start` and `end`, vertical values and
69
72
  * unknown properties are left as they are. `options` turns kinds off (or `urls` on).
70
73
  */
71
74
  export declare function rtl(options?: RtlOptions): RtlPlugin;
package/dist/index.d.mts CHANGED
@@ -42,7 +42,9 @@ interface DirValues<T> {
42
42
  /**
43
43
  * A value of a property for each direction (`marginTop: dir({ ltr: 2, rtl: 4 })`): with the plugin
44
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.
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.
46
48
  */
47
49
  export declare function dir<T extends string | number>(values: DirValues<T>): T;
48
50
  /**
@@ -65,7 +67,8 @@ export interface RtlPlugin extends CachePlugin$1 {
65
67
  * other side under `:dir(rtl)` (`margin-left` is `margin-right`, never a logical property), and a value
66
68
  * that names or measures a side is written for the other one; `@supports not selector(:dir(ltr))` gives
67
69
  * 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
70
+ * each side under its own condition. In development a rule whose selector names a direction (`[dir` or
71
+ * `:dir(`) is warned about once: it is flipped like any other. Logical properties, the values `start` and `end`, vertical values and
69
72
  * unknown properties are left as they are. `options` turns kinds off (or `urls` on).
70
73
  */
71
74
  export declare function rtl(options?: RtlOptions): RtlPlugin;
package/dist/index.mjs CHANGED
@@ -455,7 +455,9 @@ function createName(base, switches) {
455
455
  /**
456
456
  * A value of a property for each direction (`marginTop: dir({ ltr: 2, rtl: 4 })`): with the plugin
457
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.
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.
459
461
  */
460
462
  function dir(values) {
461
463
  const given = {};
@@ -468,6 +470,26 @@ function dir(values) {
468
470
  const LTR = ":dir(ltr)";
469
471
  /** The condition of a declaration flipped, for right to left. */
470
472
  const RTL = ":dir(rtl)";
473
+ /** A selector that already names a direction: `[dir="rtl"] .c`, `.c:dir(rtl)`. */
474
+ const NAMES_DIRECTION = /\[dir|:dir\(/;
475
+ /** Whether this is a production build: `process.env.NODE_ENV` is `production`. Where `process` does not exist it is taken as one. */
476
+ function isProduction() {
477
+ try {
478
+ return process.env.NODE_ENV === "production";
479
+ } catch {
480
+ return true;
481
+ }
482
+ }
483
+ /**
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
486
+ * about; nothing is warned about in production.
487
+ */
488
+ function warnAboutDirection(selector, warned) {
489
+ if (isProduction() || selector.includes("&") || !NAMES_DIRECTION.test(selector) || warned.has(selector)) return;
490
+ warned.add(selector);
491
+ console.warn(`[rtl] The rule "${selector}" names a direction, and with rtl() it is flipped too, so its value is flipped twice. Use dir({ ltr, rtl }) for a value per direction instead.`);
492
+ }
471
493
  /** The declaration of the property of the other side, with the same value. */
472
494
  const renameEdge = (property, value) => [flipEdge(property), value];
473
495
  /** A rule that flips the value of a declaration with `flip`, and keeps its property. */
@@ -537,17 +559,20 @@ function toSides(property, value) {
537
559
  * other side under `:dir(rtl)` (`margin-left` is `margin-right`, never a logical property), and a value
538
560
  * that names or measures a side is written for the other one; `@supports not selector(:dir(ltr))` gives
539
561
  * 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
562
+ * each side under its own condition. In development a rule whose selector names a direction (`[dir` or
563
+ * `:dir(`) is warned about once: it is flipped like any other. Logical properties, the values `start` and `end`, vertical values and
541
564
  * unknown properties are left as they are. `options` turns kinds off (or `urls` on).
542
565
  */
543
566
  function rtl(options = {}) {
567
+ const warned = /* @__PURE__ */ new Set();
544
568
  const switches = readSwitches(options);
545
569
  return {
546
570
  name: createName("rtl", switches),
547
571
  native: (style, { dir }) => transformNativeStyle(style, dir, switches),
548
572
  conditions: [LTR, RTL],
549
573
  fallback: `@supports not selector(${LTR})`,
550
- declaration(property, value) {
574
+ declaration(property, value, selector) {
575
+ warnAboutDirection(selector, warned);
551
576
  if (typeof value !== "string") return value.mark === "dir" ? toSides(property, value) : void 0;
552
577
  const rule = RULES.get(property);
553
578
  const isRuled = rule !== void 0 && switches[rule[0]];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umber-style/rtl",
3
- "version": "0.10.0",
3
+ "version": "0.11.0-alpha.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",
@@ -12,7 +12,7 @@
12
12
  ],
13
13
  "license": "MIT",
14
14
  "author": "Masoud Soroush <masoud@soroush.tech>",
15
- "homepage": "https://umber-style.soroush.design",
15
+ "homepage": "https://github.com/soroush-tech/umber-style/blob/main/docs/README.md",
16
16
  "repository": {
17
17
  "type": "git",
18
18
  "url": "git+https://github.com/soroush-tech/umber-style.git",
@@ -42,7 +42,7 @@
42
42
  "access": "public"
43
43
  },
44
44
  "dependencies": {
45
- "@umber-style/core": "^0.10.0"
45
+ "@umber-style/core": "0.11.0-alpha.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@types/node": "^26.6.6",