Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
26 changes: 18 additions & 8 deletions packages/mui-material/src/CssBaseline/CssBaseline.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import useThemeProps from '../styles/useThemeProps';
import GlobalStyles from '../GlobalStyles';
import { createUseThemeProps, globalCss } from '../zero-styled';

const useThemeProps = createUseThemeProps('MuiCssBaseline');

// `ecs` stands for enableColorScheme. This is internal logic to make it work with Pigment CSS, so shorter is better.
const SELECTOR = 'mui-ecs';

export const html = (theme, enableColorScheme) => ({
WebkitFontSmoothing: 'antialiased', // Antialiasing.
Expand All @@ -13,7 +17,8 @@ export const html = (theme, enableColorScheme) => ({
// Fix font resize problem in iOS
WebkitTextSizeAdjust: '100%',
// When used under CssVarsProvider, colorScheme should not be applied dynamically because it will generate the stylesheet twice for server-rendered applications.
...(enableColorScheme && !theme.vars && { colorScheme: theme.palette.mode }),
...(enableColorScheme &&
!theme.vars && { [`&:has(.${SELECTOR})`]: { colorScheme: theme.palette.mode } }),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad we bump safari to 15.4, otherwise I have no clue how to make it work 😊

});

export const body = (theme) => ({
Expand All @@ -26,17 +31,19 @@ export const body = (theme) => ({
},
});

export const styles = (theme, enableColorScheme = false) => {
export const styles = (theme) => {
const colorSchemeStyles = {};
if (enableColorScheme && theme.colorSchemes) {
if (theme.colorSchemes) {
Object.entries(theme.colorSchemes).forEach(([key, scheme]) => {
colorSchemeStyles[theme.getColorSchemeSelector(key).replace(/\s*&/, '')] = {
colorScheme: scheme.palette?.mode,
[`&:has(.${SELECTOR})`]: {
colorScheme: scheme.palette?.mode,
},
};
});
}
let defaultStyles = {
html: html(theme, enableColorScheme),
html: html(theme, true),
'*, *::before, *::after': {
boxSizing: 'inherit',
},
Expand All @@ -63,6 +70,8 @@ export const styles = (theme, enableColorScheme = false) => {
return defaultStyles;
};

const GlobalStyles = globalCss(({ theme }) => styles(theme));

/**
* Kickstart an elegant, consistent, and simple baseline to build upon.
*/
Expand All @@ -71,7 +80,8 @@ function CssBaseline(inProps) {
const { children, enableColorScheme = false } = props;
return (
<React.Fragment>
<GlobalStyles styles={(theme) => styles(theme, enableColorScheme)} />
<GlobalStyles />
{enableColorScheme && <span className={SELECTOR} style={{ display: 'none' }} />}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What’s the need for enableColorScheme: false?
In other words, why would anyone want the browser to show light color scheme for a dark theme?

Having a hanging span element is another freak (break of principle of least surprise).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good argument, I think we should revisit in v7. For v6 we try to keep the breaking changes minimal, so I wouldn't just the API just yet, only add the opt in support for Pigment CSS.

Copy link
Member

@oliviertassinari oliviertassinari Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do the opposite here? Only a DOM node if the prop is false?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, done.

{children}
</React.Fragment>
);
Expand Down
13 changes: 8 additions & 5 deletions packages/mui-material/src/Popper/Popper.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use client';
import { Popper as BasePopper, PopperProps as BasePopperProps } from '@mui/base/Popper';
import { Direction, SxProps } from '@mui/system';
import useTheme from '@mui/system/useThemeWithoutDefault';
import { SxProps } from '@mui/system';
import { useRtl } from '@mui/system/RtlProvider';
import refType from '@mui/utils/refType';
import HTMLElementType from '@mui/utils/HTMLElementType';
import PropTypes from 'prop-types';
import * as React from 'react';
import { styled, Theme, useThemeProps } from '../styles';
import { Theme } from '../styles';
import { styled, createUseThemeProps } from '../zero-styled';

const useThemeProps = createUseThemeProps('MuiPopper');

export interface PopperProps extends Omit<BasePopperProps, 'direction'> {
/**
Expand Down Expand Up @@ -59,7 +62,7 @@ const Popper = React.forwardRef(function Popper(
inProps: PopperProps,
ref: React.ForwardedRef<HTMLDivElement>,
) {
const theme = useTheme<{ direction?: Direction }>();
const isRtl = useRtl();
const props = useThemeProps({
props: inProps,
name: 'MuiPopper',
Expand Down Expand Up @@ -101,7 +104,7 @@ const Popper = React.forwardRef(function Popper(
return (
<PopperRoot
as={component}
direction={theme?.direction}
direction={isRtl ? 'rtl' : 'ltr'}
slots={{ root: RootComponent }}
slotProps={slotProps ?? componentsProps}
{...otherProps}
Expand Down
18 changes: 12 additions & 6 deletions packages/mui-material/src/ScopedCssBaseline/ScopedCssBaseline.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import composeClasses from '@mui/utils/composeClasses';
import useThemeProps from '../styles/useThemeProps';
import styled from '../styles/styled';
import { styled, createUseThemeProps } from '../zero-styled';
import { html, body } from '../CssBaseline/CssBaseline';
import { getScopedCssBaselineUtilityClass } from './scopedCssBaselineClasses';

const useThemeProps = createUseThemeProps('MuiScopedCssBaseline');

const useUtilityClasses = (ownerState) => {
const { classes } = ownerState;

Expand All @@ -22,25 +23,30 @@ const ScopedCssBaselineRoot = styled('div', {
name: 'MuiScopedCssBaseline',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})(({ theme, ownerState }) => {
})(({ theme }) => {
const colorSchemeStyles = {};
if (ownerState.enableColorScheme && theme.colorSchemes) {
if (theme.colorSchemes) {
Object.entries(theme.colorSchemes).forEach(([key, scheme]) => {
colorSchemeStyles[`&${theme.getColorSchemeSelector(key).replace(/\s*&/, '')}`] = {
colorScheme: scheme.palette?.mode,
};
});
}
return {
...html(theme, ownerState.enableColorScheme),
...html(theme, false),
...body(theme),
'& *, & *::before, & *::after': {
boxSizing: 'inherit',
},
'& strong, & b': {
fontWeight: theme.typography.fontWeightBold,
},
...colorSchemeStyles,
variants: [
{
props: { enableColorScheme: true },
style: theme.vars ? colorSchemeStyles : { colorScheme: theme.palette.mode },
},
],
};
});

Expand Down
9 changes: 6 additions & 3 deletions packages/mui-material/src/zero-styled/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import * as React from 'react';
import { extendSxProp } from '@mui/system/styleFunctionSx';
import useThemeProps from '../styles/useThemeProps';
import useTheme from '../styles/useTheme';
import GlobalStyles, { GlobalStylesProps } from '../GlobalStyles';
import GlobalStyles from '../GlobalStyles';

export { css, keyframes } from '@mui/system';

export { default as styled } from '../styles/styled';

export function globalCss(styles: GlobalStylesProps['styles']) {
export function globalCss(styles: any) {
return function GlobalStylesWrapper() {
return <GlobalStyles styles={styles} />;
return (
// Pigment CSS `globalCss` support callback with theme inside an object but `GlobalStyles` support theme as a callback value.
<GlobalStyles styles={typeof styles === 'function' ? (theme) => styles({ theme }) : styles} />
);
};
}

Expand Down