Skip to content

Commit 24e30e0

Browse files
committed
chore: Widgetize flashbar
1 parent fbe6454 commit 24e30e0

9 files changed

+54
-24
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165
{
166166
"path": "lib/components/internal/widget-exports.js",
167167
"brotli": false,
168-
"limit": "750 kB"
168+
"limit": "820 kB"
169169
}
170170
],
171171
"browserslist": [

src/flashbar/collapsible-flashbar.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ import { Transition } from '../internal/components/transition';
1515
import { getVisualContextClassname } from '../internal/components/visual-context';
1616
import customCssProps from '../internal/generated/custom-css-properties';
1717
import { useEffectOnUpdate } from '../internal/hooks/use-effect-on-update';
18+
import { useMergeRefs } from '../internal/hooks/use-merge-refs';
1819
import { useUniqueId } from '../internal/hooks/use-unique-id';
20+
import { useVisualRefresh } from '../internal/hooks/use-visual-mode';
1921
import { scrollElementIntoView } from '../internal/utils/scrollable-containers';
2022
import { throttle } from '../internal/utils/throttle';
2123
import { GeneratedAnalyticsMetadataFlashbarExpand } from './analytics-metadata/interfaces';
2224
import { getComponentsAnalyticsMetadata, getItemAnalyticsMetadata } from './analytics-metadata/utils';
2325
import { useFlashbar } from './common';
2426
import { Flash, focusFlashById } from './flash';
25-
import { FlashbarProps } from './interfaces';
27+
import { FlashbarProps, InternalFlashbarProps } from './interfaces';
2628
import { counterTypes, getFlashTypeCount, getItemColor, getVisibleCollapsedItems, StackableItem } from './utils';
2729

2830
import styles from './styles.css.js';
@@ -33,10 +35,11 @@ const maxNonCollapsibleItems = 1;
3335

3436
const resizeListenerThrottleDelay = 100;
3537

36-
export default function CollapsibleFlashbar({ items, ...restProps }: FlashbarProps) {
38+
export default function CollapsibleFlashbar({ __internalRootRef, items, ...restProps }: InternalFlashbarProps) {
3739
const [enteringItems, setEnteringItems] = useState<ReadonlyArray<FlashbarProps.MessageDefinition>>([]);
3840
const [exitingItems, setExitingItems] = useState<ReadonlyArray<FlashbarProps.MessageDefinition>>([]);
3941
const [isFlashbarStackExpanded, setIsFlashbarStackExpanded] = useState(false);
42+
const isVisualRefresh = useVisualRefresh();
4043

4144
const getElementsToAnimate = useCallback(() => {
4245
const flashElements = isFlashbarStackExpanded ? expandedItemRefs.current : collapsedItemRefs.current;
@@ -48,7 +51,7 @@ export default function CollapsibleFlashbar({ items, ...restProps }: FlashbarPro
4851
setInitialAnimationState(rects);
4952
}, [getElementsToAnimate]);
5053

51-
const { baseProps, breakpoint, isReducedMotion, isVisualRefresh, mergedRef, ref } = useFlashbar({
54+
const { baseProps, breakpoint, isReducedMotion, mergedRef, ref } = useFlashbar({
5255
items,
5356
...restProps,
5457
onItemsAdded: newItems => {
@@ -308,7 +311,7 @@ export default function CollapsibleFlashbar({ items, ...restProps }: FlashbarPro
308311
isFlashbarStackExpanded && styles.expanded,
309312
isVisualRefresh && styles['visual-refresh']
310313
)}
311-
ref={mergedRef}
314+
ref={useMergeRefs(mergedRef, __internalRootRef)}
312315
{...getAnalyticsMetadataAttribute(getComponentsAnalyticsMetadata(items.length, true, isFlashbarStackExpanded))}
313316
>
314317
{isFlashbarStackExpanded && renderList()}

src/flashbar/common.tsx

+1-8
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import { useReducedMotion, warnOnce } from '@cloudscape-design/component-toolkit
66

77
import { getBaseProps } from '../internal/base-component';
88
import { useContainerBreakpoints } from '../internal/hooks/container-queries';
9-
import useBaseComponent from '../internal/hooks/use-base-component';
109
import { useMergeRefs } from '../internal/hooks/use-merge-refs';
11-
import { useVisualRefresh } from '../internal/hooks/use-visual-mode';
1210
import { isDevelopment } from '../internal/is-development';
1311
import { focusFlashById } from './flash';
1412
import { FlashbarProps } from './interfaces';
@@ -25,16 +23,12 @@ export function useFlashbar({
2523
onItemsRemoved?: (items: FlashbarProps.MessageDefinition[]) => void;
2624
onItemsChanged?: (options?: { allItemsHaveId?: boolean; isReducedMotion?: boolean }) => void;
2725
}) {
28-
const { __internalRootRef } = useBaseComponent('Flashbar', {
29-
props: { stackItems: restProps.stackItems },
30-
});
3126
const allItemsHaveId = useMemo(() => items.every(item => 'id' in item), [items]);
3227
const baseProps = getBaseProps(restProps);
3328
const ref = useRef<HTMLDivElement | null>(null);
3429
const [breakpoint, breakpointRef] = useContainerBreakpoints(['xs']);
35-
const mergedRef = useMergeRefs(ref, breakpointRef, __internalRootRef);
30+
const mergedRef = useMergeRefs(ref, breakpointRef);
3631
const isReducedMotion = useReducedMotion(ref);
37-
const isVisualRefresh = useVisualRefresh();
3832
const [previousItems, setPreviousItems] = useState<ReadonlyArray<FlashbarProps.MessageDefinition>>(items);
3933
const [nextFocusId, setNextFocusId] = useState<string | null>(null);
4034

@@ -76,7 +70,6 @@ export function useFlashbar({
7670
baseProps,
7771
breakpoint,
7872
isReducedMotion,
79-
isVisualRefresh,
8073
mergedRef,
8174
ref,
8275
};

src/flashbar/implementation.tsx

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import React from 'react';
4+
5+
import { createWidgetizedComponent } from '../internal/widgets';
6+
import CollapsibleFlashbar from './collapsible-flashbar';
7+
import { InternalFlashbarProps } from './interfaces';
8+
import NonCollapsibleFlashbar from './non-collapsible-flashbar';
9+
10+
export function FlashbarImplementation(props: InternalFlashbarProps) {
11+
if (props.stackItems) {
12+
return <CollapsibleFlashbar {...props} />;
13+
} else {
14+
return <NonCollapsibleFlashbar {...props} />;
15+
}
16+
}
17+
18+
export const createWidgetizedFlashbar = createWidgetizedComponent(FlashbarImplementation);

src/flashbar/index.tsx

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22
// SPDX-License-Identifier: Apache-2.0
33
import React from 'react';
44

5+
import useBaseComponent from '../internal/hooks/use-base-component';
56
import { applyDisplayName } from '../internal/utils/apply-display-name';
6-
import CollapsibleFlashbar from './collapsible-flashbar';
77
import { FlashbarProps } from './interfaces';
8-
import NonCollapsibleFlashbar from './non-collapsible-flashbar';
8+
import { InternalFlashbar } from './internal';
99

1010
export { FlashbarProps };
1111

1212
export default function Flashbar(props: FlashbarProps) {
13-
if (props.stackItems) {
14-
return <CollapsibleFlashbar {...props} />;
15-
} else {
16-
return <NonCollapsibleFlashbar {...props} />;
17-
}
13+
const { __internalRootRef } = useBaseComponent('Flashbar', {
14+
props: { stackItems: props.stackItems },
15+
});
16+
return <InternalFlashbar __internalRootRef={__internalRootRef} {...props} />;
1817
}
1918

2019
applyDisplayName(Flashbar, 'Flashbar');

src/flashbar/interfaces.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React from 'react';
44

55
import { ButtonProps } from '../button/interfaces';
66
import { BaseComponentProps } from '../internal/base-component';
7+
import { InternalBaseComponentProps } from '../internal/hooks/use-base-component';
78

89
export namespace FlashbarProps {
910
export interface MessageDefinition {
@@ -99,3 +100,5 @@ export interface FlashbarProps extends BaseComponentProps {
99100
*/
100101
i18nStrings?: FlashbarProps.I18nStrings;
101102
}
103+
104+
export type InternalFlashbarProps = FlashbarProps & InternalBaseComponentProps;

src/flashbar/internal.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import { createWidgetizedFlashbar } from './implementation';
4+
5+
export const InternalFlashbar = createWidgetizedFlashbar();

src/flashbar/non-collapsible-flashbar.tsx

+12-4
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,24 @@ import { getAnalyticsMetadataAttribute } from '@cloudscape-design/component-tool
88

99
import { useInternalI18n } from '../i18n/context';
1010
import { Transition } from '../internal/components/transition';
11+
import { useMergeRefs } from '../internal/hooks/use-merge-refs';
12+
import { useVisualRefresh } from '../internal/hooks/use-visual-mode';
1113
import { getComponentsAnalyticsMetadata, getItemAnalyticsMetadata } from './analytics-metadata/utils';
1214
import { useFlashbar } from './common';
1315
import { TIMEOUT_FOR_ENTERING_ANIMATION } from './constant';
1416
import { Flash } from './flash';
15-
import { FlashbarProps } from './interfaces';
17+
import { FlashbarProps, InternalFlashbarProps } from './interfaces';
1618

1719
import styles from './styles.css.js';
1820

19-
export default function NonCollapsibleFlashbar({ items, i18nStrings, ...restProps }: FlashbarProps) {
20-
const { allItemsHaveId, baseProps, breakpoint, isReducedMotion, isVisualRefresh, mergedRef } = useFlashbar({
21+
export default function NonCollapsibleFlashbar({
22+
__internalRootRef,
23+
items,
24+
i18nStrings,
25+
...restProps
26+
}: InternalFlashbarProps) {
27+
const isVisualRefresh = useVisualRefresh();
28+
const { allItemsHaveId, baseProps, breakpoint, isReducedMotion, mergedRef } = useFlashbar({
2129
items,
2230
...restProps,
2331
});
@@ -125,7 +133,7 @@ export default function NonCollapsibleFlashbar({ items, i18nStrings, ...restProp
125133
<div
126134
{...baseProps}
127135
className={clsx(baseProps.className, styles.flashbar, styles[`breakpoint-${breakpoint}`])}
128-
ref={mergedRef}
136+
ref={useMergeRefs(mergedRef, __internalRootRef)}
129137
>
130138
{renderFlatItemsWithTransitions()}
131139
{renderFlatItemsWithoutTransitions()}

src/internal/widget-exports.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ export { AppLayoutToolbarImplementation as AppLayoutToolbar } from '../app-layou
1515
export { SplitPanelImplementation as SplitPanel } from '../split-panel/implementation';
1616
export { BreadcrumbGroupImplementation as BreadcrumbGroup } from '../breadcrumb-group/implementation';
1717
export { DrawerImplementation as Drawer } from '../drawer/implementation';
18+
export { FlashbarImplementation as Flashbar } from '../flashbar/implementation';
1819
export { SideNavigationImplementation as SideNavigation } from '../side-navigation/implementation';
1920
export { HelpPanelImplementation as HelpPanel } from '../help-panel/implementation';

0 commit comments

Comments
 (0)