From a9e99f1cbe7dbddb98ea66e9b18161fef2affc31 Mon Sep 17 00:00:00 2001 From: Andreas Stassivik Date: Fri, 15 Nov 2024 14:37:59 -0800 Subject: [PATCH] `control/anchor` with semantic type names replace `state/useAnchor` --- .../map/fullscreen/control/anchor/anchor.ts | 45 +++++++++++++++++++ src/leaf/map/fullscreen/state/use-anchor.ts | 40 ----------------- 2 files changed, 45 insertions(+), 40 deletions(-) create mode 100644 src/leaf/map/fullscreen/control/anchor/anchor.ts delete mode 100644 src/leaf/map/fullscreen/state/use-anchor.ts diff --git a/src/leaf/map/fullscreen/control/anchor/anchor.ts b/src/leaf/map/fullscreen/control/anchor/anchor.ts new file mode 100644 index 0000000..0aa3472 --- /dev/null +++ b/src/leaf/map/fullscreen/control/anchor/anchor.ts @@ -0,0 +1,45 @@ +import { DomEvent } from 'leaflet' + +export type ControlAnchorAttributes = Record +export type ControlAnchorOptions = { + attributes: ControlAnchorAttributes + element: HTMLElement +} + +type ControlAnchorOnClickHandler = (event: Event) => Promise +export type ControlAnchorOnClick = ( + handler: ControlAnchorOnClickHandler, +) => void +export type ControlAnchorAssign = ( + props: ControlAnchorAttributes, +) => HTMLElement +export type ControlAnchor = { + assign: ControlAnchorAssign + onClick: ControlAnchorOnClick +} + +const domEventOn = < + ( + el: HTMLElement, + types: string, + fn: ControlAnchorOnClickHandler, + ) => typeof DomEvent +>DomEvent.on + +export function controlAnchor({ + attributes, + element, +}: ControlAnchorOptions): ControlAnchor { + function assign(anchorAttributes: ControlAnchorAttributes): HTMLElement { + return Object.assign(element, anchorAttributes) + } + + assign(attributes) + + return { + assign, + onClick(handler: ControlAnchorOnClickHandler): void { + domEventOn(element, 'click', handler) + }, + } +} diff --git a/src/leaf/map/fullscreen/state/use-anchor.ts b/src/leaf/map/fullscreen/state/use-anchor.ts deleted file mode 100644 index 6dce45d..0000000 --- a/src/leaf/map/fullscreen/state/use-anchor.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { DomEvent } from 'leaflet' - -export type AnchorAttributes = Record -export type UseAnchorOptions = { - attributes: AnchorAttributes - element: HTMLElement -} - -export type AnchorAssign = (props: AnchorAttributes) => HTMLElement -export type AnchorOnClick = (handler: (event: Event) => Promise) => void -export type UseAnchor = { - assign: AnchorAssign - onClick: AnchorOnClick -} - -const domEventOn = < - ( - el: HTMLElement, - types: string, - fn: (event: Event) => Promise, - ) => typeof DomEvent ->DomEvent.on - -export function useAnchor({ - attributes, - element, -}: UseAnchorOptions): UseAnchor { - function assign(anchorAttributes: AnchorAttributes): HTMLElement { - return Object.assign(element, anchorAttributes) - } - - assign(attributes) - - return { - assign, - onClick(handler: (event: Event) => Promise): void { - domEventOn(element, 'click', handler) - }, - } -}