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

Q-205: Fix YandexMap #188

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14.18
15 changes: 0 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"react-redux": "^7.2.4",
"react-text-loop": "^2.3.0",
"react-vk": "^5.0.2",
"react-yandex-maps": "^4.6.0",
"react-youtube": "^7.13.1",
"redux": "^4.1.0",
"redux-thunk": "^2.3.0",
Expand Down
4 changes: 3 additions & 1 deletion preview/docs/YandexMap.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ To get the `API key`, follow the [instructions](https://yandex.ru/blog/mapsapi/n
| Traffic | `Unchecked` | `Checked` |
| Ruler | `Unchecked` | `Checked` |
| Layers options | `Unchecked` | `Checked` |
| Localization | `ru_RU` | `en_US` |

### In the code (for developers)

| Prop name | Название в коде | Type | Default | Example |
| Prop name | Name in the code | Type | Default | Example |
| :--------------- | :------------------: | :-------: | :----------: | :-------------------------------------------------------------------------------: |
| API Key | `apikey` | `string` | `-` | [`Your API Key`](https://yandex.ru/blog/mapsapi/novye-pravila-dostupa-k-api-kart) |
| Map scale | `zoomValue` | `string` | `9` | `5` |
Expand All @@ -43,6 +44,7 @@ To get the `API key`, follow the [instructions](https://yandex.ru/blog/mapsapi/n
| Traffic | `trafficControl` | `boolean` | `false` | `true` |
| Ruler | `rulerControl` | `boolean` | `false` | `true` |
| Layers options | `typeSelectorContol` | `boolean` | `false` | `true` |
| Localization | `lang` | `string` | `ru_RU` | `en_US` |

## 🗄 GitHub

Expand Down
2 changes: 2 additions & 0 deletions preview/docs/ru/YandexMap.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
| Пробки | `Не отмечен` | `Отмечен` |
| Линейка | `Не отмечен` | `Отмечен` |
| Варианты слоев | `Не отмечен` | `Отмечен` |
| Локализация | `ru_RU` | `en_US` |

### В коде (для разработчиков)

Expand All @@ -43,6 +44,7 @@
| Пробки | `trafficControl` | `boolean` | `false` | `true` |
| Линейка | `rulerControl` | `boolean` | `false` | `true` |
| Варианты слоев | `typeSelectorContol` | `boolean` | `false` | `true` |
| Локализация | `lang` | `string` | `ru_RU` | `en_US` |

## 🗄 GitHub

Expand Down
3 changes: 1 addition & 2 deletions src/ScrollAnimationCustom/ScrollAnimationCustom.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import React, { useMemo, useEffect, useRef } from 'react';
import { Box } from '@quarkly/widgets';
import { useOverrides } from '@quarkly/components';
import ComponentNotice from '../ComponentNotice';
import { isEmptyChildren } from '../utils';
import { isEmptyChildren, useScript } from '../utils';
import { propInfo, defaultProps, overrides } from './props';
import useScript from './hooks/useScript';
import makeAnim from './utils/makeAnim';

const duration = 1000; // Totally arbitrary!
Expand Down
13 changes: 13 additions & 0 deletions src/YandexMap/Control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useEffect } from 'react';
import { toggleControl } from './utils';

// hack to not break the react hooks rule
function Control({ map, control, enabled }) {
useEffect(() => {
toggleControl(map.current, control, enabled);
}, [map, control, enabled]);

return null;
}

export default Control;
114 changes: 55 additions & 59 deletions src/YandexMap/YandexMap.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import React, { useRef } from 'react';
import useResizeObserver from '@react-hook/resize-observer';
import React, { useEffect, useRef } from 'react';
import { Box } from '@quarkly/widgets';

import {
YMaps,
Map,
ZoomControl,
RulerControl,
TrafficControl,
TypeSelector,
SearchControl,
GeolocationControl,
FullscreenControl,
} from 'react-yandex-maps';
import { useDebounce } from '../utils';
import { useDebounce, useScript } from '../utils';
import { propInfo, defaultProps } from './props';
import withPropsTransformer from '../utils/withPropsTransformer';
import { getInitialControls } from './utils';
import Control from './Control';

const YandexMap = ({
apikey,
Expand All @@ -24,59 +14,65 @@ const YandexMap = ({
longitudeCenter,
trafficControl,
rulerControl,
typeSelectorContol,
typeSelectorContol: typeSelector,
searchControl,
geolocationControl,
fullscreenControl,
lang,
...props
}) => {
const ymapRef = useRef({});
const containerRef = useRef(null);
const map = useRef(null);

const mapRef = useRef(null);
const dapiKey = useDebounce(apikey, 2000);
const key = useDebounce(
`yandexmap${zoomValue}${latitudeCenter}${longitudeCenter}`,
2000
);

useResizeObserver(containerRef, () =>
ymapRef.current?.container?.fitToViewport()
const ns = `ymaps_${dapiKey}_${lang}`;

const { ready } = useScript(
`https://api-maps.yandex.ru/2.1?apikey=${dapiKey}&lang=${lang}&ns=${ns}`
);

const controls = {
trafficControl,
rulerControl,
typeSelector,
searchControl,
geolocationControl,
fullscreenControl,
zoomControl,
};

useEffect(() => {
if (ready) {
window[ns].ready(() => {
const ymaps = window[ns];

if (!map.current) {
map.current = new ymaps.Map(mapRef.current, {
Maks1mS marked this conversation as resolved.
Show resolved Hide resolved
center: [latitudeCenter, longitudeCenter],
zoom: zoomValue,
controls: getInitialControls(controls),
});
}
});
}

return () => {
map.current?.destroy();
map.current = null;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ready, lang]);

useEffect(() => {
map.current?.setCenter([latitudeCenter, longitudeCenter], zoomValue);
}, [latitudeCenter, longitudeCenter, zoomValue]);

return (
<Box ref={containerRef} height="250px" display="block" {...props}>
<YMaps key={dapiKey} query={{ apikey: dapiKey }}>
<Map
key={key}
height="100%"
width="100%"
defaultState={{
center: [
parseFloat(latitudeCenter),
parseFloat(longitudeCenter),
],
zoom: parseInt(zoomValue, 10),
}}
options={{
autoFitToViewport: 'allways',
}}
defaultOptions={{
autoFitToViewport: 'allways',
}}
instanceRef={ymapRef}
>
{fullscreenControl && <FullscreenControl />}
{geolocationControl && <GeolocationControl />}
{zoomControl && <ZoomControl />}
{trafficControl && <TrafficControl />}
{rulerControl && <RulerControl />}
{typeSelectorContol && <TypeSelector />}
{searchControl && (
<SearchControl
options={{ provider: 'yandex#search' }}
/>
)}
</Map>
</YMaps>
<Box {...props} ref={mapRef}>
{Object.entries(controls).map(([key, value]) => (
<Control key={key} map={map} control={key} enabled={value} />
))}
</Box>
);
};
Expand All @@ -91,4 +87,4 @@ Object.assign(YandexMap, {
defaultProps,
});

export default YandexMap;
export default withPropsTransformer(YandexMap);
12 changes: 12 additions & 0 deletions src/YandexMap/YandexMap.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,16 @@ export default {

export const StoryDefault = (props) => <YandexMap {...props} />;

export const StoryMultiple = (props) => (
<>
<YandexMap {...props} />
<YandexMap {...props} />
<YandexMap {...props} />
<YandexMap {...props} />
<YandexMap {...props} />
<YandexMap {...props} />
<YandexMap {...props} />
</>
);

StoryDefault.storyName = 'Default';
11 changes: 11 additions & 0 deletions src/YandexMap/props/propsDefault.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@ export default {
latitudeCenter: 40.714599,
longitudeCenter: -74.002791,
zoomValue: 9,
searchControl: false,
fullscreenControl: false,
geolocationControl: false,
zoomControl: false,
trafficControl: false,
rulerControl: false,
typeSelectorContol: false,
lang: 'ru_RU',
// Style
height: '250px',
display: 'block',
};
14 changes: 12 additions & 2 deletions src/YandexMap/props/propsInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
ru: 'Широта',
},
control: 'input',
type: 'text',
type: 'number',
category: 'Center',
weight: 0.5,
},
Expand All @@ -38,7 +38,7 @@ export default {
ru: 'Долгота',
},
control: 'input',
type: 'text',
type: 'number',
category: 'Center',
weight: 0.5,
},
Expand Down Expand Up @@ -105,4 +105,14 @@ export default {
category: 'Controls',
weight: 0.5,
},
lang: {
title: {
en: 'Localization',
ru: 'Локализация',
},
control: 'input',
type: 'text',
variants: ['ru_RU', 'en_US', 'en_RU', 'ru_UA', 'uk_UA', 'tr_TR'],
weight: 0.5,
},
};
23 changes: 23 additions & 0 deletions src/YandexMap/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export function getInitialControls(controlsObject) {
const controls = [];

Object.entries(controlsObject).forEach(([key, value]) => {
if (value) controls.push(key);
});

return controls;
}

export function toggleControls(map, controlsObject) {
Object.entries(controlsObject).forEach(([key, value]) =>
toggleControl(map, key, value)
);
}

export function toggleControl(map, key, value) {
if (value) {
map?.controls?.add(key);
} else {
map?.controls?.remove(key);
}
}
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export { default as useDebounce } from './useDebounce';
export { default as useDebouncedCallback } from './useDebouncedCallback';
export { default as useUpdateEffect } from './useUpdateEffect';
export { default as useUniqueId } from './useUniqueId';
export { default as useScript } from './useScript';

export { default as isString } from './isString';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useState, useEffect } from 'react';

const initialValue = {
ready: false,
error: false,
};

export default function useScript(src) {
const [state, setState] = useState({
ready: false,
loaded: false,
error: false,
});
const [state, setState] = useState(initialValue);
const [prevSrc, setPrevSrc] = useState(src);

useEffect(
() => {
Expand All @@ -31,7 +33,6 @@ export default function useScript(src) {
const status = script.getAttribute('data-status');

setState({
loading: status === 'loading',
ready: status === 'ready',
error: status === 'error',
});
Expand All @@ -56,5 +57,12 @@ export default function useScript(src) {
},
[src] // Only re-run effect if script src changes
);

// Reset state immediately if src is changed
if (src !== prevSrc) {
setPrevSrc(src);
setState(initialValue);
}

return state;
}
Loading