-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from dabapps/collapse-component
Collapse component
- Loading branch information
Showing
10 changed files
with
1,116 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#### Examples | ||
|
||
```js | ||
class CollapseExample extends React.Component { | ||
constructor (props) { | ||
super(props); | ||
|
||
this.state = { | ||
open: false | ||
}; | ||
|
||
this.onClickToggleCollapse = this.onClickToggleCollapse.bind(this); | ||
} | ||
|
||
onClickToggleCollapse () { | ||
const { open } = this.state; | ||
|
||
this.setState({ | ||
open: !open | ||
}); | ||
} | ||
|
||
render () { | ||
const { open } = this.state; | ||
|
||
return ( | ||
<div> | ||
<Collapse | ||
open={open} | ||
maxCollapsedHeight={100} | ||
fadeOut | ||
fadeColor="#FFF" | ||
fadeHeight={50} | ||
animationDuration={200} | ||
> | ||
<DabIpsum count={10} /> | ||
</Collapse> | ||
<Button className="primary margin-top-base" onClick={this.onClickToggleCollapse}> | ||
{open ? 'Collapse' : 'Expand'} | ||
</Button> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
<CollapseExample /> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import * as classNames from 'classnames'; | ||
import * as React from 'react'; | ||
import { ComponentProps } from '../types'; | ||
|
||
const ENOUGH_TIME_FOR_RERENDER = 50; | ||
const DEFAULT_HEIGHT = 0; | ||
const DEFAULT_DURATION = 200; | ||
const DEFAULT_FADE_HEIGHT = 50; | ||
const DEFAULT_FADE_COLOR = '#FFF'; | ||
|
||
export interface CollapseProps extends ComponentProps, React.HTMLAttributes<HTMLDivElement> { | ||
/** | ||
* Whether the collapse is open or not | ||
* @default false | ||
*/ | ||
open: boolean; | ||
/** | ||
* Duration of the animation (milliseconds) | ||
* @default 200 | ||
*/ | ||
animationDuration?: number; | ||
/** | ||
* Maximum height when collapsed | ||
* @default 0 | ||
*/ | ||
maxCollapsedHeight?: number; | ||
/** | ||
* Whether to fade out the content | ||
* @default false | ||
*/ | ||
fadeOut?: boolean; | ||
/** | ||
* Color to fade to | ||
* @default white | ||
*/ | ||
fadeColor?: string; | ||
/** | ||
* Height of the faded area | ||
* @default 50 | ||
*/ | ||
fadeHeight?: number; | ||
} | ||
|
||
export interface CollapseState { // tslint:disable-line:no-unused-variable | ||
height: number; | ||
opened: boolean; | ||
opening: boolean; | ||
} | ||
|
||
/** | ||
* Component to expand and collapse content, optionally displaying a small preview. | ||
*/ | ||
export class Collapse extends React.PureComponent<CollapseProps, CollapseState> { | ||
private element: Element; | ||
private timeout: number; | ||
|
||
public constructor (props: CollapseProps) { | ||
super(props); | ||
|
||
const { maxCollapsedHeight = DEFAULT_HEIGHT, open } = props; | ||
|
||
this.state = { | ||
height: maxCollapsedHeight, | ||
opening: false, | ||
opened: open | ||
}; | ||
} | ||
|
||
public componentDidUpdate (previousProps: CollapseProps) { | ||
if (this.props.open !== previousProps.open) { | ||
window.clearTimeout(this.timeout); | ||
|
||
const { maxCollapsedHeight = DEFAULT_HEIGHT, animationDuration = DEFAULT_DURATION } = this.props; | ||
|
||
this.setState({ | ||
opened: false, | ||
opening: previousProps.open, | ||
height: this.props.open ? maxCollapsedHeight : this.element.scrollHeight | ||
}); | ||
|
||
this.timeout = window.setTimeout(() => { | ||
this.setState({ | ||
opened: false, | ||
opening: this.props.open, | ||
height: this.props.open ? this.element.scrollHeight : maxCollapsedHeight | ||
}); | ||
|
||
this.timeout = window.setTimeout(() => { | ||
this.setState({ | ||
opened: this.props.open, | ||
opening: this.props.open | ||
}); | ||
}, animationDuration); | ||
}, ENOUGH_TIME_FOR_RERENDER); | ||
} | ||
} | ||
|
||
public componentDidMount () { | ||
const { maxCollapsedHeight = DEFAULT_HEIGHT } = this.props; | ||
|
||
this.setState({ | ||
height: this.props.open ? this.element.scrollHeight : maxCollapsedHeight | ||
}); | ||
} | ||
|
||
public componentWillMount () { | ||
window.clearTimeout(this.timeout); | ||
} | ||
|
||
public render () { | ||
const { | ||
children, | ||
className, | ||
fadeOut, | ||
fadeColor = DEFAULT_FADE_COLOR, | ||
fadeHeight = DEFAULT_FADE_HEIGHT, | ||
open, | ||
maxCollapsedHeight, | ||
animationDuration = DEFAULT_DURATION, | ||
component: Component = 'div', | ||
...remainingProps | ||
} = this.props; | ||
|
||
const { opening, opened, height } = this.state; | ||
|
||
const collapseStyle = { | ||
height: opened ? 'auto' : height, | ||
position: 'relative' as 'relative', | ||
overflow: 'hidden' as 'hidden', | ||
transition: `ease-in-out ${animationDuration}ms height` | ||
}; | ||
|
||
const fadeStyle = { | ||
height: fadeHeight, | ||
width: '100%', | ||
position: 'absolute' as 'absolute', | ||
bottom: 0, | ||
opacity: opening ? 0 : 1, | ||
background: `linear-gradient(transparent, ${fadeColor} 80%)`, | ||
transition: `ease-in-out ${animationDuration}ms opacity` | ||
}; | ||
|
||
return ( | ||
<Component | ||
ref={(element: HTMLDivElement) => this.element = element} | ||
{...remainingProps} | ||
className={classNames('clearfix collapse', open ? 'collapse-open' : null, className)} | ||
style={collapseStyle} | ||
> | ||
{children} | ||
{ | ||
fadeOut && !opened && ( | ||
<div | ||
className="collapse-fade" | ||
style={fadeStyle} | ||
/> | ||
) | ||
} | ||
</Component> | ||
); | ||
} | ||
} | ||
|
||
export default Collapse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,32 @@ | ||
export { Alert } from './components/alert'; | ||
export { Anchor } from './components/anchor'; | ||
export { Button } from './components/forms/button'; | ||
export { CodeBlock } from './components/code-block'; | ||
export { Column } from './components/grid/column'; | ||
export { Container } from './components/grid/container'; | ||
export { ContentBox } from './components/content/content-box'; | ||
export { ContentBoxHeader } from './components/content/content-box-header'; | ||
export { ContentBoxFooter } from './components/content/content-box-footer'; | ||
export { DabIpsum } from './components/prototyping/dab-ipsum'; | ||
export { FormGroup } from './components/forms/form-group'; | ||
export { InputGroup } from './components/forms/input-group'; | ||
export { InputGroupAddon } from './components/forms/input-group-addon'; | ||
export { Modal } from './components/modals/modal'; | ||
export { ModalBody } from './components/modals/modal-body'; | ||
export { ModalCloseIcon } from './components/modals/modal-close-icon'; | ||
export { ModalFooter } from './components/modals/modal-footer'; | ||
export { ModalHeader } from './components/modals/modal-header'; | ||
export { ModalRenderer } from './components/modals/modal-renderer'; | ||
export { Row } from './components/grid/row'; | ||
export { Section } from './components/content/section'; | ||
export { SpacedGroup } from './components/spaced-group'; | ||
export { Tabs } from './components/tabs/tabs'; | ||
export { Tab } from './components/tabs/tab'; | ||
export { Table } from './components/tables/table'; | ||
export { TableBody } from './components/tables/table-body'; | ||
export { TableCell } from './components/tables/table-cell'; | ||
export { TableHead } from './components/tables/table-head'; | ||
export { TableHeader } from './components/tables/table-header'; | ||
export { TableRow } from './components/tables/table-row'; | ||
export { Well } from './components/well'; | ||
export { default as Alert } from './components/alert'; | ||
export { default as Anchor } from './components/anchor'; | ||
export { default as Button } from './components/forms/button'; | ||
export { default as CodeBlock } from './components/code-block'; | ||
export { default as Collapse } from './components/collapse'; | ||
export { default as Column } from './components/grid/column'; | ||
export { default as Container } from './components/grid/container'; | ||
export { default as ContentBox } from './components/content/content-box'; | ||
export { default as ContentBoxHeader } from './components/content/content-box-header'; | ||
export { default as ContentBoxFooter } from './components/content/content-box-footer'; | ||
export { default as DabIpsum } from './components/prototyping/dab-ipsum'; | ||
export { default as FormGroup } from './components/forms/form-group'; | ||
export { default as InputGroup } from './components/forms/input-group'; | ||
export { default as InputGroupAddon } from './components/forms/input-group-addon'; | ||
export { default as Modal } from './components/modals/modal'; | ||
export { default as ModalBody } from './components/modals/modal-body'; | ||
export { default as ModalCloseIcon } from './components/modals/modal-close-icon'; | ||
export { default as ModalFooter } from './components/modals/modal-footer'; | ||
export { default as ModalHeader } from './components/modals/modal-header'; | ||
export { default as ModalRenderer } from './components/modals/modal-renderer'; | ||
export { default as Row } from './components/grid/row'; | ||
export { default as Section } from './components/content/section'; | ||
export { default as SpacedGroup } from './components/spaced-group'; | ||
export { default as Tabs } from './components/tabs/tabs'; | ||
export { default as Tab } from './components/tabs/tab'; | ||
export { default as Table } from './components/tables/table'; | ||
export { default as TableBody } from './components/tables/table-body'; | ||
export { default as TableCell } from './components/tables/table-cell'; | ||
export { default as TableHead } from './components/tables/table-head'; | ||
export { default as TableHeader } from './components/tables/table-header'; | ||
export { default as TableRow } from './components/tables/table-row'; | ||
export { default as Well } from './components/well'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.