|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { useState } from "react"; |
| 5 | +import clsx from "clsx"; |
| 6 | + |
| 7 | +import Box from "@cloudscape-design/components/box"; |
| 8 | +import Button from "@cloudscape-design/components/button"; |
| 9 | +import Container from "@cloudscape-design/components/container"; |
| 10 | +import Header from "@cloudscape-design/components/header"; |
| 11 | +import Icon, { IconProps } from "@cloudscape-design/components/icon"; |
| 12 | +import SpaceBetween from "@cloudscape-design/components/space-between"; |
| 13 | + |
| 14 | +import { ChatBubble } from "../../lib/components"; |
| 15 | +import { Page } from "../app/templates"; |
| 16 | +import { ChatBubbleAvatarGenAI, ChatBubbleAvatarUser } from "./util-components"; |
| 17 | + |
| 18 | +import styles from "./styles.module.scss"; |
| 19 | + |
| 20 | +export default function ChatBubbleSelectableCustomPage() { |
| 21 | + const [selectedCardId, setSelectedCardId] = useState<null | string>(null); |
| 22 | + return ( |
| 23 | + <Page title="Chat bubble: selectable custom card"> |
| 24 | + <SpaceBetween direction="horizontal" size="l"> |
| 25 | + <Container header={<Header variant="h3">Chat container</Header>}> |
| 26 | + <SpaceBetween size="m"> |
| 27 | + <ChatBubble avatar={<ChatBubbleAvatarUser />} type="outgoing" ariaLabel="User at 4:24:12pm"> |
| 28 | + <Box color="text-body-secondary">User request example</Box> |
| 29 | + </ChatBubble> |
| 30 | + |
| 31 | + <ChatBubble avatar={<ChatBubbleAvatarGenAI />} type="incoming" ariaLabel="Gen AI at 4:24:24pm"> |
| 32 | + <Box color="text-body-secondary">Normal response example</Box> |
| 33 | + </ChatBubble> |
| 34 | + |
| 35 | + <ChatBubble |
| 36 | + avatar={<ChatBubbleAvatarGenAI />} |
| 37 | + hideAvatar={true} |
| 38 | + type="incoming" |
| 39 | + ariaLabel="Gen AI at 4:24:25pm" |
| 40 | + additionalContent={ |
| 41 | + <SpaceBetween size="s" direction="horizontal"> |
| 42 | + <CustomSelectableCard |
| 43 | + header="Selectable card 1" |
| 44 | + pressed={selectedCardId === "1"} |
| 45 | + iconName="expand" |
| 46 | + iconNamePressed="shrink" |
| 47 | + onClick={() => setSelectedCardId(selectedCardId === "1" ? null : "1")} |
| 48 | + > |
| 49 | + Selectable card 1 content |
| 50 | + </CustomSelectableCard> |
| 51 | + <CustomSelectableCard |
| 52 | + header="Selectable card 2" |
| 53 | + pressed={selectedCardId === "2"} |
| 54 | + iconName="expand" |
| 55 | + iconNamePressed="shrink" |
| 56 | + onClick={() => setSelectedCardId(selectedCardId === "2" ? null : "2")} |
| 57 | + > |
| 58 | + Selectable card 2 content |
| 59 | + </CustomSelectableCard> |
| 60 | + </SpaceBetween> |
| 61 | + } |
| 62 | + > |
| 63 | + Selectable response example |
| 64 | + </ChatBubble> |
| 65 | + </SpaceBetween> |
| 66 | + </Container> |
| 67 | + |
| 68 | + <Container header={<Header variant="h3">Presentation container</Header>}> |
| 69 | + {selectedCardId ? ( |
| 70 | + <SpaceBetween size="m"> |
| 71 | + <Box>{selectedCardId === "1" ? "First" : "Second"} card is selected</Box> |
| 72 | + <Button onClick={() => setSelectedCardId(null)}>Clear selection</Button> |
| 73 | + </SpaceBetween> |
| 74 | + ) : null} |
| 75 | + </Container> |
| 76 | + </SpaceBetween> |
| 77 | + </Page> |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +function CustomSelectableCard({ |
| 82 | + header, |
| 83 | + children, |
| 84 | + iconName, |
| 85 | + iconNamePressed, |
| 86 | + pressed, |
| 87 | + onClick, |
| 88 | +}: { |
| 89 | + header: React.ReactNode; |
| 90 | + children: React.ReactNode; |
| 91 | + iconName: IconProps.Name; |
| 92 | + iconNamePressed: IconProps.Name; |
| 93 | + pressed: boolean; |
| 94 | + onClick: () => void; |
| 95 | +}) { |
| 96 | + return ( |
| 97 | + <button |
| 98 | + aria-pressed={pressed} |
| 99 | + className={clsx(styles["selectable-card"], pressed && styles["selectable-card-pressed"])} |
| 100 | + onClick={onClick} |
| 101 | + > |
| 102 | + <SpaceBetween size="s"> |
| 103 | + <div className={styles["selectable-card-header"]}> |
| 104 | + <Box fontWeight="bold">{header}</Box> |
| 105 | + <Icon name={pressed ? iconNamePressed : iconName} /> |
| 106 | + </div> |
| 107 | + <Box>{children}</Box> |
| 108 | + </SpaceBetween> |
| 109 | + </button> |
| 110 | + ); |
| 111 | +} |
0 commit comments