-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[charts] HTML Labels #15813
[charts] HTML Labels #15813
Changes from all commits
986d94d
3a60f62
7f3113c
8dae344
11a9f05
f6c7c4c
84fc162
1ee30cb
dbfb95f
0d4524d
e62716a
bd9acfa
f4c97e0
7f305e2
6707594
1656a23
3b34b6d
1fb08dc
312474d
5788e4d
437f099
436b169
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as React from 'react'; | ||
import { createRenderer } from '@mui/internal-test-utils/createRenderer'; | ||
import { describeConformance } from 'test/utils/describeConformance'; | ||
import { ChartsLabel } from '@mui/x-charts/ChartsLabel/ChartsLabel'; | ||
import { labelClasses } from '@mui/x-charts/ChartsLabel/labelClasses'; | ||
import { createTheme, ThemeProvider } from '@mui/material/styles'; | ||
|
||
describe('<ChartsLabel />', () => { | ||
const { render } = createRenderer(); | ||
|
||
describeConformance(<ChartsLabel />, () => ({ | ||
classes: labelClasses, | ||
inheritComponent: 'div', | ||
render, | ||
muiName: 'MuiChartsLabel', | ||
testComponentPropWith: 'div', | ||
refInstanceof: window.HTMLSpanElement, | ||
ThemeProvider, | ||
createTheme, | ||
// SKIP | ||
skip: ['themeVariants', 'componentProp', 'componentsProp'], | ||
})); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { styled, SxProps, Theme } from '@mui/material/styles'; | ||
import clsx from 'clsx'; | ||
import { ChartsLabelClasses, useUtilityClasses } from './labelClasses'; | ||
import { consumeThemeProps } from '../internals/consumeThemeProps'; | ||
|
||
export interface ChartsLabelProps { | ||
/** | ||
* Override or extend the styles applied to the component. | ||
*/ | ||
classes?: Partial<ChartsLabelClasses>; | ||
children?: React.ReactNode; | ||
className?: string; | ||
sx?: SxProps<Theme>; | ||
} | ||
|
||
const Root = styled('span', { | ||
name: 'MuiChartsLabel', | ||
slot: 'Root', | ||
overridesResolver: (props, styles) => styles.root, | ||
})<{ ownerState: ChartsLabelProps }>(({ theme }) => ({ | ||
...theme.typography.caption, | ||
color: (theme.vars || theme).palette.text.primary, | ||
lineHeight: undefined, | ||
display: 'flex', | ||
})); | ||
|
||
/** | ||
* @ignore - internal component. | ||
* | ||
* Generates the label mark for the tooltip and legend. | ||
*/ | ||
const ChartsLabel = consumeThemeProps( | ||
'MuiChartsLabel', | ||
{ | ||
classesResolver: useUtilityClasses, | ||
}, | ||
function ChartsLabel(props: ChartsLabelProps, ref: React.Ref<HTMLSpanElement>) { | ||
const { children, className, classes, ...other } = props; | ||
|
||
return ( | ||
<Root className={clsx(classes?.root, className)} ownerState={props} ref={ref} {...other}> | ||
{children} | ||
</Root> | ||
); | ||
}, | ||
); | ||
|
||
ChartsLabel.propTypes = { | ||
// ----------------------------- Warning -------------------------------- | ||
// | These PropTypes are generated from the TypeScript type definitions | | ||
// | To update them edit the TypeScript types and run "pnpm proptypes" | | ||
// ---------------------------------------------------------------------- | ||
children: PropTypes.node, | ||
/** | ||
* Override or extend the styles applied to the component. | ||
*/ | ||
classes: PropTypes.object, | ||
} as any; | ||
|
||
export { ChartsLabel }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import * as React from 'react'; | ||
import { createRenderer } from '@mui/internal-test-utils/createRenderer'; | ||
import { describeConformance } from 'test/utils/describeConformance'; | ||
import { createTheme, ThemeProvider } from '@mui/material/styles'; | ||
import { ChartsLabelGradient } from '@mui/x-charts/ChartsLabel/ChartsLabelGradient'; | ||
import { labelGradientClasses } from '@mui/x-charts/ChartsLabel/labelGradientClasses'; | ||
|
||
describe('<ChartsLabelGradient />', () => { | ||
const { render } = createRenderer(); | ||
|
||
describeConformance(<ChartsLabelGradient gradientId="ChartsLabelGradient.test-id" />, () => ({ | ||
classes: labelGradientClasses, | ||
inheritComponent: 'div', | ||
render: (node) => | ||
render(node, { | ||
wrapper: ({ children }) => ( | ||
<React.Fragment> | ||
{children} | ||
<Gradient id="ChartsLabelGradient.test-id" /> | ||
</React.Fragment> | ||
), | ||
}), | ||
muiName: 'MuiChartsLabelGradient', | ||
testComponentPropWith: 'div', | ||
refInstanceof: window.HTMLDivElement, | ||
ThemeProvider, | ||
createTheme, | ||
// SKIP | ||
skip: ['themeVariants', 'componentProp', 'componentsProp'], | ||
})); | ||
}); | ||
|
||
function Gradient({ id }: any) { | ||
return ( | ||
<svg width="0" height="0" viewBox="0 0 0 0" fill="none" xmlns="http://www.w3.org/2000/svg"> | ||
<defs> | ||
<linearGradient id={id} x1="0" y1="0" x2="1" y2="0" gradientUnits="objectBoundingBox"> | ||
<stop offset="0" stopColor="#CAD4EE" /> | ||
<stop offset="0.5" stopColor="#4254FB" /> | ||
<stop offset="1" stopColor="#091159" /> | ||
</linearGradient> | ||
</defs> | ||
</svg> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { styled, SxProps, Theme } from '@mui/material/styles'; | ||
import clsx from 'clsx'; | ||
import { | ||
ChartsLabelGradientClasses, | ||
useUtilityClasses, | ||
labelGradientClasses, | ||
} from './labelGradientClasses'; | ||
import { consumeThemeProps } from '../internals/consumeThemeProps'; | ||
|
||
export interface ChartsLabelGradientProps { | ||
/** | ||
* A unique identifier for the gradient. | ||
* | ||
* The `gradientId` will be used as `fill="url(#gradientId)"`. | ||
*/ | ||
gradientId: string; | ||
Comment on lines
+14
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WHat about removing this function ChartsLabelGradient(){
const id = useId()
return <svg>
<ChartsContinuousGradientProps id={id}/>
<rect fill={`url(#${id})`} />
</svg>
} Otherwise you get a kind of duplicate information. If you want to switch from row to column, you need to define again the gradient
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried it at first, my main gripe with adding the gradient inside it that this component loses a bit of flexibility. I'll try something new and report back There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can work by removing the hardcoded sizes in the svg and adding some CSS There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated the sandbox |
||
/** | ||
* The direction of the gradient. | ||
* | ||
* @default 'row' | ||
*/ | ||
direction?: 'column' | 'row'; | ||
/** | ||
* If `true`, the gradient will be reversed. | ||
*/ | ||
reverse?: boolean; | ||
/** | ||
* If provided, the gradient will be rotated by 90deg. | ||
* | ||
* Useful for linear gradients that are not in the correct orientation. | ||
*/ | ||
rotate?: boolean; | ||
/** | ||
* Override or extend the styles applied to the component. | ||
*/ | ||
classes?: Partial<ChartsLabelGradientClasses>; | ||
className?: string; | ||
sx?: SxProps<Theme>; | ||
} | ||
|
||
const getRotation = (direction?: 'column' | 'row', reverse?: boolean, rotate?: boolean) => { | ||
if (!rotate && reverse) { | ||
return direction === 'column' ? 90 : 180; | ||
} | ||
|
||
if (rotate && !reverse) { | ||
return direction === 'column' ? 0 : 90; | ||
} | ||
|
||
if (rotate && reverse) { | ||
return direction === 'column' ? 180 : -90; | ||
} | ||
|
||
return direction === 'column' ? -90 : 0; | ||
}; | ||
|
||
const Root = styled('div', { | ||
name: 'MuiChartsLabelGradient', | ||
slot: 'Root', | ||
overridesResolver: (props, styles) => styles.root, | ||
})<{ ownerState: ChartsLabelGradientProps }>(({ ownerState }) => { | ||
const rotation = getRotation(ownerState.direction, ownerState.reverse, ownerState.rotate); | ||
|
||
return { | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
[`.${labelGradientClasses.mask}`]: { | ||
borderRadius: 2, | ||
overflow: 'hidden', | ||
}, | ||
[`&.${labelGradientClasses.row}`]: { | ||
width: '100%', | ||
[`.${labelGradientClasses.mask}`]: { | ||
height: 12, | ||
width: '100%', | ||
}, | ||
}, | ||
[`&.${labelGradientClasses.column}`]: { | ||
height: '100%', | ||
[`.${labelGradientClasses.mask}`]: { | ||
width: 12, | ||
height: '100%', | ||
'> svg': { | ||
height: '100%', | ||
}, | ||
}, | ||
}, | ||
svg: { | ||
transform: `rotate(${rotation}deg)`, | ||
display: 'block', | ||
}, | ||
}; | ||
}); | ||
|
||
/** | ||
* @ignore - internal component. | ||
* | ||
* Generates the label Gradient for the tooltip and legend. | ||
*/ | ||
const ChartsLabelGradient = consumeThemeProps( | ||
'MuiChartsLabelGradient', | ||
{ | ||
defaultProps: { | ||
direction: 'row', | ||
}, | ||
classesResolver: useUtilityClasses, | ||
}, | ||
function ChartsLabelGradient(props: ChartsLabelGradientProps, ref: React.Ref<HTMLDivElement>) { | ||
const { gradientId, direction, classes, className, ...other } = props; | ||
|
||
return ( | ||
<Root | ||
className={clsx(classes?.root, className)} | ||
ownerState={props} | ||
aria-hidden="true" | ||
ref={ref} | ||
{...other} | ||
> | ||
<div className={classes?.mask}> | ||
<svg viewBox="0 0 24 24"> | ||
<rect width="24" height="24" fill={`url(#${gradientId})`} /> | ||
</svg> | ||
</div> | ||
</Root> | ||
); | ||
}, | ||
); | ||
|
||
ChartsLabelGradient.propTypes = { | ||
// ----------------------------- Warning -------------------------------- | ||
// | These PropTypes are generated from the TypeScript type definitions | | ||
// | To update them edit the TypeScript types and run "pnpm proptypes" | | ||
// ---------------------------------------------------------------------- | ||
/** | ||
* Override or extend the styles applied to the component. | ||
*/ | ||
classes: PropTypes.object, | ||
/** | ||
* The direction of the gradient. | ||
* | ||
* @default 'row' | ||
*/ | ||
direction: PropTypes.oneOf(['column', 'row']), | ||
/** | ||
* A unique identifier for the gradient. | ||
* | ||
* The `gradientId` will be used as `fill="url(#gradientId)"`. | ||
*/ | ||
gradientId: PropTypes.string.isRequired, | ||
/** | ||
* If `true`, the gradient will be reversed. | ||
*/ | ||
reverse: PropTypes.bool, | ||
/** | ||
* If provided, the gradient will be rotated by 90deg. | ||
* | ||
* Useful for linear gradients that are not in the correct orientation. | ||
*/ | ||
rotate: PropTypes.bool, | ||
} as any; | ||
|
||
export { ChartsLabelGradient }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as React from 'react'; | ||
import { createRenderer } from '@mui/internal-test-utils/createRenderer'; | ||
import { describeConformance } from 'test/utils/describeConformance'; | ||
import { createTheme, ThemeProvider } from '@mui/material/styles'; | ||
import { ChartsLabelMark } from '@mui/x-charts/ChartsLabel/ChartsLabelMark'; | ||
import { labelMarkClasses } from '@mui/x-charts/ChartsLabel/labelMarkClasses'; | ||
|
||
describe('<ChartsLabelMark />', () => { | ||
const { render } = createRenderer(); | ||
|
||
describeConformance(<ChartsLabelMark />, () => ({ | ||
classes: labelMarkClasses, | ||
inheritComponent: 'div', | ||
render, | ||
muiName: 'MuiChartsLabelMark', | ||
testComponentPropWith: 'div', | ||
refInstanceof: window.HTMLDivElement, | ||
ThemeProvider, | ||
createTheme, | ||
// SKIP | ||
skip: ['themeVariants', 'componentProp', 'componentsProp'], | ||
})); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We currently use theme colors for this, but the designs suggest other colors. I'm not sure how to proceed. If we follow the design, where do these colors come from?
https://www.figma.com/design/upttiqJUQYGUu3xo6w7COx/Design-refinement?node-id=45-334&node-type=frame&t=kteCWeOvyprNETmc-0