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

Adding MarqueeSelection component and example #74

Merged
merged 31 commits into from
Aug 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
24dd626
Adding autoscroll utility and marquee selection.
dzearing Aug 1, 2016
3921752
More fixes.
dzearing Aug 3, 2016
d5ec54c
Merge pull request #1 from OfficeDev/master
dzearing Aug 3, 2016
58281d9
Enables selection preservation even when items dematerialize.
dzearing Aug 3, 2016
c4ced04
Math rounding tweak in auto scrolling.
dzearing Aug 3, 2016
2e0a119
Updating small nits.
dzearing Aug 3, 2016
ae2bd96
Merge pull request #2 from OfficeDev/master
dzearing Aug 4, 2016
1272674
Merge branch 'master' of https://github.com/dzearing/office-ui-fabric…
dzearing Aug 4, 2016
bb4d1c0
Adding example page, improving props documentation, adding memoizatio…
dzearing Aug 4, 2016
089fd22
More performance improvements.
dzearing Aug 5, 2016
456aee3
Moving files to a more logical location.
dzearing Aug 5, 2016
3c3b43d
Missing an index change.
dzearing Aug 5, 2016
66ef16d
Adding more best practices content.
dzearing Aug 5, 2016
6729ebd
Updating documentation.
dzearing Aug 5, 2016
9d12f13
Removing unnecessary call.
dzearing Aug 5, 2016
3c08105
Removing dir from html.
dzearing Aug 5, 2016
4295470
Removing an unnecessary measure from autoscroll.
dzearing Aug 6, 2016
0c7a8a4
Updating basic details list example to use marquee selection.
dzearing Aug 8, 2016
fb61f66
With scrolltop fix (#3)
dzearing Aug 8, 2016
7ade6e9
Improving the example by removing the images.
dzearing Aug 8, 2016
bd5777d
Removing the scroll monitoring and css tweaking from Fabric component…
dzearing Aug 8, 2016
dca18b5
Fixing issues related to safari support.
dzearing Aug 8, 2016
f329966
Minor improvement to EventGroup.
dzearing Aug 8, 2016
59ab874
Lint fixes.
dzearing Aug 8, 2016
4079b67
Updates for PR comments.
dzearing Aug 8, 2016
4a08c31
Fixing hovers.
dzearing Aug 8, 2016
fa542ac
A few more fixes to test page and styles.
dzearing Aug 8, 2016
7f48aef
Removing lint error.
dzearing Aug 8, 2016
e85baa6
Cleanup.
dzearing Aug 8, 2016
142872f
Adds ability to select from anywhere in the scrollable parent. Also f…
dzearing Aug 9, 2016
b891954
Merge branch 'master' of https://github.com/OfficeDev/office-ui-fabri…
dzearing Aug 9, 2016
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
67 changes: 59 additions & 8 deletions ghdocs/BESTPRACTICES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<html dir="ltr">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
Expand Down
3 changes: 3 additions & 0 deletions src/MarqueeSelection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './components/MarqueeSelection/MarqueeSelection';
export * from './components/MarqueeSelection/MarqueeSelection.Props';
export * from './utilities/selection/index';
5 changes: 5 additions & 0 deletions src/common/BaseComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<P, S> extends React.Component<P, S> {
/**
* External consumers should override BaseComponent.onError to hook into error messages that occur from
Expand Down
4 changes: 4 additions & 0 deletions src/common/IPoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IPoint {
x: number;
y: number;
}
8 changes: 8 additions & 0 deletions src/common/IRectangle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface IRectangle {
left: number;
top: number;
width: number;
height: number;
right?: number;
bottom?: number;
}
32 changes: 16 additions & 16 deletions src/components/DetailsList/DetailsHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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';
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
Expand Down Expand Up @@ -48,23 +48,20 @@ export interface IColumnResizeDetails {
columnMinWidth: number;
}

export class DetailsHeader extends React.Component<IDetailsHeaderProps, IDetailsHeaderState> {
export class DetailsHeader extends BaseComponent<IDetailsHeaderProps, IDetailsHeaderState> {
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,
Expand All @@ -79,10 +76,7 @@ export class DetailsHeader extends React.Component<IDetailsHeaderProps, IDetails
let { selection } = this.props;

this._events.on(selection, SELECTION_CHANGE, this._onSelectionChanged);
}

public componentWillUnmount() {
this._events.dispose();
this._events.on(this.refs.root, 'mousedown', this._onSizerDown);
}

public componentWillReceiveProps(newProps) {
Expand All @@ -109,7 +103,8 @@ export class DetailsHeader extends React.Component<IDetailsHeaderProps, IDetails
}) }
onMouseMove={ this._onMove.bind(this) }
onMouseUp={ this._onUp.bind(this) }
ref='root' data-automationid='DetailsHeader'>
ref='root'
data-automationid='DetailsHeader'>
<FocusZone ref='focusZone' direction={ FocusZoneDirection.horizontal }>
{ showSelectAllCheckbox ? (
<div className='ms-DetailsHeader-cellWrapper' role='columnheader'>
Expand Down Expand Up @@ -180,10 +175,10 @@ export class DetailsHeader extends React.Component<IDetailsHeaderProps, IDetails
</div>
{ (column.isResizable) ? (
<div
data-sizer-index={ columnIndex }
className={ css('ms-DetailsHeader-cell is-sizer', {
'is-resizing': columnResizeDetails && columnResizeDetails.columnIndex === columnIndex && isSizing
}) }
onMouseDown={ this._onSizerDown.bind(this, columnIndex) }
onDoubleClick={ this._onSizerDoubleClick.bind(this, columnIndex) }
/>
) : (null) }
Expand Down Expand Up @@ -278,21 +273,26 @@ export class DetailsHeader extends React.Component<IDetailsHeaderProps, IDetails
});
}

private _onSizerDown(columnIndex: number, ev: React.MouseEvent) {
if (ev.button !== MOUSEDOWN_PRIMARY_BUTTON) {
private _onSizerDown(ev: MouseEvent) {
let columnIndexAttr = (ev.target as HTMLElement).getAttribute('data-sizer-index');
let columnIndex = Number(columnIndexAttr);
let { columns } = this.props;

if (columnIndex === null || ev.button !== MOUSEDOWN_PRIMARY_BUTTON) {
// Ignore anything except the primary button.
return;
}

let { columns } = this.props;

this.setState({
columnResizeDetails: {
columnIndex: columnIndex,
columnMinWidth: columns[columnIndex].calculatedWidth,
originX: ev.clientX
}
});

ev.preventDefault();
ev.stopPropagation();
}

private _onSelectionChanged() {
Expand Down
1 change: 0 additions & 1 deletion src/components/DetailsList/DetailsList.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
overflow-y: visible;

-webkit-overflow-scrolling: touch;
transform: translateZ(0);
}

.ms-DetailsList-cell {
Expand Down
40 changes: 20 additions & 20 deletions src/components/DetailsList/DetailsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,25 +311,25 @@ export class DetailsList extends React.Component<IDetailsListProps, IDetailsList
}

return (
<DetailsRow
item={ item }
itemIndex={ index }
columns={ columns }
groupNestingDepth={ nestingDepth }
selectionMode={ selectionMode }
selection={ selection }
onDidMount={ this._onRowDidMount }
onWillUnmount={ this._onRowWillUnmount }
onRenderItemColumn={ onRenderItemColumn }
eventsToRegister={ eventsToRegister }
dragDropEvents={ dragDropEvents }
dragDropHelper={ dragDropHelper }
viewport={ viewport }
checkboxVisibility={ checkboxVisibility }
getRowAriaLabel={ getRowAriaLabel }
canSelectItem={ canSelectItem }
checkButtonAriaLabel={ checkButtonAriaLabel }
/>
<DetailsRow
item={ item }
itemIndex={ index }
columns={ columns }
groupNestingDepth={ nestingDepth }
selectionMode={ selectionMode }
selection={ selection }
onDidMount={ this._onRowDidMount }
onWillUnmount={ this._onRowWillUnmount }
onRenderItemColumn={ onRenderItemColumn }
eventsToRegister={ eventsToRegister }
dragDropEvents={ dragDropEvents }
dragDropHelper={ dragDropHelper }
viewport={ viewport }
checkboxVisibility={ checkboxVisibility }
getRowAriaLabel={ getRowAriaLabel }
canSelectItem={ canSelectItem }
checkButtonAriaLabel={ checkButtonAriaLabel }
/>
);
}

Expand Down Expand Up @@ -443,7 +443,7 @@ export class DetailsList extends React.Component<IDetailsListProps, IDetailsList
viewportWidth = this.props.viewport.width;
}

newColumns = newColumns || buildColumns(newItems);
newColumns = newColumns || buildColumns(newItems, true);

let adjustedColumns: IColumn[];

Expand Down
6 changes: 3 additions & 3 deletions src/components/DetailsList/DetailsRow.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ $unselectedHoverColor: $ms-color-neutralLighter;
}
}

.ms-Fabric.is-stationary .ms-DetailsRow:hover {
.ms-DetailsRow:hover {
background: $unselectedHoverColor;
}

.ms-Fabric.is-stationary .ms-DetailsRow.is-selected:hover {
.ms-DetailsRow.is-selected:hover {
background: $selectedHoverColor;
}

Expand Down Expand Up @@ -82,7 +82,7 @@ $unselectedHoverColor: $ms-color-neutralLighter;
}
}

.ms-Fabric.is-stationary .ms-DetailsRow:hover .ms-DetailsRow-check,
.ms-DetailsRow:hover .ms-DetailsRow-check,
.ms-DetailsRow.is-selected .ms-DetailsRow-check,
.ms-DetailsRow.is-check-visible .ms-DetailsRow-check {
opacity: 1;
Expand Down
35 changes: 3 additions & 32 deletions src/components/Fabric/Fabric.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ const DIRECTIONAL_KEY_CODES = [
KeyCodes.pageDown
];

const STATIONARY_DETECTION_DELAY = 100;

export interface IFabricState {
isFocusVisible?: boolean;
isStationary?: boolean;
}

export class Fabric extends React.Component<React.HTMLProps<Fabric>, IFabricState> {
Expand All @@ -29,37 +26,30 @@ export class Fabric extends React.Component<React.HTMLProps<Fabric>, 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 (
Expand All @@ -82,23 +72,4 @@ export class Fabric extends React.Component<React.HTMLProps<Fabric>, 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
});
}
}
Loading