Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';

↓ ↓ ↓ ↓ ↓ ↓
Expand All @@ -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";

`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
1 change: 0 additions & 1 deletion packages/babel-plugin-react-native-web/src/moduleMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,5 @@ module.exports = {
useAnimatedValue: true,
useAnimatedValueXY: true,
useColorScheme: true,
useLocaleContext: true,
useWindowDimensions: true
};
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -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 `<form action>` 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 %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -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 %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -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 %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,30 @@ 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
const style = { paddingInlineStart: 10, marginInlineStart: 10 };
return <View style={style} />;
```

## Localization props
## Document language and direction

To automatically flip the layout of a component and its subtree, you can either:
Set the language and writing direction of the app on the `html` element. The `dir` attribute sets the direction of the whole page, and logical styles follow it. The `lang` attribute tells browsers and screen readers which language the content uses, for pronunciation, hyphenation, and font selection.

1. Set the `dir` prop to the desired writing direction (e.g., `"rtl"`).
2. Set the `lang` prop to the locale of the content.

```jsx
<View dir="ltr">...</View>
<Text lang="ar">...</Text>
```html
<html lang="ar" dir="rtl">
```

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.
Browsers, assistive technologies, and tools that read the page without CSS (e.g., reader modes and search engines) all use these attributes, so prefer them over styles for the direction of the whole app.

## Localization hook
## Writing direction

The `useLocaleContext` API can be used for fine-grained control of layout, e.g., flipping images or transforms.
To flip the layout of a component and its subtree, set the `direction` style to the desired writing direction (e.g., `"rtl"`).

```jsx
const { direction, locale } = useLocaleContext();

const isRTL = direction === 'rtl';
const style = { scaleX: isRTL ? -1 : 1 };
const styles = StyleSheet.create({ root: { direction: 'rtl' } });

<Image source="forward.svg" style={style} />
<Image source={isRTL ? 'back.svg' : 'forward.svg'} />
<View style={styles.root}>...</View>;
```
10 changes: 10 additions & 0 deletions packages/react-native-web-docs/src/pages/docs/concepts/styling.md
Original file line number Diff line number Diff line change
Expand Up @@ -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', '?("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.
Expand All @@ -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 %}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
Loading