UNPKG

chakra-ui-bottom-navigation

Version:

Bottom navigation component built for chakra

288 lines (248 loc) 8.82 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var react = require('@chakra-ui/react'); var reactUtils = require('@chakra-ui/react-utils'); var utils = require('@chakra-ui/utils'); var React = _interopDefault(require('react')); var descendant = require('@chakra-ui/descendant'); var themeTools = require('@chakra-ui/theme-tools'); 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 _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; } var _createDescendantCont = /*#__PURE__*/descendant.createDescendantContext(), BottomNavigationDescendantsProvider = _createDescendantCont[0], useBottomNavigationDescendantsContext = _createDescendantCont[1], useBottomNavigationDescendants = _createDescendantCont[2], useBottomNavigationDescendant = _createDescendantCont[3]; var _createContext = /*#__PURE__*/reactUtils.createContext({ name: 'BottomNavigationContext', errorMessage: 'useBottomNavigationContext: `context` is undefined. Seems you forgot to wrap the accordion components in `<BottomNavigation />`' }), BottomNavigationProvider = _createContext[0], useBottomNavigationContext = _createContext[1]; var _createContext2 = /*#__PURE__*/reactUtils.createContext({ name: 'BottomNavigationItemContext', errorMessage: 'useBottomNavigationItemContext: `context` is undefined. Seems you forgot to wrap the accordion components in `<BottomNavigationItem />`' }), BottomNavigationItemProvider = _createContext2[0], useBottomNavigationItemContext = _createContext2[1]; var _excluded = ["value", "onChange", "showLabel"], _excluded2 = ["value", "onClick"]; var _createStylesContext = /*#__PURE__*/react.createStylesContext('BottomNavigation'), StylesProvider = _createStylesContext[0], useStyles = _createStylesContext[1]; var BottomNavigation = /*#__PURE__*/react.forwardRef(function (_ref, ref) { var value = _ref.value, onChange = _ref.onChange, showLabel = _ref.showLabel, props = _objectWithoutPropertiesLoose(_ref, _excluded); var styles = react.useMultiStyleConfig('BottomNavigation', props); var ownProps = react.omitThemingProps(props); var descendants = useBottomNavigationDescendants(); var ctx = React.useMemo(function () { return { value: value, onChange: onChange, showLabel: showLabel }; }, [value, onChange, showLabel]); return React.createElement(BottomNavigationProvider, { value: ctx }, React.createElement(BottomNavigationDescendantsProvider, { value: descendants }, React.createElement(StylesProvider, { value: styles }, React.createElement(react.chakra.nav, Object.assign({ ref: ref, className: utils.cx('chakra-bottom-navigation', props.className), __css: styles.container }, ownProps))))); }); if (utils.__DEV__) { BottomNavigation.displayName = 'BottomNavigation'; } BottomNavigation.defaultProps = { showLabel: 'always' }; var BottomNavigationItem = /*#__PURE__*/react.forwardRef(function (_ref2, ref) { var value = _ref2.value, onClick = _ref2.onClick, props = _objectWithoutPropertiesLoose(_ref2, _excluded2); var isDisabled = props.disabled || false; var styles = useStyles(); var context = useBottomNavigationContext(); var _useBottomNavigationD = useBottomNavigationDescendant({ disabled: isDisabled }), index = _useBottomNavigationD.index, register = _useBottomNavigationD.register; var itemValue = value || index; var isSelected = itemValue === context.value; var handleClick = React.useCallback(function (e) { onClick == null ? void 0 : onClick(e); context.onChange(itemValue); }, [itemValue, context.onChange]); var itemStyles = _extends({ opacity: isDisabled || !isSelected ? 0.4 : 1 }, styles.item); var ctx = React.useMemo(function () { return { isDisabled: isDisabled, isSelected: isSelected }; }, [isDisabled, isSelected]); return React.createElement(BottomNavigationItemProvider, { value: ctx }, React.createElement(react.chakra.button, Object.assign({ ref: reactUtils.mergeRefs(register, ref), className: utils.cx('chakra-bottom-navigation__item', props.className), __css: itemStyles, "aria-selected": utils.ariaAttr(isSelected), onClick: handleClick }, props))); }); if (utils.__DEV__) { BottomNavigationItem.displayName = 'BottomNavigationItem'; } var BottomNavigationIcon = function BottomNavigationIcon(props) { var styles = useStyles(); return React.createElement(react.Icon, Object.assign({ "aria-hidden": true, __css: styles.icon }, props, { className: utils.cx('chakra-bottom-navigation__icon', props.className) })); }; if (utils.__DEV__) { BottomNavigationIcon.displayName = 'BottomNavigationIcon'; } var BottomNavigationLabel = /*#__PURE__*/react.forwardRef(function (props, ref) { var _useBottomNavigationC = useBottomNavigationContext(), showLabel = _useBottomNavigationC.showLabel; var _useBottomNavigationI = useBottomNavigationItemContext(), isSelected = _useBottomNavigationI.isSelected; var styles = useStyles(); var dataIsLabelHidden = React.useMemo(function () { return utils.dataAttr(showLabel === 'never' || showLabel === 'if-active' && !isSelected); }, [showLabel, isSelected]); var labelStyles = _extends({ flex: 1 }, styles.label); return React.createElement(StylesProvider, { value: styles }, React.createElement(react.chakra.div, Object.assign({ ref: ref, className: utils.cx('chakra-bottom-navigation__label', props.className), __css: labelStyles, "data-hidden": dataIsLabelHidden }, props))); }); if (utils.__DEV__) { BottomNavigationLabel.displayName = 'BottomNavigationLabel'; } var parts = ['container', 'item', 'label', 'icon']; var baseStyle = { container: { position: 'fixed', display: 'flex', justifyContent: 'space-between', px: 4, py: 2 }, item: { flex: 1, mx: 4, opacity: 0.4, _selected: { opacity: 1 } }, label: { _hidden: { opacity: 0, height: 0, width: 0, overflow: 'hidden' } } }; function variantFlat(props) { var c = props.colorScheme; return { container: { bottom: 0, left: 0, right: 0, color: themeTools.mode(c + ".900", "#fff")(props), bg: themeTools.mode(c + ".300", c + ".900")(props), boxShadow: 'lg' } }; } function variantFloat(props) { var flatVariantStyles = variantFlat(props); return _extends({}, flatVariantStyles, { container: _extends({}, flatVariantStyles.container, { bottom: 4, left: 4, right: 4, borderRadius: 'base' }) }); } var variants = { flat: variantFlat, "float": variantFloat }; var defaultProps = { variant: 'float', colorScheme: 'blue' }; var BottomNavigationStyleConfig = { parts: parts, baseStyle: baseStyle, variants: variants, defaultProps: defaultProps }; var withDefaultStyles = function withDefaultStyles(overrides) { return react.mergeThemeOverride(BottomNavigationStyleConfig, overrides); }; exports.BottomNavigation = BottomNavigation; exports.BottomNavigationDescendantsProvider = BottomNavigationDescendantsProvider; exports.BottomNavigationIcon = BottomNavigationIcon; exports.BottomNavigationItem = BottomNavigationItem; exports.BottomNavigationItemProvider = BottomNavigationItemProvider; exports.BottomNavigationLabel = BottomNavigationLabel; exports.BottomNavigationProvider = BottomNavigationProvider; exports.BottomNavigationStyleConfig = BottomNavigationStyleConfig; exports.useBottomNavigationContext = useBottomNavigationContext; exports.useBottomNavigationDescendant = useBottomNavigationDescendant; exports.useBottomNavigationDescendants = useBottomNavigationDescendants; exports.useBottomNavigationDescendantsContext = useBottomNavigationDescendantsContext; exports.useBottomNavigationItemContext = useBottomNavigationItemContext; exports.withDefaultStyles = withDefaultStyles; //# sourceMappingURL=chakra-ui-bottom-navigation.cjs.development.js.map