Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layer: Reintroduce Portals with Event Blocking #6211

Merged
merged 17 commits into from
Sep 6, 2018
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@uifabric/utilities",
"comment": "Add helpers for setting and detecting portals",
"type": "minor"
}
],
"packageName": "@uifabric/utilities",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "office-ui-fabric-react",
"comment": "Layer: Add optional event blocking. Tooltip: Detect targets in portals.",
"type": "minor"
}
],
"packageName": "office-ui-fabric-react",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "office-ui-fabric-react",
"comment": "Layer: Now use React Portals.",
"type": "minor"
}
],
"packageName": "office-ui-fabric-react",
"email": "[email protected]"
}
1 change: 1 addition & 0 deletions packages/office-ui-fabric-react/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const config = createConfig({

moduleNameMapper: {
// These mappings allow Jest to run snapshot tests against Example files.
'office-ui-fabric-react/lib/codepen/(.*)$': '<rootDir>/lib/codepen/$1',
'office-ui-fabric-react/lib/(.*)$': '<rootDir>/src/$1'
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,18 @@ export class CalloutContentBase extends BaseComponent<ICalloutProps, ICalloutSta
}

private _updatePosition(): void {
// Try to update the target, page might have changed
this._setTargetWindowAndElement(this._getTarget());

const { positions } = this.state;
const hostElement: HTMLElement | null = this._hostElement.current;
const calloutElement: HTMLElement | null = this._calloutElement.current;

if (hostElement && calloutElement) {
// If we expect a target element to position against, we need to wait until `this._target` is resolved. Otherwise
// we can try to position.
const expectsTarget = !!this.props.target;

if (hostElement && calloutElement && (!expectsTarget || this._target)) {
let currentProps: IPositionProps | undefined;
currentProps = assign(currentProps, this.props);
currentProps!.bounds = this._getBounds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { resetIds } from '../Utilities';
import * as DataUtil from '../utilities/exampleData';
import * as mergeStylesSerializer from '@uifabric/jest-serializer-merge-styles';

const ReactDOM = require('react-dom');

// Extend Jest Expect to allow us to map each component example to its own snapshot file.
const snapshotsStateMap = new Map();
const jestSnapshot = require('jest-snapshot');
Expand Down Expand Up @@ -100,10 +102,16 @@ describe('Component Examples', () => {
const realToLocaleString = global.Date.prototype.toLocaleString;
const realToLocaleTimeString = global.Date.prototype.toLocaleTimeString;
const realToLocaleDateString = global.Date.prototype.toLocaleDateString;
const constantDate = new Date(Date.UTC(2017, 13, 6, 4, 41, 20));
const constantDate = new Date(Date.UTC(2017, 0, 6, 4, 41, 20));
kenotron marked this conversation as resolved.
Show resolved Hide resolved
const files: string[] = glob.sync(path.resolve(process.cwd(), 'src/components/**/examples/*Example*.tsx'));
const createPortal = ReactDOM.createPortal;

beforeAll(() => {
// Mock createPortal to capture its component hierarchy in snapshot output.
ReactDOM.createPortal = jest.fn(element => {
return element;
});

// Ensure test output is consistent across machine locale and time zone config.
const mockToLocaleString = () => {
return constantDate.toUTCString();
Expand Down Expand Up @@ -134,6 +142,9 @@ describe('Component Examples', () => {

afterAll(() => {
jest.restoreAllMocks();

ReactDOM.createPortal = createPortal;

global.Date = realDate;
global.Date.prototype.toLocaleString = realToLocaleString;
global.Date.prototype.toLocaleTimeString = realToLocaleTimeString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { ContextualMenu } from './ContextualMenu';
import { canAnyMenuItemsCheck } from './ContextualMenu.base';
import { IContextualMenuItem, ContextualMenuItemType } from './ContextualMenu.types';
import { IContextualMenuRenderItem, IContextualMenuItemStyles } from './ContextualMenuItem.types';
import { LayerBase as Layer } from '../Layer/Layer.base';

describe('ContextualMenu', () => {
afterEach(() => {
Expand Down Expand Up @@ -842,58 +841,6 @@ describe('ContextualMenu', () => {
}).catch(done());
});

it('ContextualMenu menuOpened callback is called only when menu is available', () => {
let layerMounted = false;
let menuMounted = false;
let menuMountedFirst = false;
let layerMountedFirst = false;

// Alter the Layer's prototype so that we can confirm that it mounts before the contextualmenu mounts.
/* tslint:disable:no-function-expression */
Layer.prototype.componentDidMount = (function(componentDidMount): () => void {
return function(): void {
if (menuMounted) {
menuMountedFirst = true;
}
layerMounted = true;
return componentDidMount.call(this);
};
})(Layer.prototype.componentDidMount);
/* tslint:enable:no-function-expression */

const items: IContextualMenuItem[] = [
{
name: 'TestText 1',
key: 'TestKey1',
className: 'testkey1'
},
{
name: 'TestText 2',
key: 'TestKey2'
}
];

const onMenuOpened = (): void => {
if (layerMounted) {
layerMountedFirst = true;
}
menuMounted = true;
};

ReactTestUtils.renderIntoDocument<HTMLDivElement>(
<div>
<button id="target" style={{ top: '10px', left: '10px', height: '0', width: '0px' }}>
{' '}
target{' '}
</button>
<ContextualMenu target="#target" items={items} onMenuOpened={onMenuOpened} />
</div>
);
expect(menuMounted).toEqual(true);
expect(layerMountedFirst).toEqual(true);
expect(menuMountedFirst).toEqual(false);
});

it('merges callout classNames', () => {
ReactTestUtils.renderIntoDocument<IContextualMenuProps>(
<ContextualMenu
Expand Down
Loading