Skip to content

Commit 532510c

Browse files
committed
[docs] Add docs for positionTry API
1 parent 140e36f commit 532510c

5 files changed

Lines changed: 67 additions & 7 deletions

File tree

packages/@stylexjs/babel-plugin/src/visitors/stylex-create-theme.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export default function transformStyleXCreateTheme(
7979
);
8080
}
8181

82-
const otherInjectedCSSRules: { [animationName: string]: InjectableStyle } =
82+
const otherInjectedCSSRules: { [propertyName: string]: InjectableStyle } =
8383
{};
8484

8585
// eslint-disable-next-line no-inner-declarations
@@ -98,10 +98,10 @@ export default function transformStyleXCreateTheme(
9898

9999
// eslint-disable-next-line no-inner-declarations
100100
function positionTry<Obj: { +[k: string]: string | number }>(
101-
animation: Obj,
101+
fallbackStyles: Obj,
102102
): string {
103103
const [positionTryName, injectedStyle] = stylexPositionTry(
104-
animation,
104+
fallbackStyles,
105105
state.options,
106106
);
107107
otherInjectedCSSRules[positionTryName] = injectedStyle;

packages/@stylexjs/babel-plugin/src/visitors/stylex-create.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default function transformStyleXCreate(
7373

7474
state.inStyleXCreate = true;
7575

76-
const otherInjectedCSSRules: { [animationName: string]: InjectableStyle } =
76+
const otherInjectedCSSRules: { [propertyName: string]: InjectableStyle } =
7777
{};
7878

7979
// eslint-disable-next-line no-inner-declarations

packages/docs/docs/api/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ sidebar_position: 1
2222
- [`stylex.defineVars()`](./javascript/defineVars.mdx)
2323
- [`stylex.firstThatWorks()`](./javascript/firstThatWorks.mdx)
2424
- [`stylex.keyframes()`](./javascript/keyframes.mdx)
25+
- [`stylex.positionTry()`](./javascript/positionTry.mdx)
2526
- [`stylex.props()`](./javascript/props.mdx)
2627
- [`stylex.types.*()`](./javascript/types.mdx)
2728

packages/docs/docs/api/javascript/keyframes.mdx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ const styles = stylex.create({
3838
});
3939
```
4040

41-
To declare keyframes in a separate file, you can use `defineVars` to hold
42-
animation names:
41+
To use `keyframes` return values in a separate file, you can use `defineVars` to hold animation names:
4342

4443
```tsx title="animations.stylex.js"
4544
import * as stylex from '@stylexjs/stylex';
@@ -68,4 +67,4 @@ export const animations = stylex.defineVars({
6867
```
6968

7069
These variables can then be imported and used like any other variables created
71-
with defineVars.
70+
with `defineVars`.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
#
4+
# This source code is licensed under the MIT license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
sidebar_position: 6
7+
---
8+
9+
# `stylex.positionTry`
10+
11+
Creates a [`@position-try`](https://developer.mozilla.org/en-US/docs/Web/CSS/@position-try) rule used to define a custom position-try fallback option, which can be used to define positioning and alignment for anchor-positioned elements. One or more sets of position-try fallback options can be applied to the anchored element via the `positionTryFallbacks` property.
12+
13+
```ts
14+
function positionTry(descriptors: {[key: string]: RawStyles}): string;
15+
```
16+
17+
The only properties allowed in a `positionTry` call are `positionAnchor`, `positionArea`, inset properties (`top`, `left`, `insetInline`, etc.), margin properties, size properties (`height`, `inlineSize`, etc.), and self-alignment properties (`alignSelf`, `justifySelf`, `placeSelf`).
18+
19+
You must declare your position-try rules in the same file as where you use them. Duplicate declarations will be deduplicated in the generated CSS output.
20+
21+
### Example use:
22+
23+
```tsx
24+
import * as stylex from '@stylexjs/stylex';
25+
26+
const fallback = stylex.positionTry({
27+
positionAnchor: '--anchor',
28+
top: '0',
29+
left: '0',
30+
width: '100px',
31+
height: '100px'
32+
});
33+
34+
const styles = stylex.create({
35+
anchor: {
36+
positionTryFallbacks: fallback,
37+
},
38+
});
39+
```
40+
41+
To use `positionTry` return values in a separate file, you can use `defineVars` to hold position fallback values:
42+
43+
```tsx title="positionFallbacks.stylex.js"
44+
import * as stylex from '@stylexjs/stylex';
45+
46+
const topLeftCorner = stylex.positionTry({
47+
positionAnchor: '--anchor',
48+
top: '0',
49+
left: '0',
50+
width: '100px',
51+
height: '100px'
52+
});
53+
54+
export const positionFallbacks = stylex.defineVars({
55+
topLeftCorner
56+
});
57+
```
58+
59+
These variables can then be imported and used like any other variables created
60+
with `defineVars`.

0 commit comments

Comments
 (0)