rexr 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,34 @@
1
+ import { EventState, event } from 'reev/src'
2
+ import { WindowSizeState } from './types'
3
+
4
+ export * from './types'
5
+
6
+ export const windowSizeEvent = () => {
7
+ const mount = () => {
8
+ self.change()
9
+ window.addEventListener('resize', self.change)
10
+ }
11
+
12
+ const clean = () => {
13
+ window.removeEventListener('resize', self.change)
14
+ }
15
+
16
+ const change = () => {
17
+ self.callback()
18
+ self.snapshot = [self]
19
+ self.width = window.innerWidth
20
+ self.height = window.innerHeight
21
+ }
22
+
23
+ const self = event({
24
+ width: 1920,
25
+ height: 1080,
26
+ change,
27
+ mount,
28
+ clean,
29
+ }) as EventState<WindowSizeState>
30
+
31
+ self.snapshot = [self]
32
+
33
+ return self
34
+ }
@@ -0,0 +1,19 @@
1
+ import { useSyncExternalStore } from 'react'
2
+ import { windowSizeEvent } from './index'
3
+ import { WindowSizeState } from './types'
4
+ import { EventState } from 'reev/src'
5
+
6
+ let self: EventState<WindowSizeState>
7
+
8
+ const subscribe = (callback = () => {}) => {
9
+ self({ callback }).mount()
10
+ return () => self({ callback }).clean()
11
+ }
12
+
13
+ const getSnapshot = () => self.snapshot
14
+
15
+ export const useWindowSize = () => {
16
+ if (!self) self = windowSizeEvent()
17
+ const [ret] = useSyncExternalStore(subscribe, getSnapshot, getSnapshot)
18
+ return ret
19
+ }
@@ -0,0 +1,11 @@
1
+ export interface WindowSizeState {
2
+ snapshot: [WindowSizeState]
3
+ width: number
4
+ height: number
5
+ callback(): void
6
+ change(): void
7
+ mount(): void
8
+ clean(): void
9
+ }
10
+
11
+ export type WindowSizeArg = Partial<WindowSizeState> | WindowSizeState['change']