Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support concurrent safe #118

Merged
merged 2 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/core/src/useActiveElement/index.ts
childrentime marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback, useState } from 'react'
import { useEventListener } from '../useEventListener'
import { useMount } from '../useMount'
import { defaultWindow } from '../utils/browser'
import type { UseActiveElement } from './interface'

export const useActiveElement: UseActiveElement = <T extends Element>(): T | null => {
Expand All @@ -9,8 +10,8 @@ export const useActiveElement: UseActiveElement = <T extends Element>(): T | nul
const listener = useCallback(() => {
setActive(window?.document.activeElement as T)
}, [])
useEventListener('blur', listener, () => window, true)
useEventListener('focus', listener, () => window, true)
useEventListener('blur', listener, defaultWindow, true)
useEventListener('focus', listener, defaultWindow, true)

useMount(() => {
setActive(window?.document.activeElement as T)
Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/useCustomCompareEffect/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { DependencyList, EffectCallback } from 'react'
import { useEffect, useRef } from 'react'
import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect'
import { useUpdate } from '../useUpdate'
import type { DepsEqualFnType, UseCustomCompareEffect } from './interface'

export const useCustomCompareEffect: UseCustomCompareEffect = <TDeps extends DependencyList>(
Expand All @@ -22,11 +24,19 @@ export const useCustomCompareEffect: UseCustomCompareEffect = <TDeps extends Dep
}

const ref = useRef<TDeps | undefined>(undefined)
const forceUpdate = useUpdate()

if (!ref.current || !depsEqual(deps, ref.current)) {
if (!ref.current) {
ref.current = deps
}

useIsomorphicLayoutEffect(() => {
if (!depsEqual(deps, ref.current as TDeps)) {
ref.current = deps
forceUpdate()
}
})

// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(effect, ref.current)
}
8 changes: 4 additions & 4 deletions packages/core/src/useDarkMode/index.ts
childrentime marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { isBrowser } from '../utils/is'
import useStorage from '../createStorage'
import type { UseDarkMode, UseDarkOptions } from './interface'

function value() {
return window.matchMedia('(prefers-color-scheme: dark)').matches
}

export const useDarkMode: UseDarkMode = (options: UseDarkOptions) => {
const {
selector = 'html',
Expand All @@ -14,10 +18,6 @@ export const useDarkMode: UseDarkMode = (options: UseDarkOptions) => {
defaultValue = false,
} = options

const value = (): boolean => {
return window.matchMedia('(prefers-color-scheme: dark)').matches
}

const [dark, setDark] = useStorage<boolean>(
storageKey,
defaultValue,
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/useLatest/index.ts
childrentime marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { MutableRefObject } from 'react'
import { useRef } from 'react'
import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect'
import type { UseLatest } from './interface'

export const useLatest: UseLatest = <T>(value: T): MutableRefObject<T> => {
const ref = useRef(value)
ref.current = value
useIsomorphicLayoutEffect(() => {
ref.current = value
}, [value])
return ref
}
3 changes: 2 additions & 1 deletion packages/core/src/useMouse/index.ts
childrentime marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { BasicTarget } from '../utils/domTarget'
import { getTargetElement } from '../utils/domTarget'
import { useEventListener } from '../useEventListener'
import { useRafState } from '../useRafState'
import { defaultDocument } from '../utils/browser'
import type { UseMouse, UseMouseCursorState } from './interface'

const initState: UseMouseCursorState = {
Expand Down Expand Up @@ -53,7 +54,7 @@ export const useMouse: UseMouse = (target?: BasicTarget): UseMouseCursorState =>
}
setState(newState)
},
() => document,
defaultDocument,
)

return state
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utils/browser.ts
childrentime marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export function off<T extends Window | Document | HTMLElement | EventTarget>(
}

export const defaultWindow = /* #__PURE__ */ isBrowser ? window : undefined
export const defaultDocument = /* #__PURE__ */ isBrowser ? document : undefined
Loading