Skip to content

Commit

Permalink
wip(react): map control
Browse files Browse the repository at this point in the history
  • Loading branch information
rendrom committed Jan 24, 2022
1 parent 5113c31 commit fb236d9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
25 changes: 25 additions & 0 deletions packages/react-ngw-map/src/ButtonControl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useRef } from 'react';

import { useNgwMapContext } from './context';
import { mapControlCreator } from './mapControlCreator';

import type { ReactNode } from 'react';
import type { ControlOptions, ButtonControlOptions } from '@nextgis/webmap';

interface MapControlProps extends ButtonControlOptions, ControlOptions {
children?: ReactNode;
}

export function ButtonControl<P extends MapControlProps = MapControlProps>(
props: P,
) {
const context = useNgwMapContext();
const el = document.createElement('div');
const portal = useRef(el);

function createControl() {
return context.ngwMap.createButtonControl(props);
}

return mapControlCreator({ ...props, context, portal, createControl });
}
26 changes: 5 additions & 21 deletions packages/react-ngw-map/src/MapControl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ReactNode, useEffect, useRef } from 'react';
import { createPortal } from 'react-dom';
import { MutableRefObject } from 'react';
import { useNgwMapContext } from './context';
import { mapControlCreator } from './mapControlCreator';

import type { ReactNode } from 'react';
import type { ControlOptions, CreateControlOptions } from '@nextgis/webmap';

interface MapControlProps extends CreateControlOptions, ControlOptions {
Expand All @@ -12,13 +13,9 @@ export function MapControl<P extends MapControlProps = MapControlProps>(
props: P,
) {
const { bar, margin, addClass } = props;

const context = useNgwMapContext();

const el = document.createElement('div');
const portal = useRef(el);

function createInstance() {
function createControl(portal: MutableRefObject<HTMLDivElement>) {
return context.ngwMap.createControl(
{
onAdd() {
Expand All @@ -34,19 +31,6 @@ export function MapControl<P extends MapControlProps = MapControlProps>(
{ bar, margin, addClass },
);
}
const instance = useRef(createInstance());
const position = props.position || 'top-left';

useEffect(
function addControl() {
context.ngwMap.addControl(instance.current, position);

return function removeControl() {
context.ngwMap.removeControl(instance.current);
};
},
[context.ngwMap, instance],
);

return createPortal(props.children, portal.current);
return mapControlCreator({ ...props, context, createControl });
}
1 change: 1 addition & 0 deletions packages/react-ngw-map/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './interfaces';

export { ReactNgwMap } from './ReactNgwMap';
export { MapControl } from './MapControl';
37 changes: 37 additions & 0 deletions packages/react-ngw-map/src/mapControlCreator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useEffect, useRef } from 'react';
import { createPortal } from 'react-dom';

import type { ReactNode, MutableRefObject } from 'react';
import type { ControlOptions } from '@nextgis/webmap';
import type { NgwMapContextInterface } from './interfaces';

interface MapControlProps extends ControlOptions {
children?: ReactNode;
context: NgwMapContextInterface;
createControl: (portal: MutableRefObject<HTMLDivElement>) => Promise<unknown>;
}

export function mapControlCreator<P extends MapControlProps = MapControlProps>(
props: P,
) {
const { context, createControl, children, position } = props;

const el = document.createElement('div');
const portal = useRef(el);

const instance = useRef(createControl(portal));
const pos = position || 'top-left';

useEffect(
function addControl() {
context.ngwMap.addControl(instance.current, pos);

return function removeControl() {
context.ngwMap.removeControl(instance.current);
};
},
[context.ngwMap, instance],
);

return createPortal(children, portal.current);
}

0 comments on commit fb236d9

Please sign in to comment.