Adding a spinner to the map
vanilla.html
react.html
vue.html
variables.ts
variables.css
common.ts
common.css
<!DOCTYPE html>
<html>
<head>
<title>Vanilla example ymaps3-spinner</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
<!-- To make the map appear, you must add your apikey -->
<script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>
<script
data-plugins="transform-modules-umd"
data-presets="typescript"
type="text/babel"
src="../variables.ts"
></script>
<script
data-plugins="transform-modules-umd"
data-presets="typescript"
type="text/babel"
src="./common.ts"
></script>
<script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
import {YMapSpinnerProps} from '@yandex/ymaps3-spinner';
import {SpinnerPropsControl} from './common';
import {LOCATION} from '../variables';
window.map = null;
async function main() {
await ymaps3.ready;
const {YMap, YMapDefaultSchemeLayer, YMapControls} = ymaps3;
const {YMapSpinner} = await ymaps3.import('@yandex/ymaps3-spinner');
map = new YMap(document.getElementById('app'), {location: LOCATION});
map.addChild(new YMapDefaultSchemeLayer({}));
// Create spinner control
const spinner = new YMapSpinner({});
map.addChild(new YMapControls({position: 'left'}, [spinner]));
map.addChild(
new YMapControls({position: 'top right'}, [
new SpinnerPropsControl({
onChange: (props: YMapSpinnerProps) => {
spinner.update(props);
}
})
])
);
}
// Initialize the application
main();
</script>
<style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; } </style>
<link rel="stylesheet" href="../variables.css" />
<link rel="stylesheet" href="./common.css" />
</head>
<body>
<div id="app"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>React example ymaps3-spinner</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<script crossorigin src="https://cdn.jsdelivr.net/npm/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/react-dom@17/umd/react-dom.production.min.js"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
<!-- To make the map appear, you must add your apikey -->
<script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>
<script
data-plugins="transform-modules-umd"
data-presets="typescript"
type="text/babel"
src="../variables.ts"
></script>
<script
data-plugins="transform-modules-umd"
data-presets="typescript"
type="text/babel"
src="./common.ts"
></script>
<script data-plugins="transform-modules-umd" data-presets="react, typescript" type="text/babel">
import {YMapSpinnerProps} from '@yandex/ymaps3-spinner';
import {SpinnerPropsControl} from './common';
import {LOCATION} from '../variables';
// Initialize the map variable
window.map = null;
// Main function to set up the application
main();
async function main() {
// Import necessary modules from ymaps3 and wait for ymaps3 to be ready
const [ymaps3React] = await Promise.all([ymaps3.import('@yandex/ymaps3-reactify'), ymaps3.ready]);
// Reactify the ymaps3 library to use with React
const reactify = ymaps3React.reactify.bindTo(React, ReactDOM);
const {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapControls, YMapControl} = reactify.module(ymaps3);
const {YMapSpinner} = reactify.module(await ymaps3.import('@yandex/ymaps3-spinner'));
const SpinnerPropsControlR = reactify.entity(SpinnerPropsControl);
const {useState, useCallback, useMemo, useEffect} = React;
// Render the main App component
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('app')
);
// Main application component
function App() {
const [spinnerProps, setSpinnerProps] = useState<YMapSpinnerProps>();
const onChange = useCallback((props: YMapSpinnerProps) => {
setSpinnerProps(props);
}, []);
return (
<YMap location={LOCATION} ref={(x) => (map = x)}>
<YMapDefaultSchemeLayer />
<YMapControls position="left">
<YMapSpinner {...spinnerProps} />
</YMapControls>
<YMapControls position="top right">
<SpinnerPropsControlR onChange={onChange} />
</YMapControls>
</YMap>
);
}
}
</script>
<style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; } </style>
<link rel="stylesheet" href="../variables.css" />
<link rel="stylesheet" href="./common.css" />
</head>
<body>
<div id="app"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Vue example ymaps3-spinner</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<script crossorigin src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js"></script>
<script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
<!-- To make the map appear, you must add your apikey -->
<script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>
<script
data-plugins="transform-modules-umd"
data-presets="typescript"
type="text/babel"
src="../variables.ts"
></script>
<script
data-plugins="transform-modules-umd"
data-presets="typescript"
type="text/babel"
src="./common.ts"
></script>
<script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
import {YMapSpinnerProps} from '@yandex/ymaps3-spinner';
import {SpinnerPropsControl} from './common';
import {LOCATION} from '../variables';
// Global variables to hold map and spinner references
window.map = null;
// Main function to initialize the application
main();
async function main() {
// Import necessary Yandex components and wait for Yandex API readiness
const [ymaps3Vue] = await Promise.all([ymaps3.import('@yandex/ymaps3-vuefy'), ymaps3.ready]);
// Bind Yandex components to Vue
const vuefy = ymaps3Vue.vuefy.bindTo(Vue);
const {YMap, YMapDefaultSchemeLayer, YMapControls} = vuefy.module(ymaps3);
const {YMapSpinner} = vuefy.module(await ymaps3.import('@yandex/ymaps3-spinner'));
const SpinnerPropsControlV = vuefy.entity(SpinnerPropsControl);
// Create a Vue application
const app = Vue.createApp({
components: {YMap, YMapDefaultSchemeLayer, YMapControls, YMapSpinner, SpinnerPropsControlV},
setup() {
const refMap = (ref) => {
window.map = ref?.entity; // Store the map entity in the global variable
};
const spinnerProps = Vue.ref<YMapSpinnerProps>();
const onChange = (props: YMapSpinnerProps) => {
spinnerProps.value = props;
};
// Return reactive properties and methods to the template
return {LOCATION, refMap, spinnerProps, onChange};
},
template: `
<YMap :location="LOCATION" :ref="refMap">
<YMapDefaultSchemeLayer />
<YMapControls position="left">
<YMapSpinner v-bind="spinnerProps" />
</YMapControls>
<YMapControls position="top right">
<SpinnerPropsControlV :onChange="onChange" />
</YMapControls>
</YMap>
`
});
// Mount the Vue application to the DOM
app.mount('#app');
}
</script>
<style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; } </style>
<link rel="stylesheet" href="../variables.css" />
<link rel="stylesheet" href="./common.css" />
</head>
<body>
<div id="app"></div>
</body>
</html>
import type {YMapLocationRequest} from '@yandex/ymaps3-types';
export const LOCATION: YMapLocationRequest = {
center: [37.623082, 55.75254], // starting position [lng, lat]
zoom: 9 // starting zoom
};
:root {
--text-color: #000;
--color-primary: #313133;
}
import {YMapSpinnerProps} from '@yandex/ymaps3-spinner';
import {YMapControl} from '@yandex/ymaps3-types';
// Load the spinner package
ymaps3.import.registerCdn('https://cdn.jsdelivr.net/npm/{package}', ['@yandex/ymaps3-spinner@0.0']);
export let SpinnerPropsControl = null;
ymaps3.ready.then(() => {
type SpinnerPropsControlProps = {
initialProps?: YMapSpinnerProps;
onChange: (props: YMapSpinnerProps) => void;
};
const defaultProps = Object.freeze({
initialProps: {
lines: 12,
length: 7,
width: 5,
radius: 10,
corners: 1,
speed: 1,
color: '#969696',
fadeColor: 'transparent',
zIndex: 'unset',
animation: 'spinner-line-fade-default',
direction: 1
}
});
class GroupSpinnerPropsControlClass extends ymaps3.YMapComplexEntity<
SpinnerPropsControlProps,
typeof defaultProps
> {
constructor(props: SpinnerPropsControlProps) {
super(props);
const control = new ymaps3.YMapControl({transparent: true});
const commonProps = new SpinnerPropsControlClass({
initialProps: this._props.initialProps,
onChange: this._props.onChange
});
const colorProps = new SpinnerColorPropsClass({
initialProps: this._props.initialProps,
onChange: this._props.onChange
});
control.addChild(commonProps).addChild(colorProps);
this.addChild(control);
}
}
class SpinnerPropsControlClass extends ymaps3.YMapComplexEntity<SpinnerPropsControlProps, typeof defaultProps> {
static defaultProps = defaultProps;
private _container: HTMLDivElement;
private _spinnerProps: YMapSpinnerProps;
protected _onAttach(): void {
this._spinnerProps = this._props.initialProps;
this._container = document.createElement('div');
this._container.classList.add('spinner-props-control');
ymaps3.useDomContext(this, this._container, this._container);
const amountRangeControl = new RangeControlClass({
label: 'amount',
value: this._spinnerProps.lines,
min: 5,
max: 20,
step: 1,
onChange: (newValue: number) => this._updateSpinnerProps('lines', newValue)
});
const lengthRangeControl = new RangeControlClass({
label: 'length',
value: this._spinnerProps.length,
min: 0,
max: 80,
step: 1,
onChange: (newValue: number) => this._updateSpinnerProps('length', newValue)
});
const widthRangeControl = new RangeControlClass({
label: 'width',
value: this._spinnerProps.width,
min: 2,
max: 52,
step: 1,
onChange: (newValue: number) => this._updateSpinnerProps('width', newValue)
});
const radiusRangeControl = new RangeControlClass({
label: 'radius',
value: this._spinnerProps.radius,
min: 0,
max: 84,
step: 1,
onChange: (newValue: number) => this._updateSpinnerProps('radius', newValue)
});
const cornersRangeControl = new RangeControlClass({
label: 'corners',
value: this._spinnerProps.corners,
min: 0,
max: 1,
step: 0.1,
onChange: (newValue: number) => this._updateSpinnerProps('corners', newValue)
});
const speedRangeControl = new RangeControlClass({
label: 'speed',
value: this._spinnerProps.speed,
min: 0.5,
max: 2.2,
step: 0.1,
onChange: (newValue: number) => this._updateSpinnerProps('speed', newValue)
});
const animationRadioControl = new RadioControlClass({
value: this._spinnerProps.animation,
group: 'animation',
options: [
{value: 'spinner-line-fade-default', label: 'fade-default'},
{value: 'spinner-line-fade-quick', label: 'fade-quick'},
{value: 'spinner-line-fade-more', label: 'fade-more'},
{value: 'spinner-line-shrink', label: 'shrink'}
],
onChange: (newValue: string) => this._updateSpinnerProps('animation', newValue)
});
const directionCheckboxControl = new CheckboxControlClass({
value: Boolean(this._spinnerProps.direction),
label: 'clockwise direction',
onChange: (newValue: boolean) => this._updateSpinnerProps('direction', newValue ? 1 : -1)
});
this.addChild(amountRangeControl)
.addChild(lengthRangeControl)
.addChild(widthRangeControl)
.addChild(radiusRangeControl)
.addChild(cornersRangeControl)
.addChild(speedRangeControl)
.addChild(new DividerClass({}))
.addChild(animationRadioControl)
.addChild(new DividerClass({}))
.addChild(directionCheckboxControl);
}
private _updateSpinnerProps = (propsKey: string, newValue: number | string): void => {
this._spinnerProps[propsKey] = newValue;
this._props.onChange({...this._spinnerProps});
};
}
class SpinnerColorPropsClass extends ymaps3.YMapComplexEntity<SpinnerPropsControlProps, typeof defaultProps> {
static defaultProps = defaultProps;
private _container: HTMLDivElement;
private _spinnerProps: YMapSpinnerProps;
protected _onAttach(): void {
this._spinnerProps = this._props.initialProps;
this._container = document.createElement('div');
this._container.classList.add('spinner-color-props');
ymaps3.useDomContext(this, this._container, this._container);
const primaryColorControl = new ColorControlClass({
label: 'primary color',
value: this._props.initialProps.color as string,
onChange: (newValue: string) => this._updateSpinnerProps('color', newValue)
});
const secondaryColorControl = new ColorControlClass({
label: 'secondary color',
value: this._props.initialProps.fadeColor as string,
onChange: (newValue: string) => this._updateSpinnerProps('fadeColor', newValue)
});
this.addChild(primaryColorControl).addChild(secondaryColorControl);
}
private _updateSpinnerProps = (propsKey: string, newValue: string): void => {
this._spinnerProps[propsKey] = newValue;
this._props.onChange({...this._spinnerProps});
};
}
class RangeControlClass extends ymaps3.YMapComplexEntity<{
label: string;
value: number;
step?: number;
min: number;
max: number;
onChange: (value: number) => void;
}> {
private _container: HTMLDivElement;
private _input: HTMLInputElement;
protected _onAttach(): void {
this._container = document.createElement('div');
this._container.classList.add('range-control');
const inputId = `label-${this._props.label}`;
const label = document.createElement('label');
label.htmlFor = inputId;
label.textContent = this._props.label;
this._input = document.createElement('input');
this._input.type = 'range';
this._input.id = inputId;
this._input.value = this._props.value.toString();
this._input.min = this._props.min.toString();
this._input.max = this._props.max.toString();
if (this._props.step !== undefined) {
this._input.step = this._props.step.toString();
}
this._input.addEventListener('input', this._handleInputChange);
this._container.appendChild(label);
this._container.appendChild(this._input);
ymaps3.useDomContext(this, this._container, null);
this._handleInputChange(); // immediately trigger change hook
}
protected _onDetach(): void {
this._input.removeEventListener('input', this._handleInputChange);
}
private _handleInputChange = () => {
const value = parseFloat(this._input.value);
const {min, max} = this._props;
const progress = ((value - min) / (max - min)) * 100;
this._input.style.background = `linear-gradient(to right, var(--color-primary) ${progress}%, #F5F6F7 ${progress}%)`;
this._props.onChange(value);
};
}
class RadioControlClass extends ymaps3.YMapComplexEntity<{
group: string;
options: {label: string; value: string}[];
value: string;
onChange: (value: string) => void;
}> {
private _container: HTMLDivElement;
protected _onAttach(): void {
this._container = document.createElement('div');
this._container.classList.add('radio-control');
this._props.options.forEach((option) => {
const label = document.createElement('label');
label.textContent = option.label;
const input = document.createElement('input');
input.type = 'radio';
input.name = this._props.group;
input.value = option.value;
input.checked = option.value === this._props.value;
label.appendChild(input);
this._container.appendChild(label);
});
this._container.addEventListener('change', this._handleInputChange);
ymaps3.useDomContext(this, this._container, null);
}
protected _onDetach(): void {
this._container.removeEventListener('change', this._handleInputChange);
}
private _handleInputChange = (event: Event) => {
const target = event.target as HTMLInputElement;
this._props.onChange(target.value);
};
}
class CheckboxControlClass extends ymaps3.YMapComplexEntity<{
label: string;
value: boolean;
onChange: (value: boolean) => void;
}> {
private _container: HTMLDivElement;
private _input: HTMLInputElement;
protected _onAttach(): void {
this._container = document.createElement('div');
this._container.classList.add('checkbox-control');
const label = document.createElement('label');
label.textContent = this._props.label;
this._input = document.createElement('input');
this._input.type = 'checkbox';
this._input.checked = this._props.value;
this._input.addEventListener('change', this._handleInputChange);
label.appendChild(this._input);
this._container.appendChild(label);
ymaps3.useDomContext(this, this._container, null);
}
protected _onDetach(): void {
this._input.removeEventListener('change', this._handleInputChange);
}
private _handleInputChange = () => {
this._props.onChange(this._input.checked);
};
}
class ColorControlClass extends ymaps3.YMapComplexEntity<{
label: string;
value: string;
onChange: (value: string) => void;
}> {
private _container: HTMLDivElement;
private _input: HTMLInputElement;
protected _onAttach(): void {
this._container = document.createElement('div');
this._container.classList.add('color-control');
const label = document.createElement('label');
label.textContent = this._props.label;
this._input = document.createElement('input');
this._input.type = 'color';
this._input.value = this._props.value;
this._input.addEventListener('change', this._handleInputChange);
label.appendChild(this._input);
this._container.appendChild(label);
ymaps3.useDomContext(this, this._container, null);
}
protected _onDetach(): void {
this._input.removeEventListener('change', this._handleInputChange);
}
private _handleInputChange = () => {
this._props.onChange(this._input.value);
};
}
class DividerClass extends ymaps3.YMapComplexEntity<{}> {
private _divider: HTMLDivElement;
protected _onAttach(): void {
this._divider = document.createElement('div');
this._divider.classList.add('divider');
ymaps3.useDomContext(this, this._divider, null);
}
}
SpinnerPropsControl = GroupSpinnerPropsControlClass;
});
.spinner-props-control {
position: absolute;
top: 8px;
right: 8px;
display: flex;
flex-direction: column;
box-sizing: border-box;
width: 240px;
padding: 8px 16px;
font-size: 14px;
font-style: normal;
line-height: 16px;
color: var(--text-color);
border-radius: 12px;
background: #fff;
box-shadow: 0 4px 12px 0 rgba(95, 105, 131, 0.1), 0 4px 24px 0 rgba(95, 105, 131, 0.04);
}
.spinner-color-props {
position: absolute;
top: 8px;
right: calc(8px + 240px + 20px);
display: flex;
flex-direction: column;
box-sizing: border-box;
width: 200px;
padding: 14px 16px;
font-size: 14px;
font-style: normal;
line-height: 16px;
color: var(--text-color);
border-radius: 12px;
background: #fff;
box-shadow: 0 4px 12px 0 rgba(95, 105, 131, 0.1), 0 4px 24px 0 rgba(95, 105, 131, 0.04);
}
.range-control {
display: flex;
flex-direction: row;
align-items: center;
gap: 12px;
padding: 10px 0;
}
.range-control label {
width: 60px;
}
.range-control input {
flex-grow: 1;
}
.radio-control {
display: grid;
grid-template-columns: repeat(2, 1fr);
padding: 12px 0;
row-gap: 12px;
}
.radio-control label {
display: flex;
flex-direction: row-reverse;
justify-content: flex-end;
align-items: center;
}
.checkbox-control {
padding: 12px 0;
}
.checkbox-control label {
display: flex;
flex-direction: row-reverse;
justify-content: flex-end;
align-items: center;
}
.color-control {
padding: 10px 0;
}
.color-control label {
display: flex;
flex-direction: row-reverse;
justify-content: flex-end;
align-items: center;
}
.divider {
width: 100%;
height: 1px;
background: rgba(92, 94, 102, 0.14);
}
input[type='range'] {
height: 2px;
cursor: pointer;
outline: none;
background: linear-gradient(to right, var(--color-primary) 100%, #f5f6f7 0%);
-webkit-appearance: none;
appearance: none;
}
input[type='range']::-webkit-slider-thumb {
width: 16px;
height: 16px;
cursor: pointer;
border: 2px solid var(--color-primary);
border-radius: 50%;
background-color: #fff;
-webkit-appearance: none;
appearance: none;
}
input[type='radio'] {
position: relative;
width: 16px;
height: 16px;
margin: 0 8px 0 0;
cursor: pointer;
border: 2px solid #e8e8e9;
border-radius: 50%;
appearance: none;
}
input[type='radio']:checked {
border: 5px solid var(--color-primary);
}
/* Create the inner dot */
input[type='radio']:checked::before {
position: absolute;
z-index: 1;
top: 50%;
left: 50%;
display: block;
width: 6px;
height: 6px;
content: '';
border-radius: 50%;
background-color: #fff;
transform: translate(-50%, -50%);
}
input[type='checkbox'] {
position: relative; /* Position for the inner checkmark */
width: 16px; /* Width of the checkbox */
height: 16px; /* Height of the checkbox */
margin: 0 8px 0 0; /* Margin */
cursor: pointer; /* Show pointer cursor on hover */
border: 2px solid #e8e8e9; /* Default border */
border-radius: 3px; /* Optional: Slightly rounded corners */
background-color: transparent; /* Default background */
appearance: none; /* Remove default styling */
}
/* Style when checked */
input[type='checkbox']:checked {
border: 2px solid var(--color-primary);
background-color: var(--color-primary);
}
/* Create a checkmark */
input[type='checkbox']:checked::after {
position: absolute; /* Position it absolutely */
z-index: 1;
top: 1px;
left: 3px;
display: block; /* Make it a block element */
width: 4px; /* Width of the checkmark */
height: 6px; /* Height of the checkmark */
content: ''; /* Required for pseudo-elements */
border: solid #fff; /* Checkmark color */
border-width: 0 2px 2px 0; /* Create a checkmark shape */
transform: rotate(45deg);
}
input[type='color'] {
width: 20px;
height: 20px;
margin-right: 8px;
padding: 0;
border: 1px solid #5c5e6624;
border-radius: 4px;
background-color: #fff;
-webkit-appearance: none;
}
input[type='color']::-webkit-color-swatch-wrapper {
padding: 0;
}
input[type='color']::-webkit-color-swatch {
border: none;
}