UNPKG

@kiwicom/orbit-components

Version:

Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.

83 lines (82 loc) 4.03 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; exports.__esModule = true; exports.default = void 0; var React = _interopRequireWildcard(require("react")); var _styledComponents = _interopRequireWildcard(require("styled-components")); var _defaultTheme = _interopRequireDefault(require("../defaultTheme")); var _Circle = _interopRequireDefault(require("../icons/Circle")); var _handleKeyDown = _interopRequireDefault(require("../utils/handleKeyDown")); var _common = require("../utils/common"); const StyledSwitch = _styledComponents.default.label.withConfig({ displayName: "Switch__StyledSwitch", componentId: "sc-1ip27uw-0" })(["display:inline-block;"]); const StyledSwitchBase = _styledComponents.default.div.withConfig({ displayName: "Switch__StyledSwitchBase", componentId: "sc-1ip27uw-1" })(["", ";"], ({ theme, checked, disabled }) => (0, _styledComponents.css)(["display:flex;align-items:center;justify-content:space-between;cursor:", ";width:40px;height:20px;background-color:", ";border-radius:100px;position:relative;transition:background-color ", ";opacity:", ";", ";"], !disabled && "pointer", theme.orbit.backgroundSwitch, theme.orbit.durationFast, disabled && "0.5", checked && (0, _styledComponents.css)(["background-color:", ";"], theme.orbit.backgroundSwitchChecked))); const StyledSwitchButton = _styledComponents.default.div.withConfig({ displayName: "Switch__StyledSwitchButton", componentId: "sc-1ip27uw-2" })(["", ";"], ({ theme, checked, hasCustomIcon, disabled }) => (0, _styledComponents.css)(["box-sizing:border-box;display:inline-flex;align-items:center;justify-content:center;position:absolute;left:-3px;width:24px;height:24px;border-radius:", ";background:", ";transition:", ";box-shadow:inset 0 0 1px 0 rgba(7,64,92,0.1),", ";&:active{box-shadow:", ";}svg{height:12px;width:12px;color:", ";}", ";"], theme.orbit.borderRadiusCircle, theme.orbit.paletteWhite, theme.orbit.durationFast, theme.orbit.boxShadowAction, !disabled && theme.orbit.boxShadowActionActive, hasCustomIcon ? theme.orbit.paletteInkNormal : theme.orbit.backgroundSwitch, checked && (0, _styledComponents.css)(["left:calc(100% + 2px);transform:translateX(-100%);svg{color:", ";}"], theme.orbit.backgroundSwitchChecked))); const StyledSwitchInput = _styledComponents.default.input.withConfig({ displayName: "Switch__StyledSwitchInput", componentId: "sc-1ip27uw-3" })(["cursor:inherit;position:absolute;opacity:0;width:100%;height:100%;top:0;left:0;margin:0;padding:0;&:focus + ", "{", ";}"], StyledSwitchButton, _common.defaultFocus); StyledSwitchInput.defaultProps = { theme: _defaultTheme.default }; StyledSwitchBase.defaultProps = { theme: _defaultTheme.default }; StyledSwitchButton.defaultProps = { theme: _defaultTheme.default }; const Switch = /*#__PURE__*/React.forwardRef(({ onChange, checked, dataTest, id, icon, onBlur, onFocus, disabled, ariaLabelledby }, ref) => { return /*#__PURE__*/React.createElement(StyledSwitch, null, /*#__PURE__*/React.createElement(StyledSwitchBase, { checked: checked, disabled: disabled }, /*#__PURE__*/React.createElement(StyledSwitchInput, { ref: ref, checked: checked, disabled: disabled, "aria-checked": checked, role: "switch", "aria-labelledby": ariaLabelledby, onKeyDown: !disabled ? (0, _handleKeyDown.default)(undefined, onChange) : undefined, onBlur: !disabled ? onBlur : undefined, onChange: !disabled ? onChange : undefined, onFocus: !disabled ? onFocus : undefined, type: "checkbox", "data-test": dataTest, id: id }), /*#__PURE__*/React.createElement(StyledSwitchButton, { checked: checked, disabled: disabled, hasCustomIcon: !!icon }, icon || /*#__PURE__*/React.createElement(_Circle.default, null)))); }); Switch.displayName = "Switch"; var _default = Switch; exports.default = _default;