|
| 1 | +// Copyright (c) 2025 CodeLibs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +'use client'; |
| 14 | + |
| 15 | +import { |
| 16 | + MessageBar, |
| 17 | + MessageBarBody, |
| 18 | + MessageBarTitle, |
| 19 | + MessageBarActions, |
| 20 | + Button, |
| 21 | + makeStyles, |
| 22 | + type MessageBarIntent, |
| 23 | +} from '@fluentui/react-components'; |
| 24 | +import { |
| 25 | + DismissRegular, |
| 26 | + CheckmarkCircleRegular, |
| 27 | + InfoRegular, |
| 28 | + WarningRegular, |
| 29 | + ErrorCircleRegular, |
| 30 | +} from '@fluentui/react-icons'; |
| 31 | + |
| 32 | +const useStyles = makeStyles({ |
| 33 | + actionButton: { |
| 34 | + minWidth: 'auto', |
| 35 | + }, |
| 36 | +}); |
| 37 | + |
| 38 | +export type BannerIntent = 'info' | 'warning' | 'success' | 'error'; |
| 39 | + |
| 40 | +interface BannerProps { |
| 41 | + intent: BannerIntent; |
| 42 | + title?: string; |
| 43 | + message: string; |
| 44 | + onAction?: () => void; |
| 45 | + actionLabel?: string; |
| 46 | + onDismiss?: () => void; |
| 47 | + className?: string; |
| 48 | +} |
| 49 | + |
| 50 | +const iconMap: Record<BannerIntent, React.ReactElement> = { |
| 51 | + success: <CheckmarkCircleRegular />, |
| 52 | + info: <InfoRegular />, |
| 53 | + warning: <WarningRegular />, |
| 54 | + error: <ErrorCircleRegular />, |
| 55 | +}; |
| 56 | + |
| 57 | +/** |
| 58 | + * Banner component inspired by Atlassian Design System |
| 59 | + * Displays important messages with various intent levels |
| 60 | + */ |
| 61 | +export function Banner({ |
| 62 | + intent, |
| 63 | + title, |
| 64 | + message, |
| 65 | + onAction, |
| 66 | + actionLabel, |
| 67 | + onDismiss, |
| 68 | + className, |
| 69 | +}: BannerProps) { |
| 70 | + const styles = useStyles(); |
| 71 | + |
| 72 | + // Map our intent to Fluent UI's MessageBarIntent |
| 73 | + const fluentIntent: MessageBarIntent = intent; |
| 74 | + |
| 75 | + return ( |
| 76 | + <MessageBar intent={fluentIntent} icon={iconMap[intent]} className={className}> |
| 77 | + <MessageBarBody> |
| 78 | + {title && <MessageBarTitle>{title}</MessageBarTitle>} |
| 79 | + {message} |
| 80 | + </MessageBarBody> |
| 81 | + <MessageBarActions |
| 82 | + containerAction={ |
| 83 | + onDismiss ? ( |
| 84 | + <Button |
| 85 | + appearance="transparent" |
| 86 | + icon={<DismissRegular />} |
| 87 | + onClick={onDismiss} |
| 88 | + aria-label="Dismiss" |
| 89 | + size="small" |
| 90 | + /> |
| 91 | + ) : undefined |
| 92 | + } |
| 93 | + > |
| 94 | + {onAction && actionLabel && ( |
| 95 | + <Button |
| 96 | + appearance="transparent" |
| 97 | + onClick={onAction} |
| 98 | + className={styles.actionButton} |
| 99 | + size="small" |
| 100 | + > |
| 101 | + {actionLabel} |
| 102 | + </Button> |
| 103 | + )} |
| 104 | + </MessageBarActions> |
| 105 | + </MessageBar> |
| 106 | + ); |
| 107 | +} |
0 commit comments