UNPKG

refun

Version:

A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:

1 lines 4.69 kB
{"version":3,"sources":["map-keyboard-focused.ts"],"names":["useState","useRef","isFunction","NOOP","mapKeyboardFocused","props","isKeyboardFocused","setIsKeyboardFocused","origOnFocusRef","origOnBlurRef","origOnPressInRef","origOnPressOutRef","isPressed","onPressInRef","onPressOutRef","onFocusRef","onBlurRef","current","onFocus","onBlur","onPressIn","onPressOut","Boolean"],"mappings":";AAAA,SAASA,QAAT,EAAmBC,MAAnB,QAAiC,OAAjC;AACA,SAASC,UAAT,EAAqBC,IAArB,QAAiC,MAAjC;AAUA,OAAO,IAAMC,kBAAkB,GAAG,SAArBA,kBAAqB,CAAgCC,KAAhC,EAAgF;AAAA,kBAC9DL,QAAQ,CAAC,KAAD,CADsD;AAAA;AAAA,MACzGM,iBADyG;AAAA,MACtFC,oBADsF;;AAGhH,MAAMC,cAAc,GAAGP,MAAM,EAA7B;AACA,MAAMQ,aAAa,GAAGR,MAAM,EAA5B;AACA,MAAMS,gBAAgB,GAAGT,MAAM,EAA/B;AACA,MAAMU,iBAAiB,GAAGV,MAAM,EAAhC;AACA,MAAMW,SAAS,GAAGX,MAAM,CAAC,KAAD,CAAxB;AACA,MAAMY,YAAY,GAAGZ,MAAM,CAAaE,IAAb,CAA3B;AACA,MAAMW,aAAa,GAAGb,MAAM,CAAaE,IAAb,CAA5B;AACA,MAAMY,UAAU,GAAGd,MAAM,CAAaE,IAAb,CAAzB;AACA,MAAMa,SAAS,GAAGf,MAAM,CAAaE,IAAb,CAAxB;AAEAK,EAAAA,cAAc,CAACS,OAAf,GAAyBZ,KAAK,CAACa,OAA/B;AACAT,EAAAA,aAAa,CAACQ,OAAd,GAAwBZ,KAAK,CAACc,MAA9B;AACAT,EAAAA,gBAAgB,CAACO,OAAjB,GAA2BZ,KAAK,CAACe,SAAjC;AACAT,EAAAA,iBAAiB,CAACM,OAAlB,GAA4BZ,KAAK,CAACgB,UAAlC;;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AAU,CAACE,OAHtB;AAIEE,IAAAA,MAAM,EAAEH,SAAS,CAACC,OAJpB;AAKEG,IAAAA,SAAS,EAAEP,YAAY,CAACI,OAL1B;AAMEI,IAAAA,UAAU,EAAEP,aAAa,CAACG;AAN5B;AAQD,CApEM","sourcesContent":["import { useState, useRef } from 'react'\nimport { isFunction, NOOP } from 'tsfn'\n\nexport type TMapKeyboardFocused = {\n isKeyboardFocused?: boolean,\n onFocus?: () => void,\n onBlur?: () => void,\n onPressIn?: () => void,\n onPressOut?: () => void,\n}\n\nexport const mapKeyboardFocused = <P extends TMapKeyboardFocused>(props: P): P & Required<TMapKeyboardFocused> => {\n const [isKeyboardFocused, setIsKeyboardFocused] = useState(false)\n\n const origOnFocusRef = useRef<() => void>()\n const origOnBlurRef = useRef<() => void>()\n const origOnPressInRef = useRef<() => void>()\n const origOnPressOutRef = useRef<() => void>()\n const isPressed = useRef(false)\n const onPressInRef = useRef<() => void>(NOOP)\n const onPressOutRef = useRef<() => void>(NOOP)\n const onFocusRef = useRef<() => void>(NOOP)\n const onBlurRef = useRef<() => void>(NOOP)\n\n origOnFocusRef.current = props.onFocus\n origOnBlurRef.current = props.onBlur\n origOnPressInRef.current = props.onPressIn\n origOnPressOutRef.current = props.onPressOut\n\n if (onPressInRef.current === NOOP) {\n onPressInRef.current = () => {\n isPressed.current = true\n\n if (isFunction(origOnPressInRef.current)) {\n origOnPressInRef.current()\n }\n }\n }\n\n if (onPressOutRef.current === NOOP) {\n onPressOutRef.current = () => {\n isPressed.current = false\n\n if (isFunction(origOnPressOutRef.current)) {\n origOnPressOutRef.current()\n }\n }\n }\n\n if (onFocusRef.current === NOOP) {\n onFocusRef.current = () => {\n if (isPressed.current === false) {\n setIsKeyboardFocused(true)\n }\n\n if (isFunction(origOnFocusRef.current)) {\n origOnFocusRef.current()\n }\n }\n }\n\n if (onBlurRef.current === NOOP) {\n onBlurRef.current = () => {\n setIsKeyboardFocused(false)\n\n if (isFunction(origOnBlurRef.current)) {\n origOnBlurRef.current()\n }\n }\n }\n\n return {\n ...props,\n isKeyboardFocused: isKeyboardFocused || Boolean(props.isKeyboardFocused),\n onFocus: onFocusRef.current,\n onBlur: onBlurRef.current,\n onPressIn: onPressInRef.current,\n onPressOut: onPressOutRef.current,\n }\n}\n"],"file":"map-keyboard-focused.js"}