Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions docs/examples/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const builtinPlacements = {
adjustY: true,
},
offset: [0, 10],
htmlRegion: 'scroll' as const,
},
left: {
points: ['cr', 'cl'],
Expand All @@ -54,7 +55,7 @@ const builtinPlacements = {
},
};

const popupPlacement = 'topLeft';
const popupPlacement = 'bottom';

export default () => {
console.log('Demo Render!');
Expand Down Expand Up @@ -140,7 +141,7 @@ export default () => {
</div>
}
popupStyle={{ boxShadow: '0 0 5px red' }}
// popupVisible
popupVisible
// getPopupContainer={() => popHolderRef.current}
popupPlacement={popupPlacement}
builtinPlacements={builtinPlacements}
Expand All @@ -163,6 +164,8 @@ export default () => {
</div>
</div>
</div>

{/* <div style={{ height: '100vh' }} /> */}
</React.StrictMode>
);
};
46 changes: 31 additions & 15 deletions src/hooks/useAlign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ export default function useAlign(
const doc = popupElement.ownerDocument;
const win = getWin(popupElement);

// Placement
const placementInfo: AlignType =
builtinPlacements[placement] || popupAlign || {};

// Reset first
popupElement.style.left = '0';
popupElement.style.top = '0';
Expand All @@ -150,18 +154,34 @@ export default function useAlign(
}
const popupRect = popupElement.getBoundingClientRect();
const { width, height } = win.getComputedStyle(popupElement);
const { clientWidth, clientHeight } = doc.documentElement;
const {
clientWidth,
clientHeight,
scrollWidth,
scrollHeight,
scrollTop,
scrollLeft,
} = doc.documentElement;

const popupHeight = popupRect.height;
const popupWidth = popupRect.width;

// Get bounding of visible area
const visibleArea = {
left: 0,
top: 0,
right: clientWidth,
bottom: clientHeight,
};
const visibleArea =
placementInfo.htmlRegion === 'scroll'
? // Scroll region should take scrollLeft & scrollTop into account
{
left: -scrollLeft,
top: -scrollTop,
right: scrollWidth - scrollLeft,
bottom: scrollHeight - scrollTop,
}
: {
left: 0,
top: 0,
right: clientWidth,
bottom: clientHeight,
};

(scrollerList || []).forEach((ele) => {
const eleRect = ele.getBoundingClientRect();
Expand All @@ -179,10 +199,10 @@ export default function useAlign(
Math.round((eleRect.height / eleOutHeight) * 1000) / 1000,
);

const scrollWidth = (eleOutWidth - eleInnerWidth) * scaleX;
const scrollHeight = (eleOutHeight - eleInnerHeight) * scaleY;
const eleRight = eleRect.x + eleRect.width - scrollWidth;
const eleBottom = eleRect.y + eleRect.height - scrollHeight;
const eleScrollWidth = (eleOutWidth - eleInnerWidth) * scaleX;
const eleScrollHeight = (eleOutHeight - eleInnerHeight) * scaleY;
const eleRight = eleRect.x + eleRect.width - eleScrollWidth;
const eleBottom = eleRect.y + eleRect.height - eleScrollHeight;

visibleArea.left = Math.max(visibleArea.left, eleRect.left);
visibleArea.top = Math.max(visibleArea.top, eleRect.top);
Expand All @@ -202,10 +222,6 @@ export default function useAlign(
Math.round((popupHeight / parseFloat(height)) * 1000) / 1000,
);

// Placement
const placementInfo: AlignType =
builtinPlacements[placement] || popupAlign || {};

// Offset
const { offset, targetOffset } = placementInfo;
const [popupOffsetX = 0, popupOffsetY = 0] = offset || [];
Expand Down
6 changes: 6 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ export interface AlignType {
};
/** Auto adjust arrow position */
autoArrow?: boolean;
/**
* Config visible region check of html node. Default `visible`:
* - `visible`: The visible region of user browser window. Use `clientHeight` for check.
* - `scroll`: The whole region of the html scroll area. Use `scrollHeight` for check.
*/
htmlRegion?: 'visible' | 'scroll';
/**
* Whether use css right instead of left to position
*/
Expand Down