Skip to content

Commit

Permalink
chore: optimize calculating scrollbar width (#2063)
Browse files Browse the repository at this point in the history
chore: optimize calculating scrollbar width
---------

Co-authored-by: linwenping <[email protected]>
  • Loading branch information
CFDylan and linwenping authored Jul 12, 2024
1 parent fd0d7f9 commit 8df2fd0
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IPositionFunction } from '../position-function';
import { prefix } from './prefix';
import getViewportSize from '../../utils/dom/getViewportSize';
import { SCROLLBAR_WIDTH } from '../../utils/getScrollbarWidth';
import measureScrollbar from '../../utils/dom/measureScrollbar';

/**
* ---------
Expand All @@ -26,7 +26,7 @@ export const BottomLeftInViewport: IPositionFunction = ({
return {
style: {
position: 'absolute',
left: x - offsetWidth - SCROLLBAR_WIDTH,
left: x - offsetWidth - measureScrollbar(),
top: y,
},

Expand Down
4 changes: 2 additions & 2 deletions packages/zent/src/popover/placement/top-left-in-viewport.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IPositionFunction } from '../position-function';
import { prefix } from './prefix';
import getViewportSize from '../../utils/dom/getViewportSize';
import { SCROLLBAR_WIDTH } from '../../utils/getScrollbarWidth';
import measureScrollbar from '../../utils/dom/measureScrollbar';

/**
* ---------
Expand All @@ -26,7 +26,7 @@ export const TopLeftInViewport: IPositionFunction = ({
return {
style: {
position: 'absolute',
left: x - offsetWidth - SCROLLBAR_WIDTH,
left: x - offsetWidth - measureScrollbar(),
top: y,
},

Expand Down
4 changes: 2 additions & 2 deletions packages/zent/src/portal/Portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import PurePortal, { IPurePortalProps } from './PurePortal';
import { getNodeFromSelector, hasScrollbarY } from './util';
import memorize from '../utils/memorize-one';
import createElement from '../utils/dom/createElement';
import { SCROLLBAR_WIDTH } from '../utils/getScrollbarWidth';
import { setValueForStyles } from '../utils/style/CSSPropertyOperations';
import { addEventListener } from '../utils/component/event-handler';
import isBrowser from '../utils/isBrowser';
import { useIsomorphicLayoutEffect } from '../utils/hooks/useIsomorphicLayoutEffect';
import measureScrollbar from '../utils/dom/measureScrollbar';

function diffStyle(prev: React.CSSProperties, next: React.CSSProperties) {
const result: React.CSSProperties = {};
Expand Down Expand Up @@ -50,7 +50,7 @@ function patchElement(parent: HTMLElement) {
} else {
const { overflowY, paddingRight } = parent.style;
const originalPadding = getComputedStyle(parent).paddingRight;
const newPadding = parseFloat(originalPadding || '0') + SCROLLBAR_WIDTH;
const newPadding = parseFloat(originalPadding || '0') + measureScrollbar();
parent.style.overflowY = 'hidden';
parent.style.paddingRight = `${newPadding}px`;
const newMeta: IPatchMeta = {
Expand Down
3 changes: 2 additions & 1 deletion packages/zent/src/utils/dom/measureScrollbar.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import isBrowser from '../isBrowser';
import createElement from './createElement';

let scrollbarWidth = 0;
Expand All @@ -12,7 +13,7 @@ const scrollbarMeasure = {
};

export default function measureScrollbar() {
if (typeof document === 'undefined' || typeof window === 'undefined') {
if (!isBrowser) {
return 0;
}
if (scrollbarWidth) {
Expand Down
25 changes: 0 additions & 25 deletions packages/zent/src/utils/getScrollbarWidth.ts

This file was deleted.

0 comments on commit 8df2fd0

Please sign in to comment.