|
| 1 | +import type {IconProps} from '@primer/octicons-react' |
| 2 | +import React from 'react' |
| 3 | +import styled from 'styled-components' |
| 4 | +import {get} from '../constants' |
| 5 | +import sx, {SxProp} from '../sx' |
| 6 | + |
| 7 | +/** |
| 8 | + * Contract for props passed to the `Item` component. |
| 9 | + */ |
| 10 | +export interface ItemProps extends React.ComponentPropsWithoutRef<'div'>, SxProp { |
| 11 | + /** |
| 12 | + * Primary text which names an `Item`. |
| 13 | + */ |
| 14 | + text: string |
| 15 | + |
| 16 | + /** |
| 17 | + * Secondary text which provides additional information about an `Item`. |
| 18 | + */ |
| 19 | + description?: string |
| 20 | + |
| 21 | + /** |
| 22 | + * Secondary text style variations. Usage is discretionary. |
| 23 | + * |
| 24 | + * - `"inline"` - Secondary text is positioned beside primary text. |
| 25 | + * - `"block"` - Secondary text is positioned below primary text. |
| 26 | + */ |
| 27 | + descriptionVariant?: 'inline' | 'block' |
| 28 | + |
| 29 | + /** |
| 30 | + * Icon (or similar) positioned before `Item` text. |
| 31 | + */ |
| 32 | + leadingVisual?: React.FunctionComponent<IconProps> |
| 33 | + |
| 34 | + /** |
| 35 | + * Style variations associated with various `Item` types. |
| 36 | + * |
| 37 | + * - `"default"` - An action `Item`. |
| 38 | + * - `"danger"` - A destructive action `Item`. |
| 39 | + */ |
| 40 | + variant?: 'default' | 'danger' |
| 41 | +} |
| 42 | + |
| 43 | +const StyledItem = styled.div<{variant: ItemProps['variant']} & SxProp>` |
| 44 | + /* 6px vertical padding + 20px line height = 32px total height |
| 45 | + * |
| 46 | + * TODO: When rem-based spacing on a 4px scale lands, replace |
| 47 | + * hardcoded '6px' with 'calc((${get('space.s32')} - ${get('space.20')}) / 2)'. |
| 48 | + */ |
| 49 | + padding: 6px ${get('space.2')}; |
| 50 | + display: flex; |
| 51 | + border-radius: ${get('radii.2')}; |
| 52 | + color: ${({variant}) => (variant === 'danger' ? get('colors.text.danger') : 'inherit')}; |
| 53 | +
|
| 54 | + @media (hover: hover) and (pointer: fine) { |
| 55 | + :hover { |
| 56 | + background: ${props => |
| 57 | + props.variant === 'danger' ? get('colors.bg.danger') : get('colors.selectMenu.tapHighlight')}; |
| 58 | + cursor: pointer; |
| 59 | + } |
| 60 | + } |
| 61 | +
|
| 62 | + ${sx} |
| 63 | +` |
| 64 | + |
| 65 | +const StyledTextContainer = styled.div<{descriptionVariant: ItemProps['descriptionVariant']}>` |
| 66 | + flex-direction: ${({descriptionVariant}) => (descriptionVariant === 'inline' ? 'row' : 'column')}; |
| 67 | +` |
| 68 | + |
| 69 | +const LeadingVisualContainer = styled.div` |
| 70 | + { |
| 71 | + /* Match visual height to adjacent text line height. |
| 72 | + * |
| 73 | + * TODO: When rem-based spacing on a 4px scale lands, replace |
| 74 | + * hardcoded '20px' with '${get('space.s20')}'. |
| 75 | + */ |
| 76 | + } |
| 77 | + height: 20px; |
| 78 | + width: ${get('space.3')}; |
| 79 | + display: flex; |
| 80 | + flex-direction: column; |
| 81 | + justify-content: center; |
| 82 | + margin-right: ${get('space.2')}; |
| 83 | +
|
| 84 | + svg { |
| 85 | + fill: ${get('colors.text.secondary')}; |
| 86 | + font-size: ${get('fontSizes.0')}; |
| 87 | + } |
| 88 | +` |
| 89 | + |
| 90 | +const DescriptionContainer = styled.span` |
| 91 | + color: ${get('colors.text.secondary')}; |
| 92 | +` |
| 93 | + |
| 94 | +/** |
| 95 | + * An actionable or selectable `Item` with an optional icon and description. |
| 96 | + */ |
| 97 | +export function Item({ |
| 98 | + text, |
| 99 | + description, |
| 100 | + descriptionVariant = 'inline', |
| 101 | + leadingVisual: LeadingVisual, |
| 102 | + variant = 'default', |
| 103 | + ...props |
| 104 | +}: Partial<ItemProps>): JSX.Element { |
| 105 | + return ( |
| 106 | + <StyledItem variant={variant} {...props}> |
| 107 | + {LeadingVisual && ( |
| 108 | + <LeadingVisualContainer> |
| 109 | + <LeadingVisual /> |
| 110 | + </LeadingVisualContainer> |
| 111 | + )} |
| 112 | + <StyledTextContainer descriptionVariant={descriptionVariant}> |
| 113 | + <div>{text}</div> |
| 114 | + {description && <DescriptionContainer>{description}</DescriptionContainer>} |
| 115 | + </StyledTextContainer> |
| 116 | + </StyledItem> |
| 117 | + ) |
| 118 | +} |
0 commit comments