From 1025b79bb46698cce378c9da3807cebf050bf065 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Wed, 25 Nov 2026 13:06:53 +0100 Subject: [PATCH 1/5] Use CSS logical properties instead of flipping styles in RTL --- .../__snapshots__/index-test.js.snap | 4 +- .../src/__tests__/index-test.js | 2 +- .../src/moduleMap.js | 1 - .../src/pages/docs/appendix/unstable-apis.md | 22 +- .../src/pages/docs/components/text-input.md | 8 - .../src/pages/docs/components/text.md | 8 - .../src/pages/docs/components/view.md | 8 - .../src/pages/docs/concepts/localization.md | 28 +- .../src/pages/docs/concepts/styling.md | 10 + .../pages/docs/hooks/use-locale-context.md | 37 - .../compiler-createReactDOMStyle-test.ts | 4 +- .../StyleSheet/__tests__/compiler-test.ts | 717 +++++++----------- .../StyleSheet/__tests__/index-test.ts | 153 +--- .../StyleSheet/__tests__/preprocess-test.ts | 212 ++---- .../StyleSheet/__tests__/validate-test.ts | 3 +- .../compiler/createReactDOMStyle.ts | 242 ++++-- .../src/exports/StyleSheet/compiler/index.ts | 250 +----- .../compiler/normalizeValueWithProperty.ts | 6 + .../src/exports/StyleSheet/index.ts | 73 +- .../src/exports/StyleSheet/localizeStyle.ts | 71 -- .../src/exports/StyleSheet/preprocess.ts | 53 +- .../src/exports/StyleSheet/validate.ts | 10 +- .../__snapshots__/index-test.tsx.snap | 8 +- .../__snapshots__/index-test.tsx.snap | 67 -- .../src/exports/Text/__tests__/index-test.tsx | 34 - .../src/exports/Text/index.tsx | 17 +- .../src/exports/Text/types.ts | 3 +- .../src/exports/TextInput/index.ts | 17 +- .../src/exports/TextInput/types.ts | 6 +- .../__snapshots__/index-test.tsx.snap | 44 -- .../src/exports/View/__tests__/index-test.tsx | 34 - .../src/exports/View/index.ts | 11 +- .../src/exports/View/types.ts | 2 - .../createElement/__tests__/index-test.tsx | 6 +- .../createElement/{index.tsx => index.ts} | 21 +- .../src/exports/useLocaleContext/index.ts | 11 - packages/react-native-web/src/index.ts | 1 - .../src/modules/createDOMProps/index.ts | 36 +- .../src/modules/forwardedProps/index.ts | 1 - .../src/modules/useLocale/index.tsx | 60 -- .../src/modules/useLocale/isLocaleRTL.ts | 72 -- packages/react-native-web/types/index.d.ts | 10 +- .../types/react-native-web.d.ts | 6 - 43 files changed, 698 insertions(+), 1691 deletions(-) delete mode 100644 packages/react-native-web-docs/src/pages/docs/hooks/use-locale-context.md delete mode 100644 packages/react-native-web/src/exports/StyleSheet/localizeStyle.ts rename packages/react-native-web/src/exports/createElement/{index.tsx => index.ts} (60%) delete mode 100644 packages/react-native-web/src/exports/useLocaleContext/index.ts delete mode 100644 packages/react-native-web/src/modules/useLocale/index.tsx delete mode 100644 packages/react-native-web/src/modules/useLocale/isLocaleRTL.ts diff --git a/packages/babel-plugin-react-native-web/src/__tests__/__snapshots__/index-test.js.snap b/packages/babel-plugin-react-native-web/src/__tests__/__snapshots__/index-test.js.snap index b7616ce705..126b17dd84 100644 --- a/packages/babel-plugin-react-native-web/src/__tests__/__snapshots__/index-test.js.snap +++ b/packages/babel-plugin-react-native-web/src/__tests__/__snapshots__/index-test.js.snap @@ -5,7 +5,7 @@ exports[`Rewrite react-native to react-native-web > 1. import from "react-native import ReactNative from 'react-native'; import { View } from 'react-native'; import { Invalid, View as MyView } from 'react-native'; -import { useLocaleContext } from 'react-native'; +import { useColorScheme } from 'react-native'; import * as ReactNativeModules from 'react-native'; ↓ ↓ ↓ ↓ ↓ ↓ @@ -14,7 +14,7 @@ import ReactNative from "react-native-web/dist/index.js"; import View from "react-native-web/dist/exports/View"; import { Invalid } from "react-native-web/dist/index.js"; import MyView from "react-native-web/dist/exports/View"; -import useLocaleContext from "react-native-web/dist/exports/useLocaleContext"; +import useColorScheme from "react-native-web/dist/exports/useColorScheme"; import * as ReactNativeModules from "react-native-web/dist/index.js"; `; diff --git a/packages/babel-plugin-react-native-web/src/__tests__/index-test.js b/packages/babel-plugin-react-native-web/src/__tests__/index-test.js index bbf6958591..24373b674b 100644 --- a/packages/babel-plugin-react-native-web/src/__tests__/index-test.js +++ b/packages/babel-plugin-react-native-web/src/__tests__/index-test.js @@ -8,7 +8,7 @@ const tests = [ code: `import ReactNative from 'react-native'; import { View } from 'react-native'; import { Invalid, View as MyView } from 'react-native'; -import { useLocaleContext } from 'react-native'; +import { useColorScheme } from 'react-native'; import * as ReactNativeModules from 'react-native';`, snapshot: true }, diff --git a/packages/babel-plugin-react-native-web/src/moduleMap.js b/packages/babel-plugin-react-native-web/src/moduleMap.js index 57a8c1f5e3..90801b2525 100644 --- a/packages/babel-plugin-react-native-web/src/moduleMap.js +++ b/packages/babel-plugin-react-native-web/src/moduleMap.js @@ -59,6 +59,5 @@ module.exports = { useAnimatedValue: true, useAnimatedValueXY: true, useColorScheme: true, - useLocaleContext: true, useWindowDimensions: true }; diff --git a/packages/react-native-web-docs/src/pages/docs/appendix/unstable-apis.md b/packages/react-native-web-docs/src/pages/docs/appendix/unstable-apis.md index c005e963f2..450cd18ee4 100644 --- a/packages/react-native-web-docs/src/pages/docs/appendix/unstable-apis.md +++ b/packages/react-native-web-docs/src/pages/docs/appendix/unstable-apis.md @@ -17,6 +17,12 @@ The following APIs are unstable and subject to breaking changes. Use at your own React Native for Web exports a web-specific module called `unstable_createElement`, which can be used to wrap React DOM components. This allows you to use React Native's accessibility and style optimizations. Since this is a web-specific export it should always be imported from the `react-native-web` package. +It has the same signature as `React.createElement`: + +```js +unstable_createElement(component, props, ...children); +``` + In the example below, `Video` will now accept common React Native props such as `aria-label`, `accessible`, `style`, and even the Responder event props. ```js @@ -29,17 +35,13 @@ This also works with composite components defined in your existing component gal ```js import RaisedButton from 'material-ui/RaisedButton'; import { StyleSheet } from 'react-native'; -import { unstable_createElement, useLocaleContext } from 'react-native-web'; - -const CustomButton = (props) => { - const { direction } = useLocaleContext(); +import { unstable_createElement } from 'react-native-web'; - return unstable_createElement( - RaisedButton, - { ...props, style: [styles.button, props.style] }, - { writingDirection: direction } - ); -}; +const CustomButton = (props) => + unstable_createElement(RaisedButton, { + ...props, + style: [styles.button, props.style] + }); const styles = StyleSheet.create({ button: { diff --git a/packages/react-native-web-docs/src/pages/docs/components/text-input.md b/packages/react-native-web-docs/src/pages/docs/components/text-input.md index 7c64eafc23..ae3b56cecc 100644 --- a/packages/react-native-web-docs/src/pages/docs/components/text-input.md +++ b/packages/react-native-web-docs/src/pages/docs/components/text-input.md @@ -76,10 +76,6 @@ Equivalent to [HTMLElement.dataset](https://developer.mozilla.org/en-US/docs/Web The initial value of the input. Useful for simple use-cases where you don't want to deal with listening to events and updating the value prop to keep the controlled state in sync. {% endcall %} -{% call macro.prop('dir', '?("auto" | "ltr" | "rtl") = "auto"') %} -Equivalent to [HTMLElement.dir](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dir). -{% endcall %} - {% call macro.prop('disabled', '?boolean = false') %} Equivalent to [HTMLElement.disabled](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled) {% endcall %} @@ -96,10 +92,6 @@ Equivalent to [HTMLElement.id](https://developer.mozilla.org/en-US/docs/Web/HTML Hints at the type of data that might be entered by the user while editing the element or its contents. Equivalent to [HTMLElement.inputMode](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inputmode). Safari iOS requires an ancestral `
` element to display the search keyboard. (Not available when multiline is true.) {% endcall %} -{% call macro.prop('lang', '?string') %} -Equivalent to [HTMLElement.lang](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang). This prop is used to infer writing direction if no `dir` is set. -{% endcall %} - {% call macro.prop('maxLength', '?number') %} Equivalent to [HTMLElement.maxlength](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/maxlength). {% endcall %} diff --git a/packages/react-native-web-docs/src/pages/docs/components/text.md b/packages/react-native-web-docs/src/pages/docs/components/text.md index 6080cb2482..6a030e6ec5 100644 --- a/packages/react-native-web-docs/src/pages/docs/components/text.md +++ b/packages/react-native-web-docs/src/pages/docs/components/text.md @@ -60,10 +60,6 @@ The children of a `Text` element can be strings as well as other elements like ` Equivalent to [HTMLElement.dataset](https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/dataset). {% endcall %} -{% call macro.prop('dir', '?("auto" | "ltr" | "rtl") = "auto"') %} -Equivalent to [HTMLElement.dir](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dir). The default value of `"auto"` is not set on _nested_ text elements. -{% endcall %} - {% call macro.prop('ellipsizeMode', '?("clip" | "head" | "middle" | "tail") = "tail"') %} When `numberOfLines` is set, defines how the text is truncated. Only `"clip"` and `"tail"` are supported on web. {% endcall %} @@ -80,10 +76,6 @@ If `href` is defined, this prop defines related attributes to include on the anc Equivalent to [HTMLElement.id](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id). {% endcall %} -{% call macro.prop('lang', '?string') %} -Equivalent to [HTMLElement.lang](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang). This prop is used to infer writing direction if no `dir` is set. -{% endcall %} - {% call macro.prop('numberOfLines', '?number') %} Truncates the text with an ellipsis after this many lines. {% endcall %} diff --git a/packages/react-native-web-docs/src/pages/docs/components/view.md b/packages/react-native-web-docs/src/pages/docs/components/view.md index 3468b245c9..ed51a15e5c 100644 --- a/packages/react-native-web-docs/src/pages/docs/components/view.md +++ b/packages/react-native-web-docs/src/pages/docs/components/view.md @@ -62,10 +62,6 @@ The children of a `View` element can be other elements and must _not_ include st Equivalent to [HTMLElement.dataset](https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/dataset). {% endcall %} -{% call macro.prop('dir', '?("ltr" | "rtl")') %} -Equivalent to [HTMLElement.dir](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/dir). -{% endcall %} - {% call macro.prop('href', '?string') %} If `href` is defined, the view is rendered as an anchor tag pointing to this URL. {% endcall %} @@ -78,10 +74,6 @@ If `href` is defined, this prop defines related attributes to include on the anc Equivalent to [HTMLElement.id](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id). {% endcall %} -{% call macro.prop('lang', '?string') %} -Equivalent to [HTMLElement.lang](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang). This prop is used to infer writing direction if no `dir` is set. -{% endcall %} - {% call macro.prop('onLayout', '?({ nativeEvent: { layout: { x, y, width, height } } }) => void') %} This is invoked when a component is mounted and when its layout changes. `x` and `y` are the offsets from the parent node. {% endcall %} diff --git a/packages/react-native-web-docs/src/pages/docs/concepts/localization.md b/packages/react-native-web-docs/src/pages/docs/concepts/localization.md index 7b671d2743..f188f29714 100644 --- a/packages/react-native-web-docs/src/pages/docs/concepts/localization.md +++ b/packages/react-native-web-docs/src/pages/docs/concepts/localization.md @@ -17,7 +17,7 @@ A guide to producing localized layout for different locales. ## Localization styles -The non-standard [direction-independent style properties and values]({{ '/docs/styling/#non-standard-properties' | url }}) (e.g., `marginStart`) should be used as much as possible. {{ site.name }} will automatically flip the direction of these properties within subtrees based on the writing direction of the ancestor tree. +The non-standard [direction-independent style properties and values]({{ '/docs/styling/#non-standard-properties' | url }}) (e.g., `marginStart`) should be used as much as possible. They are output as CSS logical properties (e.g., `margin-inline-start`), so the browser resolves them from the writing direction of the element. ```jsx // "start" is "left" for LTR and "right" for RTL @@ -25,30 +25,10 @@ const style = { paddingInlineStart: 10, marginInlineStart: 10 }; return ; ``` -## Localization props +## Writing direction -To automatically flip the layout of a component and its subtree, you can either: - -1. Set the `dir` prop to the desired writing direction (e.g., `"rtl"`). -2. Set the `lang` prop to the locale of the content. +To flip the layout of a component and its subtree, set the `direction` style to the desired writing direction (e.g., `"rtl"`). ```jsx -... -... -``` - -By default, the native writing direction of `Text` and `TextInput` components is set to `"auto"`. This uses the browser's built-in writing direction algorithm to detect whether the text should be displayed left-to-right or right-to-left. - -## Localization hook - -The `useLocaleContext` API can be used for fine-grained control of layout, e.g., flipping images or transforms. - -```jsx -const { direction, locale } = useLocaleContext(); - -const isRTL = direction === 'rtl'; -const style = { scaleX: isRTL ? -1 : 1 }; - - - +... ``` diff --git a/packages/react-native-web-docs/src/pages/docs/concepts/styling.md b/packages/react-native-web-docs/src/pages/docs/concepts/styling.md index 5917781f6b..8af0cc8a4a 100644 --- a/packages/react-native-web-docs/src/pages/docs/concepts/styling.md +++ b/packages/react-native-web-docs/src/pages/docs/concepts/styling.md @@ -60,6 +60,12 @@ You have greater control over how styles are composed when compared to using cla {{ site.name }} supports all _long-form_ CSS properties. There is no direct support for `@`-rules, selectors, pseudo-selectors, and pseudo-elements, equivalents of which are demonstrated in the [styling patterns](#styling-patterns) section below. +### Direction + +{% call macro.prop('direction', '?("inherit" | "ltr" | "rtl")') %} +Equivalent to [direction](https://developer.mozilla.org/en-US/docs/Web/CSS/direction). Sets the writing direction of a component and its subtree. Logical properties (e.g., `marginInlineStart`) follow it. +{% endcall %} + ### Short-form properties The supported short-form CSS properties accept only a _single_ value. @@ -85,6 +91,10 @@ Accepts only positive integers, `0`, or `-1`. The value of `-1` is non-standard and equivalent to setting `flowGrow:0` and `flexShrink:1`. {% endcall %} +{% call macro.prop('gap', '?(number | string)') %} +Accepts only a single value that is applied to both axes. +{% endcall %} + {% call macro.prop('inset', '?(string | number)') %} Equivalent to [inset](https://developer.mozilla.org/en-US/docs/Web/CSS/inset). {% endcall %} diff --git a/packages/react-native-web-docs/src/pages/docs/hooks/use-locale-context.md b/packages/react-native-web-docs/src/pages/docs/hooks/use-locale-context.md deleted file mode 100644 index 9052816619..0000000000 --- a/packages/react-native-web-docs/src/pages/docs/hooks/use-locale-context.md +++ /dev/null @@ -1,37 +0,0 @@ ---- -title: useLocaleContext -date: Last Modified -permalink: /docs/use-locale-context/index.html -eleventyNavigation: - key: useLocaleContext - parent: Hooks ---- - -{% import "fragments/macros.html" as macro with context %} - -:::lead -Respond to locale and writing direction changes from ancestors. -::: - -The return value matches the current locale and writing direction of the ancestral tree. The value will be updated for descendants when setting the `dir` or `lang` prop on an element. - -```js -import { useLocaleContext } from 'react-native'; -const { direction, locale } = useLocaleContext(); -``` - ---- - -## API - -### Return value - -`useLocaleContext` returns the locale context. - -{% call macro.prop('direction', '"ltr" | "rtl"') %} -A string representing the writing direction context. -{% endcall %} - -{% call macro.prop('locale', '?string') %} -The locale context. Locale is `null` if a new direction context is set without an associated locale. -{% endcall %} diff --git a/packages/react-native-web/src/exports/StyleSheet/__tests__/compiler-createReactDOMStyle-test.ts b/packages/react-native-web/src/exports/StyleSheet/__tests__/compiler-createReactDOMStyle-test.ts index b0d55ba27e..4eb754142f 100644 --- a/packages/react-native-web/src/exports/StyleSheet/__tests__/compiler-createReactDOMStyle-test.ts +++ b/packages/react-native-web/src/exports/StyleSheet/__tests__/compiler-createReactDOMStyle-test.ts @@ -35,12 +35,12 @@ describe('compiler/createReactDOMStyle', () => { borderBottomWidth: 1, borderWidth: 0, marginTop: 50, - marginBlock: 25, + marginVertical: 25, margin: 10, overflow: 'hidden', overscrollBehavior: 'contain', paddingLeft: 50, - paddingInline: 25, + paddingHorizontal: 25, padding: 10 }; diff --git a/packages/react-native-web/src/exports/StyleSheet/__tests__/compiler-test.ts b/packages/react-native-web/src/exports/StyleSheet/__tests__/compiler-test.ts index 489141e406..82c9af115a 100644 --- a/packages/react-native-web/src/exports/StyleSheet/__tests__/compiler-test.ts +++ b/packages/react-native-web/src/exports/StyleSheet/__tests__/compiler-test.ts @@ -5,7 +5,11 @@ * LICENSE file in the root directory of this source tree. */ -import { atomic, classic, inline } from '../compiler'; +import { atomic, classic } from '../compiler'; +import createReactDOMStyle from '../compiler/createReactDOMStyle'; + +const inline = (style: Readonly>) => + createReactDOMStyle(style, true); describe('StyleSheet/compile', () => { describe('atomic', () => { @@ -30,145 +34,108 @@ describe('StyleSheet/compile', () => { }); expect(result).toMatchInlineSnapshot(` - [ - { - "$$css": true, - "$$css$localize": true, - "animationDirection": "r-animationDirection-1wgwto7", - "animationKeyframes": "r-animationKeyframes-zacbmr", - "fontFamily": "r-fontFamily-1qd0xha", - "insetInlineStart": [ - "r-insetInlineStart-1xn1m1p", - "r-insetInlineStart-1y2vi53", - ], - "marginBlockEnd": "r-marginBlockEnd-1xf1q1v", - "marginInline": "r-marginInline-lcslpx", - "marginInlineEnd": [ - "r-marginInlineEnd-n2wkgt", - "r-marginInlineEnd-r7lizz", - ], - "marginInlineStart": [ - "r-marginInlineStart-hjq6k0", - "r-marginInlineStart-1iwt575", - ], - "placeholderTextColor": "r-placeholderTextColor-1418aci", - "pointerEvents": "r-pointerEvents-ah5dr5", - "scrollbarWidth": "r-scrollbarWidth-2eszeu", - "textAlign": [ - "r-textAlign-fdjqy7", - "r-textAlign-1ff274t", - ], - "transform": "r-transform-d7xd9i", - }, - [ - [ - [ - ".r-animationDirection-1wgwto7{animation-direction:alternate,alternate-reverse;}", - ], - 3, - ], - [ - [ - ".r-animationKeyframes-zacbmr{animation-name:r-animation-8jhqzh,r-animation-5azpl5;}", - "@keyframes r-animation-8jhqzh{0%{top:0px;}50%{top:5px;}100%{top:10px;}}", - "@keyframes r-animation-5azpl5{from{left:0px;}to{left:10px;}}", - ], - 3, - ], - [ - [ - ".r-fontFamily-1qd0xha{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;}", - ], - 3, - ], - [ - [ - ".r-insetInlineStart-1xn1m1p{left:12.34%;}", - ], - 2.2, - ], - [ - [ - ".r-insetInlineStart-1y2vi53{right:12.34%;}", - ], - 2.2, - ], - [ - [ - ".r-marginBlockEnd-1xf1q1v{margin-bottom:5px;}", - ], - 2.2, - ], - [ - [ - ".r-marginInline-lcslpx{margin-left:10px;margin-right:10px;}", - ], - 2.1, - ], - [ - [ - ".r-marginInlineEnd-n2wkgt{margin-right:2px;}", - ], - 2.2, - ], - [ - [ - ".r-marginInlineEnd-r7lizz{margin-left:2px;}", - ], - 2.2, - ], - [ - [ - ".r-marginInlineStart-hjq6k0{margin-left:1px;}", - ], - 2.2, - ], - [ - [ - ".r-marginInlineStart-1iwt575{margin-right:1px;}", - ], - 2.2, - ], - [ - [ - ".r-placeholderTextColor-1418aci::placeholder{color:rgba(128,128,128,1.00);opacity:1;}", - ], - 3, - ], - [ - [ - ".r-pointerEvents-ah5dr5>* {pointer-events:none;}", - ".r-pointerEvents-ah5dr5{pointer-events:auto!important;}", - ], - 3, - ], - [ - [ - ".r-scrollbarWidth-2eszeu::-webkit-scrollbar{display:none}", - ".r-scrollbarWidth-2eszeu{scrollbar-width:none;}", - ], - 3, - ], - [ - [ - ".r-textAlign-fdjqy7{text-align:left;}", - ], - 3, - ], - [ - [ - ".r-textAlign-1ff274t{text-align:right;}", - ], - 3, - ], - [ - [ - ".r-transform-d7xd9i{transform:translateX(50px) scale(-1);}", - ], - 3, - ], - ], - ] + [ + { + "$$css": true, + "animationDirection": "r-animationDirection-1wgwto7", + "animationKeyframes": "r-animationKeyframes-zacbmr", + "fontFamily": "r-fontFamily-1qd0xha", + "insetInlineStart": "r-insetInlineStart-1a9qabf", + "marginBlockEnd": "r-marginBlockEnd-1xf1q1v", + "marginInline": "r-marginInline-lcslpx", + "marginInlineEnd": "r-marginInlineEnd-1c1fkyo", + "marginInlineStart": "r-marginInlineStart-1k2jm4g", + "placeholderTextColor": "r-placeholderTextColor-1418aci", + "pointerEvents": "r-pointerEvents-ah5dr5", + "scrollbarWidth": "r-scrollbarWidth-2eszeu", + "textAlign": "r-textAlign-14537ki", + "transform": "r-transform-d7xd9i", + }, + [ + [ + [ + ".r-animationDirection-1wgwto7{animation-direction:alternate,alternate-reverse;}", + ], + 3, + ], + [ + [ + ".r-animationKeyframes-zacbmr{animation-name:r-animation-8jhqzh,r-animation-5azpl5;}", + "@keyframes r-animation-8jhqzh{0%{top:0px;}50%{top:5px;}100%{top:10px;}}", + "@keyframes r-animation-5azpl5{from{left:0px;}to{left:10px;}}", + ], + 3, + ], + [ + [ + ".r-fontFamily-1qd0xha{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;}", + ], + 3, + ], + [ + [ + ".r-insetInlineStart-1a9qabf{inset-inline-start:12.34%;}", + ], + 2.2, + ], + [ + [ + ".r-marginBlockEnd-1xf1q1v{margin-block-end:5px;}", + ], + 2.2, + ], + [ + [ + ".r-marginInline-lcslpx{margin-inline:10px;}", + ], + 2.1, + ], + [ + [ + ".r-marginInlineEnd-1c1fkyo{margin-inline-end:2px;}", + ], + 2.2, + ], + [ + [ + ".r-marginInlineStart-1k2jm4g{margin-inline-start:1px;}", + ], + 2.2, + ], + [ + [ + ".r-placeholderTextColor-1418aci::placeholder{color:rgba(128,128,128,1.00);opacity:1;}", + ], + 3, + ], + [ + [ + ".r-pointerEvents-ah5dr5>* {pointer-events:none;}", + ".r-pointerEvents-ah5dr5{pointer-events:auto!important;}", + ], + 3, + ], + [ + [ + ".r-scrollbarWidth-2eszeu::-webkit-scrollbar{display:none}", + ".r-scrollbarWidth-2eszeu{scrollbar-width:none;}", + ], + 3, + ], + [ + [ + ".r-textAlign-14537ki{text-align:start;}", + ], + 3, + ], + [ + [ + ".r-transform-d7xd9i{transform:translateX(50px) scale(-1);}", + ], + 3, + ], + ], + ] `); }); @@ -220,64 +187,23 @@ describe('StyleSheet/compile', () => { `); }); - test('converts logical transitionProperty values', () => { - expect(atomic({ transitionProperty: 'margin-inline-start, opacity' })) - .toMatchInlineSnapshot(` - [ - { - "$$css": true, - "$$css$localize": true, - "transitionProperty": [ - "r-transitionProperty-1b4l1tn", - "r-transitionProperty-1mussrw", - ], - }, - [ - [ - [ - ".r-transitionProperty-1b4l1tn{transition-property:margin-left, opacity;}", - ], - 3, - ], - [ - [ - ".r-transitionProperty-1mussrw{transition-property:margin-right, opacity;}", - ], - 3, - ], - ], - ] - `); - - expect(atomic({ transitionProperty: 'margin-left, opacity' })) - .toMatchInlineSnapshot(` - [ - { - "$$css": true, - "transitionProperty": "r-transitionProperty-1b4l1tn", - }, - [ - [ - [ - ".r-transitionProperty-1b4l1tn{transition-property:margin-left, opacity;}", - ], - 3, - ], - ], - ] + test('expands physical axis shortform properties', () => { + expect(atomic({ marginHorizontal: 10 })).toMatchInlineSnapshot(` + [ + { + "$$css": true, + "marginHorizontal": "r-marginHorizontal-vlx1xi", + }, + [ + [ + [ + ".r-marginHorizontal-vlx1xi{margin-left:10px;margin-right:10px;}", + ], + 2.3, + ], + ], + ] `); - - expect( - atomic({ transitionProperty: 'marginInlineStart, opacity' }) - ).toEqual(atomic({ transitionProperty: 'margin-inline-start, opacity' })); - - expect(atomic({ transitionProperty: 'marginStart, opacity' })).toEqual( - atomic({ transitionProperty: 'margin-inline-start, opacity' }) - ); - - expect( - atomic({ transitionProperty: ['marginInlineStart', 'opacity'] }) - ).toEqual(atomic({ transitionProperty: 'margin-inline-start, opacity' })); }); }); @@ -297,22 +223,22 @@ describe('StyleSheet/compile', () => { 'test' ); expect(result).toMatchInlineSnapshot(` - [ - { - "$$css": true, - "css-test-tbk4su": "css-test-tbk4su", - }, - [ - [ - [ - "@keyframes r-animation-8jhqzh{0%{top:0px;}50%{top:5px;}100%{top:10px;}}", - "@keyframes r-animation-5azpl5{from{left:0px;}to{left:10px;}}", - ".css-test-tbk4su{animation-direction:alternate,alternate-reverse;animation-name:r-animation-8jhqzh,r-animation-5azpl5;font:14px -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;margin-left:10px;margin-right:10px;transform:translateX(50px) scale(-1);}", - ], - 1, - ], - ], - ] + [ + { + "$$css": true, + "css-test-tbk4su": "css-test-tbk4su", + }, + [ + [ + [ + "@keyframes r-animation-8jhqzh{0%{top:0px;}50%{top:5px;}100%{top:10px;}}", + "@keyframes r-animation-5azpl5{from{left:0px;}to{left:10px;}}", + ".css-test-tbk4su{animation-direction:alternate,alternate-reverse;animation-name:r-animation-8jhqzh,r-animation-5azpl5;font:14px -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;margin-inline:10px;transform:translateX(50px) scale(-1);}", + ], + 1, + ], + ], + ] `); }); }); @@ -327,245 +253,146 @@ describe('StyleSheet/compile', () => { }); expect(result).toMatchInlineSnapshot(` - { - "display": "flex", - "flexShrink": 1, - "marginBottom": "5px", - "marginLeft": "10px", - "marginRight": "10px", - } + { + "display": "flex", + "flexShrink": 1, + "marginBlockEnd": "5px", + "marginInlineEnd": "10px", + "marginInlineStart": "10px", + } `); }); - describe('logical style polyfill', () => { - [false, true].forEach((isRTL) => { - const dir = isRTL ? 'rtl' : 'ltr'; - - test(`ignores "left" properties for ${dir}`, () => { - const initial = { - borderLeftColor: 'rgba(255,0,0,1.00)', - borderLeftStyle: 'solid', - borderLeftWidth: '1px', - left: '1px', - marginLeft: '5px', - paddingLeft: '10px' - }; - expect(inline(initial, isRTL)).toEqual(initial); - }); - - test(`ignores "right" properties for ${dir}`, () => { - const initial = { - borderRightColor: 'rgba(255,0,0,1.00)', - borderRightStyle: 'solid', - borderRightWidth: '1px', - right: '1px', - marginRight: '5px', - paddingRight: '10px' - }; - expect(inline(initial, isRTL)).toEqual(initial); - }); - - test(`ignores "left" values for ${dir}`, () => { - const initial = { - clear: 'left', - float: 'left', - textAlign: 'left', - transitionProperty: 'left' - }; - expect(inline(initial, isRTL)).toEqual(initial); - }); - - test(`ignores "right" values for ${dir}`, () => { - const initial = { - clear: 'right', - float: 'right', - textAlign: 'right', - transitionProperty: 'right' - }; - expect(inline(initial, isRTL)).toEqual(initial); - }); - - test(`converts transitionProperty values for ${dir}`, () => { - const string = { - transitionProperty: 'inset-inline-start, marginInlineEnd,opacity' - }; - - const array = { - transitionProperty: [ - 'borderStartEndRadius', - 'padding-inline-start,opacity' - ] - }; - - expect(inline(string, isRTL)).toEqual({ - transitionProperty: isRTL - ? 'right, margin-left, opacity' - : 'left, margin-right, opacity' - }); - - expect(inline(array, isRTL)).toEqual({ - transitionProperty: isRTL - ? 'border-top-left-radius, padding-right, opacity' - : 'border-top-right-radius, padding-left, opacity' - }); - }); - - test(`converts "start" properties for ${dir}`, () => { - const initial = { - borderInlineStartColor: 'red', - borderInlineStartStyle: 'solid', - borderInlineStartWidth: 1, - insetInlineStart: 1, - marginBlockStart: 5, - marginInlineStart: 5, - paddingInlineStart: 10 - }; - - const expectedLTR = { - borderLeftColor: 'rgba(255,0,0,1.00)', - borderLeftStyle: 'solid', - borderLeftWidth: '1px', - left: '1px', - marginLeft: '5px', - marginTop: '5px', - paddingLeft: '10px' - }; - const expectedRTL = { - borderRightColor: 'rgba(255,0,0,1.00)', - borderRightStyle: 'solid', - borderRightWidth: '1px', - right: '1px', - marginRight: '5px', - marginTop: '5px', - paddingRight: '10px' - }; - expect(inline(initial, isRTL)).toEqual( - isRTL ? expectedRTL : expectedLTR - ); - }); - - test(`converts "end" properties for ${dir}`, () => { - const initial = { - borderInlineEndColor: 'red', - borderInlineEndStyle: 'solid', - borderInlineEndWidth: 1, - insetInlineEnd: 1, - marginBlockEnd: 5, - marginInlineEnd: 5, - paddingInlineEnd: 10 - }; - - const expectedLTR = { - borderRightColor: 'rgba(255,0,0,1.00)', - borderRightStyle: 'solid', - borderRightWidth: '1px', - right: '1px', - marginBottom: '5px', - marginRight: '5px', - paddingRight: '10px' - }; - const expectedRTL = { - borderLeftColor: 'rgba(255,0,0,1.00)', - borderLeftStyle: 'solid', - borderLeftWidth: '1px', - left: '1px', - marginBottom: '5px', - marginLeft: '5px', - paddingLeft: '10px' - }; - expect(inline(initial, isRTL)).toEqual( - isRTL ? expectedRTL : expectedLTR - ); - }); - - test(`converts "start" values for ${dir}`, () => { - const initial = { - clear: 'start', - float: 'start', - textAlign: 'start', - transitionProperty: 'insetInlineStart' - }; - - const expectedLTR = { - clear: 'left', - float: 'left', - textAlign: 'left', - transitionProperty: 'left' - }; - const expectedRTL = { - clear: 'right', - float: 'right', - textAlign: 'right', - transitionProperty: 'right' - }; - expect(inline(initial, isRTL)).toEqual( - isRTL ? expectedRTL : expectedLTR - ); - }); + test('axis shortform properties take precedence over shortform properties', () => { + const expected = { + borderBlockEndColor: 'rgba(255,0,0,1.00)', + borderBlockStartColor: 'rgba(255,0,0,1.00)', + borderLeftColor: 'rgba(0,0,255,1.00)', + borderRightColor: 'rgba(0,0,255,1.00)' + }; + expect(inline({ borderBlockColor: 'red', borderColor: 'blue' })).toEqual( + expected + ); + expect(inline({ borderColor: 'blue', borderBlockColor: 'red' })).toEqual( + expected + ); + expect(inline({ marginInline: 4, margin: 8 })).toEqual({ + marginBottom: '8px', + marginInlineEnd: '4px', + marginInlineStart: '4px', + marginTop: '8px' + }); + expect(inline({ marginHorizontal: 4, margin: 8 })).toEqual({ + marginBottom: '8px', + marginLeft: '4px', + marginRight: '4px', + marginTop: '8px' + }); + }); - test(`converts "end" values for ${dir}`, () => { - const initial = { - clear: 'end', - float: 'end', - textAlign: 'end', - transitionProperty: 'insetInlineEnd' - }; + test('longform properties take precedence over shortform properties', () => { + expect(inline({ rowGap: 5, gap: 10 })).toEqual({ + columnGap: '10px', + rowGap: '5px' + }); + expect(inline({ marginBlock: 16 })).toEqual({ + marginBlockEnd: '16px', + marginBlockStart: '16px' + }); + expect(inline({ marginVertical: 16 })).toEqual({ + marginBottom: '16px', + marginTop: '16px' + }); + expect(inline({ marginLeft: 8, marginHorizontal: 16 })).toEqual({ + marginLeft: '8px', + marginRight: '16px' + }); + expect(inline({ marginInlineStart: 8, marginInline: 16 })).toEqual({ + marginInlineEnd: '16px', + marginInlineStart: '8px' + }); + expect(inline({ paddingTop: 4, padding: 10 })).toEqual({ + paddingBottom: '10px', + paddingLeft: '10px', + paddingRight: '10px', + paddingTop: '4px' + }); + }); - const expectedLTR = { - clear: 'right', - float: 'right', - textAlign: 'right', - transitionProperty: 'right' - }; - const expectedRTL = { - clear: 'left', - float: 'left', - textAlign: 'left', - transitionProperty: 'left' - }; - expect(inline(initial, isRTL)).toEqual( - isRTL ? expectedRTL : expectedLTR - ); - }); + test('shortform properties expand to logical properties next to logical longform properties', () => { + expect(inline({ marginBlockStart: 8, margin: 16 })).toEqual({ + marginBlockEnd: '16px', + marginBlockStart: '8px', + marginLeft: '16px', + marginRight: '16px' + }); + expect(inline({ paddingInlineStart: 4, padding: 10 })).toEqual({ + paddingBottom: '10px', + paddingInlineEnd: '10px', + paddingInlineStart: '4px', + paddingTop: '10px' + }); + expect(inline({ insetInlineEnd: 0, inset: 10 })).toEqual({ + bottom: '10px', + insetInlineEnd: '0px', + insetInlineStart: '10px', + top: '10px' + }); + expect( + inline({ borderInlineStartColor: 'red', borderColor: 'blue' }) + ).toEqual({ + borderBottomColor: 'rgba(0,0,255,1.00)', + borderInlineEndColor: 'rgba(0,0,255,1.00)', + borderInlineStartColor: 'rgba(255,0,0,1.00)', + borderTopColor: 'rgba(0,0,255,1.00)' + }); + expect(inline({ borderStartStartRadius: 10, borderRadius: 4 })).toEqual({ + borderEndEndRadius: '4px', + borderEndStartRadius: '4px', + borderStartEndRadius: '4px', + borderStartStartRadius: '10px' + }); + }); - test('physical properties take precedence over logical properties', () => { - const initialLTR = { - borderInlineStartWidth: 10, - borderLeftWidth: 0, - insetInlineEnd: 10, - right: 0, - insetBlockStart: 5, - top: 0, - marginInlineStart: 10, - marginLeft: 0 - }; - const expectedLTR = { - borderLeftWidth: '0px', - marginLeft: '0px', - right: '0px', - top: '0px' - }; + test('static shortform properties expand to logical properties next to logical longform properties', () => { + expect( + createReactDOMStyle({ + borderColor: 'blue', + borderInlineStartColor: 'red' + }) + ).toEqual({ + borderBottomColor: 'rgba(0,0,255,1.00)', + borderInlineEndColor: 'rgba(0,0,255,1.00)', + borderInlineStartColor: 'rgba(255,0,0,1.00)', + borderTopColor: 'rgba(0,0,255,1.00)' + }); + }); - const initialRTL = { - borderInlineStartWidth: 10, - borderRightWidth: 0, - insetInlineEnd: 10, - left: 0, - insetBlockStart: 5, - top: 0, - marginInlineStart: 10, - marginRight: 0 - }; - const expectedRTL = { - borderRightWidth: '0px', - marginRight: '0px', - left: '0px', - top: '0px' - }; - expect(inline(isRTL ? initialRTL : initialLTR, isRTL)).toEqual( - isRTL ? expectedRTL : expectedLTR - ); - }); + test('keeps logical properties and values', () => { + const initial = { + borderInlineEndColor: 'red', + borderInlineStartWidth: 1, + borderStartStartRadius: 2, + clear: 'inline-start', + float: 'inline-end', + insetInlineStart: 1, + marginInlineEnd: 5, + paddingInlineStart: 10, + textAlign: 'start', + transitionProperty: 'margin-inline-start' + }; + + expect(inline(initial)).toEqual({ + borderInlineEndColor: 'rgba(255,0,0,1.00)', + borderInlineStartWidth: '1px', + borderStartStartRadius: '2px', + clear: 'inline-start', + float: 'inline-end', + insetInlineStart: '1px', + marginInlineEnd: '5px', + paddingInlineStart: '10px', + textAlign: 'start', + transitionProperty: 'margin-inline-start' }); }); }); diff --git a/packages/react-native-web/src/exports/StyleSheet/__tests__/index-test.ts b/packages/react-native-web/src/exports/StyleSheet/__tests__/index-test.ts index f421fa20cd..3c3722a521 100644 --- a/packages/react-native-web/src/exports/StyleSheet/__tests__/index-test.ts +++ b/packages/react-native-web/src/exports/StyleSheet/__tests__/index-test.ts @@ -237,18 +237,6 @@ describe('StyleSheet', () => { ); }); - test('localizes compiled objects', () => { - const compiledStyle = { - $$css: true, - $$css$localize: true, - marginStart: ['marginLeft-10', 'marginRight-10'] - }; - expect(StyleSheet([compiledStyle])[0]).toBe('marginLeft-10'); - expect(StyleSheet([compiledStyle], { writingDirection: 'rtl' })[0]).toBe( - 'marginRight-10' - ); - }); - test('dedupes class names and inline styles', () => { const styleACompiled = { $$css: true, @@ -335,7 +323,7 @@ describe('StyleSheet', () => { ).toBeNull(); }); - test('polyfills logical styles', () => { + test('keeps logical styles', () => { const inlineA = { start: '12.34%' }; const inlineB = { textAlign: 'start' }; const inlineC = { marginEnd: 10 }; @@ -343,127 +331,50 @@ describe('StyleSheet', () => { const a = StyleSheet.create({ x: { ...inlineA } }).x; const b = StyleSheet.create({ x: { ...inlineB } }).x; const c = StyleSheet.create({ x: { ...inlineC } }).x; - const writingDirection = 'rtl'; // inline styles const inlineStyle = [inlineA, inlineB, inlineC]; expect(StyleSheet(inlineStyle)).toMatchInlineSnapshot(` - [ - "", - { - "left": "12.34%", - "marginRight": "10px", - "textAlign": "left", - }, - "", - ] + [ + "", + { + "insetInlineStart": "12.34%", + "marginInlineEnd": "10px", + "textAlign": "start", + }, + "", + ] `); - expect(StyleSheet(inlineStyle, { writingDirection })) + expect(StyleSheet([inlineStyle, { marginEnd: null, marginLeft: 11 }])) .toMatchInlineSnapshot(` - [ - "", - { - "marginLeft": "10px", - "right": "12.34%", - "textAlign": "right", - }, - "", - ] - `); - expect( - StyleSheet( - [ - inlineStyle, - { marginLeft: 1, marginEnd: 0, marginStart: 0, marginRight: 11 } - ], - - { writingDirection } - ) - ).toMatchInlineSnapshot(` - [ - "", - { - "marginLeft": "1px", - "marginRight": "11px", - "right": "12.34%", - "textAlign": "right", - }, - "", - ] - `); - expect( - StyleSheet([inlineStyle, { marginEnd: null, marginLeft: 11 }], { - writingDirection - }) - ).toMatchInlineSnapshot(` - [ - "", - { - "marginLeft": "11px", - "right": "12.34%", - "textAlign": "right", - }, - "", - ] + [ + "", + { + "insetInlineStart": "12.34%", + "marginLeft": "11px", + "textAlign": "start", + }, + "", + ] `); // static styles const staticStyle = [a, b, c]; expect(StyleSheet(staticStyle)).toMatchInlineSnapshot(` - [ - "r-insetInlineStart-1xn1m1p r-textAlign-fdjqy7 r-marginInlineEnd-1l8l4mf", - null, - "", - ] - `); - expect(StyleSheet(staticStyle, { writingDirection })) - .toMatchInlineSnapshot(` - [ - "r-insetInlineStart-1y2vi53 r-textAlign-1ff274t r-marginInlineEnd-t1sew1", - null, - "", - ] - `); - const z = StyleSheet.create({ x: { marginRight: 33 } }).x; - expect(StyleSheet([staticStyle, z])).toMatchInlineSnapshot(` - [ - "r-insetInlineStart-1xn1m1p r-textAlign-fdjqy7 r-marginInlineEnd-1l8l4mf r-marginRight-j4vy6k", - null, - "", - ] - `); - expect( - StyleSheet( - [ - staticStyle, - { marginLeft: 1, marginEnd: 0, marginStart: 0, marginRight: 11 } - ], - - { - writingDirection - } - ) - ).toMatchInlineSnapshot(` - [ - "r-insetInlineStart-1y2vi53 r-textAlign-1ff274t", - { - "marginLeft": "1px", - "marginRight": "11px", - }, - "", - ] + [ + "r-insetInlineStart-1a9qabf r-textAlign-14537ki r-marginInlineEnd-j2y3bo", + null, + "", + ] `); // logical can be nulled - expect( - StyleSheet([staticStyle, { marginEnd: null }], { - writingDirection - }) - ).toMatchInlineSnapshot(` - [ - "r-insetInlineStart-1y2vi53 r-textAlign-1ff274t", - null, - "", - ] + expect(StyleSheet([staticStyle, { marginEnd: null }])) + .toMatchInlineSnapshot(` + [ + "r-insetInlineStart-1a9qabf r-textAlign-14537ki", + null, + "", + ] `); }); }); diff --git a/packages/react-native-web/src/exports/StyleSheet/__tests__/preprocess-test.ts b/packages/react-native-web/src/exports/StyleSheet/__tests__/preprocess-test.ts index 0bb6ab34fa..0d7bb4499e 100644 --- a/packages/react-native-web/src/exports/StyleSheet/__tests__/preprocess-test.ts +++ b/packages/react-native-web/src/exports/StyleSheet/__tests__/preprocess-test.ts @@ -14,24 +14,16 @@ describe('StyleSheet/preprocess', () => { preprocess({ end: 1, marginEnd: 1, - marginHorizontal: 1, marginStart: 1, - marginVertical: 1, paddingEnd: 1, - paddingHorizontal: 1, paddingStart: 1, - paddingVertical: 1, start: 1 }) ).toEqual({ insetInlineEnd: 1, insetInlineStart: 1, - marginBlock: 1, - marginInline: 1, marginInlineEnd: 1, marginInlineStart: 1, - paddingBlock: 1, - paddingInline: 1, paddingInlineEnd: 1, paddingInlineStart: 1 }); @@ -42,13 +34,9 @@ describe('StyleSheet/preprocess', () => { preprocess({ end: 1, marginEnd: 1, - marginHorizontal: 1, marginStart: 1, - marginVertical: 1, paddingEnd: 1, - paddingHorizontal: 1, paddingStart: 1, - paddingVertical: 1, start: 1, // standard insetInlineEnd: 2, @@ -350,22 +338,36 @@ describe('StyleSheet/preprocess', () => { transitionProperty: 'opacity, transform' }); }); + + test('logical properties', () => { + expect( + preprocess({ + transitionProperty: 'marginStart, paddingInlineEnd,opacity' + }) + ).toEqual({ + transitionProperty: 'margin-inline-start, padding-inline-end,opacity' + }); + + expect( + preprocess({ + transitionProperty: ['borderTopStartRadius', 'inset-inline-start'] + }) + ).toEqual({ + transitionProperty: 'border-start-start-radius, inset-inline-start' + }); + }); }); }); describe('preprocesses multiple shadow styles into a single declaration', () => { - const options = { shadow: true }; - test('shadowColor only', () => { - expect(preprocess({ shadowColor: 'red' }, options)).toEqual({ + expect(preprocess({ shadowColor: 'red' })).toEqual({ boxShadow: '0px 0px 0px rgba(255,0,0,1.00)' }); }); test('shadowColor and shadowOpacity only', () => { - expect( - preprocess({ shadowColor: 'red', shadowOpacity: 0.5 }, options) - ).toEqual({ + expect(preprocess({ shadowColor: 'red', shadowOpacity: 0.5 })).toEqual({ boxShadow: '0px 0px 0px rgba(255,0,0,0.50)' }); }); @@ -373,34 +375,28 @@ describe('StyleSheet/preprocess', () => { test('shadowColor system color', () => { const spy = vi.spyOn(console, 'error').mockImplementation(() => {}); - expect(preprocess({ shadowColor: 'CanvasText' }, options)).toEqual({ + expect(preprocess({ shadowColor: 'CanvasText' })).toEqual({ boxShadow: '0px 0px 0px CanvasText' }); - expect(preprocess({ shadowColor: 'Mark' }, options)).toEqual({ + expect(preprocess({ shadowColor: 'Mark' })).toEqual({ boxShadow: '0px 0px 0px var(--rnw-mark)' }); // Opacity is ignored for system and web colors expect( - preprocess({ shadowColor: 'CanvasText', shadowOpacity: 0.5 }, options) + preprocess({ shadowColor: 'CanvasText', shadowOpacity: 0.5 }) ).toEqual({ boxShadow: '0px 0px 0px CanvasText' }); expect( - preprocess( - { shadowColor: 'var(--rnw-mark, red)', shadowOpacity: 0.5 }, - options - ) + preprocess({ shadowColor: 'var(--rnw-mark, red)', shadowOpacity: 0.5 }) ).toEqual({ boxShadow: '0px 0px 0px var(--rnw-mark, red)' }); // No warning for other web colors expect( - preprocess( - { shadowColor: 'var(--shadow)', shadowOpacity: 0.5 }, - options - ) + preprocess({ shadowColor: 'var(--shadow)', shadowOpacity: 0.5 }) ).toEqual({ boxShadow: '0px 0px 0px var(--shadow)' }); @@ -409,30 +405,25 @@ describe('StyleSheet/preprocess', () => { }); test('shadowOffset only', () => { - expect( - preprocess({ shadowOffset: { width: 1, height: 2 } }, options) - ).toEqual({ + expect(preprocess({ shadowOffset: { width: 1, height: 2 } })).toEqual({ boxShadow: '1px 2px 0px rgba(0,0,0,1.00)' }); }); test('shadowRadius only', () => { - expect(preprocess({ shadowRadius: 5 }, options)).toEqual({ + expect(preprocess({ shadowRadius: 5 })).toEqual({ boxShadow: '0px 0px 5px rgba(0,0,0,1.00)' }); }); test('shadowOffset, shadowRadius, shadowColor', () => { expect( - preprocess( - { - shadowColor: 'rgba(50,60,70,0.5)', - shadowOffset: { width: 1, height: 2 }, - shadowOpacity: 0.5, - shadowRadius: 3 - }, - options - ) + preprocess({ + shadowColor: 'rgba(50,60,70,0.5)', + shadowOffset: { width: 1, height: 2 }, + shadowOpacity: 0.5, + shadowRadius: 3 + }) ).toEqual({ boxShadow: '1px 2px 3px rgba(50,60,70,0.25)' }); @@ -440,16 +431,13 @@ describe('StyleSheet/preprocess', () => { test('shadow styles and string boxShadow together', () => { expect( - preprocess( - { - shadowColor: 'rgba(50,60,70,0.5)', - shadowOffset: { width: 1, height: 2 }, - shadowOpacity: 0.5, - shadowRadius: 3, - boxShadow: '4px 5px 6px black' - }, - options - ) + preprocess({ + shadowColor: 'rgba(50,60,70,0.5)', + shadowOffset: { width: 1, height: 2 }, + shadowOpacity: 0.5, + shadowRadius: 3, + boxShadow: '4px 5px 6px black' + }) ).toEqual({ boxShadow: '4px 5px 6px black, 1px 2px 3px rgba(50,60,70,0.25)' }); @@ -457,24 +445,21 @@ describe('StyleSheet/preprocess', () => { test('shadow styles and array boxShadow together', () => { expect( - preprocess( - { - shadowColor: 'rgba(50,60,70,0.5)', - shadowOffset: { width: 1, height: 2 }, - shadowOpacity: 0.5, - shadowRadius: 3, - boxShadow: [ - { - offsetX: 2, - offsetY: 3, - spreadDistance: 5, - color: 'rgba(10,20,30,0.45)', - inset: true - } - ] - }, - options - ) + preprocess({ + shadowColor: 'rgba(50,60,70,0.5)', + shadowOffset: { width: 1, height: 2 }, + shadowOpacity: 0.5, + shadowRadius: 3, + boxShadow: [ + { + offsetX: 2, + offsetY: 3, + spreadDistance: 5, + color: 'rgba(10,20,30,0.45)', + inset: true + } + ] + }) ).toEqual({ boxShadow: 'inset 2px 3px 0 5px rgba(10,20,30,0.45), 1px 2px 3px rgba(50,60,70,0.25)' @@ -483,51 +468,40 @@ describe('StyleSheet/preprocess', () => { }); describe('preprocesses multiple textShadow styles into a single declaration', () => { - const options = { textShadow: true }; - test('textShadowColor only', () => { - expect(preprocess({ textShadowColor: 'red' }, options)).toEqual({}); + expect(preprocess({ textShadowColor: 'red' })).toEqual({}); }); test('textShadowOffset only', () => { - expect( - preprocess({ textShadowOffset: { width: 1, height: 2 } }, options) - ).toEqual({}); + expect(preprocess({ textShadowOffset: { width: 1, height: 2 } })).toEqual( + {} + ); }); test('textShadowRadius only', () => { - expect(preprocess({ textShadowRadius: 5 }, options)).toEqual({}); + expect(preprocess({ textShadowRadius: 5 })).toEqual({}); }); test('textShadowColor and textShadowOffset only', () => { expect( - preprocess( - { - textShadowColor: 'red', - textShadowOffset: { width: 0, height: 0 } - }, - options - ) + preprocess({ + textShadowColor: 'red', + textShadowOffset: { width: 0, height: 0 } + }) ).toEqual({}); expect( - preprocess( - { - textShadowColor: 'red', - textShadowOffset: { width: -1, height: 0 } - }, - options - ) + preprocess({ + textShadowColor: 'red', + textShadowOffset: { width: -1, height: 0 } + }) ).toEqual({ textShadow: '-1px 0px 0px rgba(255,0,0,1.00)' }); expect( - preprocess( - { - textShadowColor: 'red', - textShadowOffset: { width: 1, height: 2 } - }, - options - ) + preprocess({ + textShadowColor: 'red', + textShadowOffset: { width: 1, height: 2 } + }) ).toEqual({ textShadow: '1px 2px 0px rgba(255,0,0,1.00)' }); @@ -535,10 +509,10 @@ describe('StyleSheet/preprocess', () => { test('textShadowColor and textShadowRadius only', () => { expect( - preprocess({ textShadowColor: 'red', textShadowRadius: 0 }, options) + preprocess({ textShadowColor: 'red', textShadowRadius: 0 }) ).toEqual({}); expect( - preprocess({ textShadowColor: 'red', textShadowRadius: 5 }, options) + preprocess({ textShadowColor: 'red', textShadowRadius: 5 }) ).toEqual({ textShadow: '0px 0px 5px rgba(255,0,0,1.00)' }); @@ -546,42 +520,14 @@ describe('StyleSheet/preprocess', () => { test('textShadowColor, textShadowOffset, textShadowRadius', () => { expect( - preprocess( - { - textShadowColor: 'rgba(50,60,70,0.50)', - textShadowOffset: { width: 5, height: 10 }, - textShadowRadius: 15 - }, - options - ) + preprocess({ + textShadowColor: 'rgba(50,60,70,0.50)', + textShadowOffset: { width: 5, height: 10 }, + textShadowRadius: 15 + }) ).toEqual({ textShadow: '5px 10px 15px rgba(50,60,70,0.50)' }); }); }); - - describe('shadow conversion options', () => { - test('shadow styles are not converted when options.shadow is not true', () => { - expect(preprocess({ shadowColor: 'red' })).toEqual({}); - expect(preprocess({ shadowColor: 'red' }, { shadow: false })).toEqual({}); - }); - - test('textShadow styles are not converted when options.textShadow is not true', () => { - expect( - preprocess({ - textShadowColor: 'red', - textShadowOffset: { width: 1, height: 2 } - }) - ).toEqual({}); - expect( - preprocess( - { - textShadowColor: 'red', - textShadowOffset: { width: 1, height: 2 } - }, - { textShadow: false } - ) - ).toEqual({}); - }); - }); }); diff --git a/packages/react-native-web/src/exports/StyleSheet/__tests__/validate-test.ts b/packages/react-native-web/src/exports/StyleSheet/__tests__/validate-test.ts index 4c1ed8df08..b41c9b5363 100644 --- a/packages/react-native-web/src/exports/StyleSheet/__tests__/validate-test.ts +++ b/packages/react-native-web/src/exports/StyleSheet/__tests__/validate-test.ts @@ -31,6 +31,7 @@ describe('validate', () => { validate({ flex: '1 1 25%' }); validate({ margin: '10px 20px' }); validate({ margin: 'calc(10 * 1px) var(--test)' }); - expect(console.error).toHaveBeenCalledTimes(3); + validate({ gap: '10px 20px' }); + expect(console.error).toHaveBeenCalledTimes(4); }); }); diff --git a/packages/react-native-web/src/exports/StyleSheet/compiler/createReactDOMStyle.ts b/packages/react-native-web/src/exports/StyleSheet/compiler/createReactDOMStyle.ts index db96ef40e3..4c316a1f09 100644 --- a/packages/react-native-web/src/exports/StyleSheet/compiler/createReactDOMStyle.ts +++ b/packages/react-native-web/src/exports/StyleSheet/compiler/createReactDOMStyle.ts @@ -13,12 +13,12 @@ type Style = Record; /** * The browser implements the CSS cascade, where the order of properties is a * factor in determining which styles to paint. React Native is different. It - * gives giving precedence to the more specific style property. For example, - * the value of `paddingTop` takes precedence over that of `padding`. + * gives precedence to the more specific style property. For example, the value + * of `paddingTop` takes precedence over that of `padding`. * - * This module creates mutally exclusive style declarations by expanding all of + * This module creates mutually exclusive inline style declarations by expanding * React Native's supported shortform properties (e.g. `padding`) to their - * longfrom equivalents. + * longform equivalents. Static styles rely on the rules order instead. */ const emptyObject = {}; @@ -28,15 +28,19 @@ const MONOSPACE_FONT_STACK = 'monospace,monospace'; const SYSTEM_FONT_STACK = '-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif'; -const STYLE_SHORT_FORM_EXPANSIONS: Record> = { +const SHORT_FORM_EXPANSIONS: Record> = { + borderBlockColor: ['borderBlockStartColor', 'borderBlockEndColor'], + borderBlockStyle: ['borderBlockStartStyle', 'borderBlockEndStyle'], + borderBlockWidth: ['borderBlockStartWidth', 'borderBlockEndWidth'], borderColor: [ 'borderTopColor', 'borderRightColor', 'borderBottomColor', 'borderLeftColor' ], - borderBlockColor: ['borderTopColor', 'borderBottomColor'], - borderInlineColor: ['borderRightColor', 'borderLeftColor'], + borderInlineColor: ['borderInlineStartColor', 'borderInlineEndColor'], + borderInlineStyle: ['borderInlineStartStyle', 'borderInlineEndStyle'], + borderInlineWidth: ['borderInlineStartWidth', 'borderInlineEndWidth'], borderRadius: [ 'borderTopLeftRadius', 'borderTopRightRadius', @@ -49,54 +53,136 @@ const STYLE_SHORT_FORM_EXPANSIONS: Record> = { 'borderBottomStyle', 'borderLeftStyle' ], - borderBlockStyle: ['borderTopStyle', 'borderBottomStyle'], - borderInlineStyle: ['borderRightStyle', 'borderLeftStyle'], borderWidth: [ 'borderTopWidth', 'borderRightWidth', 'borderBottomWidth', 'borderLeftWidth' ], - borderBlockWidth: ['borderTopWidth', 'borderBottomWidth'], - borderInlineWidth: ['borderRightWidth', 'borderLeftWidth'], - insetBlock: ['top', 'bottom'], - insetInline: ['left', 'right'], - marginBlock: ['marginTop', 'marginBottom'], - marginInline: ['marginRight', 'marginLeft'], - paddingBlock: ['paddingTop', 'paddingBottom'], - paddingInline: ['paddingRight', 'paddingLeft'], + gap: ['rowGap', 'columnGap'], + inset: ['top', 'right', 'bottom', 'left'], + insetBlock: ['insetBlockStart', 'insetBlockEnd'], + insetInline: ['insetInlineStart', 'insetInlineEnd'], + margin: ['marginTop', 'marginRight', 'marginBottom', 'marginLeft'], + marginBlock: ['marginBlockStart', 'marginBlockEnd'], + marginHorizontal: ['marginLeft', 'marginRight'], + marginInline: ['marginInlineStart', 'marginInlineEnd'], + marginVertical: ['marginTop', 'marginBottom'], overflow: ['overflowX', 'overflowY'], overscrollBehavior: ['overscrollBehaviorX', 'overscrollBehaviorY'], - borderBlockStartColor: ['borderTopColor'], - borderBlockStartStyle: ['borderTopStyle'], - borderBlockStartWidth: ['borderTopWidth'], - borderBlockEndColor: ['borderBottomColor'], - borderBlockEndStyle: ['borderBottomStyle'], - borderBlockEndWidth: ['borderBottomWidth'], - //borderInlineStartColor: ['borderLeftColor'], - //borderInlineStartStyle: ['borderLeftStyle'], - //borderInlineStartWidth: ['borderLeftWidth'], - //borderInlineEndColor: ['borderRightColor'], - //borderInlineEndStyle: ['borderRightStyle'], - //borderInlineEndWidth: ['borderRightWidth'], - borderEndStartRadius: ['borderBottomLeftRadius'], - borderEndEndRadius: ['borderBottomRightRadius'], - borderStartStartRadius: ['borderTopLeftRadius'], - borderStartEndRadius: ['borderTopRightRadius'], - insetBlockEnd: ['bottom'], - insetBlockStart: ['top'], - //insetInlineEnd: ['right'], - //insetInlineStart: ['left'], - marginBlockStart: ['marginTop'], - marginBlockEnd: ['marginBottom'], - //marginInlineStart: ['marginLeft'], - //marginInlineEnd: ['marginRight'], - paddingBlockStart: ['paddingTop'], - paddingBlockEnd: ['paddingBottom'] - //paddingInlineStart: ['marginLeft'], - //paddingInlineEnd: ['marginRight'], + padding: ['paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft'], + paddingBlock: ['paddingBlockStart', 'paddingBlockEnd'], + paddingHorizontal: ['paddingLeft', 'paddingRight'], + paddingInline: ['paddingInlineStart', 'paddingInlineEnd'], + paddingVertical: ['paddingTop', 'paddingBottom'] }; +// Static styles expand these shortforms too +const STATIC_SHORT_FORMS = new Set([ + 'borderColor', + 'borderRadius', + 'borderStyle', + 'borderWidth', + 'marginHorizontal', + 'marginVertical', + 'overflow', + 'overscrollBehavior', + 'paddingHorizontal', + 'paddingVertical' +]); + +const PHYSICAL_TO_LOGICAL: Array<[physical: string[], logical: string[]]> = [ + [ + ['top', 'bottom'], + ['insetBlockStart', 'insetBlockEnd'] + ], + [ + ['left', 'right'], + ['insetInlineStart', 'insetInlineEnd'] + ], + [ + ['marginTop', 'marginBottom'], + ['marginBlockStart', 'marginBlockEnd'] + ], + [ + ['marginLeft', 'marginRight'], + ['marginInlineStart', 'marginInlineEnd'] + ], + [ + ['paddingTop', 'paddingBottom'], + ['paddingBlockStart', 'paddingBlockEnd'] + ], + [ + ['paddingLeft', 'paddingRight'], + ['paddingInlineStart', 'paddingInlineEnd'] + ], + [ + ['borderTopColor', 'borderBottomColor'], + ['borderBlockStartColor', 'borderBlockEndColor'] + ], + [ + ['borderLeftColor', 'borderRightColor'], + ['borderInlineStartColor', 'borderInlineEndColor'] + ], + [ + ['borderTopStyle', 'borderBottomStyle'], + ['borderBlockStartStyle', 'borderBlockEndStyle'] + ], + [ + ['borderLeftStyle', 'borderRightStyle'], + ['borderInlineStartStyle', 'borderInlineEndStyle'] + ], + [ + ['borderTopWidth', 'borderBottomWidth'], + ['borderBlockStartWidth', 'borderBlockEndWidth'] + ], + [ + ['borderLeftWidth', 'borderRightWidth'], + ['borderInlineStartWidth', 'borderInlineEndWidth'] + ], + [ + [ + 'borderTopLeftRadius', + 'borderTopRightRadius', + 'borderBottomRightRadius', + 'borderBottomLeftRadius' + ], + [ + 'borderStartStartRadius', + 'borderStartEndRadius', + 'borderEndEndRadius', + 'borderEndStartRadius' + ] + ] +]; + +const LOGICAL_LONGHANDS: Record = {}; +const PHYSICAL_LONGHANDS: Record = {}; + +for (const [physical, logical] of PHYSICAL_TO_LOGICAL) { + for (const [index, prop] of physical.entries()) { + LOGICAL_LONGHANDS[prop] = { prop: logical[index], group: logical }; + PHYSICAL_LONGHANDS[logical[index]] = prop; + } +} + +const AXIS_SHORT_FORM_NAMES = new Set(); + +// Physical longform to its axis shortforms, +// e.g. 'marginTop' => ['marginBlock', 'marginVertical'] +const AXIS_SHORT_FORMS: Record = {}; + +for (const [shortForm, longForms] of Object.entries(SHORT_FORM_EXPANSIONS)) { + if (/Block|Inline|Horizontal|Vertical/.test(shortForm)) { + AXIS_SHORT_FORM_NAMES.add(shortForm); + + for (const longForm of longForms) { + const physical = PHYSICAL_LONGHANDS[longForm] ?? longForm; + (AXIS_SHORT_FORMS[physical] ??= []).push(shortForm); + } + } +} + const PREFIXED_PROPERTIES = Object.fromEntries( [ 'appearance', @@ -222,6 +308,30 @@ const createReactDOMStyle = ( const resolvedStyle: Style = {}; + // The value of any longform property in the original styles takes + // precedence over the shortform's value. + const setLongForm = (shortForm: string, longForm: string, value: unknown) => { + let prop = longForm; + + // Axis shortforms (e.g. 'marginInline') only expand to their own longforms + if (!AXIS_SHORT_FORM_NAMES.has(shortForm)) { + // An axis shortform set in the original styles takes precedence + if (AXIS_SHORT_FORMS[longForm]?.some((p) => style[p] != null)) { + return; + } + + const logical = LOGICAL_LONGHANDS[longForm]; + + if (logical?.group.some((p) => style[p] != null)) { + prop = logical.prop; + } + } + + if (style[prop] == null) { + resolvedStyle[prop] = value; + } + }; + for (const prop in style) { const value = style[prop]; @@ -264,42 +374,14 @@ const createReactDOMStyle = ( resolvedStyle.direction = value; } else { const value = normalizeValueWithProperty(style[prop], prop); - const longFormProperties = STYLE_SHORT_FORM_EXPANSIONS[prop]; - if (isInline && prop === 'inset') { - if (style.insetInline == null) { - resolvedStyle.left = value; - resolvedStyle.right = value; - } - if (style.insetBlock == null) { - resolvedStyle.top = value; - resolvedStyle.bottom = value; - } - } else if (isInline && prop === 'margin') { - if (style.marginInline == null) { - resolvedStyle.marginLeft = value; - resolvedStyle.marginRight = value; - } - if (style.marginBlock == null) { - resolvedStyle.marginTop = value; - resolvedStyle.marginBottom = value; - } - } else if (isInline && prop === 'padding') { - if (style.paddingInline == null) { - resolvedStyle.paddingLeft = value; - resolvedStyle.paddingRight = value; - } - if (style.paddingBlock == null) { - resolvedStyle.paddingTop = value; - resolvedStyle.paddingBottom = value; + const longFormProperties = + isInline || STATIC_SHORT_FORMS.has(prop) + ? SHORT_FORM_EXPANSIONS[prop] + : undefined; + if (longFormProperties != null) { + for (const longForm of longFormProperties) { + setLongForm(prop, longForm, value); } - } else if (longFormProperties) { - longFormProperties.forEach((longForm) => { - // The value of any longform property in the original styles takes - // precedence over the shortform's value. - if (style[longForm] == null) { - resolvedStyle[longForm] = value; - } - }); } else if (isInline) { resolvedStyle[prop] = value; } else { diff --git a/packages/react-native-web/src/exports/StyleSheet/compiler/index.ts b/packages/react-native-web/src/exports/StyleSheet/compiler/index.ts index 86742e7b1d..8bb98f2798 100644 --- a/packages/react-native-web/src/exports/StyleSheet/compiler/index.ts +++ b/packages/react-native-web/src/exports/StyleSheet/compiler/index.ts @@ -6,7 +6,6 @@ */ import type { Nullable } from '../../../types'; -import { PROPERTIES_STANDARD } from '../preprocess'; import createReactDOMStyle from './createReactDOMStyle'; import hash from './hash'; import hyphenateStyleName from './hyphenateStyleName'; @@ -23,15 +22,15 @@ type Rules = Array; type RulesData = [Rules, number]; type CompiledStyle = { $$css: true; - [key: string]: string | Array | boolean; + [key: string]: string | boolean; }; type CompilerOutput = [CompiledStyle, Array]; const cache = new Map(); -const emptyObject = {}; const classicGroup = 1; const atomicGroup = 3; + const customGroup: Record = { borderColor: 2, borderRadius: 2, @@ -39,152 +38,66 @@ const customGroup: Record = { borderWidth: 2, display: 2, flex: 2, + gap: 2, inset: 2, margin: 2, overflow: 2, overscrollBehavior: 2, padding: 2, + + borderBlockColor: 2.1, + borderBlockStyle: 2.1, + borderBlockWidth: 2.1, + borderInlineColor: 2.1, + borderInlineStyle: 2.1, + borderInlineWidth: 2.1, insetBlock: 2.1, insetInline: 2.1, - marginInline: 2.1, marginBlock: 2.1, - paddingInline: 2.1, + marginInline: 2.1, paddingBlock: 2.1, - borderBlockStartColor: 2.2, - borderBlockStartStyle: 2.2, - borderBlockStartWidth: 2.2, + paddingInline: 2.1, + borderBlockEndColor: 2.2, borderBlockEndStyle: 2.2, borderBlockEndWidth: 2.2, - borderInlineStartColor: 2.2, - borderInlineStartStyle: 2.2, - borderInlineStartWidth: 2.2, + borderBlockStartColor: 2.2, + borderBlockStartStyle: 2.2, + borderBlockStartWidth: 2.2, + borderEndEndRadius: 2.2, + borderEndStartRadius: 2.2, borderInlineEndColor: 2.2, borderInlineEndStyle: 2.2, borderInlineEndWidth: 2.2, - borderEndStartRadius: 2.2, - borderEndEndRadius: 2.2, - borderStartStartRadius: 2.2, + borderInlineStartColor: 2.2, + borderInlineStartStyle: 2.2, + borderInlineStartWidth: 2.2, borderStartEndRadius: 2.2, + borderStartStartRadius: 2.2, insetBlockEnd: 2.2, insetBlockStart: 2.2, insetInlineEnd: 2.2, insetInlineStart: 2.2, - marginBlockStart: 2.2, marginBlockEnd: 2.2, - marginInlineStart: 2.2, + marginBlockStart: 2.2, marginInlineEnd: 2.2, - paddingBlockStart: 2.2, + marginInlineStart: 2.2, paddingBlockEnd: 2.2, + paddingBlockStart: 2.2, + paddingInlineEnd: 2.2, paddingInlineStart: 2.2, - paddingInlineEnd: 2.2 -}; -const borderTopLeftRadius = 'borderTopLeftRadius'; -const borderTopRightRadius = 'borderTopRightRadius'; -const borderBottomLeftRadius = 'borderBottomLeftRadius'; -const borderBottomRightRadius = 'borderBottomRightRadius'; -const borderLeftColor = 'borderLeftColor'; -const borderLeftStyle = 'borderLeftStyle'; -const borderLeftWidth = 'borderLeftWidth'; -const borderRightColor = 'borderRightColor'; -const borderRightStyle = 'borderRightStyle'; -const borderRightWidth = 'borderRightWidth'; -const right = 'right'; -const marginLeft = 'marginLeft'; -const marginRight = 'marginRight'; -const paddingLeft = 'paddingLeft'; -const paddingRight = 'paddingRight'; -const left = 'left'; - -// Map of LTR property names to their BiDi equivalent. -const PROPERTIES_FLIP: Record = { - [borderTopLeftRadius]: borderTopRightRadius, - [borderTopRightRadius]: borderTopLeftRadius, - [borderBottomLeftRadius]: borderBottomRightRadius, - [borderBottomRightRadius]: borderBottomLeftRadius, - [borderLeftColor]: borderRightColor, - [borderLeftStyle]: borderRightStyle, - [borderLeftWidth]: borderRightWidth, - [borderRightColor]: borderLeftColor, - [borderRightStyle]: borderLeftStyle, - [borderRightWidth]: borderLeftWidth, - [left]: right, - [marginLeft]: marginRight, - [marginRight]: marginLeft, - [paddingLeft]: paddingRight, - [paddingRight]: paddingLeft, - [right]: left + marginHorizontal: 2.3, + marginVertical: 2.3, + paddingHorizontal: 2.3, + paddingVertical: 2.3 }; -// Map of I18N property names to their LTR equivalent. -const PROPERTIES_I18N: Record = { - borderStartStartRadius: borderTopLeftRadius, - borderStartEndRadius: borderTopRightRadius, - borderEndStartRadius: borderBottomLeftRadius, - borderEndEndRadius: borderBottomRightRadius, - borderInlineStartColor: borderLeftColor, - borderInlineStartStyle: borderLeftStyle, - borderInlineStartWidth: borderLeftWidth, - borderInlineEndColor: borderRightColor, - borderInlineEndStyle: borderRightStyle, - borderInlineEndWidth: borderRightWidth, - insetInlineEnd: right, - insetInlineStart: left, - marginInlineStart: marginLeft, - marginInlineEnd: marginRight, - paddingInlineStart: paddingLeft, - paddingInlineEnd: paddingRight -}; - -const PROPERTIES_VALUE = new Set(['clear', 'float', 'textAlign']); - -// Maps of I18N property names and React Native aliases to their hyphenated -// LTR and RTL equivalent, for transitionProperty values. -const TRANSITION_PROPERTIES_LTR: Record = {}; -const TRANSITION_PROPERTIES_RTL: Record = {}; - -for (const prop of [ - ...Object.keys(PROPERTIES_I18N), - ...Object.keys(PROPERTIES_STANDARD) -]) { - const ltrProp = PROPERTIES_I18N[PROPERTIES_STANDARD[prop] ?? prop]; - - if (ltrProp != null) { - const ltrName = hyphenateStyleName(ltrProp); - const rtlName = hyphenateStyleName(PROPERTIES_FLIP[ltrProp]); - - [prop, hyphenateStyleName(prop)].forEach((name) => { - TRANSITION_PROPERTIES_LTR[name] = ltrName; - TRANSITION_PROPERTIES_RTL[name] = rtlName; - }); - } -} - -function localizeTransitionProperty( - value: StyleValue, - isRTL?: boolean -): string { - const map = isRTL ? TRANSITION_PROPERTIES_RTL : TRANSITION_PROPERTIES_LTR; - - return (Array.isArray(value) ? value.join(', ') : String(value)) - .split(',') - .map((name) => { - const trimmedName = name.trim(); - return map[trimmedName] ?? trimmedName; - }) - .join(', '); -} - export function atomic(style: Style): CompilerOutput { const compiledStyle: CompiledStyle = { $$css: true }; const compiledRules: Array = []; - function atomicCompile( - srcProp: string, - prop: string, - value: StyleValue - ): string { + function atomicCompile(prop: string, value: StyleValue): string { const valueString = stringifyValueWithProperty(value, prop); const cacheKey = prop + valueString; const cachedResult = cache.get(cacheKey); @@ -193,9 +106,8 @@ export function atomic(style: Style): CompilerOutput { identifier = cachedResult[0]; compiledRules.push(cachedResult[1]); } else { - const v = srcProp !== prop ? cacheKey : valueString; - identifier = createIdentifier('r', srcProp, v); - const order = customGroup[srcProp] || atomicGroup; + identifier = createIdentifier('r', prop, valueString); + const order = customGroup[prop] || atomicGroup; const rules = createAtomicRules(identifier, prop, value); const orderedRules: RulesData = [rules, order]; compiledRules.push(orderedRules); @@ -209,46 +121,7 @@ export function atomic(style: Style): CompilerOutput { .forEach((srcProp) => { const value = style[srcProp]; if (value != null) { - let localizeableValue: string | Array | undefined; - // BiDi flip values - if (PROPERTIES_VALUE.has(srcProp)) { - const left = atomicCompile(srcProp, srcProp, 'left'); - const right = atomicCompile(srcProp, srcProp, 'right'); - if (value === 'start') { - localizeableValue = [left, right]; - } else if (value === 'end') { - localizeableValue = [right, left]; - } - } - // BiDi flip properties - const propPolyfill = PROPERTIES_I18N[srcProp]; - if (propPolyfill != null) { - const ltr = atomicCompile(srcProp, propPolyfill, value); - const rtl = atomicCompile( - srcProp, - PROPERTIES_FLIP[propPolyfill], - value - ); - localizeableValue = [ltr, rtl]; - } - // BiDi flip transitionProperty value - if (srcProp === 'transitionProperty') { - const ltrValue = localizeTransitionProperty(value, false); - const rtlValue = localizeTransitionProperty(value, true); - if (ltrValue !== rtlValue) { - const ltr = atomicCompile(srcProp, srcProp, ltrValue); - const rtl = atomicCompile(srcProp, srcProp, rtlValue); - localizeableValue = [ltr, rtl]; - } - } - - if (localizeableValue == null) { - localizeableValue = atomicCompile(srcProp, srcProp, value); - } else { - compiledStyle['$$css$localize'] = true; - } - - compiledStyle[srcProp] = localizeableValue; + compiledStyle[srcProp] = atomicCompile(srcProp, value); } }); @@ -281,61 +154,6 @@ export function classic(style: Style, name: string): CompilerOutput { return [compiledStyle, [[compiledRules, classicGroup]]]; } -/** - * Compile simple style object to inline DOM styles. - * No support for 'animationKeyframes', 'placeholderTextColor', 'scrollbarWidth', or 'pointerEvents'. - */ -export function inline( - originalStyle: Readonly>, - isRTL?: boolean -): Record { - const style = originalStyle || emptyObject; - const frozenProps: Record = {}; - const nextStyle: Record = {}; - - for (const originalProp in style) { - const originalValue = style[originalProp]; - let prop = originalProp; - let value = originalValue; - - if ( - !Object.prototype.hasOwnProperty.call(style, originalProp) || - originalValue == null - ) { - continue; - } - - // BiDi flip values - if (PROPERTIES_VALUE.has(originalProp)) { - if (originalValue === 'start') { - value = isRTL ? 'right' : 'left'; - } else if (originalValue === 'end') { - value = isRTL ? 'left' : 'right'; - } - } - // BiDi flip properties - const propPolyfill = PROPERTIES_I18N[originalProp]; - if (propPolyfill != null) { - prop = isRTL ? PROPERTIES_FLIP[propPolyfill] : propPolyfill; - } - // BiDi flip transitionProperty value - if (originalProp === 'transitionProperty') { - value = localizeTransitionProperty(originalValue as StyleValue, isRTL); - } - - // Create finalized style - if (!frozenProps[prop]) { - nextStyle[prop] = value; - } - - if (prop === originalProp) { - frozenProps[prop] = true; - } - } - - return createReactDOMStyle(nextStyle, true); -} - /** * Create a value string that normalizes different input values with a common * output. diff --git a/packages/react-native-web/src/exports/StyleSheet/compiler/normalizeValueWithProperty.ts b/packages/react-native-web/src/exports/StyleSheet/compiler/normalizeValueWithProperty.ts index 3fb855dbd4..2e42eede47 100644 --- a/packages/react-native-web/src/exports/StyleSheet/compiler/normalizeValueWithProperty.ts +++ b/packages/react-native-web/src/exports/StyleSheet/compiler/normalizeValueWithProperty.ts @@ -17,8 +17,14 @@ export type StyleValue = const colorProps = new Set([ 'backgroundColor', + 'borderBlockColor', + 'borderBlockEndColor', + 'borderBlockStartColor', 'borderBottomColor', 'borderColor', + 'borderInlineColor', + 'borderInlineEndColor', + 'borderInlineStartColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', diff --git a/packages/react-native-web/src/exports/StyleSheet/index.ts b/packages/react-native-web/src/exports/StyleSheet/index.ts index 54225a30c2..b27303d243 100644 --- a/packages/react-native-web/src/exports/StyleSheet/index.ts +++ b/packages/react-native-web/src/exports/StyleSheet/index.ts @@ -9,53 +9,25 @@ import { styleq, type CompiledStyle, type Styles } from 'styleq'; import canUseDOM from '../../modules/canUseDom'; import type { GenericStyleProp } from '../../types'; -import { atomic, classic, inline, type Style } from './compiler'; +import { atomic, classic, type Style } from './compiler'; +import createReactDOMStyle from './compiler/createReactDOMStyle'; import { createSheet } from './dom'; -import { localizeStyle, type LocalizedStyle } from './localizeStyle'; import { preprocess } from './preprocess'; -import type { PreprocessOptions } from './preprocess'; import { validate } from './validate'; -const staticStyleMap: WeakMap = new WeakMap(); +const staticStyleMap: WeakMap = new WeakMap(); const sheet = createSheet(); -const defaultPreprocessOptions: PreprocessOptions = { - shadow: true, - textShadow: true -}; - -function createStyleq(isRTL: boolean, preprocessOptions: PreprocessOptions) { - return styleq.factory({ - transform(style) { - // Don't preprocess compiled objects: it rewrites keys and breaks styleq's cache - const compiledStyle = - staticStyleMap.get(style) ?? - (style.$$css === true ? (style as CompiledStyle) : null); - - return compiledStyle != null - ? localizeStyle(compiledStyle, isRTL) - : preprocess(style, preprocessOptions); - } - }); -} - -const styleqLTR = createStyleq(false, defaultPreprocessOptions); -const styleqRTL = createStyleq(true, defaultPreprocessOptions); +const customStyleq = styleq.factory({ + transform(style) { + // Don't preprocess compiled objects: it rewrites keys and breaks styleq's cache + const compiledStyle = + staticStyleMap.get(style) ?? + (style.$$css === true ? (style as CompiledStyle) : null); -function customStyleq(styles: Styles, options: StyleSheetOptions = {}) { - if (!('shadow' in options) && !('textShadow' in options)) { - return options.writingDirection === 'rtl' - ? styleqRTL(styles) - : styleqLTR(styles); + return compiledStyle ?? preprocess(style); } - - const { writingDirection, ...preprocessOptions } = options; - - return createStyleq(writingDirection === 'rtl', { - ...defaultPreprocessOptions, - ...preprocessOptions - })(styles); -} +}); function insertRules(compiledOrderedRules: Array<[Array, number]>) { compiledOrderedRules.forEach(([rules, order]) => { @@ -68,9 +40,7 @@ function insertRules(compiledOrderedRules: Array<[Array, number]>) { } function compileAndInsertAtomic(style: Style) { - const [compiledStyle, compiledOrderedRules] = atomic( - preprocess(style, defaultPreprocessOptions) - ); + const [compiledStyle, compiledOrderedRules] = atomic(preprocess(style)); insertRules(compiledOrderedRules); return compiledStyle; } @@ -183,24 +153,17 @@ function getSheet(): { id: string; textContent: string } { */ type StyleProps = [string, Record | null, string]; -export type StyleSheetOptions = { - shadow?: boolean; - textShadow?: boolean; - writingDirection?: 'auto' | 'ltr' | 'rtl'; -}; - -function resolve(styles?: Styles, options: StyleSheetOptions = {}): StyleProps { - const isRTL = options.writingDirection === 'rtl'; - const styleProps: StyleProps = customStyleq(styles, options); +function resolve(styles?: Styles): StyleProps { + const styleProps: StyleProps = customStyleq(styles); if (Array.isArray(styleProps) && styleProps[1] != null) { - styleProps[1] = inline(styleProps[1], isRTL); + styleProps[1] = createReactDOMStyle(styleProps[1], true); } return styleProps; } // Only kept for backward compatibility. Use `StyleSheet.resolve` instead. -function StyleSheet(styles?: Styles, options?: StyleSheetOptions): StyleProps { - return resolve(styles, options); +function StyleSheet(styles?: Styles): StyleProps { + return resolve(styles); } StyleSheet.absoluteFill = absoluteFill; @@ -226,7 +189,7 @@ if (devToolsHook) { } export type IStyleSheet = { - (styles?: Styles, options?: StyleSheetOptions): StyleProps; + (styles?: Styles): StyleProps; absoluteFill: AbsoluteFillStyle; create: typeof create; compose: typeof compose; diff --git a/packages/react-native-web/src/exports/StyleSheet/localizeStyle.ts b/packages/react-native-web/src/exports/StyleSheet/localizeStyle.ts deleted file mode 100644 index 3ac32cdb0c..0000000000 --- a/packages/react-native-web/src/exports/StyleSheet/localizeStyle.ts +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright (c) Nicolas Gallagher. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -type LocalizableValue = T | Array; - -export type LocalizedStyle = Readonly< - Record | boolean> ->; - -type CompiledStyle = Record; - -const cache = new WeakMap>(); -const markerProp = '$$css$localize'; - -/** - * The compiler polyfills logical properties and values, generating a class - * name for both writing directions. The style objects are annotated by - * the compiler as needing this runtime transform. The results are memoized. - * - * { '$$css$localize': true, float: [ 'float-left', 'float-right' ] } - * => { float: 'float-left' } - */ - -function compileStyle(style: LocalizedStyle, isRTL: boolean): CompiledStyle { - // Create a new compiled style for styleq - const compiledStyle: CompiledStyle = {}; - for (const prop in style) { - if (prop !== markerProp) { - const value = style[prop]; - if (Array.isArray(value)) { - compiledStyle[prop] = isRTL ? value[1] : value[0]; - } else { - compiledStyle[prop] = value; - } - } - } - return compiledStyle; -} - -export function localizeStyle( - style: LocalizedStyle, - isRTL: boolean -): LocalizedStyle { - if (style[markerProp] != null) { - const compiledStyleIndex = isRTL ? 1 : 0; - // Check the cache in case we've already seen this object - let cachedStyles = cache.get(style); - if (cachedStyles != null) { - let compiledStyle = cachedStyles[compiledStyleIndex]; - if (compiledStyle == null) { - // Update the missing cache entry - compiledStyle = compileStyle(style, isRTL); - cachedStyles[compiledStyleIndex] = compiledStyle; - cache.set(style, cachedStyles); - } - return compiledStyle; - } - - // Create a new compiled style for styleq - const compiledStyle = compileStyle(style, isRTL); - cachedStyles = new Array(2); - cachedStyles[compiledStyleIndex] = compiledStyle; - cache.set(style, cachedStyles); - return compiledStyle; - } - return style; -} diff --git a/packages/react-native-web/src/exports/StyleSheet/preprocess.ts b/packages/react-native-web/src/exports/StyleSheet/preprocess.ts index 3aa91cf635..894ae4c505 100644 --- a/packages/react-native-web/src/exports/StyleSheet/preprocess.ts +++ b/packages/react-native-web/src/exports/StyleSheet/preprocess.ts @@ -6,6 +6,7 @@ */ import type { Nullable } from '../../types'; +import hyphenateStyleName from './compiler/hyphenateStyleName'; import normalizeColor from './compiler/normalizeColor'; import normalizeValueWithProperty from './compiler/normalizeValueWithProperty'; @@ -154,29 +155,35 @@ const mergeFontFeatureSettings = (a: unknown, b: unknown): unknown => { return a; }; -export const PROPERTIES_STANDARD: Record = { +const PROPERTIES_STANDARD: Record = { borderBottomEndRadius: 'borderEndEndRadius', borderBottomStartRadius: 'borderEndStartRadius', - borderTopEndRadius: 'borderStartEndRadius', - borderTopStartRadius: 'borderStartStartRadius', borderEndColor: 'borderInlineEndColor', borderEndStyle: 'borderInlineEndStyle', borderEndWidth: 'borderInlineEndWidth', borderStartColor: 'borderInlineStartColor', borderStartStyle: 'borderInlineStartStyle', borderStartWidth: 'borderInlineStartWidth', + borderTopEndRadius: 'borderStartEndRadius', + borderTopStartRadius: 'borderStartStartRadius', end: 'insetInlineEnd', marginEnd: 'marginInlineEnd', - marginHorizontal: 'marginInline', marginStart: 'marginInlineStart', - marginVertical: 'marginBlock', paddingEnd: 'paddingInlineEnd', - paddingHorizontal: 'paddingInline', paddingStart: 'paddingInlineStart', - paddingVertical: 'paddingBlock', start: 'insetInlineStart' }; +// React Native aliases and camelCase names to CSS names, e.g. +// 'marginStart' and 'marginInlineStart' => 'margin-inline-start' +const TRANSITION_PROPERTIES = new Map(); + +for (const [alias, prop] of Object.entries(PROPERTIES_STANDARD)) { + const name = hyphenateStyleName(prop); + TRANSITION_PROPERTIES.set(alias, name); + TRANSITION_PROPERTIES.set(prop, name); +} + const ignoredProps = new Set([ 'elevation', 'overlayColor', @@ -191,28 +198,21 @@ const ignoredProps = new Set([ 'tintColor' ]); -export type PreprocessOptions = { - readonly shadow?: boolean; - readonly textShadow?: boolean; -}; - /** * Preprocess styles */ export const preprocess = >>( - originalStyle: T, - options: PreprocessOptions = {} + originalStyle: T ): T => { const style = originalStyle || emptyObject; const nextStyle: Record = {}; // Convert shadow styles if ( - options.shadow === true && - (style.shadowColor != null || - style.shadowOffset != null || - style.shadowOpacity != null || - style.shadowRadius != null) + style.shadowColor != null || + style.shadowOffset != null || + style.shadowOpacity != null || + style.shadowRadius != null ) { const boxShadowValue = createBoxShadowValue(style); if (boxShadowValue != null) { @@ -222,10 +222,9 @@ export const preprocess = >>( // Convert text shadow styles if ( - options.textShadow === true && - (style.textShadowColor != null || - style.textShadowOffset != null || - style.textShadowRadius != null) + style.textShadowColor != null || + style.textShadowOffset != null || + style.textShadowRadius != null ) { const textShadowValue = createTextShadowValue(style); if (textShadowValue != null && nextStyle.textShadow == null) { @@ -319,7 +318,13 @@ export const preprocess = >>( if (Array.isArray(value)) { value = value.join(', '); } - nextStyle.transitionProperty = value; + nextStyle.transitionProperty = + typeof value === 'string' + ? value.replace( + /[^\s,]+/g, + (name) => TRANSITION_PROPERTIES.get(name) ?? name + ) + : value; } else { nextStyle[prop] = value; } diff --git a/packages/react-native-web/src/exports/StyleSheet/validate.ts b/packages/react-native-web/src/exports/StyleSheet/validate.ts index 6e488f4dc0..5631768286 100644 --- a/packages/react-native-web/src/exports/StyleSheet/validate.ts +++ b/packages/react-native-web/src/exports/StyleSheet/validate.ts @@ -21,11 +21,18 @@ const invalidShortforms = new Set([ const invalidMultiValueShortforms = new Set([ 'backgroundPosition', + 'borderBlockColor', + 'borderBlockStyle', + 'borderBlockWidth', 'borderColor', + 'borderInlineColor', + 'borderInlineStyle', + 'borderInlineWidth', 'borderRadius', 'borderStyle', 'borderWidth', 'flex', + 'gap', 'inset', 'insetBlock', 'insetInline', @@ -67,9 +74,6 @@ export function validate(obj: Record) { if (prop === 'animation' || prop === 'animationName') { suggestion = 'Did you mean "animationKeyframes"?'; isInvalid = true; - } else if (prop === 'direction') { - suggestion = 'Did you mean "writingDirection"?'; - isInvalid = true; } else if (invalidShortforms.has(prop)) { suggestion = 'Please use long-form properties.'; isInvalid = true; diff --git a/packages/react-native-web/src/exports/Switch/__tests__/__snapshots__/index-test.tsx.snap b/packages/react-native-web/src/exports/Switch/__tests__/__snapshots__/index-test.tsx.snap index b6732250cc..dad6f83ab1 100644 --- a/packages/react-native-web/src/exports/Switch/__tests__/__snapshots__/index-test.tsx.snap +++ b/packages/react-native-web/src/exports/Switch/__tests__/__snapshots__/index-test.tsx.snap @@ -9,8 +9,8 @@ exports[`components/Switch > disabled > when "true" and value="false", a disable exports[`components/Switch > disabled > when "true" and value="false", a disabled checkbox is rendered with provided thumbColor 1`] = `
`; @@ -23,8 +23,8 @@ exports[`components/Switch > disabled > when "true" and value="false", a disable exports[`components/Switch > disabled > when "true" and value="true", a disabled checkbox is rendered with provided activeThumbColor 1`] = `
`; diff --git a/packages/react-native-web/src/exports/Text/__tests__/__snapshots__/index-test.tsx.snap b/packages/react-native-web/src/exports/Text/__tests__/__snapshots__/index-test.tsx.snap index 650926da86..b42efe4c0e 100644 --- a/packages/react-native-web/src/exports/Text/__tests__/__snapshots__/index-test.tsx.snap +++ b/packages/react-native-web/src/exports/Text/__tests__/__snapshots__/index-test.tsx.snap @@ -3,14 +3,12 @@ exports[`components/Text > default 1`] = `
`; exports[`components/Text > nested 1`] = `
prop "aria-label" > value is set 1`] = `
`; @@ -31,7 +28,6 @@ exports[`components/Text > prop "aria-labelledby" > value is set 1`] = `
`; @@ -39,28 +35,12 @@ exports[`components/Text > prop "aria-live" > value is set 1`] = `
-`; - -exports[`components/Text > prop "dir" > value is "ltr" 1`] = ` -
-`; - -exports[`components/Text > prop "dir" > value is "rtl" 1`] = ` -
`; exports[`components/Text > prop "href" > href with role 1`] = ` @@ -69,7 +49,6 @@ exports[`components/Text > prop "href" > href with role 1`] = ` exports[`components/Text > prop "href" > value is set 1`] = ` `; @@ -77,7 +56,6 @@ exports[`components/Text > prop "href" > value is set 1`] = ` exports[`components/Text > prop "hrefAttrs" > null values are excluded 1`] = ` `; @@ -85,14 +63,12 @@ exports[`components/Text > prop "hrefAttrs" > null values are excluded 1`] = ` exports[`components/Text > prop "hrefAttrs" > requires "href" 1`] = `
`; exports[`components/Text > prop "hrefAttrs" > target variant is set 1`] = ` @@ -101,7 +77,6 @@ exports[`components/Text > prop "hrefAttrs" > target variant is set 1`] = ` exports[`components/Text > prop "hrefAttrs" > value is set 1`] = ` prop "hrefAttrs" > value is set 1`] = ` /> `; -exports[`components/Text > prop "lang" > ar 1`] = ` -
-`; - -exports[`components/Text > prop "lang" > fr 1`] = ` -
-`; - -exports[`components/Text > prop "lang" > undefined 1`] = ` -
-`; - -exports[`components/Text > prop "lang" > with dir 1`] = ` -
-`; - exports[`components/Text > prop "nativeID" > value is set 1`] = `
`; @@ -151,7 +94,6 @@ exports[`components/Text > prop "nativeID" > value is set 1`] = ` exports[`components/Text > prop "numberOfLines" > value is set 1`] = `
`; @@ -159,21 +101,18 @@ exports[`components/Text > prop "numberOfLines" > value is set 1`] = ` exports[`components/Text > prop "numberOfLines" > value is set to one 1`] = `
`; exports[`components/Text > prop "numberOfLines" > value is set to one with "clip" ellipsizeMode 1`] = `
`; exports[`components/Text > prop "role" > value alters HTML element 1`] = `
`; @@ -181,7 +120,6 @@ exports[`components/Text > prop "role" > value alters HTML element 1`] = ` exports[`components/Text > prop "role" > value is "button" 1`] = `