@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.
77 lines (74 loc) • 3.65 kB
JavaScript
import * as React from "react";
import styled, { css } from "styled-components";
import defaultTheme from "../defaultTheme";
import Circle from "../icons/Circle";
import handleKeyDown from "../utils/handleKeyDown";
const StyledSwitch = styled.label.withConfig({
displayName: "Switch__StyledSwitch",
componentId: "sc-yfde4g-0"
})(["display:inline-block;"]);
const StyledSwitchBase = styled.div.withConfig({
displayName: "Switch__StyledSwitchBase",
componentId: "sc-yfde4g-1"
})(["", ";"], ({
theme,
checked,
disabled
}) => 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.paletteCloudDarker, theme.orbit.durationFast, disabled && "0.5", checked && css(["background-color:", ";"], theme.orbit.paletteBlueNormal)));
const StyledSwitchButton = styled.div.withConfig({
displayName: "Switch__StyledSwitchButton",
componentId: "sc-yfde4g-2"
})(["", ";"], ({
theme,
checked,
hasCustomIcon,
disabled
}) => 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.paletteInkLight : theme.orbit.paletteCloudDarker, checked && css(["left:calc(100% + 2px);transform:translateX(-100%);svg{color:", ";}"], theme.orbit.paletteBlueNormal)));
const StyledSwitchInput = styled.input.withConfig({
displayName: "Switch__StyledSwitchInput",
componentId: "sc-yfde4g-3"
})(["cursor:inherit;position:absolute;opacity:0;width:100%;height:100%;top:0;left:0;margin:0;padding:0;&:focus + ", "{box-shadow:rgba(95,115,140,0.3) 0px 0px 0px 2px;}"], StyledSwitchButton); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
StyledSwitchInput.defaultProps = {
theme: defaultTheme
}; // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
StyledSwitchBase.defaultProps = {
theme: defaultTheme
}; // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
StyledSwitchButton.defaultProps = {
theme: defaultTheme
};
const Switch = /*#__PURE__*/React.forwardRef(({
onChange,
checked,
dataTest,
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 // $FlowFixMe: fix the type of handleKeyDown
,
onKeyDown: !disabled ? handleKeyDown(onChange) : undefined,
onBlur: !disabled ? onBlur : undefined,
onChange: !disabled ? onChange : undefined,
onFocus: !disabled ? onFocus : undefined,
type: "checkbox",
"data-test": dataTest
}), /*#__PURE__*/React.createElement(StyledSwitchButton, {
checked: checked,
disabled: disabled,
hasCustomIcon: !!icon
}, icon || /*#__PURE__*/React.createElement(Circle, null))));
});
Switch.displayName = "Switch";
export default Switch;