Skip to content
Open
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
11 changes: 9 additions & 2 deletions packages/core/src/useClipboard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ import { useEventListener } from '../useEventListener'
import { defaultWindow } from '../utils/browser'
import type { UseClipboard } from './interface'

export const useClipboard: UseClipboard = (): readonly [
export const useClipboard: UseClipboard = (options = { read: false }): readonly [
string,
(txt: string) => Promise<void>,
] => {
const [text, setText] = useState('')

const read = options.read ?? false

const updateText = useCallback(async () => {
// if read-only, do not run
if (read) {
return
}

// Check if document is focused before attempting to read clipboard
if (!document.hasFocus()) {
return
Expand All @@ -23,7 +30,7 @@ export const useClipboard: UseClipboard = (): readonly [
// Handle cases where clipboard access is denied or unavailable
console.warn('Failed to read clipboard:', error)
}
}, [])
}, [read])

useEventListener('copy', updateText)
useEventListener('cut', updateText)
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/useClipboard/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
* @returns 返回只读元组.
* @returns_zh-Hant 返回唯讀元組.
*/
export type UseClipboard = () => readonly [string, (txt: string) => Promise<void>]
export type UseClipboard = (options?: {
read?: boolean
}) => readonly [string, (txt: string) => Promise<void>]