UNPKG

refun

Version:

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

1 lines • 2.68 kB
{"version":3,"sources":["map-hovered.ts"],"names":["mapHovered","props","isHovered","setIsHovered","origOnPointerEnterRef","origOnPointerLeaveRef","onPointerEnterRef","NOOP","onPointerLeaveRef","current","onPointerEnter","onPointerLeave","Boolean"],"mappings":";;;;;;;;;;;AAAA;;AACA;;AAQO,IAAMA,UAAU,GAAG,SAAbA,UAAa,CAAwBC,KAAxB,EAAgE;AAAA,kBACtD,qBAAS,KAAT,CADsD;AAAA;AAAA,MACjFC,SADiF;AAAA,MACtEC,YADsE;;AAGxF,MAAMC,qBAAqB,GAAG,oBAA9B;AACA,MAAMC,qBAAqB,GAAG,oBAA9B;AACA,MAAMC,iBAAiB,GAAG,mBAAmBC,UAAnB,CAA1B;AACA,MAAMC,iBAAiB,GAAG,mBAAmBD,UAAnB,CAA1B;AAEAH,EAAAA,qBAAqB,CAACK,OAAtB,GAAgCR,KAAK,CAACS,cAAtC;AACAL,EAAAA,qBAAqB,CAACI,OAAtB,GAAgCR,KAAK,CAACU,cAAtC;;AAEA,MAAIL,iBAAiB,CAACG,OAAlB,KAA8BF,UAAlC,EAAwC;AACtCD,IAAAA,iBAAiB,CAACG,OAAlB,GAA4B,YAAM;AAChCN,MAAAA,YAAY,CAAC,IAAD,CAAZ;;AAEA,UAAI,sBAAWC,qBAAqB,CAACK,OAAjC,CAAJ,EAA+C;AAC7CL,QAAAA,qBAAqB,CAACK,OAAtB;AACD;AACF,KAND;AAOD;;AAED,MAAID,iBAAiB,CAACC,OAAlB,KAA8BF,UAAlC,EAAwC;AACtCC,IAAAA,iBAAiB,CAACC,OAAlB,GAA4B,YAAM;AAChCN,MAAAA,YAAY,CAAC,KAAD,CAAZ;;AAEA,UAAI,sBAAWE,qBAAqB,CAACI,OAAjC,CAAJ,EAA+C;AAC7CJ,QAAAA,qBAAqB,CAACI,OAAtB;AACD;AACF,KAND;AAOD;;AAED,oCACKR,KADL;AAEEC,IAAAA,SAAS,EAAEA,SAAS,IAAIU,OAAO,CAACX,KAAK,CAACC,SAAP,CAFjC;AAGEQ,IAAAA,cAAc,EAAEJ,iBAAiB,CAACG,OAHpC;AAIEE,IAAAA,cAAc,EAAEH,iBAAiB,CAACC;AAJpC;AAMD,CArCM","sourcesContent":["import { useState, useRef } from 'react'\nimport { isFunction, NOOP } from 'tsfn'\n\nexport type TMapHovered = {\n isHovered?: boolean,\n onPointerEnter?: () => void,\n onPointerLeave?: () => void,\n}\n\nexport const mapHovered = <P extends TMapHovered>(props: P): P & Required<TMapHovered> => {\n const [isHovered, setIsHovered] = useState(false)\n\n const origOnPointerEnterRef = useRef<() => void>()\n const origOnPointerLeaveRef = useRef<() => void>()\n const onPointerEnterRef = useRef<() => void>(NOOP)\n const onPointerLeaveRef = useRef<() => void>(NOOP)\n\n origOnPointerEnterRef.current = props.onPointerEnter\n origOnPointerLeaveRef.current = props.onPointerLeave\n\n if (onPointerEnterRef.current === NOOP) {\n onPointerEnterRef.current = () => {\n setIsHovered(true)\n\n if (isFunction(origOnPointerEnterRef.current)) {\n origOnPointerEnterRef.current()\n }\n }\n }\n\n if (onPointerLeaveRef.current === NOOP) {\n onPointerLeaveRef.current = () => {\n setIsHovered(false)\n\n if (isFunction(origOnPointerLeaveRef.current)) {\n origOnPointerLeaveRef.current()\n }\n }\n }\n\n return {\n ...props,\n isHovered: isHovered || Boolean(props.isHovered),\n onPointerEnter: onPointerEnterRef.current,\n onPointerLeave: onPointerLeaveRef.current,\n }\n}\n"],"file":"map-hovered.js"}