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
24 changes: 18 additions & 6 deletions docs/examples/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export default () => {
console.log('Demo Render!');

const [scale, setScale] = React.useState('1');
const [targetVisible, setTargetVisible] = React.useState(true);

const rootRef = React.useRef<HTMLDivElement>();
const popHolderRef = React.useRef<HTMLDivElement>();
Expand All @@ -78,17 +79,28 @@ export default () => {
ref={rootRef}
style={{ background: 'rgba(0, 0, 255, 0.1)', padding: 16 }}
>
<input
type="number"
value={scale}
onChange={(e) => setScale(e.target.value)}
<div
style={{
position: 'fixed',
left: 0,
top: 0,
zIndex: 9999,
}}
/>
>
<input
type="number"
value={scale}
onChange={(e) => setScale(e.target.value)}
/>
<button
type="button"
onClick={() => {
setTargetVisible((v) => !v);
}}
>
Target Visible: ({String(targetVisible)})
</button>
</div>
<div
id="demo-holder"
ref={popHolderRef}
Expand Down Expand Up @@ -149,13 +161,13 @@ export default () => {
>
<span
style={{
display: 'inline-block',
background: 'green',
color: '#FFF',
paddingBlock: 30,
paddingInline: 70,
opacity: 0.9,
transform: 'scale(0.6)',
display: targetVisible ? 'inline-block' : 'none',
}}
>
Target
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/useAlign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ export default function useAlign(
);

// No need to align since it's not visible in view
if (scaleX === 0 || scaleY === 0) {
if (
scaleX === 0 ||
scaleY === 0 ||
(target instanceof HTMLElement && !target.offsetParent)
) {
return;
}

Expand Down
48 changes: 47 additions & 1 deletion tests/align.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { act, cleanup, fireEvent, render } from '@testing-library/react';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import React from 'react';
import type { TriggerProps } from '../src';
import type { TriggerProps, TriggerRef } from '../src';
import Trigger from '../src';
import { awaitFakeTimer } from './util';

Expand All @@ -14,6 +14,8 @@ export const triggerResize = (target: Element) => {
};

describe('Trigger.Align', () => {
let targetVisible = true;

beforeAll(() => {
spyElementPrototypes(HTMLDivElement, {
getBoundingClientRect: () => ({
Expand All @@ -23,9 +25,16 @@ describe('Trigger.Align', () => {
height: 100,
}),
});

spyElementPrototypes(HTMLElement, {
offsetParent: {
get: () => (targetVisible ? document.body : null),
},
});
});

beforeEach(() => {
targetVisible = true;
jest.useFakeTimers();
});

Expand Down Expand Up @@ -130,4 +139,41 @@ describe('Trigger.Align', () => {
document.querySelector('.rc-trigger-popup-placement-top'),
).toBeTruthy();
});

it('invisible should not align', async () => {
const onPopupAlign = jest.fn();
const triggerRef = React.createRef<TriggerRef>();

render(
<Trigger
popupVisible
popup={<span className="bamboo" />}
popupAlign={{}}
onPopupAlign={onPopupAlign}
ref={triggerRef}
>
<span />
</Trigger>,
);

await awaitFakeTimer();

expect(onPopupAlign).toHaveBeenCalled();
onPopupAlign.mockReset();

for (let i = 0; i < 10; i += 1) {
triggerRef.current!.forceAlign();

await awaitFakeTimer();
expect(onPopupAlign).toHaveBeenCalled();
onPopupAlign.mockReset();
}

// Make invisible
targetVisible = false;

triggerRef.current!.forceAlign();
await awaitFakeTimer();
expect(onPopupAlign).not.toHaveBeenCalled();
});
});
8 changes: 8 additions & 0 deletions tests/arrow.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import {
import Trigger from '../src';

describe('Trigger.Arrow', () => {
beforeAll(() => {
spyElementPrototypes(HTMLElement, {
offsetParent: {
get: () => document.body,
},
});
});

beforeEach(() => {
jest.useFakeTimers();
});
Expand Down
8 changes: 8 additions & 0 deletions tests/basic.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import Trigger from '../src';
import { awaitFakeTimer, placementAlignMap } from './util';

describe('Trigger.Basic', () => {
beforeAll(() => {
spyElementPrototypes(HTMLElement, {
offsetParent: {
get: () => document.body,
},
});
});

beforeEach(() => {
jest.useFakeTimers();
});
Expand Down