Skip to content

Commit

Permalink
feat: getTranslateDOMPositionXY support enableTransform option (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
myNameIsDu committed Jan 11, 2024
1 parent b318827 commit 8c0d762
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/translateDOMPositionXY.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand All @@ -38,20 +37,21 @@ 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);

// It appears that Safari messes up the composition order
// 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;
}

Expand Down
10 changes: 10 additions & 0 deletions test/styleSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

1 comment on commit 8c0d762

@vercel
Copy link

@vercel vercel bot commented on 8c0d762 Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

dom-lib – ./

dom-lib.vercel.app
dom-lib-rsuite.vercel.app
dom-lib-git-master-rsuite.vercel.app

Please sign in to comment.