All files / SidePanel SidePanel.tsx

81.39% Statements 35/43
66.66% Branches 16/24
50% Functions 5/10
83.33% Lines 35/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378                        1x   1x     1x 1x 1x 1x           1x                                                                                                                             1x                                                   1x 1x       1x                                                                                                                                                             28x             1x                           1x   28x       28x           28x                             28x       1x 1x         1x     28x         3x       2x                   3x 3x 3x   3x 3x                             30x   30x 30x   30x 4x     30x                                                                                                                                          
import _, { omit } from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
 
import { lucidClassNames } from '../../util/style-helpers';
import Overlay, { IOverlayProps } from '../Overlay/Overlay';
import GripperVerticalIcon from '../Icon/GripperVerticalIcon/GripperVerticalIcon';
import CloseIcon from '../Icon/CloseIcon/CloseIcon';
import DragCaptureZone from '../DragCaptureZone/DragCaptureZone';
import Button from '../Button/Button';
import { getFirst, StandardProps } from '../../util/component-types';
 
const cx = lucidClassNames.bind('&-SidePanel');
 
const { any, bool, func, oneOf, node, number, string, oneOfType } = PropTypes;
 
/* SidePanel Header **/
const SidePanelHeader = (_props: StandardProps): null => null;
SidePanelHeader.displayName = 'SidePanel.Header';
SidePanelHeader.propName = 'Header';
SidePanelHeader.peek = {
	description: `
		Defines the Header content of SidePanel. If no content is defined, it will
		still show the close button.
	`,
};
SidePanelHeader.propTypes = {
	/**
		Children that will be rendered.
	*/
	children: node,
};
 
/** Side Panel */
export interface ISidePanelProps extends Partial<IOverlayProps> {
	/** Alternative to using `<SidePanel.Header>`. */
	Header?: string | (React.ReactNode & { props: StandardProps });
 
	/** Controls the expanded/collapsed state as a boolean prop. */
	isExpanded: boolean;
 
	/** When true, hides the resizer at the edge of the SidePanel. */
	isResizeDisabled: boolean;
 
	/** Callback triggered when user clicks the background, hits the Esc key, or
	 * clicks the close button in the Header. */
	onCollapse: ({
		event,
		props,
	}: {
		event: React.MouseEvent | KeyboardEvent;
		props: ISidePanelProps;
	}) => void;
 
	/** Callback triggered after a user resizes to a new width. */
	onResize: (
		width: number,
		{
			event,
			props,
		}: {
			event: MouseEvent | TouchEvent;
			props: ISidePanelProps;
		}
	) => void;
 
	/** Controls the position on the screen. */
	position: 'left' | 'right';
 
	/** When true, it will prevent scrolling in the background when \`isExpanded\;
	 * is true. This is accomplished by setting \`document.body.style.overflow =
	 * 'hidden'\`. */
	preventBodyScroll: boolean;
 
	/** Sets the initial width in pixels. The actual width may change if the user
	 * resizes it. */
	width: number;
 
	/** Sets the minimum width in pixels. */
	minWidth: number;
 
	/** Sets the maximim width in pixels. */
	maxWidth: number;
 
	/** Sets the top margin for the panel. Defaults to \`0\`. */
	topOffset: number | string;
}
 
/** TODO: Remove the 'nonPassThroughs' when the component is converted to a functional component */
const nonPassThroughs = [
	'children',
	'className',
	'Header',
	'isAnimated',
	'isExpanded',
	'isResizeDisabled',
	'onCollapse',
	'onResize',
	'position',
	'preventBodyScroll',
	'width',
	'minWidth',
	'maxWidth',
	'topOffset',
	'initialState',
];
 
interface ISidePanelState {
	isResizing: boolean;
	width: number;
	startWidth: number;
	isExpanded: boolean;
}
 
class SidePanel extends React.Component<ISidePanelProps, ISidePanelState, {}> {
	static displayName = 'SidePanel';
	static peek = {
		description: `A fixed-positioned overlay positioned on the side of the screen at full screen height. Supports variable widths resized by the user or defined as a prop. Animated collapse and expand with optional Header and closer.`,
		categories: ['layout'],
	};
	static propTypes = {
		/**
			Content of the SidePanel, but also accepts \`<SidePanel.Header>\` to define
			header content.
		*/
		children: node,
 
		/**
			Appended to the component-specific class names set on the root element.
		*/
		className: string,
 
		/**
			Alternative to using \`<SidePanel.Header>\`.
		*/
		Header: any,
 
		/**
			Enables animated transitions during expansion and collapse.
		*/
		isAnimated: bool,
 
		/**
			Controls the expanded/collapsed state as a boolean prop.
		*/
		isExpanded: bool,
 
		/**
			When true, hides the resizer at the edge of the SidePanel.
		*/
		isResizeDisabled: bool,
 
		/**
			Callback triggered when user clicks the background, hits the Esc key, or
			clicks the close button in the Header.
			Signature: \`({ event, props }) => {}\`
		*/
		onCollapse: func,
 
		/**
			Callback triggered after a user resizes to a new width.
			Signature: \`(width, { event, props }) => {}\`
		*/
		onResize: func,
 
		/**
			Controls the position on the screen.
		*/
		position: oneOf(['left', 'right']),
 
		/**
			When true, it will prevent scrolling in the background when \`isExpanded\`
			is true. This is accomplished by setting \`document.body.style.overflow =
			'hidden'\`.
		*/
		preventBodyScroll: bool,
 
		/**
			Sets the initial width in pixels. The actual width may change if the user
			resizes it.
		*/
		width: number,
 
		/**
			Sets the minimum width of the Side Panel.
		*/
		minWidth: number,
 
		/**
			Sets the maximum width of the Side Panel.
		*/
		maxWidth: number,
 
		/**
			Sets the top margin for the panel. Defaults to \`0\`.
		*/
		topOffset: oneOfType([number, string]),
	};
 
