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: status item styles, add option to place status icon before status text #567

Merged
Merged
4 changes: 3 additions & 1 deletion src/components/Avatar/Avatar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { IconName } from '../Icon';
import {
Avatar,
AvatarProps,
StatusItemsPosition,
getStatusItemSizeAndPadding,
StatusItemIconAlign,
StatusItemsPosition,
} from './';
import { Stack } from '../Stack';
import { TooltipTheme } from '../Tooltip';
Expand Down Expand Up @@ -216,6 +217,7 @@ const Avatar_StatusItem_Story: ComponentStory<typeof Avatar> = (args) => {
backgroundColor: 'var(--blue-color-20)',
path: IconName.mdiCalendar,
text: '20',
alignIcon: StatusItemIconAlign.Left,
},
},
},
Expand Down
30 changes: 24 additions & 6 deletions src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
AvatarOutlineProps,
AvatarProps,
BaseAvatarProps,
StatusItemIconAlign,
StatusItemsPosition,
StatusItemsProps,
} from './';
Expand Down Expand Up @@ -43,6 +44,7 @@ export const getStatusItemSizeAndPadding = (
const StatusItemWrapperPaddingFactor: number = 0.06;
const DefaultStatusItemMaxTextLength: number = 3;
const MinStatusItemFontSize: number = 12;
const StatusItemFontDiff: string = '2px';

const StatusItemOutlineDefaults: React.CSSProperties = {
outlineColor: 'var(--grey-color-80)',
Expand Down Expand Up @@ -152,13 +154,29 @@ const AvatarStatusItems: FC<BaseAvatarProps> = React.forwardRef(
<>
{Object.keys(statusItems).map((position: StatusItemsPosition) => {
const statusItemProps: StatusItemsProps = statusItems[position];
const alignIcon: StatusItemIconAlign =
statusItemProps.alignIcon ?? StatusItemIconAlign.Right;
const showStatusItemText: boolean =
statusItemProps.text &&
statusItemProps.text.length <=
(statusItemProps.textMaxLength ?? DefaultStatusItemMaxTextLength);
const wrapperPadding: string | number =
statusItemProps?.wrapperStyle?.padding ??
`(${size} * ${StatusItemWrapperPaddingFactor})`;
const statusItemTextClasses = mergeClasses([
styles.avatarStatusItemText,
{ [styles.avatarStatusItemTextRtl]: htmlDir === 'rtl' },
{
[styles.textMarginRight]: alignIcon == StatusItemIconAlign.Right,
},
{ [styles.textMarginLeft]: alignIcon == StatusItemIconAlign.Left },
]);
const statusItemIconElement = (
<Icon
{...statusItemProps}
classNames={styles.avatarStatusItemIcon}
/>
);
return (
<div
key={position}
Expand Down Expand Up @@ -200,21 +218,21 @@ const AvatarStatusItems: FC<BaseAvatarProps> = React.forwardRef(
? { 'aria-label': statusItemProps.ariaLabel }
: {})}
>
{showStatusItemText && (showStatusItemsText[position] ?? true) ? (
{alignIcon == StatusItemIconAlign.Left && statusItemIconElement}
{showStatusItemText && (showStatusItemsText[position] ?? true) && (
<span
ref={(el) => (statusItemsRef.current[position] = el)}
style={{
fontSize: statusItemProps.size,
color: statusItemProps.color,
fontSize: `calc(${statusItemProps.size} + ${StatusItemFontDiff})`,
lineHeight: statusItemProps.size,
}}
className={styles.avatarStatusItemText}
className={statusItemTextClasses}
>
{statusItemProps.text}
</span>
) : (
''
)}
<Icon {...statusItemProps} />
{alignIcon == StatusItemIconAlign.Right && statusItemIconElement}
</div>
);
})}
Expand Down
10 changes: 10 additions & 0 deletions src/components/Avatar/Avatar.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export enum StatusItemsPosition {
BottomLeft = 'bottomLeft',
}

export enum StatusItemIconAlign {
Left = 'left',
Right = 'right',
}

export interface AvatarOutlineProps {
/**
* Outline color
Expand Down Expand Up @@ -67,6 +72,11 @@ export interface StatusItemsProps extends IconProps {
* Class for status item wrapper
*/
wrapperClassName?: string;
/**
* Status item icon alignment
* @default StatusItemIconAlign.Right
*/
alignIcon?: StatusItemIconAlign;
/**
* Text present with icon
*/
Expand Down
4 changes: 2 additions & 2 deletions src/components/Avatar/Styles/group.scss
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
.avatar-group {
display: inline-flex;

:not(:first-child):not(.avatar-group-tooltip):not(.avatar-status-item) {
:not(:first-child):not(.avatar-group-tooltip):not(.avatar-status-item):not(.avatar-status-item-text):not(.avatar-status-item-icon) {
margin-left: -$space-xs;
}

&.spaced {
:not(:first-child):not(.avatar-group-tooltip):not(.avatar-status-item) {
:not(:first-child):not(.avatar-group-tooltip):not(.avatar-status-item):not(.avatar-status-item-text):not(.avatar-status-item-icon) {
margin-left: -$space-xxxs;
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/components/Avatar/Styles/rtl.scss
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
.avatar-group {
&-rtl {
:not(:first-child):not(.avatar-group-tooltip):not(.avatar-status-item) {
:not(:first-child):not(.avatar-group-tooltip):not(.avatar-status-item):not(.avatar-status-item-text):not(.avatar-status-item-icon) {
margin-right: -$space-xs;
margin-left: 0;
}

&.spaced {
:not(:first-child):not(.avatar-group-spaced-tooltip):not(.avatar-status-item) {
:not(:first-child):not(.avatar-group-spaced-tooltip):not(.avatar-status-item):not(.avatar-status-item-text):not(.avatar-status-item-icon) {
margin-right: -$space-xxxs;
margin-left: 0;
}
}
}
}

.avatar-status-item-text-rtl {
&.text-margin-right {
margin-right: 0;
margin-left: 2px;
}

&.text-margin-left {
margin-left: 0;
margin-right: 2px;
}
}
14 changes: 13 additions & 1 deletion src/components/Avatar/avatar.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,19 @@
}

.avatar-status-item-text {
margin-right: 2px;
font-weight: 600;

&.text-margin-right {
margin-right: 2px;
}

&.text-margin-left {
margin-left: 2px;
}
}

.avatar-status-item-icon {
margin: 0;
}

.avatar-img-wrapper {
Expand Down
2 changes: 2 additions & 0 deletions src/octuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Avatar,
AvatarGroup,
AvatarGroupVariant,
StatusItemIconAlign,
StatusItemsPosition,
} from './components/Avatar';

Expand Down Expand Up @@ -335,6 +336,7 @@ export {
Stack,
Stat,
StatThemeName,
StatusItemIconAlign,
StatusItemsPosition,
StatValidationStatus,
Step,
Expand Down