UNPKG

@atlaskit/button

Version:

A button triggers an event or action. They let users know what will happen next.

51 lines (46 loc) • 2.08 kB
import React from 'react'; import { fg } from '@atlaskit/platform-feature-flags/fg'; function isIconRenderProp(func) { return !func.displayName && // most function components and class components have a displayName, negate them !func.render && // forwardRef doesn't require a display name, however it does include a render function, negate them typeof func === 'function' // at the very least we need to be a function ; } /** * How far an icon can move past its box before it reaches the border of the button, which is the * space around the icon in the button minus the width of the border, so that the icon doesn't go * over the border. Passed to icon render props so that, for example, `IconMotion` can use it as its * `overshoot`. These are worked out once here, as every icon in every button goes through this file. */ const OVERSHOOT_DEFAULT = `calc(${"var(--ds-space-100, 8px)"} - ${"var(--ds-border-width, 1px)"})`; const OVERSHOOT_COMPACT = `calc(${"var(--ds-space-050, 4px)"} - ${"var(--ds-border-width, 1px)"})`; const getOvershoot = spacing => spacing === 'compact' ? OVERSHOOT_COMPACT : OVERSHOOT_DEFAULT; /** * __Icon renderer__ * * Used to support render props with icons. * */ const IconRenderer = ({ icon: Icon, spacing }) => { // React Compiler opt-out: RC-incompatible (memoization breaks runtime behaviour). 'use no memo'; const isRenderProp = isIconRenderProp(Icon); let iconProps = { label: '', color: 'currentColor', size: fg('platform_dst_button_chevron_sizing') ? iconName => iconName.startsWith('Chevron') ? 'small' : 'medium' : undefined }; // Only a render prop can use `motionOvershoot`, so an icon component doesn't get it const icon = isRenderProp ? Icon({ ...iconProps, motionOvershoot: getOvershoot(spacing) }) : /*#__PURE__*/React.createElement(Icon, iconProps); // @ts-expect-error -- The render-prop guard returns IconProp (a component type), which is not a ReactNode. return /*#__PURE__*/React.createElement(React.Fragment, null, icon); }; export default IconRenderer;