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

LegendCategories: add maskedMarkers that allows disabling mask icons #473

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Not released

- LegendCategories: maskedMarkers flag that allows disabling mask icons [#473]((https://github.com/CartoDB/carto-react/pull/473))

## 1.5
### 1.5.0-alpha.0 (2022-10-29)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('LegendCategories', () => {
expect(elements[idx]).toHaveStyle(`border-color: ${color}`)
);
});
test('renders icons correctly', () => {
test('renders masked icons correctly', () => {
render(
<LegendCategories
legend={{ ...DEFAULT_LEGEND, customMarkers: 'https://xyz.com/x.png' }}
Expand All @@ -51,4 +51,20 @@ describe('LegendCategories', () => {
expect(elements[idx]).toHaveStyle(`background-color: ${color}`);
});
});
test('renders non-masked icons correctly', () => {
render(
<LegendCategories
legend={{
...DEFAULT_LEGEND,
customMarkers: 'https://xyz.com/x.png',
maskedMarkers: false
}}
/>
);
const elements = document.querySelectorAll('[class*="marker"]');
getPalette(COLOR, 2).forEach((color, idx) => {
expect(elements[idx]).toHaveStyle(`background-image: url(https://xyz.com/x.png)`);
expect(elements[idx]).toHaveStyle(`background-color: rgba(0,0,0,0)`);
});
});
});
34 changes: 24 additions & 10 deletions packages/react-ui/src/widgets/legend/LegendCategories.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { getPalette } from '../../utils/palette';
import PropTypes from 'prop-types';

function LegendCategories({ legend }) {
const { labels = [], colors = [], isStrokeColor = false, customMarkers } = legend;
const {
labels = [],
colors = [],
isStrokeColor = false,
customMarkers,
maskedMarkers = true
} = legend;

const palette = getPalette(colors, labels.length);

Expand All @@ -17,6 +23,7 @@ function LegendCategories({ legend }) {
icon={
customMarkers && Array.isArray(customMarkers) ? customMarkers[idx] : customMarkers
}
maskedIcon={maskedMarkers}
isStrokeColor={isStrokeColor}
/>
));
Expand Down Expand Up @@ -49,6 +56,7 @@ LegendCategories.propTypes = {
PropTypes.arrayOf(PropTypes.string),
PropTypes.string
]),
maskedMarkers: PropTypes.bool,
isStrokeColor: PropTypes.bool
}).isRequired
};
Expand All @@ -69,8 +77,7 @@ const useStyles = makeStyles((theme) => ({
width: '12px',
height: '12px',
borderRadius: '50%',
position: 'relative',
border: '2px solid transparent'
position: 'relative'
},
circle: {
'&::after': {
Expand All @@ -87,7 +94,9 @@ const useStyles = makeStyles((theme) => ({
},
icon: {
maskRepeat: 'no-repeat',
maskSize: 'cover'
maskSize: 'cover',
backgroundRepeat: 'no-repeat',
backgroundSize: 'cover'
},
flexParent: {
display: 'flex',
Expand All @@ -107,7 +116,7 @@ const useStyles = makeStyles((theme) => ({
}
}));

function Row({ label, isMax, isStrokeColor, color = '#000', icon }) {
function Row({ label, isMax, isStrokeColor, color = '#000', icon, maskedIcon }) {
const classes = useStyles({ isMax });

const [showTooltip, setShowTooltip] = useState(false);
Expand Down Expand Up @@ -137,11 +146,16 @@ function Row({ label, isMax, isStrokeColor, color = '#000', icon }) {
className={[classes.marker, icon ? classes.icon : classes.circle].join(' ')}
style={
icon
? {
backgroundColor: color,
maskImage: `url(${icon})`,
WebkitMaskImage: `url(${icon})`
}
? maskedIcon
? {
backgroundColor: color,
maskImage: `url(${icon})`,
WebkitMaskImage: `url(${icon})`
}
: {
backgroundColor: `rgba(0,0,0,0)`,
backgroundImage: `url(${icon})`
}
: isStrokeColor
? { borderColor: color }
: { backgroundColor: color }
Expand Down