vcc-ui
Version:
VCC UI is a collection of React UI Components that can be used for developing front-end applications at Volvo Car Corporation.
101 lines (93 loc) • 3.4 kB
JavaScript
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React from "react";
import { bool, func } from "prop-types";
import { useFela } from "react-fela";
import { Inline } from "../inline";
var rootStyle = {
width: 40,
height: 25,
position: "relative",
display: "inline-block",
verticalAlign: "middle",
flexGrow: 0,
flexShrink: 0
};
var inputStyle = function inputStyle(_ref) {
var theme = _ref.theme;
return {
appearance: "none",
border: "none",
":focus + span": {
borderColor: theme.tokens.inputBorderFocus
}
};
};
var backgroundStyle = function backgroundStyle(_ref2) {
var checked = _ref2.checked,
theme = _ref2.theme,
disabled = _ref2.disabled;
return {
position: "absolute",
borderWidth: 1,
borderStyle: "solid",
borderRadius: 45,
top: 0,
left: 0,
right: 0,
bottom: 0,
outline: 0,
padding: 0,
borderColor: theme.tokens.inputBorder,
background: theme.tokens.inputBackground,
cursor: disabled ? "not-allowed" : "pointer",
":focus": {
borderColor: theme.tokens.inputBorderFocus
},
"::after": {
content: "''",
position: "absolute",
bottom: 3,
left: 3,
width: 17,
height: 17,
background: disabled ? theme.tokens.inputDisabledControl : theme.tokens.inputControl,
borderRadius: "50%",
transition: "transform 300ms cubic-bezier(0.230, 1.000, 0.320, 1.000)",
transform: checked ? "translateX(15px)" : "translateX(0px)"
}
};
};
export function Toggle(_ref3) {
var checked = _ref3.checked,
disabled = _ref3.disabled,
onChange = _ref3.onChange,
props = _objectWithoutProperties(_ref3, ["checked", "disabled", "onChange"]);
var _useFela = useFela(),
theme = _useFela.theme;
return React.createElement(Inline, {
as: "label",
extend: rootStyle
}, React.createElement(Inline, _extends({
as: "input",
type: "checkbox",
extend: inputStyle({
theme: theme
}),
checked: checked,
onChange: onChange,
disabled: disabled
}, props)), React.createElement(Inline, {
"aria-label": "toggle",
extend: backgroundStyle({
checked: checked,
theme: theme,
disabled: disabled
})
}));
}
Toggle.propTypes = {
onChange: func.isRequired,
checked: bool
};