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

[material-ui] Convert LinearProgress to support Pigment CSS #41816

Merged
merged 8 commits into from
Apr 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const CircularProgressRoot = styled('span', {
},
style:
// For Styled-components v4+: https://github.com/styled-components/styled-components/blob/main/packages/styled-components/src/utils/errors.md#12
rotateAnimation !== 'string'
typeof rotateAnimation !== 'string'
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
? rotateAnimation
: {
animation: `${circularRotateKeyframe} 1.4s linear infinite`,
Expand Down
300 changes: 199 additions & 101 deletions packages/mui-material/src/LinearProgress/LinearProgress.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import composeClasses from '@mui/utils/composeClasses';
import { keyframes, css } from '@mui/system';
import { darken, lighten } from '@mui/system/colorManipulator';
import { useRtl } from '@mui/system/RtlProvider';
import { keyframes, css, styled, createUseThemeProps } from '../zero-styled';
import capitalize from '../utils/capitalize';
import styled from '../styles/styled';
import useThemeProps from '../styles/useThemeProps';
import { getLinearProgressUtilityClass } from './linearProgressClasses';

const useThemeProps = createUseThemeProps('MuiLinearProgress');

const TRANSITION_DURATION = 4; // seconds
const indeterminate1Keyframe = keyframes`
0% {
Expand All @@ -28,6 +28,9 @@ const indeterminate1Keyframe = keyframes`
right: -90%;
}
`;
const indeterminate1Animation = css`
animation: ${indeterminate1Keyframe} 2.1s cubic-bezier(0.65, 0.815, 0.735, 0.395) infinite;
`;

const indeterminate2Keyframe = keyframes`
0% {
Expand All @@ -45,6 +48,9 @@ const indeterminate2Keyframe = keyframes`
right: -8%;
}
`;
const indeterminate2Animation = css`
animation: ${indeterminate2Keyframe} 2.1s cubic-bezier(0.165, 0.84, 0.44, 1) 1.15s infinite;
`;

const bufferKeyframe = keyframes`
0% {
Expand All @@ -62,6 +68,9 @@ const bufferKeyframe = keyframes`
background-position: -200px -23px;
}
`;
const bufferAnimation = css`
animation: ${bufferKeyframe} 3s infinite linear;
`;

const useUtilityClasses = (ownerState) => {
const { classes, variant, color } = ownerState;
Expand Down Expand Up @@ -89,9 +98,6 @@ const useUtilityClasses = (ownerState) => {
};

const getColorShade = (theme, color) => {
if (color === 'inherit') {
return 'currentColor';
}
if (theme.vars) {
return theme.vars.palette.LinearProgress[`${color}Bg`];
}
Expand All @@ -112,32 +118,50 @@ const LinearProgressRoot = styled('span', {
styles[ownerState.variant],
];
},
})(({ ownerState, theme }) => ({
})(({ theme }) => ({
position: 'relative',
overflow: 'hidden',
display: 'block',
height: 4,
zIndex: 0, // Fix Safari's bug during composition of different paint.
// Fix Safari's bug during composition of different paint.
zIndex: 0,
'@media print': {
colorAdjust: 'exact',
},
backgroundColor: getColorShade(theme, ownerState.color),
...(ownerState.color === 'inherit' &&
ownerState.variant !== 'buffer' && {
backgroundColor: 'none',
'&::before': {
content: '""',
position: 'absolute',
left: 0,
top: 0,
right: 0,
bottom: 0,
backgroundColor: 'currentColor',
opacity: 0.3,
variants: [
...Object.entries(theme.palette)
.filter(([, value]) => value.main)
.map(([color]) => ({
props: { color },
style: {
// eslint-disable-next-line no-nested-ternary
backgroundColor: getColorShade(theme, color),
},
})),
{
props: ({ ownerState }) => ownerState.color === 'inherit' && ownerState.variant !== 'buffer',
style: {
'&::before': {
content: '""',
position: 'absolute',
left: 0,
top: 0,
right: 0,
bottom: 0,
backgroundColor: 'currentColor',
opacity: 0.3,
},
},
}),
...(ownerState.variant === 'buffer' && { backgroundColor: 'transparent' }),
...(ownerState.variant === 'query' && { transform: 'rotate(180deg)' }),
},
{
props: { variant: 'buffer' },
style: { backgroundColor: 'transparent' },
},
{
props: { variant: 'query' },
style: { transform: 'rotate(180deg)' },
},
],
}));

const LinearProgressDashed = styled('span', {
Expand All @@ -149,25 +173,35 @@ const LinearProgressDashed = styled('span', {
return [styles.dashed, styles[`dashedColor${capitalize(ownerState.color)}`]];
},
})(
({ ownerState, theme }) => {
const backgroundColor = getColorShade(theme, ownerState.color);

return {
position: 'absolute',
marginTop: 0,
height: '100%',
width: '100%',
...(ownerState.color === 'inherit' && {
opacity: 0.3,
}),
backgroundImage: `radial-gradient(${backgroundColor} 0%, ${backgroundColor} 16%, transparent 42%)`,
backgroundSize: '10px 10px',
backgroundPosition: '0 -23px',
};
},
css`
animation: ${bufferKeyframe} 3s infinite linear;
`,
({ theme }) => ({
position: 'absolute',
marginTop: 0,
height: '100%',
width: '100%',
backgroundSize: '10px 10px',
backgroundPosition: '0 -23px',
variants: [
{
props: { color: 'inherit' },
style: {
opacity: 0.3,
backgroundImage: `radial-gradient(currentColor 0%, currentColor 16%, transparent 42%)`,
},
},
...Object.entries(theme.palette)
.filter(([, value]) => value.main)
.map(([color]) => {
const backgroundColor = getColorShade(theme, color);
return {
props: { color },
style: {
backgroundImage: `radial-gradient(${backgroundColor} 0%, ${backgroundColor} 16%, transparent 42%)`,
},
};
}),
],
}),
typeof bufferAnimation !== 'string' && bufferAnimation,
);

const LinearProgressBar1 = styled('span', {
Expand All @@ -185,34 +219,68 @@ const LinearProgressBar1 = styled('span', {
ownerState.variant === 'buffer' && styles.bar1Buffer,
];
},
})(
({ ownerState, theme }) => ({
width: '100%',
position: 'absolute',
left: 0,
bottom: 0,
top: 0,
transition: 'transform 0.2s linear',
transformOrigin: 'left',
backgroundColor:
ownerState.color === 'inherit'
? 'currentColor'
: (theme.vars || theme).palette[ownerState.color].main,
...(ownerState.variant === 'determinate' && {
transition: `transform .${TRANSITION_DURATION}s linear`,
}),
...(ownerState.variant === 'buffer' && {
zIndex: 1,
transition: `transform .${TRANSITION_DURATION}s linear`,
}),
}),
({ ownerState }) =>
(ownerState.variant === 'indeterminate' || ownerState.variant === 'query') &&
css`
width: auto;
animation: ${indeterminate1Keyframe} 2.1s cubic-bezier(0.65, 0.815, 0.735, 0.395) infinite;
`,
);
})(({ theme }) => ({
width: '100%',
position: 'absolute',
left: 0,
bottom: 0,
top: 0,
transition: 'transform 0.2s linear',
transformOrigin: 'left',
variants: [
{
props: {
color: 'inherit',
},
style: {
backgroundColor: 'currentColor',
},
},
...Object.entries(theme.palette)
.filter(([, value]) => value.main)
.map(([color]) => ({
props: { color },
style: {
backgroundColor: (theme.vars || theme).palette[color].main,
},
})),
{
props: {
variant: 'determinate',
},
style: {
transition: `transform .${TRANSITION_DURATION}s linear`,
},
},
{
props: {
variant: 'buffer',
},
style: {
zIndex: 1,
transition: `transform .${TRANSITION_DURATION}s linear`,
},
},
{
props: ({ ownerState }) =>
ownerState.variant === 'indeterminate' || ownerState.variant === 'query',
style: {
width: 'auto',
},
},
{
props: ({ ownerState }) =>
ownerState.variant === 'indeterminate' || ownerState.variant === 'query',
style:
// For Styled-components v4+: https://github.com/styled-components/styled-components/blob/main/packages/styled-components/src/utils/errors.md#12
typeof indeterminate1Animation !== 'string'
? indeterminate1Animation
: {
animation: `${indeterminate1Keyframe} 2.1s cubic-bezier(0.65, 0.815, 0.735, 0.395) infinite`,
},
},
],
}));

const LinearProgressBar2 = styled('span', {
name: 'MuiLinearProgress',
Expand All @@ -228,36 +296,66 @@ const LinearProgressBar2 = styled('span', {
ownerState.variant === 'buffer' && styles.bar2Buffer,
];
},
})(
({ ownerState, theme }) => ({
width: '100%',
position: 'absolute',
left: 0,
bottom: 0,
top: 0,
transition: 'transform 0.2s linear',
transformOrigin: 'left',
...(ownerState.variant !== 'buffer' && {
backgroundColor:
ownerState.color === 'inherit'
? 'currentColor'
: (theme.vars || theme).palette[ownerState.color].main,
}),
...(ownerState.color === 'inherit' && {
opacity: 0.3,
}),
...(ownerState.variant === 'buffer' && {
backgroundColor: getColorShade(theme, ownerState.color),
transition: `transform .${TRANSITION_DURATION}s linear`,
}),
}),
({ ownerState }) =>
(ownerState.variant === 'indeterminate' || ownerState.variant === 'query') &&
css`
width: auto;
animation: ${indeterminate2Keyframe} 2.1s cubic-bezier(0.165, 0.84, 0.44, 1) 1.15s infinite;
`,
);
})(({ theme }) => ({
width: '100%',
position: 'absolute',
left: 0,
bottom: 0,
top: 0,
transition: 'transform 0.2s linear',
transformOrigin: 'left',
variants: [
...Object.entries(theme.palette)
.filter(([, value]) => value.main)
.map(([color]) => ({
props: { color },
style: {
'--LinearProgressBar2-barColor': (theme.vars || theme).palette[color].main,
},
})),
{
props: ({ ownerState }) => ownerState.variant !== 'buffer' && ownerState.color !== 'inherit',
style: {
backgroundColor: 'var(--LinearProgressBar2-barColor, currentColor)',
},
},
{
props: {
color: 'inherit',
},
style: {
opacity: 0.3,
},
},
...Object.entries(theme.palette)
.filter(([, value]) => value.main)
.map(([color]) => ({
props: { color, variant: 'buffer' },
style: {
backgroundColor: getColorShade(theme, color),
transition: `transform .${TRANSITION_DURATION}s linear`,
},
})),
{
props: ({ ownerState }) =>
ownerState.variant === 'indeterminate' || ownerState.variant === 'query',
style: {
width: 'auto',
},
},
{
props: ({ ownerState }) =>
ownerState.variant === 'indeterminate' || ownerState.variant === 'query',
style:
// For Styled-components v4+: https://github.com/styled-components/styled-components/blob/main/packages/styled-components/src/utils/errors.md#12
typeof indeterminate2Animation !== 'string'
? indeterminate2Animation
: {
animation: `${indeterminate2Keyframe} 2.1s cubic-bezier(0.165, 0.84, 0.44, 1) 1.15s infinite`,
},
},
],
}));

/**
* ## ARIA
Expand Down
Loading