diff --git a/docs/data/system/getting-started/the-sx-prop/DynamicValues.js b/docs/data/system/getting-started/the-sx-prop/DynamicValues.js new file mode 100644 index 00000000000000..7eeba066f5daa9 --- /dev/null +++ b/docs/data/system/getting-started/the-sx-prop/DynamicValues.js @@ -0,0 +1,34 @@ +import * as React from 'react'; +import Box from '@mui/material/Box'; +import Stack from '@mui/material/Stack'; +import Typography from '@mui/material/Typography'; + +export default function DynamicValues() { + const [color, setColor] = React.useState('#007fff'); + + return ( + + + Pick a color to see a live preview + setColor(event.target.value)} /> + + + + ); +} diff --git a/docs/data/system/getting-started/the-sx-prop/DynamicValues.tsx b/docs/data/system/getting-started/the-sx-prop/DynamicValues.tsx new file mode 100644 index 00000000000000..e6323daca45d7f --- /dev/null +++ b/docs/data/system/getting-started/the-sx-prop/DynamicValues.tsx @@ -0,0 +1,34 @@ +import * as React from 'react'; +import Box from '@mui/material/Box'; +import Stack from '@mui/material/Stack'; +import Typography from '@mui/material/Typography'; + +export default function DynamicValues() { + const [color, setColor] = React.useState('#007fff'); + + return ( + + + Pick a color to see a live preview + setColor(event.target.value)} /> + + + + ); +} diff --git a/docs/data/system/getting-started/the-sx-prop/the-sx-prop.md b/docs/data/system/getting-started/the-sx-prop/the-sx-prop.md index febdd81139b518..311188f80a1491 100644 --- a/docs/data/system/getting-started/the-sx-prop/the-sx-prop.md +++ b/docs/data/system/getting-started/the-sx-prop/the-sx-prop.md @@ -292,6 +292,12 @@ If you want to receive the `sx` prop from a custom component and pass it down to {{"demo": "PassingSxProp.js", "bg": true, "defaultCodeOpen": true}} +## Dynamic values + +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. This approach avoids inserting unnecessary `style` tags into the DOM, preventing 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. + +{{"demo": "DynamicValues.js", "bg": true}} + ## TypeScript usage 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: