Skip to content

Commit 998a319

Browse files
aarongarciahweb-flow
authored andcommitted
[system][docs] Add "dynamic values" section to sx prop page (#42239)
1 parent 7be02ec commit 998a319

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as React from 'react';
2+
import Box from '@mui/material/Box';
3+
import Stack from '@mui/material/Stack';
4+
import Typography from '@mui/material/Typography';
5+
6+
export default function DynamicValues() {
7+
const [color, setColor] = React.useState('#007fff');
8+
9+
return (
10+
<Stack spacing={1} alignItems="center">
11+
<Typography
12+
component="label"
13+
variant="body2"
14+
sx={{ display: 'inline-flex', alignItems: 'center', gap: 1 }}
15+
>
16+
Pick a color to see a live preview
17+
<input
18+
type="color"
19+
value={color}
20+
onChange={(event) => setColor(event.target.value)}
21+
/>
22+
</Typography>
23+
<Box
24+
component="div"
25+
sx={{
26+
display: 'flex',
27+
justifyContent: 'center',
28+
alignItems: 'center',
29+
width: 75,
30+
height: 75,
31+
borderRadius: 2,
32+
backgroundColor: 'var(--bg)',
33+
}}
34+
style={{ '--bg': color }}
35+
/>
36+
</Stack>
37+
);
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as React from 'react';
2+
import Box from '@mui/material/Box';
3+
import Stack from '@mui/material/Stack';
4+
import Typography from '@mui/material/Typography';
5+
6+
export default function DynamicValues() {
7+
const [color, setColor] = React.useState('#007fff');
8+
9+
return (
10+
<Stack spacing={1} alignItems="center">
11+
<Typography
12+
component="label"
13+
variant="body2"
14+
sx={{ display: 'inline-flex', alignItems: 'center', gap: 1 }}
15+
>
16+
Pick a color to see a live preview
17+
<input
18+
type="color"
19+
value={color}
20+
onChange={(event) => setColor(event.target.value)}
21+
/>
22+
</Typography>
23+
<Box
24+
component="div"
25+
sx={{
26+
display: 'flex',
27+
justifyContent: 'center',
28+
alignItems: 'center',
29+
width: 75,
30+
height: 75,
31+
borderRadius: 2,
32+
backgroundColor: 'var(--bg)',
33+
}}
34+
style={{ '--bg': color } as React.CSSProperties}
35+
/>
36+
</Stack>
37+
);
38+
}

docs/data/system/getting-started/the-sx-prop/the-sx-prop.md

+12
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,18 @@ If you want to receive the `sx` prop from a custom component and pass it down to
292292

293293
{{"demo": "PassingSxProp.js", "bg": true, "defaultCodeOpen": true}}
294294

295+
## Dynamic values
296+
297+
For highly dynamic CSS values, we recommend using inline CSS variables instead of passing an object with varying values to the `sx` prop on each render.
298+
This approach avoids inserting unnecessary `style` tags into the DOM, which prevents potential performance issues when dealing with CSS properties that can hold a wide range of values that change frequently—for example, a color picker with live preview.
299+
300+
:::info
301+
If you're having problems with your Content Security Policy while using inline styles with the `style` attribute, make sure you've enabled the [`style-src-attr` directive](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/style-src-attr).
302+
Visit the [Content Security Policy guide](/material-ui/guides/content-security-policy/) for configuration details.
303+
:::
304+
305+
{{"demo": "DynamicValues.js", "bg": true}}
306+
295307
## TypeScript usage
296308

297309
A frequent source of confusion with the `sx` prop is TypeScript's [type widening](https://mariusschulz.com/blog/literal-type-widening-in-typescript), which causes this example not to work as expected:

0 commit comments

Comments
 (0)