UNPKG

refun

Version:

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

1 lines 2.81 kB
{"version":3,"sources":["map-hovered.ts"],"names":["useState","useRef","isFunction","NOOP","mapHovered","props","isHovered","setIsHovered","origOnPointerEnterRef","origOnPointerLeaveRef","onPointerEnterRef","onPointerLeaveRef","current","onPointerEnter","onPointerLeave","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;AAAA,kBACtDL,QAAQ,CAAC,KAAD,CAD8C;AAAA;AAAA,MACjFM,SADiF;AAAA,MACtEC,YADsE;;AAGxF,MAAMC,qBAAqB,GAAGP,MAAM,EAApC;AACA,MAAMQ,qBAAqB,GAAGR,MAAM,EAApC;AACA,MAAMS,iBAAiB,GAAGT,MAAM,CAAaE,IAAb,CAAhC;AACA,MAAMQ,iBAAiB,GAAGV,MAAM,CAAaE,IAAb,CAAhC;AAEAK,EAAAA,qBAAqB,CAACI,OAAtB,GAAgCP,KAAK,CAACQ,cAAtC;AACAJ,EAAAA,qBAAqB,CAACG,OAAtB,GAAgCP,KAAK,CAACS,cAAtC;;AAEA,MAAIJ,iBAAiB,CAACE,OAAlB,KAA8BT,IAAlC,EAAwC;AACtCO,IAAAA,iBAAiB,CAACE,OAAlB,GAA4B,YAAM;AAChCL,MAAAA,YAAY,CAAC,IAAD,CAAZ;;AAEA,UAAIL,UAAU,CAACM,qBAAqB,CAACI,OAAvB,CAAd,EAA+C;AAC7CJ,QAAAA,qBAAqB,CAACI,OAAtB;AACD;AACF,KAND;AAOD;;AAED,MAAID,iBAAiB,CAACC,OAAlB,KAA8BT,IAAlC,EAAwC;AACtCQ,IAAAA,iBAAiB,CAACC,OAAlB,GAA4B,YAAM;AAChCL,MAAAA,YAAY,CAAC,KAAD,CAAZ;;AAEA,UAAIL,UAAU,CAACO,qBAAqB,CAACG,OAAvB,CAAd,EAA+C;AAC7CH,QAAAA,qBAAqB,CAACG,OAAtB;AACD;AACF,KAND;AAOD;;AAED,2BACKP,KADL;AAEEC,IAAAA,SAAS,EAAEA,SAAS,IAAIS,OAAO,CAACV,KAAK,CAACC,SAAP,CAFjC;AAGEO,IAAAA,cAAc,EAAEH,iBAAiB,CAACE,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"}