UNPKG

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.

136 lines (120 loc) 5.54 kB
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 _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } 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, { useContext, useEffect, useRef } from "react"; import PropTypes from "prop-types"; import { useFela } from "react-fela"; import { Click } from "../click"; import { LineTransitionContext } from "../tab-nav"; import { getThemeStyle } from "../../get-theme-style"; var styles = function styles(_ref) { var isActive = _ref.isActive, reverseOut = _ref.reverseOut, hasLineTransition = _ref.hasLineTransition, _ref$theme$colors = _ref.theme.colors, grey1 = _ref$theme$colors.grey1, primary = _ref$theme$colors.primary, white = _ref$theme$colors.white; return { display: "inline-flex", alignItems: "center", color: reverseOut ? white : grey1, fontSize: 15, height: 55, textAlign: "left", fontWeight: 200, letterSpacing: 0.3, margin: "0 20px 0 0", backgroundColor: reverseOut ? grey1 : "none", ":last-child": { margin: 0 }, transition: "color 200ms ease-out", ":hover": { color: reverseOut ? white : primary }, ":focus": { outline: "none", color: reverseOut ? white : primary }, extend: [{ condition: isActive, style: { color: reverseOut ? white : primary } }, { condition: !hasLineTransition, style: { borderBottom: "3px solid transparent", ":hover": { borderBottom: "3px solid ".concat(reverseOut ? white : primary) }, ":focus": { borderBottom: "3px solid ".concat(reverseOut ? white : primary) } } }, { condition: !hasLineTransition && isActive, style: { borderBottom: "3px solid ".concat(reverseOut ? white : primary) } }] }; }; export function TabNavItem(_ref2) { var children = _ref2.children, index = _ref2.index, isActive = _ref2.isActive, variant = _ref2.variant, props = _objectWithoutProperties(_ref2, ["children", "index", "isActive", "variant"]); var _useFela = useFela(), theme = _useFela.theme; var reverseOut = variant === "dark"; var _useContext = useContext(LineTransitionContext), updateDimensions = _useContext.updateDimensions; var ref = useRef(); useEffect(function () { if (updateDimensions && ref.current) { var _ref3 = ref.current ? ref.current.getBoundingClientRect() : { width: 0 }, width = _ref3.width; var x = ref.current ? ref.current.offsetLeft : 0; updateDimensions(index, { width: width, x: x }); } }, []); var styleProps = { isActive: isActive, reverseOut: reverseOut, hasLineTransition: !!updateDimensions, theme: theme }; if (styleProps.hasLineTransition && typeof index !== "number") { // Index property is passed down only to direct descendants of <TabNav> and is required for line transitions to work as intended. // If <TabNavItem>s are not direct descendants of <TabNav> an index will need to be passed down manually. // eslint-disable-next-line no-console console.warn("TabNavItem is not a direct descendant of TabNav. Please provide an index to each TabNavItem to enable line transitions."); return null; } return React.createElement(Click, _extends({ extend: _objectSpread({}, styles(styleProps), getThemeStyle("tabNavItem", theme, styleProps)) }, props, { innerRef: ref }), children); } TabNavItem.propTypes = { children: PropTypes.node, /** Indicate if the TabNavItem is in an active state */ isActive: PropTypes.bool, /** Dark text on light background, or vice versa */ variant: PropTypes.oneOf(["light", "dark"]) }; TabNavItem.defaultProps = { isActive: false, variant: "light" };