Files
Folo/packages/hooks/src/useInputComposition.ts
T
InneiandInnei 70acf916e5 refactor: Separate main application and external pages and SEO support (#987)
* init

* chore: move tw config

Signed-off-by: Innei <tukon479@gmail.com>

* chore: update deps

Signed-off-by: Innei <tukon479@gmail.com>

* chore: add peer deps

Signed-off-by: Innei <tukon479@gmail.com>

* init

* update

Signed-off-by: Innei <tukon479@gmail.com>

* test

Signed-off-by: Innei <tukon479@gmail.com>

* deps

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update hack

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* daisyui

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

* update

* update

Signed-off-by: Innei <tukon479@gmail.com>

* chore: auto-fix linting and formatting issues

* feat: masonry

Signed-off-by: Innei <tukon479@gmail.com>

* padding

Signed-off-by: Innei <tukon479@gmail.com>

* fix og

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* fix: masonry re-render

Signed-off-by: Innei <tukon479@gmail.com>

* update grid

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* add op

Signed-off-by: Innei <tukon479@gmail.com>

* fix: error boundary lazy suspense

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* follow

Signed-off-by: Innei <tukon479@gmail.com>

* feat: profile

Signed-off-by: Innei <tukon479@gmail.com>

* profile

Signed-off-by: Innei <tukon479@gmail.com>

* refactor: og

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* chore: auto-fix linting and formatting issues

---------

Signed-off-by: Innei <tukon479@gmail.com>
Co-authored-by: Innei <Innei@users.noreply.github.com>
2024-10-21 23:02:25 +08:00

97 lines
2.6 KiB
TypeScript

import type { CompositionEventHandler } from "react"
import { useCallback, useEffect, useRef } from "react"
type InputElementAttributes = React.DetailedHTMLProps<
React.InputHTMLAttributes<HTMLInputElement>,
HTMLInputElement
>
type TextareaElementAttributes = React.DetailedHTMLProps<
React.TextareaHTMLAttributes<HTMLTextAreaElement>,
HTMLTextAreaElement
>
export const useInputComposition = <E = HTMLInputElement>(
props: Pick<
E extends HTMLInputElement
? InputElementAttributes
: E extends HTMLTextAreaElement
? TextareaElementAttributes
: never,
"onKeyDown" | "onCompositionEnd" | "onCompositionStart" | "onKeyDownCapture"
>,
) => {
const { onKeyDown, onCompositionStart, onCompositionEnd } = props
const isCompositionRef = useRef(false)
const currentInputTargetRef = useRef<E | null>(null)
const handleCompositionStart: CompositionEventHandler<E> = useCallback(
(e) => {
currentInputTargetRef.current = e.target as E
isCompositionRef.current = true
onCompositionStart?.(e as any)
},
[onCompositionStart],
)
const handleCompositionEnd: CompositionEventHandler<E> = useCallback(
(e) => {
currentInputTargetRef.current = null
isCompositionRef.current = false
onCompositionEnd?.(e as any)
},
[onCompositionEnd],
)
const handleKeyDown: React.KeyboardEventHandler<E> = useCallback(
(e: any) => {
// The keydown event stop emit when the composition is being entered
if (isCompositionRef.current) {
e.stopPropagation()
return
}
onKeyDown?.(e)
if (e.key === "Escape") {
e.preventDefault()
e.stopPropagation()
if (!isCompositionRef.current) {
e.currentTarget.blur()
}
}
},
[onKeyDown],
)
// Register a global capture keydown listener to prevent the radix `useEscapeKeydown` from working
useEffect(() => {
const handleGlobalKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape" && currentInputTargetRef.current) {
e.stopPropagation()
e.preventDefault()
}
}
document.addEventListener("keydown", handleGlobalKeyDown, { capture: true })
return () => {
document.removeEventListener("keydown", handleGlobalKeyDown, { capture: true })
}
}, [])
const ret = {
onCompositionEnd: handleCompositionEnd,
onCompositionStart: handleCompositionStart,
onKeyDown: handleKeyDown,
}
Object.defineProperty(ret, "isCompositionRef", {
value: isCompositionRef,
enumerable: false,
})
return ret as typeof ret & {
isCompositionRef: typeof isCompositionRef
}
}