	state = {
		isResizing: false,
		width: this.props.width as number,
		startWidth: this.props.width as number,
		isExpanded: this.props.isExpanded as boolean,
	};
 
	static defaultProps = {
		isAnimated: true,
		isExpanded: true,
		isResizeDisabled: false,
		onCollapse: _.noop,
		onResize: _.noop,
		position: 'right',
		preventBodyScroll: false,
		topOffset: 0,
		width: 500,
		minWidth: 500,
		maxWidth: 1200,
	};
 
	static Header = SidePanelHeader;
 
	timerId = setTimeout((): void => {
		return;
	}, 1);
 
	handleResizeStart = (): void => {
		this.setState({
			isResizing: true,
		});
	};
 
	handleResize = ({ dX }: { dX: number }): void => {
		const { startWidth } = this.state;
		const position =
			startWidth + dX * (this.props.position === 'right' ? -1 : 1);
		this.setState({
			width: _.clamp(
				position,
				this.props.minWidth,
				this.props.maxWidth === Infinity
					? window.innerWidth
					: this.props.maxWidth
			),
		});
	};
 
	handleResizeEnd = (
		{ dX }: { dX: number },
		{ event }: { event: MouseEvent | TouchEvent }
	): void => {
		const { startWidth, width } = this.state;
		this.setState({
			width: startWidth + dX * (this.props.position === 'right' ? -1 : 1),
			isResizing: false,
			startWidth: width,
		});
		this.props.onResize(startWidth - dX, { props: this.props, event });
	};
 
	handleCollapse = ({
		event,
	}: {
		event: React.MouseEvent | KeyboardEvent;
	}): void => {
		this.props.onCollapse({ event, props: this.props });
	};
 
	componentDidUpdate(prevProps: ISidePanelProps): void {
		Iif (prevProps.isExpanded !== this.props.isExpanded) {
			this.timerId = setTimeout((): void => {
				this.setState({
					isExpanded: this.props.isExpanded as boolean,
				});
			}, 1);
		}
	}
 
	componentWillUnmount(): void {
		const { preventBodyScroll } = this.props;
		if (this.timerId) {
			clearTimeout(this.timerId);
		}
		if (preventBodyScroll) {
			window.document.body.style.overflow = '';
		}
	}
 
	render(): React.ReactNode {
		const {
			children,
			className,
			isAnimated,
			isExpanded,
			isResizeDisabled,
			position,
			preventBodyScroll,
			topOffset,
			...passThroughs
		} = this.props;
 
		const headerEl = getFirst(this.props, SidePanel.Header);
		const headerChildren = _.get(headerEl, 'props.children');
 
		if (preventBodyScroll) {
			window.document.body.style.overflow = isExpanded ? 'hidden' : '';
		}
 
		return (
			<Overlay
				className={cx(
					'&',
					{
						'&-is-expanded': isExpanded && this.state.isExpanded,
						'&-position-left': position === 'left',
						'&-position-right': position === 'right',
						'&-is-animated': isAnimated,
					},
					className
				)}
				isShown={isExpanded || this.state.isExpanded}
				onBackgroundClick={this.handleCollapse}
				onEscape={this.handleCollapse}
				isAnimated={isAnimated}
				style={{
					marginTop: topOffset,
				}}
				{...omit(passThroughs, nonPassThroughs)}
			>
				<div
					className={cx('&-pane')}
					style={{
						width: this.state.width,
						marginTop: topOffset,
					}}
				>
					{headerEl && (
						<div className={cx('&-header')}>
							<div className={cx('&-header-inner-wrapper')}>
								<div className={cx('&-header-content')}>{headerChildren}</div>
 
								<Button
									className={cx('&-header-closer-button')}
									kind='invisible'
									onClick={this.handleCollapse}
									hasOnlyIcon={true}
								>
									<CloseIcon
										className={cx('&-header-closer')}
										isClickable
										size={14}
									/>
								</Button>
							</div>
						</div>
					)}
 
					<div className={cx('&-body')}>
						{!isResizeDisabled && (
							<DragCaptureZone
								className={cx('&-grabber')}
								onDragStart={this.handleResizeStart}
								onDrag={this.handleResize}
								onDragEnd={this.handleResizeEnd}
							>
								<GripperVerticalIcon width='20' />
							</DragCaptureZone>
						)}
						<div className={cx('&-content')}>{children}</div>
					</div>
				</div>
			</Overlay>
		);
	}
}
 
export default SidePanel;