Skip to content

Commit

Permalink
custom color
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew44-mappable committed Apr 4, 2024
1 parent 7e15568 commit 870c388
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/MMapDefaultMarker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import {IconColor, IconName, iconColors, icons, glyphColors} from '../icons';
import './index.css';
import pin from './pin.svg';

export type ThemesColor = {day: string; night: string};
export type MarkerColorProps = IconColor | ThemesColor;

export type MMapDefaultMarkerProps = MMapMarkerProps & {
iconName?: IconName;
color?: IconColor;
color?: MarkerColorProps;
};

const defaultProps = Object.freeze({color: 'darkgray'});
Expand All @@ -18,7 +21,7 @@ export class MMapDefaultMarker extends mappable.MMapComplexEntity<MMapDefaultMar
private _markerElement: HTMLElement;
private _pin: HTMLElement;
private _icon: HTMLElement;
private _color: {day: string; night: string};
private _color: ThemesColor;

constructor(props: MMapDefaultMarkerProps) {
super(props);
Expand All @@ -29,7 +32,7 @@ export class MMapDefaultMarker extends mappable.MMapComplexEntity<MMapDefaultMar
}

protected _onAttach(): void {
this._color = iconColors[this._props.color];
this._color = this._getColor();
this._markerElement = document.createElement('mappable');
this._pin = document.createElement('mappable');
this._icon = document.createElement('mappable');
Expand All @@ -40,9 +43,7 @@ export class MMapDefaultMarker extends mappable.MMapComplexEntity<MMapDefaultMar
this._pin.innerHTML = pin;

this._icon.classList.add('mappable--icon');
if (this._props.iconName !== undefined) {
this._icon.innerHTML = icons[this._props.iconName].normal;
}
this._icon.innerHTML = this._getIcon();

this._pin.appendChild(this._icon);
this._markerElement.appendChild(this._pin);
Expand All @@ -54,11 +55,11 @@ export class MMapDefaultMarker extends mappable.MMapComplexEntity<MMapDefaultMar

protected _onUpdate(propsDiff: Partial<MMapDefaultMarkerProps>): void {
if (propsDiff.color !== undefined) {
this._color = iconColors[this._props.color];
this._color = this._getColor();
this._updateTheme();
}

this._icon.innerHTML = this._props.iconName !== undefined ? icons[this._props.iconName].normal : '';
this._icon.innerHTML = this._getIcon();

this._marker.update(this._props);
}
Expand All @@ -69,4 +70,23 @@ export class MMapDefaultMarker extends mappable.MMapComplexEntity<MMapDefaultMar
this._markerElement.style.borderColor = '#f8f8f8';
this._icon.style.color = glyphColors.day;
}

private _getIcon(): string {
return this._props.iconName !== undefined ? icons[this._props.iconName].normal : '';
}

private _getColor(): ThemesColor {
const color = this._props.color as MarkerColorProps;

if (typeof color === 'string') {
if (!iconColors[color]) {
throw new Error(
'The color should be one of the available color presets. If you need a custom color, pass it as an object with fields for day and night.'
);
}
return iconColors[color];
}

return color;
}
}

0 comments on commit 870c388

Please sign in to comment.