generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathflash.tsx
More file actions
232 lines (214 loc) · 8.34 KB
/
flash.tsx
File metadata and controls
232 lines (214 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { useRef } from 'react';
import clsx from 'clsx';
import { useComponentMetadata, warnOnce } from '@cloudscape-design/component-toolkit/internal';
import { getAnalyticsMetadataAttribute } from '@cloudscape-design/component-toolkit/internal/analytics-metadata';
import { ActionsWrapper } from '../alert/actions-wrapper';
import { InternalButton } from '../button/internal';
import InternalIcon from '../icon/internal';
import { DATA_ATTR_ANALYTICS_FLASHBAR } from '../internal/analytics/selectors';
import { BasePropsWithAnalyticsMetadata, getAnalyticsMetadataProps } from '../internal/base-component';
import { getVisualContextClassname } from '../internal/components/visual-context';
import { PACKAGE_VERSION } from '../internal/environment';
import { useMergeRefs } from '../internal/hooks/use-merge-refs';
import { isDevelopment } from '../internal/is-development';
import { awsuiPluginsInternal } from '../internal/plugins/api';
import { createUseDiscoveredAction, createUseDiscoveredContent } from '../internal/plugins/helpers';
import { throttle } from '../internal/utils/throttle';
import InternalLiveRegion from '../live-region/internal';
import InternalSpinner from '../spinner/internal';
import { GeneratedAnalyticsMetadataFlashbarDismiss } from './analytics-metadata/interfaces';
import { FlashbarProps } from './interfaces';
import { FOCUS_THROTTLE_DELAY } from './utils';
import analyticsSelectors from './analytics-metadata/styles.css.js';
import styles from './styles.css.js';
const ICON_TYPES = {
success: 'status-positive',
warning: 'status-warning',
info: 'status-info',
error: 'status-negative',
'in-progress': 'status-in-progress',
} as const;
const useDiscoveredAction = createUseDiscoveredAction(awsuiPluginsInternal.flashbar.onActionRegistered);
const useDiscoveredContent = createUseDiscoveredContent('flash', awsuiPluginsInternal.flashContent);
function dismissButton(
dismissLabel: FlashbarProps.MessageDefinition['dismissLabel'],
onDismiss: FlashbarProps.MessageDefinition['onDismiss']
) {
return (
<div
className={styles['dismiss-button-wrapper']}
{...getAnalyticsMetadataAttribute({
action: 'dismiss',
} as Partial<GeneratedAnalyticsMetadataFlashbarDismiss>)}
>
<InternalButton
onClick={onDismiss}
className={styles['dismiss-button']}
variant="flashbar-icon"
iconName="close"
formAction="none"
ariaLabel={dismissLabel}
/>
</div>
);
}
export const focusFlashById = throttle(
(element: HTMLElement | null, itemId: string) => {
const selector = `[data-itemid="${CSS.escape(itemId)}"] .${styles['flash-focus-container']}`;
element?.querySelector<HTMLElement>(selector)?.focus();
},
FOCUS_THROTTLE_DELAY,
{ trailing: false }
);
interface FlashProps extends FlashbarProps.MessageDefinition {
className: string;
transitionState?: string;
i18nStrings?: FlashbarProps.I18nStrings;
}
export const Flash = React.forwardRef(
(
{
id,
header,
content,
dismissible,
dismissLabel,
loading,
action,
buttonText,
onButtonClick,
onDismiss,
className,
transitionState,
ariaRole,
i18nStrings,
type = 'info',
...props
}: FlashProps,
ref: React.Ref<HTMLDivElement>
) => {
if (isDevelopment) {
if (buttonText && !onButtonClick) {
warnOnce(
'Flashbar',
`You provided a \`buttonText\` prop without an \`onButtonClick\` handler. This will render a non-interactive action button.`
);
}
if (dismissible && !onDismiss) {
warnOnce(
'Flashbar',
`You have set the \`dismissible\` prop without an \`onDismiss\` handler. This will render a non-interactive dismiss button.`
);
}
}
const analyticsMetadata = getAnalyticsMetadataProps(props as BasePropsWithAnalyticsMetadata);
const elementRef = useComponentMetadata('Flash', PACKAGE_VERSION, { ...analyticsMetadata });
const mergedRef = useMergeRefs(ref, elementRef);
const headerRefObject = useRef<HTMLDivElement>(null);
const contentRefObject = useRef<HTMLDivElement>(null);
const { discoveredActions, headerRef: headerRefAction, contentRef: contentRefAction } = useDiscoveredAction(type);
const {
initialHidden,
headerReplacementType,
contentReplacementType,
headerRef: headerRefContent,
contentRef: contentRefContent,
replacementHeaderRef,
replacementContentRef,
} = useDiscoveredContent({ type, header, children: content });
const headerRef = useMergeRefs(headerRefAction, headerRefContent, headerRefObject);
const contentRef = useMergeRefs(contentRefAction, contentRefContent, contentRefObject);
const statusIconAriaLabel =
props.statusIconAriaLabel ||
i18nStrings?.[`${loading || type === 'in-progress' ? 'inProgress' : type}IconAriaLabel`];
const iconType = ICON_TYPES[type];
const icon = loading ? (
<span role="img" aria-label={statusIconAriaLabel}>
<InternalSpinner />
</span>
) : (
<InternalIcon name={iconType} ariaLabel={statusIconAriaLabel} />
);
const effectiveType = loading ? 'info' : type;
const analyticsAttributes = {
[DATA_ATTR_ANALYTICS_FLASHBAR]: effectiveType,
};
return (
// We're not using "polite" or "assertive" here, just turning default behavior off.
// eslint-disable-next-line @cloudscape-design/prefer-live-region
<div
ref={mergedRef}
role={ariaRole}
aria-live={ariaRole ? 'off' : undefined}
data-itemid={id}
className={clsx(
styles.flash,
styles[`flash-type-${effectiveType}`],
className,
transitionState && {
[styles.enter]: transitionState === 'enter',
[styles.entering]: transitionState === 'entering',
[styles.entered]: transitionState === 'entered',
[styles.exit]: transitionState === 'exit',
[styles.exiting]: transitionState === 'exiting',
[styles.exited]: transitionState === 'exited',
},
getVisualContextClassname(type === 'warning' && !loading ? 'flashbar-warning' : 'flashbar'),
initialHidden && styles['initial-hidden']
)}
{...analyticsAttributes}
>
<div className={styles['flash-body']}>
<div className={styles['flash-focus-container']} tabIndex={-1}>
<div className={clsx(styles['flash-icon'], styles['flash-text'])}>{icon}</div>
<div className={clsx(styles['flash-message'], styles['flash-text'])}>
<div
className={clsx(
styles['flash-header'],
headerReplacementType !== 'original' ? styles.hidden : analyticsSelectors['flash-header']
)}
ref={headerRef}
>
{header}
</div>
<div
className={clsx(styles['header-replacement'], headerReplacementType !== 'replaced' && styles.hidden)}
ref={replacementHeaderRef}
></div>
<div
className={clsx(
styles['flash-content'],
contentReplacementType !== 'original' ? styles.hidden : analyticsSelectors['flash-header']
)}
ref={contentRef}
>
{content}
</div>
<div
className={clsx(styles['content-replacement'], contentReplacementType !== 'replaced' && styles.hidden)}
ref={replacementContentRef}
></div>
</div>
</div>
<ActionsWrapper
className={styles['action-button-wrapper']}
testUtilClasses={{
actionSlot: styles['action-slot'],
actionButton: styles['action-button'],
}}
action={action}
discoveredActions={discoveredActions}
buttonText={buttonText}
onButtonClick={onButtonClick}
/>
</div>
{dismissible && dismissButton(dismissLabel, onDismiss)}
{ariaRole === 'status' && (
<InternalLiveRegion sources={[statusIconAriaLabel, headerRefObject, contentRefObject]} />
)}
</div>
);
}
);