Skip to content

Commit

Permalink
functional state management utilities implementation:
Browse files Browse the repository at this point in the history
- boolean
- link/anchor
  • Loading branch information
Stassi committed Nov 14, 2024
1 parent b917b28 commit 20c3711
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/leaf/map/fullscreen/state/use-boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type UseBoolean = {
get: () => boolean
toggle: () => void
}

export function useBoolean(initialValue: boolean): UseBoolean {
let state: boolean = initialValue
return {
get(): boolean {
return state
},
toggle(): void {
state = !state
},
}
}
34 changes: 34 additions & 0 deletions src/leaf/map/fullscreen/state/use-link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { DomEvent } from 'leaflet'

export type UseLinkOptions = {
element: HTMLAnchorElement
initialProps: Record<string, string>
}

export type UseLink = {
onClick: (handler: (event: Event) => Promise<void>) => void
assign: (props: Record<string, string>) => HTMLAnchorElement
}

const domEventOn = <
(
el: HTMLElement,
types: string,
fn: (event: Event) => Promise<void>,
) => typeof DomEvent
>DomEvent.on

export function useLink({ element, initialProps }: UseLinkOptions): UseLink {
function assign(props: Record<string, string>): HTMLAnchorElement {
return Object.assign(element, props)
}

assign(initialProps)

return {
assign,
onClick(handler: (event: Event) => Promise<void>): void {
domEventOn(element, 'click', handler)
},
}
}

0 comments on commit 20c3711

Please sign in to comment.