diff --git a/ghdocs/BESTPRACTICES.md b/ghdocs/BESTPRACTICES.md
index 4023121785c15..130a90ef9c836 100644
--- a/ghdocs/BESTPRACTICES.md
+++ b/ghdocs/BESTPRACTICES.md
@@ -1,8 +1,67 @@
+# Component design
+
+## Build many smaller components and composite them together.
+
+Often we want to build something complex, like a CommandBar. We see a picture of what we want and we build one giant component that does this. Then we find other scenarios that have overlaps. The CommandBar contains a SearchBox, a set of left side collapsable links and right side links. We may find we have other cases that just want a set of links, without the extra overhead. We may also find cases where we just want a single command bar item with a dropdown menu.
+
+This is an example where instead of building a single large component, we should build more atomic building blocks that are puzzled together. While it may be more effort to think in smaller blocks, it makes the code easier to reuse and often simpler to maintain.
+
+## Use a .Props.ts file to extract out the public contracts that should be supported and documented.
+
+A props file contains all of the interface contracts that the user should know about to use the component. It is the "contract" for the component. When we evaluate semversioning, we look through the changes at Props files to determine if the change is a major, minor, or patch.
+
+The props files are also auto documented. All JSDoc comments will be extracted and shown on the demo site for documentation.
+
+When your component exposes public methods/properties, define an interface for the component in the props file and implement the interface. The auto documentation will interpret the I{Component} interface as the class contract and show it in the documentation as the class definition.
+
+```typescript
+interface IButton {
+ /**
+ * Sets focus to the button element.
+ */
+ focus(): void;
+}
+```
+
+## Extend from BaseComponent instead of React.Component in most cases.
+
+In the common folder, there exists a BaseComponent class. For simple components, it may be unnecessary to use.
+
+If you extend this, you get a few useful utilities:
+
+_events: An instance of the EventGroup, scoped to the component. It will auto dispose on component unmounting so that you don't forget.
+
+_async: A collection of utilities for performing async operations, scoped to the component. This includes setTimeout/setInterval helpers as well as utilities for generating throttled/debounced wrappers. Again, anything you use here will be automatically cleaned up on unmounting.
+
+_disposables: An array of IDisposable instances. If you have things you want disposed, you can push them into this.
+
+Another interesting thing is that when exceptions occur within React's methods, things tend to silently fail. With the BaseComponent, we
+make all methods "safe" meaning that if an exception occurs, we send the exception to a global callback which can be hooked up to a telemetry post. By default however, we forward the exception to console.error with the exact class/method that threw the exception so that there is an obvious hint what went wrong.
+
+There are some cases where it may be overkill to subclass from this; a simple Button wrapper for example really doesn't need to be more than a simple stateless component and doesn't need extra imports, which would result in making Button's dependency graph heavier. Use your best judgement.
+
+## Use React eventing, unless you need to use native.
+
+Be aware that React eventing and DOM eventing are two different systems. They do not play well with each other. DOM event handlers will always fire BEFORE React events, regardless of the DOM structure. This can introduce unexpected bugs in code that mixes both React and native DOM eventing.
+
+Unfortunately there are plenty of scenarios where we must mix the two systems together; for example, you may need to listen for application-wide clicks that bubble up to window in order to implement a light-dismiss behavior. Or perhaps you need to listen for window resizes. Or maybe you need to observe scroll events so that you can hide/show something.
+
+We use the EventGroup object for abstracting native eventing. It is simple to use; there is an "on" method and an "off" method that wrap calling addEventListener in modern browsers (or attachEvent in legacy IE.) Again if you're using the BaseComponent, it is already available to you via the _events property.
+
+## Root elements should have a component class name.
+
+Every component's root element should have a ms-Component class name. Additinally the user should be able to provide their own className via prop that should be added to the class list of the root element.
+
+If specific component elements need special classnames injected, add more classNames to props.
+
+A component's SCSS file should ONLY include files applicable to the component, and should not define styles for any other component.
+
# Class name guidelines
TODO: include our class name guidelines.
Example:
+
ms-Component-area--flags
# Style guidelines
@@ -35,14 +94,6 @@ Additionaly try to have symetrical paddings rather than using padding-right or l
E.g. using ms-font-s classname in a component is forbidden. It makes overriding CSS rules really painful. Instead, use @include ms-font-m;
-## Root elements should have a component class name.
-
-Every component's root element should have a ms-Component class name. Additinally the user should be able to provide their own className via prop that should be added to the class list of the root element.
-
-If specific component elements need special classnames injected, add more classNames to props.
-
-A component's SCSS file should ONLY include files applicable to the component, and should not define styles for any other component.
-
# Example page guidelines
Examples should follow a naming convention: Component.Desc.Example.ts
diff --git a/index.html b/index.html
index 4203ebda5e05f..ed23055b9405d 100644
--- a/index.html
+++ b/index.html
@@ -1,5 +1,5 @@
-
+
diff --git a/src/MarqueeSelection.ts b/src/MarqueeSelection.ts
new file mode 100644
index 0000000000000..75b9d1b0a56e8
--- /dev/null
+++ b/src/MarqueeSelection.ts
@@ -0,0 +1,3 @@
+export * from './components/MarqueeSelection/MarqueeSelection';
+export * from './components/MarqueeSelection/MarqueeSelection.Props';
+export * from './utilities/selection/index';
diff --git a/src/common/BaseComponent.ts b/src/common/BaseComponent.ts
index 3829a401311b6..565f952f5c4ca 100644
--- a/src/common/BaseComponent.ts
+++ b/src/common/BaseComponent.ts
@@ -3,6 +3,11 @@ import { Async } from '../utilities/Async/Async';
import { EventGroup } from '../utilities/eventGroup/EventGroup';
import { IDisposable } from './IDisposable';
+// Ensure that the HTML element has a dir specified. This helps to ensure RTL/LTR macros in css for all components will work.
+if (document && document.documentElement && !document.documentElement.getAttribute('dir')) {
+ document.documentElement.setAttribute('dir', 'ltr');
+}
+
export class BaseComponent
extends React.Component
{
/**
* External consumers should override BaseComponent.onError to hook into error messages that occur from
diff --git a/src/common/IPoint.ts b/src/common/IPoint.ts
new file mode 100644
index 0000000000000..79f682edcb744
--- /dev/null
+++ b/src/common/IPoint.ts
@@ -0,0 +1,4 @@
+export interface IPoint {
+ x: number;
+ y: number;
+}
diff --git a/src/common/IRectangle.ts b/src/common/IRectangle.ts
new file mode 100644
index 0000000000000..b13b19a8b303f
--- /dev/null
+++ b/src/common/IRectangle.ts
@@ -0,0 +1,8 @@
+export interface IRectangle {
+ left: number;
+ top: number;
+ width: number;
+ height: number;
+ right?: number;
+ bottom?: number;
+}
diff --git a/src/components/DetailsList/DetailsHeader.tsx b/src/components/DetailsList/DetailsHeader.tsx
index 891ff78a7cc8e..dda52bb335461 100644
--- a/src/components/DetailsList/DetailsHeader.tsx
+++ b/src/components/DetailsList/DetailsHeader.tsx
@@ -1,4 +1,5 @@
import * as React from 'react';
+import { BaseComponent } from '../../common/BaseComponent';
import { IColumn, DetailsListLayoutMode, ColumnActionsMode } from './DetailsList.Props';
import { FocusZone, FocusZoneDirection } from '../../FocusZone';
import { Check } from '../Check/Check';
@@ -6,7 +7,6 @@ import { GroupSpacer } from '../GroupedList/GroupSpacer';
import { css } from '../../utilities/css';
import { ISelection, SelectionMode, SELECTION_CHANGE } from '../../utilities/selection/interfaces';
import { getRTL } from '../../utilities/rtl';
-import { EventGroup } from '../../utilities/eventGroup/EventGroup';
import './DetailsHeader.scss';
const MOUSEDOWN_PRIMARY_BUTTON = 0; // for mouse down event we are using ev.button property, 0 means left button
@@ -48,23 +48,20 @@ export interface IColumnResizeDetails {
columnMinWidth: number;
}
-export class DetailsHeader extends React.Component {
+export class DetailsHeader extends BaseComponent {
public static defaultProps = {
isSelectAllVisible: true
};
public refs: {
[key: string]: React.ReactInstance;
+ root: HTMLElement;
focusZone: FocusZone;
};
- private _events: EventGroup;
-
constructor(props: IDetailsHeaderProps) {
super(props);
- this._events = new EventGroup(this);
-
this.state = {
columnResizeDetails: null,
groupNestingDepth: this.props.groupNestingDepth,
@@ -79,10 +76,7 @@ export class DetailsHeader extends React.Component
+ ref='root'
+ data-automationid='DetailsHeader'>
{ showSelectAllCheckbox ? (
@@ -180,10 +175,10 @@ export class DetailsHeader extends React.Component
{ (column.isResizable) ? (
) : (null) }
@@ -278,14 +273,16 @@ export class DetailsHeader extends React.Component
+
);
}
@@ -443,7 +443,7 @@ export class DetailsList extends React.Component, IFabricState> {
@@ -29,37 +26,30 @@ export class Fabric extends React.Component, IFabricStat
};
private _events: EventGroup;
- private _scrollTimerId: number;
constructor() {
super();
this.state = {
- isFocusVisible: false,
- isStationary: true
+ isFocusVisible: false
};
this._events = new EventGroup(this);
- this._onScrollEnd = this._onScrollEnd.bind(this);
}
public componentDidMount() {
this._events.on(document.body, 'mousedown', this._onMouseDown, true);
this._events.on(document.body, 'keydown', this._onKeyDown, true);
- this._events.on(window, 'scroll', this._onScroll, true);
}
public componentWillUnmount() {
this._events.dispose();
- clearTimeout(this._scrollTimerId);
}
public render() {
- const { isFocusVisible, isStationary } = this.state;
+ const { isFocusVisible } = this.state;
const rootClass = css('ms-Fabric ms-font-m', this.props.className, {
- 'is-focusVisible': isFocusVisible,
- 'is-stationary': isStationary,
- 'is-scrolling': !isStationary
+ 'is-focusVisible': isFocusVisible
});
return (
@@ -82,23 +72,4 @@ export class Fabric extends React.Component, IFabricStat
});
}
}
-
- private _onScroll() {
- let { isStationary } = this.state;
-
- clearTimeout(this._scrollTimerId);
- if (isStationary) {
- this.setState({
- isStationary: false
- });
- }
-
- this._scrollTimerId = setTimeout(this._onScrollEnd, STATIONARY_DETECTION_DELAY);
- }
-
- private _onScrollEnd() {
- this.setState({
- isStationary: true
- });
- }
}
diff --git a/src/components/MarqueeSelection/MarqueeSelection.Props.ts b/src/components/MarqueeSelection/MarqueeSelection.Props.ts
new file mode 100644
index 0000000000000..37939c2bd6b1c
--- /dev/null
+++ b/src/components/MarqueeSelection/MarqueeSelection.Props.ts
@@ -0,0 +1,37 @@
+import * as React from 'react';
+import { ISelection } from '../../utilities/selection/interfaces';
+import { MarqueeSelection } from './MarqueeSelection';
+
+export interface IMarqueeSelectionProps extends React.Props {
+ /**
+ * The selection object to interact with when updating selection changes.
+ */
+ selection: ISelection;
+
+ /**
+ * Optional props to mix into the root DIV element.
+ */
+ rootProps?: React.HTMLProps;
+
+ /**
+ * Optional callback that is called, when the mouse down event occurs, in order to determine
+ * if we should start a marquee selection. If true is returned, we will cancel the mousedown
+ * event to prevent upstream mousedown handlers from executing.
+ */
+ onShouldStartSelection?: (ev: React.MouseEvent) => boolean;
+
+ /**
+ * Optional flag to control the enabled state of marquee selection. This allows you to render
+ * it and have events all ready to go, but conditionally disable it. That way transitioning
+ * between enabled/disabled generate no difference in the DOM.
+ * @default true
+ */
+ isEnabled?: boolean;
+
+ /**
+ * Optional flag to restrict the drag rect to the root element, instead of allowing the drag
+ * rect to start outside of the root element boundaries.
+ * @default false
+ */
+ isDraggingConstrainedToRoot?: boolean;
+}
diff --git a/src/components/MarqueeSelection/MarqueeSelection.scss b/src/components/MarqueeSelection/MarqueeSelection.scss
new file mode 100644
index 0000000000000..2efac9dae19c2
--- /dev/null
+++ b/src/components/MarqueeSelection/MarqueeSelection.scss
@@ -0,0 +1,33 @@
+@import '../../common/common';
+
+.ms-MarqueeSelection {
+ position: relative;
+ cursor: default;
+}
+
+.ms-MarqueeSelection-dragMask {
+ position: absolute;
+ background: rgba(255, 0, 0, 0);
+ left: 0;
+ top: 0;
+ right: 0;
+ bottom: 0;
+}
+
+.ms-MarqueeSelection-box {
+ position: absolute;
+ box-sizing: border-box;
+ border: 1px solid $ms-color-themePrimary;
+ pointer-events: none;
+}
+
+.ms-MarqueeSelection-boxFill {
+ position: absolute;
+ box-sizing: border-box;
+ background-color: $ms-color-themePrimary;
+ opacity: .1;
+ left: 0;
+ top: 0;
+ right: 0;
+ bottom: 0;
+}
diff --git a/src/components/MarqueeSelection/MarqueeSelection.tsx b/src/components/MarqueeSelection/MarqueeSelection.tsx
new file mode 100644
index 0000000000000..27f93a22a56b6
--- /dev/null
+++ b/src/components/MarqueeSelection/MarqueeSelection.tsx
@@ -0,0 +1,246 @@
+import * as React from 'react';
+import { AutoScroll } from '../../utilities/AutoScroll/AutoScroll';
+import { BaseComponent } from '../../common/BaseComponent';
+import { IMarqueeSelectionProps } from './MarqueeSelection.Props';
+import { IPoint } from '../../common/IPoint';
+import { IRectangle } from '../../common/IRectangle';
+import { css } from '../../utilities/css';
+import { findScrollableParent } from '../../utilities/scrollUtilities';
+import { getDistanceBetweenPoints } from '../../utilities/math';
+
+import './MarqueeSelection.scss';
+
+export interface IMarqueeSelectionState {
+ dragOrigin?: IPoint;
+ dragRect?: IRectangle;
+}
+
+// We want to make the marquee selection start when the user drags a minimum distance. Otherwise we'd start
+// the drag even if they just click an item without moving.
+const MIN_DRAG_DISTANCE = 5;
+
+/**
+ * MarqueeSelection component abstracts managing a draggable rectangle which sets items selected/not selected.
+ * Elements which have data-selectable-index attributes are queried and measured once to determine if they
+ * fall within the bounds of the rectangle. The measure is memoized during the drag as a performance optimization
+ * so if the items change sizes while dragging, that could cause incorrect results.
+ */
+export class MarqueeSelection extends BaseComponent {
+ public static defaultProps = {
+ rootTagName: 'div',
+ rootProps: {},
+ isEnabled: true
+ };
+
+ public refs: {
+ [key: string]: React.ReactInstance;
+ root: HTMLElement;
+ };
+
+ private _dragOrigin: IPoint;
+ private _rootRect: IRectangle;
+ private _lastMouseEvent: MouseEvent;
+ private _autoScroll: AutoScroll;
+ private _selectedIndicies: { [key: string]: boolean };
+ private _itemRectCache: { [key: string]: IRectangle };
+ private _scrollableParent: HTMLElement;
+ private _scrollTop: number;
+
+ constructor(props: IMarqueeSelectionProps) {
+ super(props);
+
+ this.state = {
+ dragRect: undefined
+ };
+
+ this._onMouseDown = this._onMouseDown.bind(this);
+ }
+
+ public componentDidMount() {
+ this._scrollableParent = findScrollableParent(this.refs.root);
+
+ if (!this.props.isDraggingConstrainedToRoot && this._scrollableParent) {
+ this._events.on(this._scrollableParent, 'mousedown', this._onMouseDown);
+ }
+ }
+
+ public componentWillUnmount() {
+ if (this._autoScroll) {
+ this._autoScroll.dispose();
+ }
+ }
+
+ public render(): JSX.Element {
+ let { rootProps, children, isDraggingConstrainedToRoot } = this.props;
+ let { dragRect } = this.state;
+
+ return (
+
);
}
diff --git a/src/demo/pages/MarqueeSelectionPage/MarqueeSelectionPage.tsx b/src/demo/pages/MarqueeSelectionPage/MarqueeSelectionPage.tsx
new file mode 100644
index 0000000000000..5010b49383a38
--- /dev/null
+++ b/src/demo/pages/MarqueeSelectionPage/MarqueeSelectionPage.tsx
@@ -0,0 +1,36 @@
+import * as React from 'react';
+import { ExampleCard } from '../../components/index';
+
+import { MarqueeSelectionBasicExample } from './examples/MarqueeSelection.Basic.Example';
+
+const MarqueeSelectionBasicExampleCode = require('./examples/MarqueeSelection.Basic.Example.tsx');
+
+export class MarqueeSelectionPage extends React.Component {
+
+ public render() {
+ return (
+
+
MarqueeSelection
+
+ The MarqueeSelection component provides a service which allows the user to drag a rectangle to be drawn around
+ items to select them. This works in conjunction with a selection object, which can be used to generically store selection state, separate from a component that consumes the state.
+
+
+ MarqueeSelection also works in conjunction with the AutoScroll utility to automatically scroll the container when we drag a rectangle within the vicinity of the edges.
+
+
+ When a selection rectangle is dragged, we look for elements with the data-selection-index attribute populated. We get these elements' boundingClientRects and compare them with the root's rect to determine selection state. We update the selection state appropriately.
+
+
+ In virtualization cases where items that were once selected are dematerialized, we will keep the item in its
+ previous state until we know definitively if it's on/off. (In other words, this works with List.)
+