Skip to content

Commit 18d78fc

Browse files
author
Hector Arce De Las Heras
committed
Refactor Message Component for Enhanced Flexibility and Readability
This commit involves significant changes to the Message component and related files, with a focus on enhancing flexibility and readability. Key changes include the refactoring of styled components, code simplification, and updates to the Storybook file and styles. Key changes include: message.styled.ts: Refactored MessageHeaderStyled and ButtonSectionStyled to include style props, allowing for more flexible styling. messageStandAlone.tsx: Removed unnecessary div elements and restructured the MessageStandAloneComponent to simplify the code and improve readability. message.stories.tsx: Updated the Storybook file to include new props such as infoIcon, closeIcon, actionButton, and tag. styles.ts: Adjusted the commonProps object to modify the flex direction and gap of the container and headerContainer styles, and added a new buttonSectionContainer style. messageTheme.ts: Added a buttonSectionContainer to the MessagePropsThemeType to allow for styling of the button section container. message.ts: Changed the content prop type from string | ReactNode to React.ReactNode to allow for more flexibility in the type of content that can be passed. These changes improve the flexibility and readability of the Message component, enhancing its usability and maintainability.
1 parent 053ffd2 commit 18d78fc

File tree

7 files changed

+87
-66
lines changed

7 files changed

+87
-66
lines changed

