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: Fix FocusTrapZone detection of Layered children #6489

Merged
merged 2 commits into from
Sep 27, 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": "office-ui-fabric-react",
"comment": "Layer: Do not render content until virtual parent is set.",
"type": "patch"
}
],
"packageName": "office-ui-fabric-react",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ import {
} from '../../Utilities';
import { registerLayer, getDefaultTarget, unregisterLayer } from './Layer.notification';

export type ILayerBaseState = {
hasMounted: boolean;
};

const getClassNames = classNamesFunction<ILayerStyleProps, ILayerStyles>();

@customizable('Layer', ['theme', 'hostId'])
export class LayerBase extends BaseComponent<ILayerProps, {}> {
export class LayerBase extends BaseComponent<ILayerProps, ILayerBaseState> {
public static defaultProps: ILayerProps = {
onLayerDidMount: () => undefined,
onLayerWillUnmount: () => undefined
Expand All @@ -30,6 +34,10 @@ export class LayerBase extends BaseComponent<ILayerProps, {}> {
constructor(props: ILayerProps) {
super(props);

this.state = {
hasMounted: false
};

this._warnDeprecations({
onLayerMounted: 'onLayerDidMount'
});
Expand All @@ -50,6 +58,10 @@ export class LayerBase extends BaseComponent<ILayerProps, {}> {
}

public componentDidMount(): void {
// We can safely set state immediately because the ref wrapper will make sure the virtual
// parent has been set before componentDidMount is called.
this.setState({ hasMounted: true });

this._setVirtualParent();

const { onLayerDidMount, onLayerMounted } = this.props;
Expand Down Expand Up @@ -82,10 +94,12 @@ export class LayerBase extends BaseComponent<ILayerProps, {}> {
public render(): React.ReactNode {
const classNames = this._getClassNames();
const { eventBubblingEnabled } = this.props;
const { hasMounted } = this.state;

return (
<span className="ms-layer" ref={this._rootElement}>
<span className="ms-layer" ref={this._handleRootElementRef}>
{this._layerElement &&
hasMounted &&
ReactDOM.createPortal(
eventBubblingEnabled ? (
<Fabric className={classNames.content}>{this.props.children}</Fabric>
Expand Down Expand Up @@ -129,6 +143,20 @@ export class LayerBase extends BaseComponent<ILayerProps, {}> {
);
}

/**
* rootElement wrapper for setting virtual parent as soon as root element ref is available.
*/
private _handleRootElementRef = (ref: HTMLDivElement): void => {
this._rootElement(ref);
if (ref) {
// TODO: Calling _setVirtualParent in this ref wrapper SHOULD allow us to remove
// other calls to _setVirtualParent throughout this class. However,
// as this is an immediate fix for a P0 issue the existing _setVirtualParent
// calls are left for now to minimize potential regression.
this._setVirtualParent();
}
};

/**
* Helper to stop events from bubbling up out of Layer.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2483,7 +2483,7 @@ exports[`Component Examples renders CommandBar.CommandBarButtonAs.Example.tsx co
margin-top: 0;
white-space: nowrap;
}
id="id__13"
id="id__10"
>
Download
</div>
Expand Down Expand Up @@ -2789,7 +2789,7 @@ exports[`Component Examples renders CommandBar.CommandBarButtonAs.Example.tsx co
margin-top: 0;
white-space: nowrap;
}
id="id__19"
id="id__16"
>
Sort
</div>
Expand Down