( useElement: ElementHook<E, P>, )
| 5 | import type { ElementHook } from './element.js' |
| 6 | |
| 7 | export function createControlHook<E extends Control, P extends ControlOptions>( |
| 8 | useElement: ElementHook<E, P>, |
| 9 | ) { |
| 10 | return function useLeafletControl(props: P): ReturnType<ElementHook<E, P>> { |
| 11 | const context = useLeafletContext() |
| 12 | const elementRef = useElement(props, context) |
| 13 | const { instance } = elementRef.current |
| 14 | const positionRef = useRef(props.position) |
| 15 | const { position } = props |
| 16 | |
| 17 | useEffect( |
| 18 | function addControl() { |
| 19 | instance.addTo(context.map) |
| 20 | |
| 21 | return function removeControl() { |
| 22 | instance.remove() |
| 23 | } |
| 24 | }, |
| 25 | [context.map, instance], |
| 26 | ) |
| 27 | |
| 28 | useEffect( |
| 29 | function updateControl() { |
| 30 | if (position != null && position !== positionRef.current) { |
| 31 | instance.setPosition(position) |
| 32 | positionRef.current = position |
| 33 | } |
| 34 | }, |
| 35 | [instance, position], |
| 36 | ) |
| 37 | |
| 38 | return elementRef |
| 39 | } |
| 40 | } |
no test coverage detected
searching dependent graphs…