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.

108 lines (107 loc) 4.61 kB
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 ? (React.createElement(Tooltip, { ...tooltipProps }, children)) : (React.createElement(React.Fragment, null, 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 (React.createElement(Container, { tooltipProps: tooltipProps }, React.createElement(BaseButton, { as: Element, borderRadius: "m", colors: getIconButtonColors(elemProps['aria-pressed']), ...geButtonStyles(size, children, icon), ...elemProps }, icon && (React.createElement(BaseButton.Icon, { size: size === 'small' ? 'extraSmall' : 'medium', icon: icon, shouldMirrorIcon: useIsRTL() })), children && (React.createElement(Text, { ...textStyles, fontWeight: "bold", textAlign: "left" }, children))))); });