Skip to content

Commit

Permalink
feat(useClipboard): support onCopy & onCopiedReset callback
Browse files Browse the repository at this point in the history
  • Loading branch information
vikiboss committed Aug 14, 2024
1 parent 144172a commit 3871844
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/use-clipboard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useSetState } from '../use-set-state'
import { useStableFn } from '../use-stable-fn'
import { useSupported } from '../use-supported'
import { useTimeoutFn } from '../use-timeout-fn'
import { isDefined } from '../utils/basic'
import { unwrapGettable } from '../utils/unwrap'

import type { UsePermissionReturns } from '../use-permission'
Expand All @@ -27,6 +28,14 @@ export interface UseClipboardOptions<Source> {
* @defaultValue 1_500
*/
copiedDuration?: number
/**
* Callback when the text is copied
*/
onCopy?: (text: string) => void
/**
* Callback when the `copied` state is reset
*/
onCopiedReset?: () => void
}

export interface UseClipboardReturns<HasSource> {
Expand Down Expand Up @@ -60,14 +69,20 @@ export function useClipboard(options: UseClipboardOptions<Gettable<string>>): Us
export function useClipboard(
options: UseClipboardOptions<Gettable<string> | undefined> = {},
): UseClipboardReturns<boolean> {
const { read = false, source, copiedDuration = 1500 } = options
const { read = false, source, onCopy, onCopiedReset, copiedDuration = 1500 } = options

const [state, setState] = useSetState({ text: '', copied: false }, { deep: true })
const isSupported = useSupported(() => 'clipboard' in navigator)
const { resume: startTimeout } = useTimeoutFn(() => setState({ copied: false }), copiedDuration)

const { resume: startTimeout } = useTimeoutFn(() => {
setState({ copied: false })
latest.current.onCopiedReset?.()
}, copiedDuration)

const latest = useLatest({
read,
onCopy,
onCopiedReset,
isSupported,
sourceValue: unwrapGettable(source),
})
Expand All @@ -94,16 +109,17 @@ export function useClipboard(
})

const copy = useStableFn(async (value = latest.current.sourceValue) => {
if (latest.current.isSupported && value) {
if (latest.current.isSupported && isAllowed(permissionWrite)) {
await navigator.clipboard.writeText(value)
} else {
legacyCopy(value)
}
if (!isDefined(value)) return

setState({ text: value, copied: true })
startTimeout()
if (latest.current.isSupported && isAllowed(permissionWrite)) {
await navigator.clipboard.writeText(value)
} else {
legacyCopy(value)
}

setState({ text: value, copied: true })
startTimeout()
onCopy?.(value)
})

const clear = useStableFn(() => {
Expand Down

0 comments on commit 3871844

Please sign in to comment.