UNPKG

@workday/canvas-kit-react-side-panel

Version:

A Canvas-styled side panel

188 lines (187 loc) 8.62 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import * as React from 'react'; import styled from '@emotion/styled'; import throttle from 'lodash/throttle'; import { colors, spacing, type } from '@workday/canvas-kit-react-core'; import { IconButton } from '@workday/canvas-kit-react-button'; import { chevronLeftIcon, chevronRightIcon } from '@workday/canvas-system-icons-web'; export var SidePanelOpenDirection; (function (SidePanelOpenDirection) { SidePanelOpenDirection[SidePanelOpenDirection["Left"] = 0] = "Left"; SidePanelOpenDirection[SidePanelOpenDirection["Right"] = 1] = "Right"; })(SidePanelOpenDirection || (SidePanelOpenDirection = {})); export var SidePanelBackgroundColor; (function (SidePanelBackgroundColor) { SidePanelBackgroundColor[SidePanelBackgroundColor["White"] = 0] = "White"; SidePanelBackgroundColor[SidePanelBackgroundColor["Transparent"] = 1] = "Transparent"; SidePanelBackgroundColor[SidePanelBackgroundColor["Gray"] = 2] = "Gray"; })(SidePanelBackgroundColor || (SidePanelBackgroundColor = {})); var closedWidth = spacing.xxl; var Header = styled('h2')(__assign(__assign({}, type.h2), { marginTop: spacing.zero })); var SidePanelContainer = styled('div')({ overflow: 'hidden', height: '100%', boxSizing: 'border-box', display: 'flex', flexDirection: 'column', transition: 'width 200ms ease', position: 'absolute', }, function (_a) { var open = _a.open; return ({ alignItems: open ? undefined : 'center', boxShadow: open ? undefined : '0 8px 16px -8px rgba(0, 0, 0, 0.16)', }); }, function (_a) { var open = _a.open, backgroundColor = _a.backgroundColor; var openBackgroundColor; switch (backgroundColor) { case SidePanelBackgroundColor.Transparent: openBackgroundColor = 'transparent'; break; case SidePanelBackgroundColor.Gray: openBackgroundColor = colors.soap100; break; case SidePanelBackgroundColor.White: default: openBackgroundColor = colors.frenchVanilla100; break; } return { backgroundColor: open ? openBackgroundColor : colors.frenchVanilla100, }; }, function (_a) { var open = _a.open, openWidth = _a.openWidth; return ({ width: open ? openWidth : closedWidth, }); }, function (_a) { var open = _a.open, padding = _a.padding; return ({ padding: open ? padding || spacing.m : spacing.s + " 0", }); }, function (_a) { var openDirection = _a.openDirection; return ({ right: openDirection === SidePanelOpenDirection.Right ? spacing.zero : undefined, left: openDirection === SidePanelOpenDirection.Left ? spacing.zero : undefined, }); }); var ChildrenContainer = styled('div')({ transition: 'none', zIndex: 1, }, function (_a) { var open = _a.open, openWidth = _a.openWidth; return ({ width: open ? openWidth : closedWidth, }); }); var ToggleButton = styled(IconButton)({ position: 'absolute', bottom: spacing.s, }, function (_a) { var openDirection = _a.openDirection; return ({ right: openDirection === SidePanelOpenDirection.Left ? spacing.s : '', left: openDirection === SidePanelOpenDirection.Right ? spacing.s : '', }); }); var SidePanelFooter = styled('div')({ position: 'absolute', bottom: '0', height: 120, left: 0, background: 'linear-gradient(180deg, rgba(255, 255, 255, 0.0001) 0%, #FFFFFF 100%)', }, function (_a) { var open = _a.open, openWidth = _a.openWidth; return ({ width: open ? openWidth : spacing.xxl, }); }); var SidePanel = (function (_super) { __extends(SidePanel, _super); function SidePanel(props) { var _this = _super.call(this, props) || this; _this.state = { screenSize: typeof window !== 'undefined' ? window.innerWidth : 0, }; _this.handleResize = function () { if (!_this.props.onBreakpointChange || !_this.props.breakpoint) { return; } if (window.innerWidth > _this.props.breakpoint && !_this.props.open) { _this.props.onBreakpointChange(true); } if (window.innerWidth <= _this.props.breakpoint && _this.props.open) { _this.props.onBreakpointChange(false); } }; _this.onToggleClick = function () { if (_this.props.onToggleClick) { _this.props.onToggleClick(); } }; _this.toggleButtonDirection = function (open, openDirection) { if (openDirection !== SidePanelOpenDirection.Right) { return open ? chevronLeftIcon : chevronRightIcon; } else { return open ? chevronRightIcon : chevronLeftIcon; } }; _this.handleResize = throttle(_this.handleResize.bind(_this), 150); return _this; } SidePanel.prototype.componentDidMount = function () { window.addEventListener('resize', this.handleResize); }; SidePanel.prototype.componentWillUnmount = function () { window.removeEventListener('resize', this.handleResize); }; SidePanel.prototype.render = function () { var _a = this.props, _b = _a.backgroundColor, backgroundColor = _b === void 0 ? SidePanelBackgroundColor.White : _b, _c = _a.openNavigationAriaLabel, openNavigationAriaLabel = _c === void 0 ? 'open navigation' : _c, _d = _a.closeNavigationAriaLabel, closeNavigationAriaLabel = _d === void 0 ? 'close navigation' : _d, _e = _a.openDirection, openDirection = _e === void 0 ? SidePanelOpenDirection.Left : _e, _f = _a.breakpoint, breakpoint = _f === void 0 ? 768 : _f, _g = _a.openWidth, openWidth = _g === void 0 ? 300 : _g, header = _a.header, onToggleClick = _a.onToggleClick, open = _a.open, padding = _a.padding, onBreakpointChange = _a.onBreakpointChange, elemProps = __rest(_a, ["backgroundColor", "openNavigationAriaLabel", "closeNavigationAriaLabel", "openDirection", "breakpoint", "openWidth", "header", "onToggleClick", "open", "padding", "onBreakpointChange"]); return (React.createElement(SidePanelContainer, __assign({ role: "region", padding: padding, openDirection: openDirection, openWidth: openWidth, backgroundColor: backgroundColor, open: open }, elemProps), React.createElement(ChildrenContainer, { open: open, openWidth: openWidth }, header && open ? React.createElement(Header, null, header) : null, this.props.children), React.createElement(SidePanelFooter, { openWidth: openWidth, open: open }, onToggleClick && (React.createElement(ToggleButton, { openDirection: openDirection, "aria-label": open ? closeNavigationAriaLabel : openNavigationAriaLabel, toggled: false, size: IconButton.Size.Small, onClick: this.onToggleClick, icon: this.toggleButtonDirection(open, openDirection), variant: IconButton.Variant.CircleFilled }))))); }; SidePanel.OpenDirection = SidePanelOpenDirection; SidePanel.BackgroundColor = SidePanelBackgroundColor; return SidePanel; }(React.Component)); export default SidePanel;