UNPKG

element-plus

Version:

A Component Library for Vue 3

1 lines 2.67 kB
{"version":3,"file":"use-grid-wheel.mjs","names":[],"sources":["../../../../../../../packages/components/virtual-list/src/hooks/use-grid-wheel.ts"],"sourcesContent":["import { cAF, rAF } from '@element-plus/utils'\n\nimport type { ComputedRef } from 'vue'\n\ninterface GridWheelState {\n atXStartEdge: ComputedRef<boolean>\n atXEndEdge: ComputedRef<boolean>\n atYStartEdge: ComputedRef<boolean>\n atYEndEdge: ComputedRef<boolean>\n}\n\ntype GridWheelHandler = (x: number, y: number) => void\n\nexport const useGridWheel = (\n { atXEndEdge, atXStartEdge, atYEndEdge, atYStartEdge }: GridWheelState,\n onWheelDelta: GridWheelHandler\n) => {\n let frameHandle: number | null = null\n let xOffset = 0\n let yOffset = 0\n\n const hasReachedEdge = (x: number, y: number) => {\n const xEdgeReached =\n (x < 0 && atXStartEdge.value) || (x > 0 && atXEndEdge.value)\n const yEdgeReached =\n (y < 0 && atYStartEdge.value) || (y > 0 && atYEndEdge.value)\n return xEdgeReached || yEdgeReached\n }\n\n const onWheel = (e: WheelEvent) => {\n cAF(frameHandle!)\n\n let x = e.deltaX\n let y = e.deltaY\n // Simulate native behavior when using touch pad/track pad for wheeling.\n if (Math.abs(x) > Math.abs(y)) {\n y = 0\n } else {\n x = 0\n }\n\n // Special case for windows machine with shift key + wheel scrolling\n if (e.shiftKey && y !== 0) {\n x = y\n y = 0\n }\n\n if (hasReachedEdge(x, y)) {\n // #23524\n // Prevent browser back navigation when the table can still scroll\n // horizontally but the Y-axis normalization dropped the X delta and the Y edge was hit instead.\n if (e.deltaX !== 0 && x === 0) {\n e.preventDefault()\n }\n return\n }\n\n xOffset += x\n yOffset += y\n\n e.preventDefault()\n\n frameHandle = rAF(() => {\n onWheelDelta(xOffset, yOffset)\n xOffset = 0\n yOffset = 0\n })\n }\n\n return {\n hasReachedEdge,\n onWheel,\n }\n}\n"],"mappings":";;AAaA,MAAa,gBACX,EAAE,YAAY,cAAc,YAAY,gBACxC,iBACG;CACH,IAAI,cAA6B;CACjC,IAAI,UAAU;CACd,IAAI,UAAU;CAEd,MAAM,kBAAkB,GAAW,MAAc;EAC/C,MAAM,eACH,IAAI,KAAK,aAAa,SAAW,IAAI,KAAK,WAAW;EACxD,MAAM,eACH,IAAI,KAAK,aAAa,SAAW,IAAI,KAAK,WAAW;EACxD,OAAO,gBAAgB;;CAGzB,MAAM,WAAW,MAAkB;EACjC,IAAI,YAAa;EAEjB,IAAI,IAAI,EAAE;EACV,IAAI,IAAI,EAAE;EAEV,IAAI,KAAK,IAAI,EAAE,GAAG,KAAK,IAAI,EAAE,EAC3B,IAAI;OAEJ,IAAI;EAIN,IAAI,EAAE,YAAY,MAAM,GAAG;GACzB,IAAI;GACJ,IAAI;;EAGN,IAAI,eAAe,GAAG,EAAE,EAAE;GAIxB,IAAI,EAAE,WAAW,KAAK,MAAM,GAC1B,EAAE,gBAAgB;GAEpB;;EAGF,WAAW;EACX,WAAW;EAEX,EAAE,gBAAgB;EAElB,cAAc,UAAU;GACtB,aAAa,SAAS,QAAQ;GAC9B,UAAU;GACV,UAAU;IACV;;CAGJ,OAAO;EACL;EACA;EACD"}