src/components/message/message.styled.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ export const MessageStyled = styled.div<{
1111
${({ styles }) => getStyles(styles.container)};
1212
`;
1313

14-
export const MessageHeaderStyled = styled.div`
15-
width: 100%;
16-
`;
17-
18-
export const MessageHeaderTitleStyled = styled.div<{
14+
export const MessageHeaderStyled = styled.div<{
1915
styles: MessagePropsThemeType;
2016
isLargeMessage?: boolean;
2117
withIcon?: boolean;
@@ -52,11 +48,10 @@ export const ExtraActioButtonWrapperStyled = styled.div<{
5248
${({ styles }) => getStyles(styles.extraActionButtonContainer)};
5349
`;
5450

55-
export const ButtonSectionStyled = styled.div`
56-
display: flex;
57-
flex-direction: column;
58-
width: 100%;
59-
align-items: flex-end;
51+
export const ButtonSectionStyled = styled.div<{
52+
styles: MessagePropsThemeType;
53+
}>`
54+
${({ styles }) => getStyles(styles.buttonSectionContainer)};
6055
`;
6156

6257
export const ActioButtonWrapperStyled = styled.div<{

src/components/message/messageStandAlone.tsx

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
LinkContainerStyled,
1818
MessageContentStyled,
1919
MessageHeaderStyled,
20-
MessageHeaderTitleStyled,
2120
MessageStyled,
2221
MessageTextStyled,
2322
} from './message.styled';
@@ -60,11 +59,9 @@ const MessageStandAloneComponent = (
6059
);
6160

6261
const buildTag = () => (
63-
<div>
64-
<Tag option={''} status={''} variant={''} {...props.tag}>
65-
{props.tag?.content}
66-
</Tag>
67-
</div>
62+
<Tag option={''} status={''} variant={''} {...props.tag}>
63+
{props.tag?.content}
64+
</Tag>
6865
);
6966

7067
const buildExtraActionButton = () =>
@@ -89,31 +86,31 @@ const MessageStandAloneComponent = (
8986
);
9087
};
9188

92-
return props.open ? (
93-
<MessageStyled
94-
ref={ref}
95-
aria-live={ariaLive}
96-
data-testid={`${props.dataTestId}Message`}
97-
id={props.id}
98-
role={props.role}
99-
styles={props.styles}
100-
>
101-
<MessageHeaderStyled>
102-
<MessageHeaderTitleStyled
103-
isLargeMessage={isLargeMessage}
89+
return (
90+
<>
91+
{props.open && (
92+
<MessageStyled
93+
ref={ref}
94+
aria-live={ariaLive}
95+
data-testid={`${props.dataTestId}Message`}
96+
id={props.id}
97+
role={props.role}
10498
styles={props.styles}
105-
withIcon={!!props.closeIcon}
10699
>
107100
{buildIconOrIllustration()}
108-
<div>
101+
<MessageHeaderStyled
102+
isLargeMessage={isLargeMessage}
103+
styles={props.styles}
104+
withIcon={!!props.closeIcon}
105+
>
109106
{props.title && (
110107
<MessageTextStyled
111108
aria-errormessage={props.ariaMessageId}
112109
extraPaddingGap={!!props.closeIcon}
113110
styles={props.styles.closeIcon}
114111
>
115112
<Text
116-
component={TextComponentType.H2}
113+
component={TextComponentType.PARAGRAPH}
117114
customTypography={props.styles.title}
118115
dataTestId={`${props.dataTestId}Title`}
119116
{...props.title}
@@ -126,31 +123,31 @@ const MessageStandAloneComponent = (
126123
<MessageContentStyled isLargeMessage={isLargeMessage} styles={props.styles}>
127124
{buildContent()}
128125
</MessageContentStyled>
129-
</div>
130-
</MessageHeaderTitleStyled>
131-
</MessageHeaderStyled>
132-
<ButtonSectionStyled>
133-
{buildExtraActionButton()}
134-
{buildActionButton()}
135-
{props.closeIcon?.onClick && (
136-
<CloseButtonSectionStyled styles={props.styles}>
137-
<ElementOrIcon customIconStyles={props.styles.closeIcon} {...props.closeIcon} />
138-
</CloseButtonSectionStyled>
139-
)}
140-
</ButtonSectionStyled>
141-
{props.link?.content && (
142-
<LinkContainerStyled styles={props.styles}>
143-
<Link
144-
dataTestId={`${props.dataTestId}Link`}
145-
decoration={TextDecorationType.UNDERLINE}
146-
{...props.link}
147-
>
148-
{props.link.content}
149-
</Link>
150-
</LinkContainerStyled>
126+
<ButtonSectionStyled styles={props.styles}>
127+
{buildExtraActionButton()}
128+
{buildActionButton()}
129+
{props.closeIcon?.onClick && (
130+
<CloseButtonSectionStyled styles={props.styles}>
131+
<ElementOrIcon customIconStyles={props.styles.closeIcon} {...props.closeIcon} />
132+
</CloseButtonSectionStyled>
133+
)}
134+
</ButtonSectionStyled>
135+
{props.link?.content && (
136+
<LinkContainerStyled styles={props.styles}>
137+
<Link
138+
dataTestId={`${props.dataTestId}Link`}
139+
decoration={TextDecorationType.UNDERLINE}
140+
{...props.link}
141+
>
142+
{props.link.content}
143+
</Link>
144+
</LinkContainerStyled>
145+
)}
146+
</MessageHeaderStyled>
147+
</MessageStyled>
151148
)}
152-
</MessageStyled>
153-
) : null;
149+
</>
150+
);
154151
};
155152

156153
export const MessageStandAlone = React.forwardRef(MessageStandAloneComponent);

src/components/message/stories/message.stories.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Meta, StoryObj } from '@storybook/react';
22
import React from 'react';
33

4+
import { ICONS } from '@/assets';
45
import { ReplaceContent } from '@/components/storybook/replaceContent/replaceContent';
56
import { STYLES_NAME } from '@/constants';
67
import { themesObject, variantsObject } from '@/designSystem/themesObject';
@@ -43,7 +44,19 @@ export const Message: Story = {
4344
),
4445
},
4546
title: { content: 'Title' },
46-
link: { content: 'Link', variant: 'SECONDARY', url: '#' },
47+
infoIcon: { icon: ICONS.ICON_GHOST, altText: 'info' },
48+
closeIcon: { icon: ICONS.ICON_CLOSE, altText: 'close' },
49+
actionButton: {
50+
content: 'Action',
51+
variant: Object.values(variantsObject[themeSelected].ButtonVariantType || {})[0] as string,
52+
size: Object.values(variantsObject[themeSelected].ButtonSizeType || {})[2] as string,
53+
},
54+
tag: {
55+
variant: Object.values(variantsObject[themeSelected].TagVariantType || {})[0] as string,
56+
option: Object.values(variantsObject[themeSelected].TagOptionType || {})[0] as string,
57+
status: Object.values(variantsObject[themeSelected].TagStatusType || {})[0] as string,
58+
content: 'Tag content',
59+
},
4760
themeArgs: themesObject[themeSelected][STYLES_NAME.MESSAGE],
4861
},
4962
};

src/components/message/types/message.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReactNode } from 'react';
1+
import * as React from 'react';
22

33
import { IButton } from '@/components/button';
44
import { IElementOrIcon } from '@/components/elementOrIcon';
@@ -20,7 +20,7 @@ export type MessageExtraActionButtonType = Omit<IButton, 'children'> & {
2020
};
2121

2222
export type MessageContentType = Omit<IText<string>, 'children'> & {
23-
content: string | ReactNode;
23+
content: React.ReactNode;
2424
};
2525

2626
export type MessageTitleType = Omit<IText<string>, 'children'> & {

src/components/message/types/messageTheme.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type MessagePropsThemeType = {
1111
infoIcon?: IconTypes;
1212
closeIcon?: IconTypes;
1313
closeIconContainer?: CommonStyleType;
14+
buttonSectionContainer?: CommonStyleType;
1415
actionButtonContainer?: CommonStyleType;
1516
actionButton?: {
1617
size?: string;

src/designSystem/kubit/components/message/styles.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ import { MessageVariantType } from './variants';
88
const commonProps = {
99
container: {
1010
display: 'flex',
11-
flex_direction: 'column',
11+
flex_direction: 'row',
1212
align_items: 'flex-start',
13-
justify_content: 'space-between',
13+
justify_content: 'flex-start',
1414
position: 'relative',
1515
border_style: 'solid',
1616
border_radius: RADIUS.radius_00,
1717
border_width: BORDERS.border_50,
1818
padding: SPACINGS.spacing_300,
19+
gap: SPACINGS.spacing_150,
1920
},
2021
headerContainer: {
2122
display: 'flex',
22-
flex_direction: 'row',
23+
flex_direction: 'column',
2324
align_items: 'flex-start',
2425
gap: SPACINGS.spacing_300,
2526
margin_right: SPACINGS.spacing_450,
@@ -56,10 +57,16 @@ const commonProps = {
5657
},
5758
closeIconContainer: {
5859
gap: SPACINGS.spacing_150,
59-
margin_left: SPACINGS.spacing_300,
60+
right: SPACINGS.spacing_300,
6061
position: 'absolute',
6162
top: SPACINGS.spacing_300,
6263
},
64+
buttonSectionContainer: {
65+
display: 'flex',
66+
flex_direction: 'column',
67+
width: '100%',
68+
align_items: 'flex-start',
69+
},
6370
};
6471

6572
export const MESSAGE_STYLES: MessageStylesType<MessageVariantType> = {

src/designSystem/kubitWireframe/commons/components/message/styles.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ const commonProps = COLORS => {
1010
return {
1111
container: {
1212
display: 'flex',
13-
flex_direction: 'column',
13+
flex_direction: 'row',
1414
align_items: 'flex-start',
15-
justify_content: 'space-between',
15+
justify_content: 'flex-start',
16+
gap: SPACINGS.spacing_150,
1617
position: 'relative',
1718
padding: SPACINGS.spacing_300,
1819
border_width: BORDERS.border_50,
@@ -24,9 +25,10 @@ const commonProps = COLORS => {
2425
},
2526
headerContainer: {
2627
display: 'flex',
27-
flex_direction: 'row',
28+
flex_direction: 'column',
2829
align_items: 'flex-start',
2930
gap: SPACINGS.spacing_300,
31+
margin_right: SPACINGS.spacing_450,
3032
},
3133
headerContainerLargeMessage: {
3234
[DeviceBreakpointsType.MOBILE]: {
@@ -58,10 +60,16 @@ const commonProps = COLORS => {
5860
},
5961
closeIconContainer: {
6062
gap: SPACINGS.spacing_150,
61-
margin_left: SPACINGS.spacing_300,
63+
right: SPACINGS.spacing_300,
6264
position: 'absolute',
6365
top: SPACINGS.spacing_300,
6466
},
67+
buttonSectionContainer: {
68+
display: 'flex',
69+
flex_direction: 'column',
70+
width: '100%',
71+
align_items: 'flex-start',
72+
},
6573
};
6674
};
6775

0 commit comments

Comments
 (0)