diff --git a/src/index.ts b/src/index.ts index 8c807d0..e8a6c21 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,4 +10,4 @@ export * from './leaf/popup.js' export * from './leaf/tile-layer/tile-layer-osm.js' export * from './leaf/tile-layer/tile-layer.js' export * from './leaflet-types.js' -export * from './state/use-switch' +export * from './state/use-toggle' diff --git a/src/leaf/map/fullscreen/control/added-listener.ts b/src/leaf/map/fullscreen/control/added-listener.ts index 77a87af..972b170 100644 --- a/src/leaf/map/fullscreen/control/added-listener.ts +++ b/src/leaf/map/fullscreen/control/added-listener.ts @@ -6,7 +6,7 @@ import { updateControlAnchorTitle, } from './anchor/update-title' -import { type ControlOnAdd, type SwitchGet } from '@stassi/leaf' +import { type ControlOnAdd, type ToggleableState } from '@stassi/leaf' export type ControlAddedListenerOptions = { map: { @@ -14,7 +14,7 @@ export type ControlAddedListenerOptions = { anchor: ControlAnchor & UpdateControlAnchorTitleAnchorOptions container: { element: HTMLElement } } - fullscreen: { state: { get: SwitchGet } } + fullscreen: { state: { get: ToggleableState } } } } diff --git a/src/leaf/map/fullscreen/control/anchor/update-title.ts b/src/leaf/map/fullscreen/control/anchor/update-title.ts index c0d72bd..308b02d 100644 --- a/src/leaf/map/fullscreen/control/anchor/update-title.ts +++ b/src/leaf/map/fullscreen/control/anchor/update-title.ts @@ -1,6 +1,6 @@ import { type ControlAnchorAssign } from './anchor' -import { type SwitchGet } from '@stassi/leaf' +import { type ToggleableState } from '@stassi/leaf' export type ControlAnchorTitleStates = Record<'false' | 'true', string> export type UpdateControlAnchorTitleAnchorOptions = { @@ -11,7 +11,7 @@ export type UpdateControlAnchorTitleAnchorOptions = { export type UpdateControlAnchorTitleOptions = { map: { control: { anchor: UpdateControlAnchorTitleAnchorOptions } - fullscreen: { state: { get: SwitchGet } } + fullscreen: { state: { get: ToggleableState } } } } diff --git a/src/leaf/map/fullscreen/fullscreen.ts b/src/leaf/map/fullscreen/fullscreen.ts index 3e0bb86..f6b9ae6 100644 --- a/src/leaf/map/fullscreen/fullscreen.ts +++ b/src/leaf/map/fullscreen/fullscreen.ts @@ -20,7 +20,7 @@ import { fullscreenMapLifecycleListener, } from './lifecycle-listener' -import { control, useSwitch } from '@stassi/leaf' +import { control, useToggle } from '@stassi/leaf' type FullscreenMapDomElement = { classNames: string[] @@ -84,8 +84,8 @@ export function fullscreenMap({ id, ...mapOptions }: FullscreenMapOptions): Map { - const { get: getFullscreenState, toggle: toggleFullscreenState } = - useSwitch(false), + const { state: getFullscreenState, toggle: toggleFullscreenState } = + useToggle(false), containerElement: HTMLElement = domElement({ className: joinClassNames(containerClassNames), tag: containerTag, @@ -115,7 +115,7 @@ export function fullscreenMap({ }, fullscreen: { className: joinClassNames(fullscreenMapClassNames), - state: { get: getFullscreenState, toggle: toggleFullscreenState }, + state: { state: getFullscreenState, toggle: toggleFullscreenState }, }, lifecycleEvent: mapLifecycleEvent, map, diff --git a/src/leaf/map/fullscreen/lifecycle-listener.ts b/src/leaf/map/fullscreen/lifecycle-listener.ts index 9c22480..aa2005c 100644 --- a/src/leaf/map/fullscreen/lifecycle-listener.ts +++ b/src/leaf/map/fullscreen/lifecycle-listener.ts @@ -6,7 +6,12 @@ import { updateControlAnchorTitle, } from './control/anchor/update-title' -import { type Map, type UseSwitch, domEventOff, domEventOn } from '@stassi/leaf' +import { + type Map, + type Toggleable, + domEventOff, + domEventOn, +} from '@stassi/leaf' export type FullscreenMapLifecycleEvent = 'ready' | 'unload' export type FullscreenMapLifecycleListenerOptions = { @@ -19,7 +24,7 @@ export type FullscreenMapLifecycleListenerOptions = { } fullscreen: { className: string - state: UseSwitch + state: Toggleable } lifecycleEvent: FullscreenMapLifecycleEvent map: Map @@ -35,7 +40,7 @@ export function fullscreenMapLifecycleListener({ }, fullscreen: { className: fullscreenMapClassName, - state: { get: getFullscreenState, toggle: toggleFullscreenState }, + state: { state: getFullscreenState, toggle: toggleFullscreenState }, }, lifecycleEvent: mapLifecycleEvent, map, diff --git a/src/state/use-switch.ts b/src/state/use-switch.ts deleted file mode 100644 index 682d50c..0000000 --- a/src/state/use-switch.ts +++ /dev/null @@ -1,18 +0,0 @@ -export type SwitchGet = () => boolean -export type SwitchToggle = () => void -export type UseSwitch = { - get: SwitchGet - toggle: SwitchToggle -} - -export function useSwitch(initialState: boolean): UseSwitch { - let state: boolean = initialState - return { - get(): boolean { - return state - }, - toggle(): void { - state = !state - }, - } -} diff --git a/src/state/use-toggle.ts b/src/state/use-toggle.ts new file mode 100644 index 0000000..95e1dbe --- /dev/null +++ b/src/state/use-toggle.ts @@ -0,0 +1,19 @@ +type Callback = () => T +export type ToggleableState = Callback +export type ToggleableToggle = Callback +export type Toggleable = { + state: ToggleableState + toggle: ToggleableToggle +} + +export function useToggle(initialState: boolean): Toggleable { + let innerState: boolean = initialState + return { + state(): boolean { + return innerState + }, + toggle(): void { + innerState = !innerState + }, + } +}