Skip to content

Commit

Permalink
FocusZone and FocusTrapZone example improvements (#8505)
Browse files Browse the repository at this point in the history
* FocusZone and FocusTrapZone example updates

* Use toggles instead of buttons

* tokens
  • Loading branch information
ecraig12345 authored and dzearing committed Apr 1, 2019
1 parent a787974 commit 2a358d7
Show file tree
Hide file tree
Showing 30 changed files with 3,668 additions and 5,807 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "office-ui-fabric-react",
"comment": "FocusZone and FocusTrapZone example improvements",
"type": "patch"
}
],
"packageName": "office-ui-fabric-react",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import * as React from 'react';
import FocusTrapZoneBoxExample from './examples/FocusTrapZone.Box.Example';

import { IDocPageProps } from '../../common/DocPage.types';

import { FocusTrapZoneBoxExample } from './examples/FocusTrapZone.Box.Example';
const FocusTrapZoneBoxExampleCode = require('!raw-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.Box.Example.tsx') as string;
const FocusTrapZoneBoxExampleCodepen = require('!@uifabric/codepen-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.Box.Example.tsx') as string;

import FocusTrapZoneBoxExampleWithFocusableItem from './examples/FocusTrapZone.Box.FocusOnCustomElement.Example';
const FocusTrapZoneBoxExampleWithFocusableItemCode = require('!raw-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.Box.FocusOnCustomElement.Example.tsx') as string;
import { FocusTrapZoneBoxCustomElementExample } from './examples/FocusTrapZone.Box.FocusOnCustomElement.Example';
const FocusTrapZoneBoxCustomElementExampleCode = require('!raw-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.Box.FocusOnCustomElement.Example.tsx') as string;
const FocusTrapZoneBoxCustomElementExampleCodepen = require('!@uifabric/codepen-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.Box.FocusOnCustomElement.Example.tsx') as string;

import FocusTrapZoneBoxClickExample from './examples/FocusTrapZone.Box.Click.Example';
import { FocusTrapZoneBoxClickExample } from './examples/FocusTrapZone.Box.Click.Example';
const FocusTrapZoneBoxClickExampleCode = require('!raw-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.Box.Click.Example.tsx') as string;
const FocusTrapZoneBoxClickExampleCodepen = require('!@uifabric/codepen-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.Box.Click.Example.tsx') as string;

import FocusTrapZoneNestedExample from './examples/FocusTrapZone.Nested.Example';
import { FocusTrapZoneNestedExample } from './examples/FocusTrapZone.Nested.Example';
const FocusTrapZoneNestedExampleCode = require('!raw-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.Nested.Example.tsx') as string;
const FocusTrapZoneNestedExampleCodepen = require('!@uifabric/codepen-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.Nested.Example.tsx') as string;

import FocusTrapZoneNoTabbableExample from './examples/FocusTrapZone.NoTabbable.Example';
import { FocusTrapZoneNoTabbableExample } from './examples/FocusTrapZone.NoTabbable.Example';
const FocusTrapZoneNoTabbableExampleCode = require('!raw-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.NoTabbable.Example.tsx') as string;
const FocusTrapZoneNoTabbableExampleCodepen = require('!@uifabric/codepen-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.NoTabbable.Example.tsx') as string;

import FocusTrapZoneFocusZoneExample from './examples/FocusTrapZone.FocusZone.Example';
const FocusTrapZoneFocusZoneeExampleCode = require('!raw-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.FocusZone.Example.tsx') as string;
import { FocusTrapZoneFocusZoneExample } from './examples/FocusTrapZone.FocusZone.Example';
const FocusTrapZoneFocusZoneExampleCode = require('!raw-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.FocusZone.Example.tsx') as string;
const FocusTrapZoneFocusZoneExampleCodepen = require('!@uifabric/codepen-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.FocusZone.Example.tsx') as string;

