From 8c0d762eb16db91981108e16d3b5043e9ec8aaab Mon Sep 17 00:00:00 2001 From: Du <53305259+myNameIsDu@users.noreply.github.com> Date: Thu, 11 Jan 2024 18:47:01 +0800 Subject: [PATCH] feat: getTranslateDOMPositionXY support enableTransform option (#74) --- src/translateDOMPositionXY.ts | 12 ++++++------ test/styleSpec.js | 10 ++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/translateDOMPositionXY.ts b/src/translateDOMPositionXY.ts index 194d3db..73cf13f 100644 --- a/src/translateDOMPositionXY.ts +++ b/src/translateDOMPositionXY.ts @@ -12,12 +12,11 @@ const TRANSFORM = getVendorPrefixedName('transform'); const BACKFACE_VISIBILITY = getVendorPrefixedName('backfaceVisibility'); export interface Options { + enableTransform?: boolean; enable3DTransform?: boolean; forceUseTransform?: boolean; } -const defaultConfig = { enable3DTransform: true }; - const appendLeftAndTop = (style: CSSStyleDeclaration, x = 0, y = 0) => { style.left = `${x}px`; style.top = `${y}px`; @@ -38,12 +37,13 @@ const appendTranslate3d = (style: CSSStyleDeclaration, x = 0, y = 0) => { return style; }; -export const getTranslateDOMPositionXY = (conf: Options = defaultConfig) => { - if (conf.forceUseTransform) { +export const getTranslateDOMPositionXY = (conf?: Options) => { + const { enableTransform = true, enable3DTransform = true, forceUseTransform } = conf || {}; + if (forceUseTransform) { return conf.enable3DTransform ? appendTranslate3d : appendTranslate; } - if (BrowserSupportCore.hasCSSTransforms()) { + if (BrowserSupportCore.hasCSSTransforms() && enableTransform) { const ua = g.window ? g.window.navigator.userAgent : 'UNKNOWN'; const isSafari = /Safari\//.test(ua) && !/Chrome\//.test(ua); @@ -51,7 +51,7 @@ export const getTranslateDOMPositionXY = (conf: Options = defaultConfig) => { // of GPU-accelerated layers // (see bug https://bugs.webkit.org/show_bug.cgi?id=61824). // Use 2D translation instead. - if (!isSafari && BrowserSupportCore.hasCSS3DTransforms() && conf.enable3DTransform) { + if (!isSafari && BrowserSupportCore.hasCSS3DTransforms() && enable3DTransform) { return appendTranslate3d; } diff --git a/test/styleSpec.js b/test/styleSpec.js index a50f571..56acfb5 100644 --- a/test/styleSpec.js +++ b/test/styleSpec.js @@ -102,5 +102,15 @@ describe('Style', () => { expect(style.transform).to.contain('translate(10px,20px)'); }); + it('Should be use position', () => { + const translateDOMPositionXY = getTranslateDOMPositionXY({ + enableTransform: false + }); + const style = {}; + translateDOMPositionXY(style, 10, 20); + + expect(style.left).to.contain('10px'); + expect(style.top).to.contain('20px'); + }); }); });