Skip to content

Commit d2e76bb

Browse files
committed
Add native support for css.defineConsts
1 parent e3c5979 commit d2e76bb

6 files changed

Lines changed: 55 additions & 20 deletions

File tree

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
1-
---
2-
draft: true
3-
---
4-
51
# css.defineConsts
62

7-
<p className="text-xl">How to define constants.</p>
3+
<p className="text-xl">How to define constants for use in styles and themes.</p>
84

95
:::warning
106

11-
`css.defineConsts()` is currently only supported on web.
12-
13-
:::
7+
Constants must be defined as named exports in files with a `*.stylex.js` (or `*.stylex.ts`) extension. This limitation is currently imposed due to constraints on how styles are compiled on web.
148

159
## Overview
1610

17-
...
11+
This API creates style variables that can be imported and used within `css.create()` and `css.createTheme()` calls anywhere within a codebase. These values are inlined at build time and do not generate variables.
12+
13+
```js title="constants.stylex.js"
14+
import { css } from 'react-strict-dom';
15+
16+
export const breakpoints = css.defineConsts({
17+
small: '@media (max-width: 600px)',
18+
medium: '@media (min-width: 601px) and (max-width: 1024px)',
19+
large: '@media (min-width: 1025px)',
20+
});
21+
```
1822

1923
## API
2024

21-
...
25+
### Named variables
26+
27+
The `defineConsts` function accepts an object of named constants. These constants can be referenced elsewhere by their key name.
28+
29+
```js title="component.js"
30+
import { breakpoints } from './constants.stylex.js';
31+
32+
const styles = css.create({
33+
box: {
34+
padding: {
35+
default: '10px',,
36+
[breakpoints.medium]: '15px',
37+
[breakpoints.large]: '20px',
38+
},
39+
},
40+
});
41+
```

apps/website/docs/api/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ sidebar_position: -1
1515
* [**css**](/api/css/) - An overview of working with styles.
1616
* [css.create](/api/css/create) - How to create styles.
1717
* [css.createTheme](/api/css/createTheme) - How to create themes.
18-
<!-- * [css.defineConsts](/api/css/defineConsts) - How to define constants. -->
18+
* [css.defineConsts](/api/css/defineConsts) - How to define constants.
1919
* [css.defineVars](/api/css/defineVars) - How to define variables.
2020
* [css.firstThatWorks](/api/css/firstThatWorks) - How to declare fallback values.
2121
<!-- * [css.keyframes](/api/css/keyframes) - How to declare animation keyframes. -->

apps/website/docs/learn/05-theming-components.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ slug: /learn/themes
88

99
:::info[Requirements]
1010

11-
Variables must be defined in a file that matches the following file extension pattern: `.stylex.{js,jsx,mjs,ts,tsx}`. Only CSS variables can be defined in these files. And every `css.defineVars` call must be a named export.
11+
Variables and constants must be defined in a file that matches the following file extension pattern: `.stylex.{js,jsx,mjs,ts,tsx}`. Only CSS variables can be defined in these files. Every `css.defineConsts` and `css.defineVars` call must be a named export.
1212

1313
:::
1414

1515
## Defining variables
1616

17-
CSS variables are defined with a call to `css.defineVars`. This defines global variables which can then be imported as constants for use in any style rules. These variables and their values are essentially a "default theme".
17+
CSS variables are defined with a call to `css.defineVars`. This defines global variables which can then be imported for use in any style rules. These variables and their values are essentially a "default theme". If you don't need to override these values in themes, use `css.defineConsts` instead.
1818

1919
```js title="tokens.stylex.js"
2020
import { css } from 'react-strict-dom';
@@ -59,9 +59,9 @@ export const colors = css.defineVars({
5959
});
6060
```
6161

62-
## Using variables in styles
62+
## Using variables or constants in styles
6363

64-
Once variables have been defined, they can be imported and used directly in style rules created with `css.create`.
64+
Once variables or constants have been defined, they can be imported and used directly in style rules created with `css.create`.
6565

6666
```js title="Component.js"
6767
import { css } from 'react-strict-dom';

packages/react-strict-dom/src/native/stylex/index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -454,13 +454,10 @@ export const createTheme = (
454454
return result;
455455
};
456456

457-
export const defineConsts = (tokens: {
457+
export const defineConsts = (constants: {
458458
[string]: string
459459
}): { [string]: string } => {
460-
if (__DEV__) {
461-
errorMsg('css.defineConsts() is not supported.');
462-
}
463-
return tokens;
460+
return Object.freeze(constants);
464461
};
465462

466463
type Tokens = { [string]: string };

packages/react-strict-dom/tests/__snapshots__/css-test.native.js.snap-native

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ exports[`properties: custom property css.createTheme: theme 1`] = `
1818
}
1919
`;
2020

21+
exports[`properties: custom property css.defineConsts: constants 1`] = `
22+
{
23+
"blueColor": "blue",
24+
"redColor": "red",
25+
}
26+
`;
27+
2128
exports[`properties: custom property css.defineVars: css.__customProperties 1`] = `
2229
{
2330
"rootColor__id__1": "red",

packages/react-strict-dom/tests/css-test.native.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,17 @@ describe('properties: custom property', () => {
11651165
).toEqual('red');
11661166
});
11671167

1168+
test('css.defineConsts', () => {
1169+
const constants = css.defineConsts({
1170+
redColor: 'red',
1171+
blueColor: 'blue'
1172+
});
1173+
expect(constants).toMatchSnapshot('constants');
1174+
expect(() => {
1175+
constants.redColor = 'black';
1176+
}).toThrow();
1177+
});
1178+
11681179
test('css.defineVars', () => {
11691180
const options = {};
11701181
const tokens = css.defineVars({

0 commit comments

Comments
 (0)