UNPKG

@up-group/react-controls

Version:

We know that there are a ton of react UI library projects to choose from. Our hope with this one is to provide the next generation of react components that you can use to bootstrap your next project, or as a reference for building a UIKit. Read on to get

455 lines 17.9 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var 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 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) || 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; }; 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) t[p[i]] = s[p[i]]; return t; }; Object.defineProperty(exports, "__esModule", { value: true }); var React = require("react"); var classNames = require("classnames"); var typestyle_1 = require("typestyle"); var WrapperStyle = typestyle_1.style({ display: "inline-block", $nest: { "svg": { margin: "0px" }, ".up-toggle": { touchAction: "pan-x", display: "inline-block", position: "relative", cursor: "pointer", backgroundColor: "transparent", border: 0, padding: 0, "-webkit-touch-callout": "none", "-webkit-user-select": "none", "-khtml-user-select": "none", "-moz-user-select": "none", "-ms-user-select": "none", "user-select": "none", "-webkit-tap-highlight-color": "transparent" }, ".up-toggle-screenreader-only": { border: 0, clip: "rect(0 0 0 0)", height: "1px", margin: "-1px", overflow: "hidden", padding: 0, position: "absolute", width: "1px" }, ".up-toggle--disabled": { cursor: "not-allowed", opacity: 0.5, "-webkit-transition": "opacity 0.25s", transition: "opacity 0.25s" }, ".up-toggle-track": { width: "50px", backgroundColor: "#4D4D4D", "-webkit-transition": "all 0.2s ease", "-moz-transition": "all 0.2s ease", transition: "all 0.2s ease" }, ".up-toggle:hover:not(.up-toggle--disabled) .up-toggle-track": { backgroundColor: "#000000" }, ".up-toggle--checked .up-toggle-track": { backgroundColor: "#19AB27" }, ".up-toggle--checked:hover:not(.up-toggle--disabled) .up-toggle-track": { backgroundColor: "#128D15" }, ".up-toggle-track-check": { position: "absolute", width: "14px", height: "10px", top: "0px", bottom: "0px", marginTop: "auto", marginBottom: "auto", lineHeight: 0, left: "8px", opacity: 0, "-webkit-transition": "opacity 0.25s ease", "-moz-transition": "opacity 0.25s ease", transition: "opacity 0.25s ease" }, ".up-toggle--checked .up-toggle-track-check": { opacity: 1, "-webkit-transition": "opacity 0.25s ease", "-moz-transition": "opacity 0.25s ease", transition: "opacity 0.25s ease", }, ".up-toggle-track-x": { position: "absolute", width: "10px", height: "10px", marginTop: "auto", marginBottom: "auto", lineHeight: 0, opacity: 1, "-webkit-transition": "opacity 0.25s ease", "-moz-transition": "opacity 0.25s ease", transition: "opacity 0.25s ease" }, ".up-toggle--checked .up-toggle-track-x": { opacity: 0 }, ".up-toggle-thumb": { transition: "all 0.5s cubic-bezier(0.23, 1, 0.32, 1) 0ms", position: "absolute", backgroundColor: '#FAFAFA', "-webkit-box-sizing": 'border-box', "-moz-box-sizing": 'border-box', boxSizing: 'border-box', "-webkit-transition": "all 0.25s ease", "-moz-transition": "all 0.25s ease" }, ".up-toggle--checked .up-toggle-thumb": { left: "30px", borderColor: "#19AB27" }, ".up-toggle--focus .up-toggle-thumb": { "-webkit-box-shadow": "0px 0px 3px 2px #0099E0", "-moz-box-shadow": "0px 0px 3px 2px #0099E0", boxShadow: "0px 0px 2px 3px #0099E0" }, ".up-toggle:active:not(.up-toggle--disabled) .up-toggle-thumb": { "-webkit-box-shadow": "0px 0px 5px 5px #0099E0", "-moz-box-shadow": "0px 0px 5px 5px #0099E0", boxShadow: '0px 0px 5px 5px #0099E0' } } }); var DefaultChecked = function () { return (React.createElement("svg", { width: '14', height: '11', viewBox: '0 0 14 11' }, React.createElement("title", null, "switch-check"), React.createElement("path", { d: 'M11.264 0L5.26 6.004 2.103 2.847 0 4.95l5.26 5.26 8.108-8.107L11.264 0', fill: '#fff', fillRule: 'evenodd' }))); }; var DefaultUnchecked = function () { return React.createElement("svg", { width: '10', height: '10', viewBox: '0 0 10 10' }, React.createElement("title", null, "switch-x"), React.createElement("path", { d: 'M9.9 2.12L7.78 0 4.95 2.828 2.12 0 0 2.12l2.83 2.83L0 7.776 2.123 9.9 4.95 7.07 7.78 9.9 9.9 7.776 7.072 4.95 9.9 2.12', fill: '#fff', fillRule: 'evenodd' })); }; var UpToggle = (function (_super) { __extends(UpToggle, _super); function UpToggle(props) { var _this = _super.call(this, props) || this; _this.handleClick = function (event) { var checkbox = _this.input; if (event.target !== checkbox && !_this.moved) { _this.previouslyChecked = checkbox.checked; event.stopPropagation(); event.preventDefault(); checkbox.focus(); checkbox.click(); return; } }; _this.handleTouchStart = function (event) { _this.startX = _this.pointerCoord(event).x; _this.activated = true; }; _this.handleTouchMove = function (event) { if (!_this.activated) return; _this.moved = true; if (_this.startX) { var currentX = _this.pointerCoord(event).x; if (_this.state.checked && currentX + 15 < _this.startX) { _this.setState({ checked: false }); _this.startX = currentX; _this.activated = true; } else if (currentX - 15 > _this.startX) { _this.setState({ checked: true }); _this.startX = currentX; _this.activated = (currentX < _this.startX + 5); } } }; _this.handleTouchEnd = function (event) { if (!_this.moved) return; var checkbox = _this.input; event.preventDefault(); if (_this.startX) { var endX = _this.pointerCoord(event).x; if (_this.previouslyChecked === true && _this.startX + 4 > endX) { if (_this.previouslyChecked !== _this.state.checked) { _this.setState({ checked: false }); _this.previouslyChecked = _this.state.checked; checkbox.click(); } } else if (_this.startX - 4 < endX) { if (_this.previouslyChecked !== _this.state.checked) { _this.setState({ checked: true }); _this.previouslyChecked = _this.state.checked; checkbox.click(); } } _this.activated = false; _this.startX = null; _this.moved = false; } }; _this.handleFocus = function (event) { var onFocus = _this.props.onFocus; if (onFocus) { onFocus(event); } _this.setState({ hasFocus: true }); }; _this.handleBlur = function (event) { var onBlur = _this.props.onBlur; if (onBlur) { onBlur(event); } _this.setState({ hasFocus: false }); }; _this.handleChangeEvent = function (event) { if (!_this.props.hasOwnProperty('checked')) { var checked = _this.setState({ checked: event.target.checked }); } if (_this.props.onChange) _this.props.onChange(event.target.checked); }; _this.getIcon = function (type) { var icons = _this.props.icons; if (!icons) { return null; } return icons[type] === undefined ? null : icons[type]; }; _this.getTrackHeight = function () { var height = 'auto'; switch (_this.props.size) { case 'small': height = '14px'; break; case 'normal': height = '24px'; break; case 'large': height = '32px'; break; } return height; }; _this.getTrackWidth = function () { var width = 'auto'; switch (_this.props.size) { case 'small': width = '34px'; break; case 'normal': width = '54px'; break; case 'large': width = '32px'; break; } return width; }; _this.getThumbWidth = function () { var width = 'auto'; switch (_this.props.size) { case 'small': width = '18px'; break; case 'normal': width = '22px'; break; case 'large': width = '10px'; break; } return width; }; _this.getThumbHeight = function () { var width = 'auto'; switch (_this.props.size) { case 'small': width = '18px'; break; case 'normal': width = '22px'; break; case 'large': width = '30px'; break; } return width; }; _this.getTrackBorder = function () { var border = 'auto'; switch (_this.props.size) { case 'small': border = '24px'; break; case 'normal': border = '34px'; break; case 'large': border = '6px'; break; } return border; }; _this.getThumbBorder = function () { var border = 'auto'; switch (_this.props.size) { case 'small': border = '50%'; break; case 'normal': border = '50%'; break; case 'large': border = '6px'; break; } return border; }; _this.getThumbPosition = function () { var position = { top: "1px", left: "1px" }; if (_this.props.size == 'small') { position = { top: "-2px", left: "-2px" }; } return position; }; _this.getThumbSelectedPosition = function () { var position = { left: "31px" }; if (_this.props.size == 'small') { position = { left: "20px" }; } else if (_this.props.size == 'large') { position = { left: "21px" }; } return position; }; _this.getTrackXPosition = function () { var position = { top: "0px", bottom: "0px", right: "10px", }; if (_this.props.size == 'large') { position.right = "5px"; } return position; }; _this.getTrackCheckPosition = function () { var position = { top: "0px", bottom: "0px", left: "8px", }; if (_this.props.size == 'large') { position.left = "5px"; } return position; }; _this.previouslyChecked = !!(props.checked || props.defaultChecked); _this.state = { checked: !!(props.checked || props.defaultChecked), hasFocus: false, }; return _this; } UpToggle.prototype.pointerCoord = function (event) { if (event) { var changedTouches = event.changedTouches; if (changedTouches && changedTouches.length > 0) { var touch = changedTouches[0]; return { x: touch.clientX, y: touch.clientY }; } var pageX = event.pageX; if (pageX !== undefined) { return { x: pageX, y: event.pageY }; } } return { x: 0, y: 0 }; }; UpToggle.prototype.componentWillReceiveProps = function (nextProps) { if ('checked' in nextProps) { this.setState({ checked: !!nextProps.checked }); } }; UpToggle.prototype.render = function () { var _this = this; var _a = this.props, className = _a.className, onChange = _a.onChange, size = _a.size, _icons = _a.icons, inputProps = __rest(_a, ["className", "onChange", "size", "icons"]); var classes = classNames('up-toggle', { 'up-toggle--checked': this.state.checked, 'up-toggle--focus': this.state.hasFocus, 'up-toggle--disabled': this.props.disabled, }, className); var SizeStyle = typestyle_1.style({ $nest: { ".up-toggle-track": { height: this.getTrackHeight(), width: this.getTrackWidth(), padding: 0, borderRadius: this.getTrackBorder() }, ".up-toggle-thumb": __assign({ width: this.getThumbWidth(), height: this.getThumbHeight(), border: "1px solid #4D4D4D", borderRadius: this.getThumbBorder() }, this.getThumbPosition()), ".up-toggle-track-x": __assign({}, this.getTrackXPosition()), ".up-toggle--checked .up-toggle-thumb": __assign({}, this.getThumbSelectedPosition()), ".up-toggle-track-check": __assign({}, this.getTrackCheckPosition()) } }); return (React.createElement("div", { className: classNames(WrapperStyle, SizeStyle) }, React.createElement("div", { className: classes, onClick: this.handleClick, onTouchStart: this.handleTouchStart, onTouchMove: this.handleTouchMove, onTouchEnd: this.handleTouchEnd }, React.createElement("div", { className: 'up-toggle-track' }, React.createElement("div", { className: 'up-toggle-track-check' }, size != 'small' ? this.getIcon('checked') : null), React.createElement("div", { className: 'up-toggle-track-x' }, size != 'small' ? this.getIcon('unchecked') : null)), React.createElement("div", { className: 'up-toggle-thumb' })), React.createElement("input", __assign({}, inputProps, { onChange: this.handleChangeEvent, ref: function (ref) { _this.input = ref; }, onFocus: this.handleFocus, onBlur: this.handleBlur, onClick: function (event) { return event.stopPropagation(); }, className: 'up-toggle-screenreader-only', type: 'checkbox' })))); }; UpToggle.defaultProps = { icons: { checked: React.createElement(DefaultChecked, null), unchecked: React.createElement(DefaultUnchecked, null), }, size: 'normal' }; return UpToggle; }(React.PureComponent)); exports.default = UpToggle; //# sourceMappingURL=UpToggle.js.map