UNPKG

5.26 kBSource Map (JSON)View Raw
1{"version":3,"names":["_tools","require","_react","_react2","computeOrigin","val","size","isStr","isString","isPerc","endsWith","num","replace","perc","isNaN","useTransformOrigin","transformOrigin","transform","onLayoutProp","onLayout","needsLayoutX","needsLayoutY","x","y","undefined","everDidLayout","useEverHadValue","setSize","useState","width","height","useCallback","e","nativeEvent","layout","splice","translateY","translateX","push","exports"],"sources":["useTransformOrigin.ts"],"sourcesContent":["/* eslint-disable react-hooks/rules-of-hooks */\nimport { isString } from '@legendapp/tools';\nimport { useEverHadValue } from '@legendapp/tools/react';\nimport { useCallback, useState } from 'react';\nimport type { LayoutChangeEvent } from 'react-native';\nimport type { TransformOrigin } from './Interfaces';\n\nfunction computeOrigin(val: TransformOrigin, size: number) {\n const isStr = isString(val);\n const isPerc = isStr && (val as string).endsWith('%');\n // Chop off a % or px\n let num = isStr ? +(val as string).replace(/%|px/, '') : val;\n // Divide by 100 for percent or by view size if pixels\n const perc = isPerc ? +num / 100 : +num / size;\n // Offset by half of the size\n if (!isNaN(perc)) {\n num = (perc - 0.5) * size;\n } else {\n // Fallback to no origin\n num = 0;\n }\n\n return num;\n}\n\nexport const useTransformOrigin = function useTransformOrigin(\n transformOrigin: { x?: TransformOrigin; y?: TransformOrigin },\n transform: any[],\n onLayoutProp: (e: LayoutChangeEvent) => void\n) {\n let onLayout = onLayoutProp;\n let needsLayoutX = false;\n let needsLayoutY = false;\n\n // Compute whether x and y need layout based on input\n if (transformOrigin) {\n let { x, y } = transformOrigin;\n needsLayoutX = x !== undefined && x !== '50%';\n needsLayoutY = y !== undefined && y !== '50%';\n }\n\n // Compute whether we ever needed layout so we don't remove a hook if the origin is removed\n const everDidLayout = useEverHadValue(!!transformOrigin, true);\n\n if (everDidLayout) {\n const [size, setSize] = useState<{ width: number; height: number }>({ width: 0, height: 0 });\n onLayout = useCallback(\n (e: LayoutChangeEvent) => {\n setSize(e.nativeEvent.layout);\n onLayoutProp?.(e);\n },\n [onLayoutProp]\n );\n\n if (transformOrigin && transform) {\n let { x, y } = transformOrigin;\n\n // Compute x and y origins\n x = needsLayoutX ? computeOrigin(x, size.width) : 0;\n y = needsLayoutY ? computeOrigin(y, size.height) : 0;\n\n // First move the center of the view to the origin\n transform.splice(0, 0, {\n translateY: y,\n });\n transform.splice(0, 0, {\n translateX: x,\n });\n\n // Restore it back the the original position after transforming\n transform.push({\n translateX: -x,\n });\n transform.push({\n translateY: -y,\n });\n }\n }\n\n return onLayout;\n};\n"],"mappings":";;;;;;AACA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,OAAA,GAAAF,OAAA;AAHA;;AAOA,SAASG,aAAaA,CAACC,GAAoB,EAAEC,IAAY,EAAE;EACvD,MAAMC,KAAK,GAAG,IAAAC,eAAQ,EAACH,GAAG,CAAC;EAC3B,MAAMI,MAAM,GAAGF,KAAK,IAAKF,GAAG,CAAYK,QAAQ,CAAC,GAAG,CAAC;EACrD;EACA,IAAIC,GAAG,GAAGJ,KAAK,GAAG,CAAEF,GAAG,CAAYO,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,GAAGP,GAAG;EAC5D;EACA,MAAMQ,IAAI,GAAGJ,MAAM,GAAG,CAACE,GAAG,GAAG,GAAG,GAAG,CAACA,GAAG,GAAGL,IAAI;EAC9C;EACA,IAAI,CAACQ,KAAK,CAACD,IAAI,CAAC,EAAE;IACdF,GAAG,GAAG,CAACE,IAAI,GAAG,GAAG,IAAIP,IAAI;EAC7B,CAAC,MAAM;IACH;IACAK,GAAG,GAAG,CAAC;EACX;EAEA,OAAOA,GAAG;AACd;AAEO,MAAMI,kBAAkB,GAAG,SAASA,kBAAkBA,CACzDC,eAA6D,EAC7DC,SAAgB,EAChBC,YAA4C,EAC9C;EACE,IAAIC,QAAQ,GAAGD,YAAY;EAC3B,IAAIE,YAAY,GAAG,KAAK;EACxB,IAAIC,YAAY,GAAG,KAAK;;EAExB;EACA,IAAIL,eAAe,EAAE;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