Skip to content

Commit b0b743c

Browse files
committed
fix: styled AlertRoot, fix alert.mdx
1 parent b4b3c10 commit b0b743c

File tree

5 files changed

+79
-49
lines changed

5 files changed

+79
-49
lines changed

apps/portal/src/content/docs/components/alert.mdx

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Alert component
44
beta: true
55
---
66

7-
The `Alert` component provides a flexible UI element for displaying alert messages. It is composed of several subcomponents such as `AlertRoot`, `AlertTitle`, `AlertDescription`, and `AlertLink` to offer a structured and customizable interface.
7+
The `Alert` component provides a flexible UI element for displaying alert messages. It is composed of several subcomponents such as `Alert.Root`, `Alert.Title`, `Alert.Description`, and `Alert.Link` to offer a structured and customizable interface.
88

99
import { DocsPage } from "@/components/docs-page";
1010

@@ -16,7 +16,7 @@ import { DocsPage } from "@/components/docs-page";
1616
<Alert.Description>This is an alert description.</Alert.Description>
1717
</Alert.Root>
1818
<Alert.Root theme="danger" dismissible>
19-
<Alert.Description >We couldn’t complete your request because it violates an Open Policy Agent (OPA) rule set by your organization. Please review the applicable policies or contact your administrator for clarification, then try creating the feature flag again.</Alert.Description>
19+
<Alert.Description>We couldn’t complete your request because it violates an Open Policy Agent (OPA) rule set by your organization. Please review the applicable policies or contact your administrator for clarification, then try creating the feature flag again.</Alert.Description>
2020
</Alert.Root>
2121
<Alert.Root theme="warning" dismissible>
2222
<Alert.Title>Warning Alert</Alert.Title>
@@ -25,34 +25,41 @@ import { DocsPage } from "@/components/docs-page";
2525
<Alert.Root dismissible>
2626
<Alert.Title>Dismissible Alert</Alert.Title>
2727
</Alert.Root>
28-
<Alert.Root >
28+
<Alert.Root>
2929
<Alert.Title>Link Alert</Alert.Title>
3030
<Alert.Link to="/abc">Learn more</Alert.Link>
3131
</Alert.Root>
32-
<Alert.Root >
32+
<Alert.Root>
3333
<Alert.Title theme="warning">Link Alert as child</Alert.Title>
3434
<Alert.Link to="/abc" asChild><Button>Learn more</Button></Alert.Link>
3535
</Alert.Root>
3636
<Alert.Root theme="danger">
3737
<Alert.Title>Crowded Alert not expandable</Alert.Title>
38-
<Alert.Description >LoremLorem ipsum dolor sit amet consectetur adipisicing elit. Unde molestiae hic, iste nostrum, quia eaque amet autem optio rem earum nihil rerum illo! Possimus</Alert.Description>
38+
<Alert.Description>LoremLorem ipsum dolor sit amet consectetur adipisicing elit. Unde molestiae hic, iste nostrum, quia eaque amet autem optio rem earum nihil rerum illo! Possimus</Alert.Description>
3939
</Alert.Root>
4040
<Alert.Root expandable>
4141
<Alert.Title>Crowded Alert</Alert.Title>
42-
<Alert.Description >Lorem, ipsum dolor sit amet consectetur adipisicing elit. Placeat magnam suscipit fugit officiis dignissimos iusto similique deleniti. Quod odio amet quisquam, blanditiis necessitatibus aliquam cumque beatae debitis sunt nemo perspiciatis a saepe enim voluptas? Incidunt vel officiis quam, corrupti ea aliquid nobis! Soluta accusantium ex, alias quod velit explicabo modi exercitationem eaque incidunt fuga, nihil fugiat voluptatum doloribus repudiandae atque.</Alert.Description>
42+
<Alert.Description>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Placeat magnam suscipit fugit officiis dignissimos iusto similique deleniti. Quod odio amet quisquam, blanditiis necessitatibus aliquam cumque beatae debitis sunt nemo perspiciatis a saepe enim voluptas? Incidunt vel officiis quam, corrupti ea aliquid nobis! Soluta accusantium ex, alias quod velit explicabo modi exercitationem eaque incidunt fuga, nihil fugiat voluptatum doloribus repudiandae atque.</Alert.Description>
4343
<Alert.Link to="/abc">Learn more</Alert.Link>
4444
</Alert.Root>
4545
<Alert.Root theme="danger" expandable>
4646
<Alert.Title>Crowded Alert</Alert.Title>
47-
<Alert.Description >Lorem, ipsum dolor sit amet consectetur adipisicing elit. Placeat magnam suscipit fugit officiis dignissimos iusto similique deleniti. Quod odio amet quisquam, blanditiis necessitatibus aliquam cumque beatae debitis sunt nemo perspiciatis a saepe enim voluptas? Incidunt vel officiis quam, corrupti ea aliquid nobis! Soluta accusantium ex, alias quod velit explicabo modi exercitationem eaque incidunt fuga, nihil fugiat voluptatum doloribus repudiandae atque.</Alert.Description>
47+
<Alert.Description>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Placeat magnam suscipit fugit officiis dignissimos iusto similique deleniti. Quod odio amet quisquam, blanditiis necessitatibus aliquam cumque beatae debitis sunt nemo perspiciatis a saepe enim voluptas? Incidunt vel officiis quam, corrupti ea aliquid nobis! Soluta accusantium ex, alias quod velit explicabo modi exercitationem eaque incidunt fuga, nihil fugiat voluptatum doloribus repudiandae atque.</Alert.Description>
4848
</Alert.Root>
4949
<Alert.Root theme="warning" expandable>
5050
<Alert.Title>Crowded Alert</Alert.Title>
5151
<Alert.Description>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Placeat magnam suscipit fugit officiis dignissimos iusto similique deleniti. Quod odio amet quisquam, blanditiis necessitatibus aliquam cumque beatae debitis sunt nemo perspiciatis a saepe enim voluptas? Incidunt vel officiis quam, corrupti ea aliquid nobis! Soluta accusantium ex, alias quod velit explicabo modi exercitationem eaque incidunt fuga, nihil fugiat voluptatum doloribus repudiandae atque.</Alert.Description>
5252
</Alert.Root>
5353
<Alert.Root expandable>
5454
<Alert.Title>Expandable but not crowded</Alert.Title>
55-
<Alert.Description >LoremLorem ipsum dolor sit amet consectetur adipisicing elit. Unde molestiae hic</Alert.Description>
55+
<Alert.Description>Lorem, ipsum dolor sit amet consectetur adipisicing elit.</Alert.Description>
56+
</Alert.Root>
57+
<Alert.Root theme="danger" expandable>
58+
<Alert.Title>Crowded title Crowded title Crowded title Crowded title Crowded title Crowded title Crowded title Crowded title Crowded title Crowded title Crowded title</Alert.Title>
59+
<Alert.Description>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Placeat magnam suscipit fugit officiis dignissimos iusto similique deleniti. Quod odio amet quisquam, blanditiis necessitatibus aliquam cumque beatae debitis sunt nemo perspiciatis a saepe enim voluptas? Incidunt vel officiis quam, corrupti ea aliquid nobis! Soluta accusantium ex, alias quod velit explicabo modi exercitationem eaque incidunt fuga, nihil fugiat voluptatum doloribus repudiandae atque.</Alert.Description>
60+
</Alert.Root>
61+
<Alert.Root theme="warning" expandable>
62+
<Alert.Description>Without Title Lorem, ipsum dolor sit amet consectetur adipisicing elit. Placeat magnam suscipit fugit officiis dignissimos iusto similique deleniti. Quod odio amet quisquam, blanditiis necessitatibus aliquam cumque beatae debitis sunt nemo perspiciatis a saepe enim voluptas? Incidunt vel officiis quam, corrupti ea aliquid nobis! Soluta accusantium ex, alias quod velit explicabo modi exercitationem eaque incidunt fuga, nihil fugiat voluptatum doloribus repudiandae atque.</Alert.Description>
5663
</Alert.Root>
5764
</div>`}
5865
/>
@@ -62,22 +69,19 @@ import { DocsPage } from "@/components/docs-page";
6269
```typescript jsx
6370
import { Alert } from '@harnessio/ui/components'
6471

