Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[system][docs] Add "dynamic values" section to sx prop page #42239

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/data/system/getting-started/the-sx-prop/DynamicValues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 (
<Stack spacing={1} alignItems="center">
<Typography
component="label"
variant="body2"
sx={{ display: 'inline-flex', alignItems: 'center', gap: 1 }}
>
Pick a color to see a live preview
<input
type="color"
value={color}
onChange={(event) => setColor(event.target.value)}
/>
</Typography>
<Box
component="div"
sx={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
width: 75,
height: 75,
borderRadius: 2,
backgroundColor: 'var(--bg)',
}}
style={{ '--bg': color }}
/>
</Stack>
);
}
38 changes: 38 additions & 0 deletions docs/data/system/getting-started/the-sx-prop/DynamicValues.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 (
<Stack spacing={1} alignItems="center">
<Typography
component="label"
variant="body2"
sx={{ display: 'inline-flex', alignItems: 'center', gap: 1 }}
>
Pick a color to see a live preview
<input
type="color"
value={color}
onChange={(event) => setColor(event.target.value)}
/>
</Typography>
<Box
component="div"
sx={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
width: 75,
height: 75,
borderRadius: 2,
backgroundColor: 'var(--bg)',
}}
style={{ '--bg': color } as React.CSSProperties}
/>
</Stack>
);
}
13 changes: 13 additions & 0 deletions docs/data/system/getting-started/the-sx-prop/the-sx-prop.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,19 @@

{{"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.

Check warning on line 297 in docs/data/system/getting-started/the-sx-prop/the-sx-prop.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.We] Try to avoid using first-person plural like 'we'. Raw Output: {"message": "[Google.We] Try to avoid using first-person plural like 'we'.", "location": {"path": "docs/data/system/getting-started/the-sx-prop/the-sx-prop.md", "range": {"start": {"line": 297, "column": 32}}}, "severity": "WARNING"}
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.
aarongarciah marked this conversation as resolved.
Show resolved Hide resolved

:::info
If you're having problems with your CSP policy while using inline styles with the `style` attribute, make sure you've enabled the [`style-src-attr`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/style-src-attr) directive.
aarongarciah marked this conversation as resolved.
Show resolved Hide resolved
Check [our guide](/material-ui/guides/content-security-policy/) on how to configure CSP.

Check warning on line 303 in docs/data/system/getting-started/the-sx-prop/the-sx-prop.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.We] Try to avoid using first-person plural like 'our'. Raw Output: {"message": "[Google.We] Try to avoid using first-person plural like 'our'.", "location": {"path": "docs/data/system/getting-started/the-sx-prop/the-sx-prop.md", "range": {"start": {"line": 303, "column": 8}}}, "severity": "WARNING"}
aarongarciah marked this conversation as resolved.
Show resolved Hide resolved
:::

{{"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:
Expand Down
Loading