Skip to content

Commit

Permalink
useToggle: Toggleable renamed from useSwitch: UseSwitch
Browse files Browse the repository at this point in the history
  • Loading branch information
Stassi committed Nov 25, 2024
1 parent 89996c1 commit dbbd191
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
4 changes: 2 additions & 2 deletions src/leaf/map/fullscreen/control/added-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ 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: {
control: {
anchor: ControlAnchor & UpdateControlAnchorTitleAnchorOptions
container: { element: HTMLElement }
}
fullscreen: { state: { get: SwitchGet } }
fullscreen: { state: { get: ToggleableState } }
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/leaf/map/fullscreen/control/anchor/update-title.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -11,7 +11,7 @@ export type UpdateControlAnchorTitleAnchorOptions = {
export type UpdateControlAnchorTitleOptions = {
map: {
control: { anchor: UpdateControlAnchorTitleAnchorOptions }
fullscreen: { state: { get: SwitchGet } }
fullscreen: { state: { get: ToggleableState } }
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/leaf/map/fullscreen/fullscreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -115,7 +115,7 @@ export function fullscreenMap({
},
fullscreen: {
className: joinClassNames(fullscreenMapClassNames),
state: { get: getFullscreenState, toggle: toggleFullscreenState },
state: { state: getFullscreenState, toggle: toggleFullscreenState },
},
lifecycleEvent: mapLifecycleEvent,
map,
Expand Down
11 changes: 8 additions & 3 deletions src/leaf/map/fullscreen/lifecycle-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -19,7 +24,7 @@ export type FullscreenMapLifecycleListenerOptions = {
}
fullscreen: {
className: string
state: UseSwitch
state: Toggleable
}
lifecycleEvent: FullscreenMapLifecycleEvent
map: Map
Expand All @@ -35,7 +40,7 @@ export function fullscreenMapLifecycleListener({
},
fullscreen: {
className: fullscreenMapClassName,
state: { get: getFullscreenState, toggle: toggleFullscreenState },
state: { state: getFullscreenState, toggle: toggleFullscreenState },
},
lifecycleEvent: mapLifecycleEvent,
map,
Expand Down
18 changes: 0 additions & 18 deletions src/state/use-switch.ts

This file was deleted.

19 changes: 19 additions & 0 deletions src/state/use-toggle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
type Callback<T> = () => T
export type ToggleableState = Callback<boolean>
export type ToggleableToggle = Callback<void>
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
},
}
}

0 comments on commit dbbd191

Please sign in to comment.