semantic-ui-react
Version:
The official Semantic-UI-React integration.
115 lines (91 loc) • 2.6 kB
JavaScript
import cx from 'classnames'
import PropTypes from 'prop-types'
import React from 'react'
import {
AutoControlledComponent as Component,
childrenUtils,
customPropTypes,
getUnhandledProps,
getElementType,
META,
useKeyOnly,
} from '../../lib'
import SidebarPushable from './SidebarPushable'
import SidebarPusher from './SidebarPusher'
/**
* A sidebar hides additional content beside a page.
*/
class Sidebar extends Component {
static propTypes = {
/** An element type to render as (string or function). */
as: customPropTypes.as,
/** Animation style. */
animation: PropTypes.oneOf(['overlay', 'push', 'scale down', 'uncover', 'slide out', 'slide along']),
/** Primary content. */
children: PropTypes.node,
/** Additional classes. */
className: PropTypes.string,
/** Shorthand for primary content. */
content: customPropTypes.contentShorthand,
/** Initial value of visible. */
defaultVisible: PropTypes.bool,
/** Direction the sidebar should appear on. */
direction: PropTypes.oneOf(['top', 'right', 'bottom', 'left']),
/** Controls whether or not the sidebar is visible on the page. */
visible: PropTypes.bool,
/** Sidebar width. */
width: PropTypes.oneOf(['very thin', 'thin', 'wide', 'very wide']),
}
static defaultProps = {
direction: 'left',
}
static autoControlledProps = [
'visible',
]
static _meta = {
name: 'Sidebar',
type: META.TYPES.MODULE,
}
static Pushable = SidebarPushable
static Pusher = SidebarPusher
startAnimating = (duration = 500) => {
clearTimeout(this.stopAnimatingTimer)
this.setState({ animating: true })
this.stopAnimatingTimer = setTimeout(() => this.setState({ animating: false }), duration)
}
componentWillReceiveProps(nextProps) {
if (nextProps.visible !== this.props.visible) {
this.startAnimating()
}
}
render() {
const {
animation,
className,
children,
content,
direction,
visible,
width,
} = this.props
const { animating } = this.state
const classes = cx(
'ui',
animation,
direction,
width,
useKeyOnly(animating, 'animating'),
useKeyOnly(visible, 'visible'),
'sidebar',
className,
)
const rest = getUnhandledProps(Sidebar, this.props)
const ElementType = getElementType(Sidebar, this.props)
return (
<ElementType {...rest} className={classes}>
{childrenUtils.isNil(children) ? content : children}
</ElementType>
)
}
}
export default Sidebar