### Framework Example: Create React App Setup Source: https://v6.mui.com/material-ui/experimental-api/classname-generator Demonstrates importing the ClassNameGenerator setup file in a Create React App application. ```javascript +import './MuiClassNameSetup'; import * as React from 'react'; import * as ReactDOM from 'react-dom'; import App from './App'; ReactDOM.render(); ``` -------------------------------- ### Framework Example: Next.js Pages Router Setup Source: https://v6.mui.com/material-ui/experimental-api/classname-generator Shows how to import the ClassNameGenerator setup file in a Next.js application using the Pages Router. ```javascript +import './MuiClassNameSetup'; import * as React from 'react'; import PropTypes from 'prop-types'; import Head from 'next/head'; export default function MyApp(props) { const { Component, pageProps } = props; return ( ); } ``` -------------------------------- ### Importing Setup in App Source: https://v6.mui.com/material-ui/experimental-api/classname-generator Example of importing the ClassNameSetup file in the root of an application before other Material UI component imports. ```javascript import './MuiClassNameSetup'; import Button from '@mui/material/Button'; // ...other component imports function App() { return ; } ``` -------------------------------- ### Manual installation for Next.js Source: https://v6.mui.com/material-ui/experimental-api/pigment-css Install the required Pigment CSS packages for a Next.js project. ```bash npm install @pigment-css/react npm install --save-dev @pigment-css/nextjs-plugin ``` -------------------------------- ### Install RTL plugin Source: https://v6.mui.com/material-ui/customization/right-to-left Installs the necessary dependencies for RTL support. ```bash npm install stylis stylis-plugin-rtl ``` -------------------------------- ### Install Material Icons Source: https://v6.mui.com/material-ui/getting-started/installation Installs the Material Icons package. ```bash npm install @mui/icons-material@^6.0.0 ``` -------------------------------- ### Run development server Source: https://v6.mui.com/material-ui/migration/migrating-to-pigment-css Command to start the development server after configuration. ```bash npm run dev ``` -------------------------------- ### Install Material UI with styled-components Source: https://v6.mui.com/material-ui/getting-started/installation Installs Material UI with the styled-components engine instead of the default Emotion. ```bash npm install @mui/material @mui/styled-engine-sc styled-components ``` -------------------------------- ### Wrap component with ThemeProvider Source: https://v6.mui.com/material-ui/react-use-media-query Example showing how to wrap a component with the ThemeProvider. ```jsx ``` -------------------------------- ### Install Pigment CSS dependencies Source: https://v6.mui.com/material-ui/migration/migrating-to-pigment-css Install the required Material UI wrapper and Pigment CSS packages. ```bash npm install @mui/material-pigment-css @pigment-css/react ``` -------------------------------- ### Install Material UI Lab Source: https://v6.mui.com/material-ui/about-the-lab Command to install the lab package along with its required peer dependency, Material UI. ```bash npm install @mui/lab@^6.0.0-beta @mui/material@^6.0.0 ``` -------------------------------- ### Install Material UI with npm Source: https://v6.mui.com/material-ui/getting-started/installation Installs the core Material UI package along with Emotion dependencies. ```bash npm install @mui/material@^6.0.0 @emotion/react @emotion/styled ``` -------------------------------- ### Render responsive components Source: https://v6.mui.com/material-ui/customization/breakpoints Example structure for components that respond to breakpoint-based styling. ```jsx down(md): red up(md): blue up(lg): green ``` -------------------------------- ### Basic Masonry Implementation Source: https://v6.mui.com/material-ui/react-masonry A simple example showing how to use the Masonry component with a fixed number of columns and spacing. ```jsx {heights.map((height, index) => ( {index + 1} ))} ``` -------------------------------- ### Install and Import Roboto Font Source: https://v6.mui.com/material-ui/getting-started/installation Installs the Roboto font via Fontsource and imports the necessary weights in the project entry point. ```bash npm install @fontsource/roboto ``` ```javascript import '@fontsource/roboto/300.css'; import '@fontsource/roboto/400.css'; import '@fontsource/roboto/500.css'; import '@fontsource/roboto/700.css'; ``` -------------------------------- ### Install Dependencies for App Router Source: https://v6.mui.com/material-ui/integrations/nextjs Install the necessary Material UI and Emotion packages for Next.js App Router integration. ```bash npm install @mui/material-nextjs @emotion/cache ``` -------------------------------- ### Account Menu Source: https://v6.mui.com/material-ui/react-menu Example of integrating Menu with other components like Avatar. ```JSTS import * as React from 'react'; import Box from '@mui/material/Box'; import Avatar from '@mui/material/Avatar'; import Menu from '@mui/material/Menu'; import MenuItem from '@mui/material/MenuItem'; import ListItemIcon from '@mui/material/ListItemIcon'; import Divider from '@mui/material/Divider'; import IconButton from '@mui/material/IconButton'; import Typography from '@mui/material/Typography'; import Tooltip from '@mui/material/Tooltip'; import PersonAdd from '@mui/icons-material/PersonAdd'; import Settings from '@mui/icons-material/Settings'; import Logout from '@mui/icons-material/Logout'; export default function AccountMenu() { const [anchorEl, setAnchorEl] = React.useState(null); const open = Boolean(anchorEl); const handleClick = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget); }; const handleClose = () => { setAnchorEl(null); }; return ( Contact Profile M Profile My account Add another account Settings Logout ); } ``` -------------------------------- ### Install Dependencies for Pages Router Source: https://v6.mui.com/material-ui/integrations/nextjs Install the necessary Material UI, Emotion, and Emotion server packages for Next.js Pages Router integration. ```bash npm install @mui/material-nextjs @emotion/cache @emotion/server ``` -------------------------------- ### Basic Stack Configuration Source: https://v6.mui.com/material-ui/react-stack Example of configuring a Stack component with direction, spacing, and alignment using the sx prop. ```jsx ``` -------------------------------- ### Truncation Implementation Example Source: https://v6.mui.com/material-ui/react-stack A comparison of standard Stack layout versus one configured with min-width: 0 to support text truncation. ```jsx W {message} W {message} ``` -------------------------------- ### Theming with Generated Class Names Source: https://v6.mui.com/material-ui/experimental-api/classname-generator Example of using `[component]Classes` for theming, demonstrating how to reference generated class names like `outlinedInputClasses.notchedOutline`. ```javascript +import { outlinedInputClasses } from '@mui/material/OutlinedInput'; const theme = createTheme({ components: { MuiOutlinedInput: { styleOverrides: { root: { - '& .MuiOutlinedInput-notchedOutline': { + [`& .${outlinedInputClasses.notchedOutline}`]: { borderWidth: 1, } } } } } }); ``` -------------------------------- ### Install Vite Pigment CSS plugin Source: https://v6.mui.com/material-ui/migration/migrating-to-pigment-css Add the Pigment CSS plugin as a development dependency for Vite projects. ```bash npm install --save-dev @pigment-css/vite-plugin ``` -------------------------------- ### Custom Font Configuration Source: https://v6.mui.com/material-ui/icons Example of importing a custom font variant and configuring the Icon component to use it. ```javascript import Icon from '@mui/material/Icon'; ; ``` -------------------------------- ### Basic Popper Implementation Source: https://v6.mui.com/material-ui/react-popper A simple example showing how to toggle a Popper component using an anchor element. ```jsx The content of the Popper. ``` -------------------------------- ### Install tss-react dependencies Source: https://v6.mui.com/material-ui/migration/migrating-from-jss Commands to add tss-react to your project using npm or yarn. ```bash npm install tss-react ``` ```bash yarn add tss-react ``` -------------------------------- ### Install Next.js Pigment CSS plugin Source: https://v6.mui.com/material-ui/migration/migrating-to-pigment-css Add the Pigment CSS plugin as a development dependency for Next.js projects. ```bash npm install --save-dev @pigment-css/nextjs-plugin ``` -------------------------------- ### theme.breakpoints.between(start, end) Source: https://v6.mui.com/material-ui/customization/breakpoints Generates a media query that matches screen widths between two specified breakpoints. ```APIDOC ## theme.breakpoints.between(start, end) ### Description Returns a media query string that matches screen widths greater than the screen size given by the first argument (inclusive) and less than the screen size given by the second argument (exclusive). ### Arguments - **start** (string | number) - Required - A breakpoint key or screen width in px. - **end** (string | number) - Required - A breakpoint key or screen width in px. ### Returns - **media query** (string) - A media query string. ``` -------------------------------- ### Apply nonce to CSP header Source: https://v6.mui.com/material-ui/guides/content-security-policy Example of setting the CSP header with the generated nonce. ```javascript header('Content-Security-Policy').set( `default-src 'self'; style-src 'self' 'nonce-${nonce}';`, ); ``` -------------------------------- ### Default Class Name Generation Example Source: https://v6.mui.com/material-ui/experimental-api/classname-generator Illustrates the default HTML output for a Button component, showing the generated class names. ```html ``` -------------------------------- ### Customize component styles with classes Source: https://v6.mui.com/material-ui/guides/api Example of defining a style object with root and variant-specific classes for component customization. ```javascript const styles = { root: { color: green[600], '&$checked': { color: green[500], }, }, checked: {}, }; ``` -------------------------------- ### Generate MuiSwitch theme overrides Source: https://v6.mui.com/material-ui/design-resources/material-ui-sync Example of theme configuration generated by the Sync plugin to replicate an iOS-style Switch component. ```javascript { components: { MuiSwitch: { styleOverrides: { root: { '&.MuiSwitch-sizeMedium:has(.MuiSwitch-colorPrimary)': { '&:has(.Mui-checked):not(:has(.Mui-disabled)):not(:has(.Mui-focusVisible))': { width: '40px', height: '21px', padding: '0', '& .MuiSwitch-switchBase': { transform: 'translateX(19px) translateY(2px)', padding: '0', '& .MuiSwitch-thumb': { width: '17px', height: '17px', background: '#FAFAFA', }, '& + .MuiSwitch-track': { width: '38px', height: '21px', background: 'var(--mui-palette-success-light)', opacity: '1', }, }, }, }, }, }, }, }, } ``` -------------------------------- ### Basic Button Component Usage Source: https://v6.mui.com/material-ui/getting-started/learn A minimal example demonstrating the implementation of a contained Button component within a React application. ```javascript import * as React from 'react'; import Button from '@mui/material/Button'; export default function ButtonUsage() { return ; } ``` -------------------------------- ### Fast track project creation Source: https://v6.mui.com/material-ui/experimental-api/pigment-css Use these commands to quickly scaffold a new Pigment CSS project. ```bash curl https://codeload.github.com/mui/pigment-css/tar.gz/master | tar -xz --strip=2 pigment-css-master/examples/pigment-css-nextjs-ts cd pigment-css-nextjs-ts ``` -------------------------------- ### Example of an XSS vulnerability Source: https://v6.mui.com/material-ui/guides/content-security-policy An example of malicious script injection that a CSP header would block. ```html ``` -------------------------------- ### Setup ClassNameGenerator Source: https://v6.mui.com/material-ui/experimental-api/classname-generator Basic setup for the ClassNameGenerator API. This file should be imported before any Material UI component imports. ```javascript // create a new file called `MuiClassNameSetup.js` at the root or src folder. import { unstable_ClassNameGenerator as ClassNameGenerator } from '@mui/material/className'; ClassNameGenerator.configure( // Do something with the componentName (componentName) => componentName, ); ``` -------------------------------- ### Implement a basic media query with useMediaQuery Source: https://v6.mui.com/material-ui/react-use-media-query Demonstrates using the hook to detect if the viewport width is at least 600px. ```javascript import * as React from 'react'; import useMediaQuery from '@mui/material/useMediaQuery'; export default function SimpleMediaQuery() { const matches = useMediaQuery('(min-width:600px)'); return {`(min-width:600px) matches: ${matches}`}; } ``` -------------------------------- ### Install Roboto font via npm Source: https://v6.mui.com/material-ui/react-typography Use this command to install the Roboto font package via npm. ```bash npm install @fontsource/roboto ``` -------------------------------- ### Basic Menu Implementation Source: https://v6.mui.com/material-ui/react-menu Demonstrates a standard menu that opens over an anchor element and closes upon selection. ```javascript import * as React from 'react'; import Button from '@mui/material/Button'; import Menu from '@mui/material/Menu'; import MenuItem from '@mui/material/MenuItem'; export default function BasicMenu() { const [anchorEl, setAnchorEl] = React.useState(null); const open = Boolean(anchorEl); const handleClick = (event) => { setAnchorEl(event.currentTarget); }; const handleClose = () => { setAnchorEl(null); }; return (
Profile My account Logout
); } ``` ```typescript import * as React from 'react'; import Button from '@mui/material/Button'; import Menu from '@mui/material/Menu'; import MenuItem from '@mui/material/MenuItem'; export default function BasicMenu() { const [anchorEl, setAnchorEl] = React.useState(null); const open = Boolean(anchorEl); const handleClick = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget); }; const handleClose = () => { setAnchorEl(null); }; return (
Profile My account Logout
); } ``` -------------------------------- ### theme.transitions.create(props, options) Source: https://v6.mui.com/material-ui/customization/transitions Creates a CSS transition string based on the provided properties and optional configuration for duration, easing, and delay. ```APIDOC ## theme.transitions.create(props, options) ### Description Generates a CSS transition value that composes the specified CSS properties with defined duration, easing, and delay. ### Arguments - **props** (string | string[]) - Required - A CSS property or list of properties to transition. Defaults to ['all']. - **options** (object) - Optional - Configuration object. - **options.duration** (string | number) - Optional - Duration of the transition. Defaults to theme.transitions.duration.standard. - **options.easing** (string) - Optional - Easing function for the transition. Defaults to theme.transitions.easing.easeInOut. - **options.delay** (string | number) - Optional - Delay for the transition. Defaults to 0. ### Returns - **transition** (string) - A CSS transition value. ### Example ```javascript theme.transitions.create(['background-color', 'transform']); ``` ``` -------------------------------- ### Basic Link Usage Source: https://v6.mui.com/material-ui/react-link Demonstrates standard link implementation and the use of color and variant props. ```jsx Link {'color="inherit"'} {'variant="body2"'} ``` -------------------------------- ### Initialize color scheme script Source: https://v6.mui.com/material-ui/customization/css-theme-variables/configuration Add InitColorSchemeScript to the root layout to ensure correct color scheme application. ```javascript createTheme({ cssVariables: { cssVarPrefix: 'any' } }) ``` ```javascript import InitColorSchemeScript from '@mui/material/InitColorSchemeScript'; export default function RootLayout(props) { return ( {/* must come before the
element */}
{children}
); } ``` -------------------------------- ### Enable CSS theme variables Source: https://v6.mui.com/material-ui/customization/css-theme-variables/usage Initialize the theme with cssVariables set to true and wrap the application with ThemeProvider. ```jsx import { ThemeProvider, createTheme } from '@mui/material/styles'; const theme = createTheme({ cssVariables: true }); function App() { return {/* ...your app */}; } ``` -------------------------------- ### Implement useRadioGroup example Source: https://v6.mui.com/material-ui/react-radio-button Demonstrates using the RadioGroup component with custom FormControlLabel components. ```jsx } /> } /> ``` -------------------------------- ### Polyfill matchMedia for Testing Source: https://v6.mui.com/material-ui/react-use-media-query Use css-mediaquery to emulate matchMedia in environments like jsdom that do not support it natively. ```javascript import mediaQuery from 'css-mediaquery'; function createMatchMedia(width) { return (query) => ({ matches: mediaQuery.match(query, { width, }), addEventListener: () => {}, removeEventListener: () => {}, }); } describe('MyTests', () => { beforeAll(() => { window.matchMedia = createMatchMedia(window.innerWidth); }); }); ``` -------------------------------- ### Accessibility configurations Source: https://v6.mui.com/material-ui/react-divider Examples for hiding the divider from screen readers or using it as a container for text. ```jsx