Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Split <Item> labels #1320

Merged
merged 7 commits into from
Jun 25, 2021
73 changes: 35 additions & 38 deletions src/ActionList/Item.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {CheckIcon, IconProps} from '@primer/octicons-react'
import React, {useCallback} from 'react'
import React, {useCallback, useMemo} from 'react'
import {get} from '../constants'
import sx, {SxProp} from '../sx'
import Truncate from '../Truncate'
Expand Down Expand Up @@ -151,9 +151,13 @@ const getItemVariant = (variant = 'default', disabled?: boolean) => {
}
}

const StyledItemContent = styled.div`
const StyledItemContent = styled.div<{
descriptionVariant: ItemProps['descriptionVariant']
}>`
align-items: baseline;
display: flex;
min-width: 0;
flex-direction: ${({descriptionVariant}) => (descriptionVariant === 'inline' ? 'row' : 'column')};
smockle marked this conversation as resolved.
Show resolved Hide resolved
flex-grow: 1;
position: relative;
`
Expand Down Expand Up @@ -229,16 +233,9 @@ const StyledItem = styled.div<
${sx}
`

export const TextContainer = styled.div<{
export const TextContainer = styled.span<{
dangerouslySetInnerHtml?: React.DOMAttributes<HTMLDivElement>['dangerouslySetInnerHTML']
descriptionVariant: ItemProps['descriptionVariant']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will require an adjustment on the memex side.

}>`
display: flex;
min-width: 0;
flex-grow: 1;
flex-direction: ${({descriptionVariant}) => (descriptionVariant === 'inline' ? 'row' : 'column')};
align-items: baseline;
`
}>``

const BaseVisualContainer = styled.div<{variant?: ItemProps['variant']; disabled?: boolean}>`
// Match visual height to adjacent text line height.
Expand Down Expand Up @@ -310,10 +307,12 @@ export function Item(itemProps: Partial<ItemProps> & {item?: ItemInput}): JSX.El
onKeyPress,
children,
onClick,
id,
id: _id,
...props
} = itemProps

const id = useMemo(() => _id ?? uniqueId(), [_id])
smockle marked this conversation as resolved.
Show resolved Hide resolved

const keyPressHandler = useCallback(
event => {
if (disabled) {
Expand Down Expand Up @@ -358,6 +357,8 @@ export function Item(itemProps: Partial<ItemProps> & {item?: ItemInput}): JSX.El
variant={variant}
showDivider={showDivider}
aria-selected={selected}
aria-labelledby={text ? `${id}-label` : undefined}
dgreif marked this conversation as resolved.
Show resolved Hide resolved
aria-describedby={description ? `${id}-description` : undefined}
smockle marked this conversation as resolved.
Show resolved Hide resolved
{...props}
data-id={id}
onKeyPress={keyPressHandler}
Expand Down Expand Up @@ -393,35 +394,31 @@ export function Item(itemProps: Partial<ItemProps> & {item?: ItemInput}): JSX.El
<LeadingVisual />
</LeadingVisualContainer>
)}
<StyledItemContent>
<StyledItemContent descriptionVariant={descriptionVariant}>
{children}
{(text || description) && (
<TextContainer descriptionVariant={descriptionVariant}>
{text && <div>{text}</div>}
{description && (
<DescriptionContainer descriptionVariant={descriptionVariant}>
{descriptionVariant === 'block' ? (
description
) : (
<Truncate title={description} inline={true} maxWidth="100%">
{description}
</Truncate>
)}
</DescriptionContainer>
)}
</TextContainer>
)}
{(TrailingIcon || trailingText) && (
<TrailingVisualContainer variant={variant} disabled={disabled}>
{trailingText && <div>{trailingText}</div>}
{TrailingIcon && (
<div>
<TrailingIcon />
</div>
{text ? <TextContainer id={`${id}-label`}>{text}</TextContainer> : null}
{description ? (
<DescriptionContainer id={`${id}-description`} descriptionVariant={descriptionVariant}>
{descriptionVariant === 'block' ? (
description
) : (
<Truncate title={description} inline={true} maxWidth="100%">
{description}
</Truncate>
)}
</TrailingVisualContainer>
)}
</DescriptionContainer>
) : null}
</StyledItemContent>
{(TrailingIcon || trailingText) && (
<TrailingVisualContainer variant={variant} disabled={disabled}>
{trailingText && <div>{trailingText}</div>}
{TrailingIcon && (
<div>
<TrailingIcon />
</div>
)}
</TrailingVisualContainer>
)}
dgreif marked this conversation as resolved.
Show resolved Hide resolved
</StyledItem>
)
}