UNPKG

refun

Version:

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

1 lines • 2.54 kB
{"version":3,"sources":["map-pressed.ts"],"names":["mapPressed","props","origOnPressInRef","origOnPressOutRef","isPressed","setIsPressed","onPressInRef","NOOP","onPressOutRef","current","onPressIn","onPressOut","Boolean"],"mappings":";;;;;;;;;;;AAAA;;AACA;;AAQO,IAAMA,UAAU,GAAG,SAAbA,UAAa,CAAwBC,KAAxB,EAAgE;AACxF,MAAMC,gBAAgB,GAAG,oBAAzB;AACA,MAAMC,iBAAiB,GAAG,oBAA1B;;AAFwF,kBAGtD,qBAAS,KAAT,CAHsD;AAAA;AAAA,MAGjFC,SAHiF;AAAA,MAGtEC,YAHsE;;AAIxF,MAAMC,YAAY,GAAG,mBAAmBC,UAAnB,CAArB;AACA,MAAMC,aAAa,GAAG,mBAAmBD,UAAnB,CAAtB;AAEAL,EAAAA,gBAAgB,CAACO,OAAjB,GAA2BR,KAAK,CAACS,SAAjC;AACAP,EAAAA,iBAAiB,CAACM,OAAlB,GAA4BR,KAAK,CAACU,UAAlC;;AAEA,MAAIL,YAAY,CAACG,OAAb,KAAyBF,UAA7B,EAAmC;AACjCD,IAAAA,YAAY,CAACG,OAAb,GAAuB,YAAM;AAC3BJ,MAAAA,YAAY,CAAC,IAAD,CAAZ;;AAEA,UAAI,sBAAWH,gBAAgB,CAACO,OAA5B,CAAJ,EAA0C;AACxCP,QAAAA,gBAAgB,CAACO,OAAjB;AACD;AACF,KAND;AAOD;;AAED,MAAID,aAAa,CAACC,OAAd,KAA0BF,UAA9B,EAAoC;AAClCC,IAAAA,aAAa,CAACC,OAAd,GAAwB,YAAM;AAC5BJ,MAAAA,YAAY,CAAC,KAAD,CAAZ;;AAEA,UAAI,sBAAWF,iBAAiB,CAACM,OAA7B,CAAJ,EAA2C;AACzCN,QAAAA,iBAAiB,CAACM,OAAlB;AACD;AACF,KAND;AAOD;;AAED,oCACKR,KADL;AAEEG,IAAAA,SAAS,EAAEA,SAAS,IAAIQ,OAAO,CAACX,KAAK,CAACG,SAAP,CAFjC;AAGEM,IAAAA,SAAS,EAAEJ,YAAY,CAACG,OAH1B;AAIEE,IAAAA,UAAU,EAAEH,aAAa,CAACC;AAJ5B;AAMD,CApCM","sourcesContent":["import { useState, useRef } from 'react'\nimport { isFunction, NOOP } from 'tsfn'\n\nexport type TMapPressed = {\n isPressed?: boolean,\n onPressIn?: () => void,\n onPressOut?: () => void,\n}\n\nexport const mapPressed = <P extends TMapPressed>(props: P): P & Required<TMapPressed> => {\n const origOnPressInRef = useRef<() => void>()\n const origOnPressOutRef = useRef<() => void>()\n const [isPressed, setIsPressed] = useState(false)\n const onPressInRef = useRef<() => void>(NOOP)\n const onPressOutRef = useRef<() => void>(NOOP)\n\n origOnPressInRef.current = props.onPressIn\n origOnPressOutRef.current = props.onPressOut\n\n if (onPressInRef.current === NOOP) {\n onPressInRef.current = () => {\n setIsPressed(true)\n\n if (isFunction(origOnPressInRef.current)) {\n origOnPressInRef.current()\n }\n }\n }\n\n if (onPressOutRef.current === NOOP) {\n onPressOutRef.current = () => {\n setIsPressed(false)\n\n if (isFunction(origOnPressOutRef.current)) {\n origOnPressOutRef.current()\n }\n }\n }\n\n return {\n ...props,\n isPressed: isPressed || Boolean(props.isPressed),\n onPressIn: onPressInRef.current,\n onPressOut: onPressOutRef.current,\n }\n}\n"],"file":"map-pressed.js"}