UNPKG

@workday/canvas-kit-preview-react

Version:

Canvas Kit Preview is made up of components that have the full design and a11y review, are part of the DS ecosystem and are approved for use in product. The API's could be subject to change, but not without strong communication and migration strategies.

106 lines (105 loc) 4.6 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import * as React from 'react'; import { colors, type, space } from '@workday/canvas-kit-react/tokens'; import { createSubcomponent, useIsRTL } from '@workday/canvas-kit-react/common'; import { Tooltip } from '@workday/canvas-kit-react/tooltip'; import { BaseButton, } from '@workday/canvas-kit-react/button'; import { useSegmentedControlModel } from './hooks/useSegmentedControlModel'; import { Text } from '@workday/canvas-kit-react/text'; import { useSegmentedControlItem } from './hooks/useSegmentedControlItem'; const getMinWidthStyles = (children, size) => { switch (size) { case 'large': return children ? '112px' : '48px'; case 'medium': return children ? '96px' : space.xl; case 'small': return children ? space.xxxl : space.l; case 'extraSmall': return children ? 'auto' : space.m; default: return children ? '96px' : space.xl; } }; const getButtonSize = (size) => { switch (size) { case 'large': return 'medium'; case 'medium': return 'small'; case 'small': return 'extraSmall'; default: return 'medium'; } }; const getIconButtonColors = (toggled) => { return { default: { background: toggled ? colors.frenchVanilla100 : colors.soap200, border: toggled ? colors.licorice200 : 'transparent', icon: toggled ? colors.blackPepper400 : colors.licorice400, label: toggled ? colors.blackPepper400 : colors.licorice400, }, hover: { background: toggled ? colors.frenchVanilla100 : colors.soap400, border: toggled ? colors.licorice200 : 'transparent', icon: colors.licorice400, label: colors.licorice400, }, active: { background: toggled ? colors.frenchVanilla100 : colors.soap400, border: toggled ? colors.licorice200 : 'transparent', icon: colors.licorice400, label: colors.licorice400, }, focus: { background: toggled ? colors.frenchVanilla100 : colors.soap200, border: toggled ? colors.licorice200 : 'transparent', icon: toggled ? colors.blackPepper400 : colors.licorice400, label: toggled ? colors.blackPepper400 : colors.licorice400, }, disabled: { background: colors.soap200, opacity: '1', icon: colors.licorice400, border: toggled ? colors.licorice200 : 'transparent', label: colors.blackPepper400, }, }; }; const getPaddingStyles = (children, size, icon) => { if (!children) { return 0; } switch (size) { case 'large': return icon ? `0 ${space.m} 0 20px` : `0 ${space.m}`; case 'medium': return icon ? `0 20px 0 ${space.s}` : `0 ${space.s}`; case 'small': return icon ? `0 ${space.xs} 0 ${space.xxs}` : `0 ${space.xs}`; default: return icon ? `0 20px 0 ${space.s}` : `0 ${space.s}`; } }; const geButtonStyles = (size, children, icon) => { const buttonSize = getButtonSize(size); const minWidthValue = getMinWidthStyles(children, children ? size : buttonSize); return { height: getMinWidthStyles(false, buttonSize), minWidth: minWidthValue, padding: getPaddingStyles(children, size, icon), }; }; const Container = ({ tooltipProps, children, }) => { return tooltipProps ? (_jsx(Tooltip, { ...tooltipProps, children: children })) : (_jsx(React.Fragment, { children: children })); }; export const SegmentedControlItem = createSubcomponent('button')({ displayName: 'SegmentedControl.Item', modelHook: useSegmentedControlModel, elemPropsHook: useSegmentedControlItem, })(({ children, icon, tooltipProps, ...elemProps }, Element, { state: { size } }) => { const { color, ...textStyles } = type.levels.subtext[size === 'small' ? 'medium' : 'large']; return (_jsx(Container, { tooltipProps: tooltipProps, children: _jsxs(BaseButton, { as: Element, borderRadius: "m", colors: getIconButtonColors(elemProps['aria-pressed']), ...geButtonStyles(size, children, icon), ...elemProps, children: [icon && (_jsx(BaseButton.Icon, { size: size === 'small' ? 'extraSmall' : 'medium', icon: icon, shouldMirrorIcon: useIsRTL() })), children && (_jsx(Text, { ...textStyles, fontWeight: "bold", textAlign: "left", children: children }))] }) })); });