72+
//...
73+
6574
return (
6675
<Alert.Root
6776
theme="info"
6877
expandable
6978
dismissible
70-
onDismiss={handleDismiss}
7179
>
7280
<Alert.Title>My Alert</Alert.Title>
7381

7482
<Alert.Description>This is the description of the alert</Alert.Description>
7583

76-
<Alert.Link
77-
to="someplace"
78-
external
79-
asChild
80-
>
84+
<Alert.Link to="/someplace">
8185
Learn more
8286
</Alert.Link>
8387
</Alert.Root>
@@ -92,12 +96,13 @@ All parts of the `Alert` component can be imported and composed as required.
9296
<Alert.Root>
9397
<Alert.Title />
9498
<Alert.Description />
99+
<Alert.Link />
95100
</Alert.Root>
96101
```
97102

98103
## API Reference
99104

100-
### Root
105+
### `Root`
101106

102107
The `Root` component serves as the main container for all alert elements.
103108

@@ -106,54 +111,48 @@ The `Root` component serves as the main container for all alert elements.
106111
{
107112
name: "children",
108113
description:
109-
"You can pass in your `Alert.Title` and `Alert.Description` as children",
114+
"You can pass in your `Alert.Title`, `Alert.Description` and `Alert.Link` as children",
110115
required: true,
111116
value: "ReactNode",
112-
defaultValue: "",
113117
},
114118
{
115119
name: "className",
116120
description: "Additional class names to apply to the alert container",
117121
required: false,
118122
value: "string",
119-
defaultValue: "",
120123
},
121124
{
122125
name: "theme",
123126
description:
124127
"Theme of the alert. Can be one of 'info', 'warning', or 'danger'.",
125128
required: false,
126129
value: "'info' | 'warning' | 'danger'",
127-
defaultValue: "'info'",
128130
},
129131
{
130132
name: "dismissible",
131133
description:
132134
"If true, the alert can be dismissed by the user. Default is false.",
133135
required: false,
134136
value: "boolean",
135-
defaultValue: "false",
136137
},
137138
{
138139
name: "expandable",
139140
description:
140141
"If true, the alert can be expanded to show more content. Default is false.",
141142
required: false,
142143
value: "boolean",
143-
defaultValue: "false",
144144
},
145145
{
146146
name: "onDismiss",
147147
description:
148148
"Callback function that is called when the alert is dismissed.",
149149
required: false,
150150
value: "() => void",
151-
defaultValue: "",
152151
},
153152
]}
154153
/>
155154

156-
### Title
155+
### `Title`
157156

158157
The `Title` component displays the title of the alert.
159158

@@ -178,7 +177,7 @@ The `Title` component displays the title of the alert.
178177
]}
179178
/>
180179

181-
### Description
180+
### `Description`
182181

183182
The `Description` component displays the description of the alert.
184183

@@ -203,7 +202,7 @@ The `Description` component displays the description of the alert.
203202
]}
204203
/>
205204

206-
### Link
205+
### `Link`
207206

208207
The `Link` component is used to create a link within the alert.
209208

packages/ui/config/vitest-setup.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import { cleanup } from '@testing-library/react'
22

33
import '@testing-library/jest-dom'
44

5+
beforeAll(() => {
6+
global.ResizeObserver = class {
7+
observe() {}
8+
unobserve() {}
9+
disconnect() {}
10+
}
11+
})
12+
513
afterEach(() => {
614
cleanup()
715
})

packages/ui/src/components/alert/Alert.test.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@ import { render, screen } from '@testing-library/react'
22

33
import { Alert } from './'
44

5-
beforeAll(() => {
6-
global.ResizeObserver = class {
7-
observe() {}
8-
unobserve() {}
9-
disconnect() {}
10-
}
11-
})
12-
135
describe('Alert', () => {
146
test('it should display the alert title and description', async () => {
157
const title = 'TEST TITLE'

packages/ui/src/components/alert/AlertRoot.tsx

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
import { forwardRef, PropsWithChildren, useLayoutEffect, useRef, useState } from 'react'
1+
import {
2+
Children,
3+
forwardRef,
4+
isValidElement,
5+
PropsWithChildren,
6+
ReactNode,
7+
useLayoutEffect,
8+
useMemo,
9+
useRef,
10+
useState
11+
} from 'react'
212

313
import { Button } from '@components/button'
414
import { Icon, IconNameMap } from '@components/icon'
515
import { useResizeObserver } from '@hooks/use-resize-observer'
616
import { cn } from '@utils/cn'
717
import { cva, type VariantProps } from 'class-variance-authority'
818

19+
import { AlertTitle } from './AlertTitle'
20+
921
const alertVariants = cva('cn-alert', {
1022
variants: {
1123
theme: {
@@ -25,7 +37,8 @@ const iconMap: Record<NonNullable<VariantProps<typeof alertVariants>['theme']>,
2537
warning: 'warning-triangle-outline'
2638
}
2739

28-
const MAX_HEIGHT = 60
40+
const MAX_HEIGHT_WITH_TITLE = 70
41+
const MAX_HEIGHT_WITHOUT_TITLE = 60
2942

3043
export interface AlertRootProps extends PropsWithChildren<VariantProps<typeof alertVariants>> {
3144
className?: string
@@ -52,6 +65,17 @@ export const AlertRoot = forwardRef<HTMLDivElement, AlertRootProps>(
5265

5366
const shouldShowButton = expandable && isOverflowing
5467

68+
const hasTitle = Children.toArray(children).some(
69+
(child: ReactNode) => isValidElement(child) && child?.type === AlertTitle
70+
)
71+
72+
const minHContentClassName = useMemo(
73+
() => (hasTitle ? 'cn-alert-min-h-content' : 'cn-alert-min-h-content-no-title'),
74+
[hasTitle]
75+
)
76+
77+
const MAX_HEIGHT = useMemo(() => (hasTitle ? MAX_HEIGHT_WITH_TITLE : MAX_HEIGHT_WITHOUT_TITLE), [hasTitle])
78+
5579
useResizeObserver(
5680
contentRef,
5781
el => {
@@ -81,6 +105,7 @@ export const AlertRoot = forwardRef<HTMLDivElement, AlertRootProps>(
81105
variant="transparent"
82106
size="sm"
83107
iconOnly
108+
aria-label="Close alert"
84109
>
85110
<Icon className="cn-alert-close-button-icon" name="close" skipSize />
86111
</Button>
@@ -92,13 +117,13 @@ export const AlertRoot = forwardRef<HTMLDivElement, AlertRootProps>(
92117
<div
93118
className={cn('cn-alert-content-box', {
94119
'cn-alert-content-expanded': isExpanded,
95-
'cn-alert-overflow': shouldShowButton
120+
'cn-alert-content-overflow': shouldShowButton
96121
})}
97122
>
98123
<div
99124
ref={contentRef}
100125
className={cn('cn-alert-content', {
101-
'cn-alert-min-h-content': shouldShowButton
126+
[minHContentClassName]: shouldShowButton
102127
})}
103128
role="region"
104129
aria-label="Alert content"
@@ -125,7 +150,9 @@ export const AlertRoot = forwardRef<HTMLDivElement, AlertRootProps>(
125150
>
126151
{isExpanded ? 'Show less' : 'Show more'}
127152
<Icon
128-
className={cn('cn-alert-expand-button-icon', { 'cn-alert-rotate-180': isExpanded })}
153+
className={cn('cn-alert-expand-button-icon', {
154+
'cn-alert-expand-button-icon-rotate-180': isExpanded
155+
})}
129156
name="chevron-down"
130157
skipSize
131158
/>

packages/ui/tailwind-utils-config/components/alert.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,22 @@ export default {
5757
'&-content': {
5858
background: 'inherit',
5959
gap: 'var(--cn-spacing-1)',
60-
'@apply grid relative': ''
60+
'@apply grid relative': '',
61+
62+
'&-overflow': {
63+
overflow: 'hidden'
64+
}
6165
},
6266

6367
'&-content-expanded': {
6468
'@apply grid-rows-[1fr]': ''
6569
},
6670

6771
'&-min-h-content': {
72+
'@apply min-h-[70px]': ''
73+
},
74+
75+
'&-min-h-content-no-title': {
6876
'@apply min-h-[60px]': ''
6977
},
7078

@@ -89,15 +97,6 @@ export default {
8997
}
9098
},
9199

92-
'&-rotate-180': {
93-
transform: 'rotate(180deg)',
94-
transition: 'transform 0.2s ease-out'
95-
},
96-
97-
'&-overflow': {
98-
overflow: 'hidden'
99-
},
100-
101100
'&-title': {
102101
'@apply font-body-strong': ''
103102
},
@@ -123,7 +122,12 @@ export default {
123122
'&-expand-button': {
124123
'&-icon': {
125124
width: 'var(--cn-icon-size-default)',
126-
height: 'var(--cn-icon-size-default)'
125+
height: 'var(--cn-icon-size-default)',
126+
127+
'&-rotate-180': {
128+
transform: 'rotate(180deg)',
129+
transition: 'transform 0.2s ease-out'
130+
}
127131
}
128132
},
129133

0 commit comments

Comments
 (0)