import FocusTrapZoneDialogInPanelExample from './examples/FocusTrapZone.DialogInPanel.Example';
import { FocusTrapZoneDialogInPanelExample } from './examples/FocusTrapZone.DialogInPanel.Example';
const FocusTrapZoneDialogInPanelExampleCode = require('!raw-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.DialogInPanel.Example.tsx') as string;
const FocusTrapZoneDialogInPanelExampleCodepen = require('!@uifabric/codepen-loader!office-ui-fabric-react/src/components/FocusTrapZone/examples/FocusTrapZone.DialogInPanel.Example.tsx') as string;

export const FocusTrapZonePageProps: IDocPageProps = {
title: 'FocusTrapZone',
Expand All @@ -29,38 +37,45 @@ export const FocusTrapZonePageProps: IDocPageProps = {
'https://github.com/OfficeDev/office-ui-fabric-react/tree/master/packages/office-ui-fabric-react/src/components/FocusTrapZone',
examples: [
{
title: 'Simple Box',
title: 'Simple box',
code: FocusTrapZoneBoxExampleCode,
codepenJS: FocusTrapZoneBoxExampleCodepen,
view: <FocusTrapZoneBoxExample />
},
{
title: 'Simple Box with focus on custom focusable element',
code: FocusTrapZoneBoxExampleWithFocusableItemCode,
view: <FocusTrapZoneBoxExampleWithFocusableItem />
title: 'Simple box with focus on custom focusable element',
code: FocusTrapZoneBoxCustomElementExampleCode,
codepenJS: FocusTrapZoneBoxCustomElementExampleCodepen,
view: <FocusTrapZoneBoxCustomElementExample />
},
{
title: 'Simple Box with Clicking outside Trap Zone enabled',
title: 'Simple box with clicking outside trap zone enabled',
code: FocusTrapZoneBoxClickExampleCode,
codepenJS: FocusTrapZoneBoxClickExampleCodepen,
view: <FocusTrapZoneBoxClickExample />
},
{
title: 'Multiple Nest FocusTrapZones',
title: 'Multiple nested FocusTrapZones',
code: FocusTrapZoneNestedExampleCode,
codepenJS: FocusTrapZoneNestedExampleCodepen,
view: <FocusTrapZoneNestedExample />
},
{
title: 'FocusTrapZone with FocusZones',
code: FocusTrapZoneFocusZoneeExampleCode,
code: FocusTrapZoneFocusZoneExampleCode,
codepenJS: FocusTrapZoneFocusZoneExampleCodepen,
view: <FocusTrapZoneFocusZoneExample />
},
{
title: 'FocusTrapZone with No Tabbable Elements',
title: 'FocusTrapZone with no tabbable elements',
code: FocusTrapZoneNoTabbableExampleCode,
codepenJS: FocusTrapZoneNoTabbableExampleCodepen,
view: <FocusTrapZoneNoTabbableExample />
},
{
title: 'A Dialog Nested in a Panel',
title: 'A Dialog nested in a Panel',
code: FocusTrapZoneDialogInPanelExampleCode,
codepenJS: FocusTrapZoneDialogInPanelExampleCodepen,
view: <FocusTrapZoneDialogInPanelExample />
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export interface IFocusTrapZoneProps extends React.HTMLAttributes<HTMLDivElement
forceFocusInsideTrap?: boolean;

/**
* Indicates the selector for first focusable item. Only applies if focusPreviouslyFocusedInnerElement == false.
* Class name for first focusable item (do not append a dot).
* Only applies if `focusPreviouslyFocusedInnerElement` is false.
*/
firstFocusableSelector?: string | (() => string);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,101 +1,66 @@
import * as React from 'react';

import { DefaultButton } from 'office-ui-fabric-react/lib/Button';
import { FocusTrapZone } from 'office-ui-fabric-react/lib/FocusTrapZone';
import { Link } from 'office-ui-fabric-react/lib/Link';
import { TextField } from 'office-ui-fabric-react/lib/TextField';
import { Toggle, IToggle } from 'office-ui-fabric-react/lib/Toggle';
import './FocusTrapZone.Box.Example.scss';
import { Stack } from 'office-ui-fabric-react/lib/Stack';

export interface IBoxNoClickExampleExampleState {
isToggled: boolean;
export interface IFocusTrapZoneBoxClickExampleState {
useTrapZone: boolean;
}

export default class BoxNoClickExample extends React.Component<React.HTMLAttributes<HTMLDivElement>, IBoxNoClickExampleExampleState> {
private _toggle: IToggle;
export class FocusTrapZoneBoxClickExample extends React.Component<{}, IFocusTrapZoneBoxClickExampleState> {
public state: IFocusTrapZoneBoxClickExampleState = { useTrapZone: false };

constructor(props: React.HTMLAttributes<HTMLDivElement>) {
super(props);

this.state = {
isToggled: false
};
}
private _toggle = React.createRef<IToggle>();

public render() {
const { isToggled } = this.state;

return (
<div>
<DefaultButton secondaryText="Focuses inside the FocusTrapZone" onClick={this._onButtonClickHandler} text="Go to Trap Zone" />

{(() => {
if (isToggled) {
return (
<FocusTrapZone isClickableOutsideFocusTrap={true} forceFocusInsideTrap={false}>
{this._internalContents()}
</FocusTrapZone>
);
} else {
return <div>{this._internalContents()}</div>;
}
})()}
{this.state.useTrapZone ? (
<FocusTrapZone isClickableOutsideFocusTrap={true} forceFocusInsideTrap={false}>
{this._internalContents()}
</FocusTrapZone>
) : (
this._internalContents()
)}
</div>
);
}

private _internalContents() {
const { isToggled } = this.state;
const { useTrapZone } = this.state;

return (
<div className="ms-FocusTrapZoneBoxExample">
<TextField label="Default TextField" placeholder="Input inside Focus Trap Zone" className="" />
<Link href="" className="">
Hyperlink inside FocusTrapZone
</Link>
<br />
<br />
<Stack
horizontalAlign="start"
tokens={{ childrenGap: 15 }}
styles={{
root: { border: `2px dashed ${useTrapZone ? '#ababab' : 'transparent'}`, padding: 10 }
}}
>
<Toggle
componentRef={this._setRef}
checked={isToggled}
label="Use trap zone"
componentRef={this._toggle}
checked={useTrapZone}
onChange={this._onFocusTrapZoneToggleChanged}
label="Focus Trap Zone"
onText="On"
onText="On (toggle to exit)"
offText="Off"
/>
{(() => {
if (isToggled) {
return (
<DefaultButton secondaryText="Exit Focus Trap Zone" onClick={this._onExitButtonClickHandler} text="Exit Focus Trap Zone" />
);
}
})()}
</div>
<TextField label="Input inside trap zone" styles={{ root: { width: 300 } }} />
<Link href="https://bing.com">Hyperlink inside trap zone</Link>
</Stack>
);
}

private _onButtonClickHandler = (): void => {
this.setState({
isToggled: true
private _onFocusTrapZoneToggleChanged = (ev: React.MouseEvent<HTMLElement>, checked?: boolean): void => {
this.setState({ useTrapZone: !!checked }, () => {
// Restore focus to toggle after disabling the trap zone
// (the trap zone itself will handle initial focus when it's enabled)
if (!checked) {
this._toggle.current!.focus();
}
});
};

private _onExitButtonClickHandler = (): void => {
this.setState({
isToggled: false
});
};

private _onFocusTrapZoneToggleChanged = (ev: React.MouseEvent<HTMLElement>, isToggled: boolean): void => {
this.setState(
{
isToggled: isToggled
},
() => this._toggle.focus()
);
};

private _setRef = (toggle: IToggle): void => {
this._toggle = toggle;
};
}

This file was deleted.

Loading

0 comments on commit 2a358d7

Please sign in to comment.