UNPKG

@greensight/gds

Version:
151 lines (145 loc) 5.48 kB
import { j as jsx } from './emotion-react.esm-CuKt2qQ4.js'; import React__default, { useMemo, useCallback, forwardRef } from 'react'; import { a as _objectSpread2, c as _objectWithoutProperties, d as _extends } from './_rollupPluginBabelHelpers-D0Wa2C7U.js'; import './emotion-element-c16c303e.esm-CnTrGsmj.js'; var extractCSSOption = function extractCSSOption(optionized, option) { return optionized[option]; }; var useThemeCSSPart = function useThemeCSSPart(theme, state) { var actualTheme = useMemo(function () { return typeof theme === 'function' ? theme(state) : theme; }, [state, theme]); return useCallback(function (key, extraData) { var element = actualTheme[key]; if (typeof element === 'function') return element(extraData ? _objectSpread2(_objectSpread2({}, state), extraData) : state); return element; }, [state, actualTheme]); }; var useThemeCSS = function useThemeCSS(theme, state) { return useMemo(function () { var res = {}; var keys = Object.keys(theme); keys.forEach(function (key) { var element = theme[key]; if (typeof element === 'function') res[key] = element(state);else res[key] = element; }); return res; }, [state, theme]); }; var _excluded = ["children", "block", "size", "theme", "variant", "Icon", "iconAfter", "hidden", "type", "as", "external", "disabled", "rounded", "css", "typography", "wrap", "getTypographyCSS"], _excluded2 = ["theme", "variant", "size"]; /** * Button component. * * Renders <button /> or <a /> (pass `href`) or any custom element (pass `as`). * * Usage with themes: * @example * enum Variants { * primary = 'primary', * } * enum Sizes { * sm = 'sm', * } * const Button = createButtonWithTheme<typeof Variants, typeof Sizes>(); * const Test = () => ( * <Button * as="a" * href="#" * variant="primary" * theme={{ * button: {}, * icon: {}, * }} * > * Content * </Button> *); */ var BaseButton = function BaseButton(_ref, ref) { var children = _ref.children, _ref$block = _ref.block, block = _ref$block === void 0 ? false : _ref$block, size = _ref.size, theme = _ref.theme, variant = _ref.variant, Icon = _ref.Icon, _ref$iconAfter = _ref.iconAfter, iconAfter = _ref$iconAfter === void 0 ? false : _ref$iconAfter, _ref$hidden = _ref.hidden, hidden = _ref$hidden === void 0 ? false : _ref$hidden, _ref$type = _ref.type, type = _ref$type === void 0 ? 'button' : _ref$type, as = _ref.as, _ref$external = _ref.external, external = _ref$external === void 0 ? false : _ref$external, _ref$disabled = _ref.disabled, disabled = _ref$disabled === void 0 ? false : _ref$disabled, _ref$rounded = _ref.rounded, rounded = _ref$rounded === void 0 ? true : _ref$rounded, css = _ref.css, typography = _ref.typography, _ref$wrap = _ref.wrap, wrap = _ref$wrap === void 0 ? false : _ref$wrap, getTypographyCSS = _ref.getTypographyCSS, props = _objectWithoutProperties(_ref, _excluded); var hasChildren = !!children; var state = useMemo(function () { return { disabled: disabled, hasChildren: hasChildren, hidden: hidden, size: size, variant: variant, block: block, iconAfter: iconAfter, rounded: rounded, typography: typography, wrap: wrap }; }, [disabled, hasChildren, hidden, size, variant, block, iconAfter, rounded, typography, wrap]); if (!theme) { throw new Error('[Button] theme is required'); } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion var _useThemeCSS = useThemeCSS(theme, state), totalCSS = _useThemeCSS.button, iconCSS = _useThemeCSS.icon; var icon = Icon ? jsx(Icon, { css: iconCSS }) : null; return jsx(as || 'button', _objectSpread2({ ref: ref, type: !as || as === 'button' ? type : null, target: external ? '_blank' : null, rel: external ? 'nofollow noopener' : null, css: [totalCSS, typography && getTypographyCSS ? getTypographyCSS(typography) : {}, css], disabled: disabled }, props), jsx(React__default.Fragment, null, icon && !iconAfter && icon, hidden ? '' : children, icon && iconAfter && icon)); }; var ButtonRef = /*#__PURE__*/forwardRef(BaseButton); /** * Proxy component setting initial values for theme, variant, and size for button in the used project. */ var createButtonWithTheme = function createButtonWithTheme(defaultTheme, defaultVariant, defaultSize, getTypographyCSS) { // as any нужны т.к. в новых версиях ts ругается в целом на такой компонент var renderThemedButton = function renderThemedButton(_ref2, ref) { var _ref2$theme = _ref2.theme, theme = _ref2$theme === void 0 ? defaultTheme : _ref2$theme, _ref2$variant = _ref2.variant, variant = _ref2$variant === void 0 ? defaultVariant : _ref2$variant, _ref2$size = _ref2.size, size = _ref2$size === void 0 ? defaultSize : _ref2$size, props = _objectWithoutProperties(_ref2, _excluded2); return jsx(ButtonRef, _extends({ ref: ref, getTypographyCSS: getTypographyCSS, theme: theme, variant: variant, size: size }, props)); }; renderThemedButton.displayName = 'Button'; return /*#__PURE__*/forwardRef(renderThemedButton); }; export { BaseButton as B, useThemeCSS as a, createButtonWithTheme as c, extractCSSOption as e, useThemeCSSPart as u };