UNPKG

refun

Version:

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

1 lines 2.67 kB
{"version":3,"sources":["map-pressed.ts"],"names":["useState","useRef","isFunction","NOOP","mapPressed","props","origOnPressInRef","origOnPressOutRef","isPressed","setIsPressed","onPressInRef","onPressOutRef","current","onPressIn","onPressOut","Boolean"],"mappings":";AAAA,SAASA,QAAT,EAAmBC,MAAnB,QAAiC,OAAjC;AACA,SAASC,UAAT,EAAqBC,IAArB,QAAiC,MAAjC;AAQA,OAAO,IAAMC,UAAU,GAAG,SAAbA,UAAa,CAAwBC,KAAxB,EAAgE;AACxF,MAAMC,gBAAgB,GAAGL,MAAM,EAA/B;AACA,MAAMM,iBAAiB,GAAGN,MAAM,EAAhC;;AAFwF,kBAGtDD,QAAQ,CAAC,KAAD,CAH8C;AAAA;AAAA,MAGjFQ,SAHiF;AAAA,MAGtEC,YAHsE;;AAIxF,MAAMC,YAAY,GAAGT,MAAM,CAAaE,IAAb,CAA3B;AACA,MAAMQ,aAAa,GAAGV,MAAM,CAAaE,IAAb,CAA5B;AAEAG,EAAAA,gBAAgB,CAACM,OAAjB,GAA2BP,KAAK,CAACQ,SAAjC;AACAN,EAAAA,iBAAiB,CAACK,OAAlB,GAA4BP,KAAK,CAACS,UAAlC;;AAEA,MAAIJ,YAAY,CAACE,OAAb,KAAyBT,IAA7B,EAAmC;AACjCO,IAAAA,YAAY,CAACE,OAAb,GAAuB,YAAM;AAC3BH,MAAAA,YAAY,CAAC,IAAD,CAAZ;;AAEA,UAAIP,UAAU,CAACI,gBAAgB,CAACM,OAAlB,CAAd,EAA0C;AACxCN,QAAAA,gBAAgB,CAACM,OAAjB;AACD;AACF,KAND;AAOD;;AAED,MAAID,aAAa,CAACC,OAAd,KAA0BT,IAA9B,EAAoC;AAClCQ,IAAAA,aAAa,CAACC,OAAd,GAAwB,YAAM;AAC5BH,MAAAA,YAAY,CAAC,KAAD,CAAZ;;AAEA,UAAIP,UAAU,CAACK,iBAAiB,CAACK,OAAnB,CAAd,EAA2C;AACzCL,QAAAA,iBAAiB,CAACK,OAAlB;AACD;AACF,KAND;AAOD;;AAED,2BACKP,KADL;AAEEG,IAAAA,SAAS,EAAEA,SAAS,IAAIO,OAAO,CAACV,KAAK,CAACG,SAAP,CAFjC;AAGEK,IAAAA,SAAS,EAAEH,YAAY,CAACE,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"}