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.
118 lines (110 loc) • 4.08 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() {
return {
appearance: 'none',
border: 'none',
':focus + span span': {
boxShadow: '0 0 2px #000'
}
};
};
var backgroundStyle = function backgroundStyle(_ref) {
var checked = _ref.checked,
theme = _ref.theme,
disabled = _ref.disabled,
isValid = _ref.isValid;
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,
extend: [{
condition: !isValid,
style: {
borderColor: theme.color.foreground.alert
}
}, {
condition: disabled,
style: {
borderColor: theme.tokens.inputDisabledBorder
}
}],
transition: 'background 300ms cubic-bezier(0.230, 1.000, 0.320, 1.000)',
background: checked ? theme.color.ornament.highlight : theme.color.background.primary,
cursor: disabled ? 'not-allowed' : 'pointer',
'& span': {
position: 'absolute',
bottom: 2,
left: 2,
width: 17,
height: 17,
borderWidth: 1,
boxShadow: '0px 0px 1px inset ' + theme.color.foreground.secondary,
borderStyle: 'solid',
background: theme.color.background.primary,
borderColor: disabled ? theme.tokens.inputDisabledBorder : theme.tokens.inputBorder,
borderRadius: '50%',
transition: 'transform 300ms cubic-bezier(0.230, 1.000, 0.320, 1.000)',
transform: checked ? 'translateX(15px)' : 'translateX(0px)'
}
};
};
export var Toggle = React.forwardRef(function (_ref2, ref) {
var checked = _ref2.checked,
disabled = _ref2.disabled,
onChange = _ref2.onChange,
_ref2$isValid = _ref2.isValid,
isValid = _ref2$isValid === void 0 ? true : _ref2$isValid,
props = _objectWithoutProperties(_ref2, ["checked", "disabled", "onChange", "isValid"]);
var _useFela = useFela(),
theme = _useFela.theme;
return React.createElement(Inline, {
as: "label",
extend: rootStyle
}, React.createElement(Inline, _extends({
ref: ref,
as: "input",
type: "checkbox",
extend: inputStyle(),
checked: checked,
onChange: onChange,
disabled: disabled
}, props)), React.createElement(Inline, {
"aria-label": "toggle",
extend: backgroundStyle({
checked: checked,
theme: theme,
disabled: disabled,
isValid: isValid
})
}, React.createElement(Inline, null)));
});
Toggle.displayName = 'Toggle';
Toggle.propTypes = {
onChange: func.isRequired,
checked: bool,
disabled: bool,
isValid: bool
};