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

Migrate 'LegendWidgetUI' component from makeStyles to styled-component + cleanup #637

Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Storybook documentation and fixes [#629](https://github.com/CartoDB/carto-react/pull/629)
- Note component cleaned styles from makeStyles [#630](https://github.com/CartoDB/carto-react/pull/630)
- OpacityControl component migrated from makeStyles to styled-components + cleanup [#631](https://github.com/CartoDB/carto-react/pull/631)
- LegendWidgetUI component migrated from makeStyles to styled-components + cleanup [#637](https://github.com/CartoDB/carto-react/pull/637)

### 2.0.0 (2023-04-05)

Expand Down
80 changes: 34 additions & 46 deletions packages/react-ui/src/widgets/legend/LegendWidgetUI.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { createRef, Fragment } from 'react';
import { Box, Button, Collapse, Divider, Grid, SvgIcon } from '@mui/material';
import makeStyles from '@mui/styles/makeStyles';
import { Box, Button, Collapse, Divider, Grid, styled, SvgIcon } from '@mui/material';
import LegendWrapper from './LegendWrapper';
import LegendCategories from './LegendCategories';
import LegendIcon from './LegendIcon';
Expand Down Expand Up @@ -28,13 +27,11 @@ const LayersIcon = () => (
</SvgIcon>
);

const useStyles = makeStyles((theme) => ({
legend: {
minWidth: '240px',
backgroundColor: theme.palette.background.paper,
boxShadow: theme.shadows[1],
borderRadius: 4
}
const LegendBox = styled(Box)(({ theme }) => ({
minWidth: theme.spacing(30),
backgroundColor: theme.palette.background.paper,
boxShadow: theme.shadows[1],
borderRadius: theme.spacing(0.5)
}));

function LegendWidgetUI({
Expand All @@ -49,11 +46,10 @@ function LegendWidgetUI({
onChangeLegendRowCollapsed,
title
}) {
const classes = useStyles();
const isSingle = layers.length === 1;

return (
<Box className={`${classes.legend} ${className}`}>
<LegendBox className={className}>
<LegendContainer
isSingle={isSingle}
collapsed={collapsed}
Expand All @@ -69,7 +65,7 @@ function LegendWidgetUI({
onChangeCollapsed={onChangeLegendRowCollapsed}
/>
</LegendContainer>
</Box>
</LegendBox>
);
}

Expand All @@ -96,22 +92,25 @@ LegendWidgetUI.propTypes = {

export default LegendWidgetUI;

const useStylesLegendContainer = makeStyles((theme) => ({
header: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
height: '36px'
},
button: {
flex: '1 1 auto',
justifyContent: 'space-between',
padding: theme.spacing(0.75, 1.25, 0.75, 3),
borderTop: ({ collapsed }) =>
collapsed ? 'none' : `1px solid ${theme.palette.divider}`,
cursor: 'pointer'
},
wrapperInner: {
const Header = styled(Grid)(({ theme }) => ({
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
height: theme.spacing(4.5)
}));

const HeaderButton = styled(Button, {
shouldForwardProp: (prop) => prop !== 'collapsed'
})(({ theme, collapsed }) => ({
flex: '1 1 auto',
justifyContent: 'space-between',
padding: theme.spacing(0.75, 1.25, 0.75, 3),
borderTop: collapsed ? 'none' : `1px solid ${theme.palette.divider}`,
cursor: 'pointer'
}));

const CollapseWrapper = styled(Collapse)(() => ({
'.MuiCollapse-wrapperInner': {
maxHeight: 'max(350px, 80vh)',
overflowY: 'auto',
overflowX: 'hidden'
Expand All @@ -120,9 +119,6 @@ const useStylesLegendContainer = makeStyles((theme) => ({

function LegendContainer({ isSingle, children, collapsed, onChangeCollapsed, title }) {
const wrapper = createRef();
const classes = useStylesLegendContainer({
collapsed
});

const handleExpandClick = () => {
if (onChangeCollapsed) onChangeCollapsed(!collapsed);
Expand All @@ -132,26 +128,18 @@ function LegendContainer({ isSingle, children, collapsed, onChangeCollapsed, tit
children
) : (
<>
<Collapse
ref={wrapper}
in={!collapsed}
timeout='auto'
unmountOnExit
classes={{
wrapperInner: classes.wrapperInner
}}
>
<CollapseWrapper ref={wrapper} in={!collapsed} timeout='auto' unmountOnExit>
{children}
</Collapse>
<Grid container className={classes.header}>
<Button
className={classes.button}
</CollapseWrapper>
<Header container>
<HeaderButton
collapsed={collapsed}
endIcon={<LayersIcon />}
onClick={handleExpandClick}
>
<Typography variant='subtitle1'>{title}</Typography>
</Button>
</Grid>
</HeaderButton>
</Header>
</>
);
}
Expand Down