Skip to content

Commit b27b1ef

Browse files
authored
Update docs related to position to account for static (facebook#4235)
* Update docs related to position to account for static * fix typo * fix tsx lint * fix format * update RN version for linting to 0.74.1 * requested changes * address nit
1 parent 7a6ace8 commit b27b1ef

File tree

4 files changed

+475
-1266
lines changed

4 files changed

+475
-1266
lines changed

docs/flexbox.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,18 +2575,20 @@ export default WidthHeightBasics;
25752575
</TabItem>
25762576
</Tabs>
25772577

2578-
## Absolute & Relative Layout
2578+
## Position
25792579

2580-
The `position` type of an element defines how it is positioned within its parent.
2580+
The `position` type of an element defines how it is positioned relative to either itself, its parent, or its [containing block](./flexbox.md#the-containing-block).
25812581

25822582
- `relative` (**default value**) By default, an element is positioned relatively. This means an element is positioned according to the normal flow of the layout, and then offset relative to that position based on the values of `top`, `right`, `bottom`, and `left`. The offset does not affect the position of any sibling or parent elements.
25832583

2584-
- `absolute` When positioned absolutely, an element doesn't take part in the normal layout flow. It is instead laid out independent of its siblings. The position is determined based on the `top`, `right`, `bottom`, and `left` values.
2584+
- `absolute` When positioned absolutely, an element doesn't take part in the normal layout flow. It is instead laid out independent of its siblings. The position is determined based on the `top`, `right`, `bottom`, and `left` values. These values will posistion the element relative to its containing block.
2585+
2586+
- `static` When positioned statically, an element is positioned according to the normal flow of layout, and will ignore the `top`, `right`, `bottom`, and `left` values. This `position` will also cause the element to not form a containing block for absolute descendants, unless some other superceding style prop is present (e.g. `transform`). This allows `absolute` elements to be positioned to something that is not their parent. Note that **`static` is only available on the New Architecture**.
25852587

25862588
<Tabs groupId="language" queryString defaultValue={constants.defaultSnackLanguage} values={constants.snackLanguages}>
25872589
<TabItem value="javascript">
25882590

2589-
```SnackPlayer name=Absolute%20%26%20Relative%20Layout&ext=js
2591+
```SnackPlayer name=Position&ext=js
25902592
import React, {useState} from 'react';
25912593
import {View, TouchableOpacity, Text, StyleSheet} from 'react-native';
25922594
@@ -2597,7 +2599,7 @@ const PositionLayout = () => {
25972599
<PreviewLayout
25982600
label="position"
25992601
selectedValue={position}
2600-
values={['relative', 'absolute']}
2602+
values={['relative', 'absolute', 'static']}
26012603
setSelectedValue={setPosition}>
26022604
<View
26032605
style={[
@@ -2716,19 +2718,21 @@ export default PositionLayout;
27162718
</TabItem>
27172719
<TabItem value="typescript">
27182720

2719-
```SnackPlayer name=Absolute%20%26%20Relative%20Layout&ext=tsx
2721+
```SnackPlayer name=Position&ext=tsx
27202722
import React, {useState} from 'react';
27212723
import {View, TouchableOpacity, Text, StyleSheet} from 'react-native';
27222724
import type {PropsWithChildren} from 'react';
27232725
27242726
const PositionLayout = () => {
2725-
const [position, setPosition] = useState<'relative' | 'absolute'>('relative');
2727+
const [position, setPosition] = useState<'relative' | 'absolute' | 'static'>(
2728+
'relative',
2729+
);
27262730
27272731
return (
27282732
<PreviewLayout
27292733
label="position"
27302734
selectedValue={position}
2731-
values={['relative', 'absolute']}
2735+
values={['relative', 'absolute', 'static']}
27322736
setSelectedValue={setPosition}>
27332737
<View
27342738
style={[
@@ -2769,9 +2773,9 @@ const PositionLayout = () => {
27692773
27702774
type PreviewLayoutProps = PropsWithChildren<{
27712775
label: string;
2772-
values: Array<'relative' | 'absolute'>;
2776+
values: Array<'relative' | 'absolute' | 'static'>;
27732777
selectedValue: string;
2774-
setSelectedValue: (value: 'relative' | 'absolute') => void;
2778+
setSelectedValue: (value: 'relative' | 'absolute' | 'static') => void;
27752779
}>;
27762780
27772781
const PreviewLayout = ({
@@ -2854,6 +2858,23 @@ export default PositionLayout;
28542858
</TabItem>
28552859
</Tabs>
28562860

2861+
## The Containing Block
2862+
2863+
The containing block of an element is an ancestor element which controls its position and size.
2864+
The way containing blocks work in React Native is very similar to [how they work on the web](https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block), with some simplifications due to the lack of some web features.
2865+
2866+
The `top`, `right`, `bottom`, and `left` values of an absolutely positioned element will be
2867+
relative to its containing block.
2868+
2869+
Percentage lengths (e.g.: `width: '50%'` or `padding: '10%'`) applied to absolutely positioned elements will be calculated relatively to the size of its containing block. For example, if the containing block is 100 points wide, then `width: 50%` on an absolutely positioned element will cause it to be 50 points wide.
2870+
2871+
The following list will help you determine the containing block of any given element:
2872+
2873+
- If that element has a `position` type of `relative` or `static`, then its containing block is its parent.
2874+
- If that element has a `position` type of `absolute`, then its containing block is the nearest ancestor in which one of the following is true:
2875+
- It has a `position` type other than `static`
2876+
- It has a `transform`
2877+
28572878
## Going Deeper
28582879

28592880
Check out the interactive [yoga playground](https://www.yogalayout.dev/playground) that you can use to get a better understanding of flexbox.

docs/layout-props.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -923,17 +923,20 @@ Setting `paddingVertical` is like setting both of `paddingTop` and `paddingBotto
923923

924924
### `position`
925925

926-
`position` in React Native is similar to regular CSS, but everything is set to `relative` by default, so `absolute` positioning is always relative to the parent.
926+
`position` in React Native is similar to [regular CSS](https://developer.mozilla.org/en-US/docs/Web/CSS/position), but everything is set to `relative` by default.
927927

928-
If you want to position a child using specific numbers of logical pixels relative to its parent, set the child to have `absolute` position.
928+
`relative` will position an element according to the normal flow of the layout. Insets (`top`, `bottom`, `left`, `right`) will offset relative to this layout.
929929

930-
If you want to position a child relative to something that is not its parent, don't use styles for that. Use the component tree.
930+
`absolute` takes the element out of the normal flow of the layout. Insets will offset relative to its [containing block](./flexbox.md#the-containing-block).
931931

932-
See https://github.com/facebook/yoga for more details on how `position` differs between React Native and CSS.
932+
`static` will position an element according to the normal flow of the layout. Insets will have no effect.
933+
`static` elements do not form a containing block for absolute descendants.
933934

934-
| Type | Required |
935-
| ---------------------------- | -------- |
936-
| enum('absolute', 'relative') | No |
935+
For more information, see the [Layout with Flexbox docs](./flexbox.md#position). Also, [the Yoga documentation](https://www.yogalayout.dev/docs/styling/position) has more details on how `position` differs between React Native and CSS.
936+
937+
| Type | Required |
938+
| -------------------------------------- | -------- |
939+
| enum('absolute', 'relative', 'static') | No |
937940

938941
---
939942

scripts/lint-examples/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
"@babel/core": "^7.25.2",
1212
"@babel/preset-env": "^7.25.4",
1313
"@babel/runtime": "^7.25.6",
14-
"@react-native/babel-preset": "^0.75.3",
14+
"@react-native/babel-preset": "^0.76.0-rc.2",
1515
"@react-native-community/slider": "^4.5.3",
16-
"@react-native/eslint-config": "^0.75.3",
17-
"@react-native/typescript-config": "^0.75.3",
16+
"@react-native/eslint-config": "^0.76.0-rc.2",
17+
"@react-native/typescript-config": "^0.76.0-rc.2",
1818
"@types/react": "^18.2.79",
1919
"eslint": "^8.57.1",
2020
"glob-promise": "^4.2.2",
2121
"prettier": "2.8.8",
22-
"react": "18.2.0",
23-
"react-native": "^0.75.3",
22+
"react": "18.3.1",
23+
"react-native": "^0.76.0-rc.2",
2424
"react-native-safe-area-context": "^4.11.0",
2525
"typescript": "5.5.4"
2626
}

0 commit comments

Comments
 (0)