|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import React, { useContext, useState } from 'react'; |
| 5 | + |
| 6 | +import { Drawer, Header, Icon, SideNavigation, SpaceBetween, Toggle } from '~components'; |
| 7 | +import AppLayout, { AppLayoutProps } from '~components/app-layout'; |
| 8 | +import Box from '~components/box'; |
| 9 | +import SplitPanel from '~components/split-panel'; |
| 10 | + |
| 11 | +import AppContext, { AppContextType } from '../app/app-context'; |
| 12 | +import ScreenshotArea from '../utils/screenshot-area'; |
| 13 | +import { Breadcrumbs } from './utils/content-blocks'; |
| 14 | +import * as content from './utils/contents'; |
| 15 | +import { drawerLabels } from './utils/drawers'; |
| 16 | +import labels from './utils/labels'; |
| 17 | +import { splitPaneli18nStrings } from './utils/strings'; |
| 18 | + |
| 19 | +type DrawerHeaderAndFooterDemoContext = React.Context< |
| 20 | + AppContextType<{ |
| 21 | + drawerOpen: string | null; |
| 22 | + hasHeader: boolean; |
| 23 | + hasFooter: boolean; |
| 24 | + splitPanelPosition: AppLayoutProps.SplitPanelPreferences['position']; |
| 25 | + longHeader: boolean; |
| 26 | + longFooter: boolean; |
| 27 | + longContent: boolean; |
| 28 | + splitPanelOpen: boolean; |
| 29 | + }> |
| 30 | +>; |
| 31 | + |
| 32 | +const getAriaLabels = (title: string) => { |
| 33 | + return { |
| 34 | + closeButton: `${title} close button`, |
| 35 | + drawerName: `${title}`, |
| 36 | + triggerButton: `${title} trigger button`, |
| 37 | + resizeHandle: `${title} resize handle`, |
| 38 | + }; |
| 39 | +}; |
| 40 | + |
| 41 | +export default function () { |
| 42 | + const { |
| 43 | + urlParams: { |
| 44 | + splitPanelPosition, |
| 45 | + longHeader = false, |
| 46 | + longFooter = false, |
| 47 | + longContent = false, |
| 48 | + hasHeader = true, |
| 49 | + hasFooter = true, |
| 50 | + splitPanelOpen = false, |
| 51 | + }, |
| 52 | + setUrlParams, |
| 53 | + } = useContext(AppContext as DrawerHeaderAndFooterDemoContext); |
| 54 | + |
| 55 | + const [activeDrawerId, setActiveDrawerId] = useState<string | null>('sample-demo'); |
| 56 | + |
| 57 | + return ( |
| 58 | + <ScreenshotArea gutters={false}> |
| 59 | + <AppLayout |
| 60 | + ariaLabels={{ ...labels, ...drawerLabels }} |
| 61 | + breadcrumbs={<Breadcrumbs />} |
| 62 | + navigation={ |
| 63 | + <SideNavigation |
| 64 | + header={{ |
| 65 | + href: '#', |
| 66 | + text: 'On the other side', |
| 67 | + }} |
| 68 | + items={[]} |
| 69 | + /> |
| 70 | + } |
| 71 | + splitPanelPreferences={{ position: splitPanelPosition }} |
| 72 | + onSplitPanelPreferencesChange={event => { |
| 73 | + const { position } = event.detail; |
| 74 | + setUrlParams({ splitPanelPosition: position === 'side' ? position : undefined }); |
| 75 | + }} |
| 76 | + activeDrawerId={activeDrawerId} |
| 77 | + drawers={[ |
| 78 | + { |
| 79 | + ariaLabels: getAriaLabels('sample-demo'), |
| 80 | + id: 'sample-demo', |
| 81 | + resizable: true, |
| 82 | + defaultSize: 420, |
| 83 | + content: ( |
| 84 | + <Drawer |
| 85 | + header={hasHeader && (longHeader ? content.longHeader : content.shortHeader)} |
| 86 | + footer={hasFooter && (longFooter ? content.longFooter : content.shortFooter)} |
| 87 | + headerActions={[<Icon key="icon" name="add-plus" data-testid="drawer-header-action-button" />]} |
| 88 | + > |
| 89 | + {longContent ? content.longContent : content.shortContent} |
| 90 | + </Drawer> |
| 91 | + ), |
| 92 | + trigger: { |
| 93 | + iconName: 'contact', |
| 94 | + }, |
| 95 | + }, |
| 96 | + ]} |
| 97 | + onDrawerChange={event => { |
| 98 | + setActiveDrawerId(event.detail.activeDrawerId); |
| 99 | + }} |
| 100 | + contentType="table" |
| 101 | + splitPanelOpen={splitPanelOpen} |
| 102 | + onSplitPanelToggle={event => setUrlParams({ splitPanelOpen: event.detail.open })} |
| 103 | + splitPanel={ |
| 104 | + <SplitPanel header="Split panel header" i18nStrings={splitPaneli18nStrings}> |
| 105 | + <Box>Content</Box> |
| 106 | + </SplitPanel> |
| 107 | + } |
| 108 | + content={ |
| 109 | + <SpaceBetween size="xs"> |
| 110 | + <Header variant="h1" description="Sometimes all you need is a large cappuchino."> |
| 111 | + Drawers with Footer Demo |
| 112 | + </Header> |
| 113 | + {activeDrawerId === 'sample-demo' && ( |
| 114 | + <Toggle checked={longContent} onChange={({ detail }) => setUrlParams({ longContent: detail.checked })}> |
| 115 | + Long content |
| 116 | + </Toggle> |
| 117 | + )} |
| 118 | + |
| 119 | + <Toggle |
| 120 | + checked={hasHeader} |
| 121 | + onChange={({ detail }) => setUrlParams({ hasHeader: detail.checked })} |
| 122 | + disabled={!activeDrawerId} |
| 123 | + > |
| 124 | + Has Header |
| 125 | + </Toggle> |
| 126 | + |
| 127 | + {hasHeader && activeDrawerId === 'sample-demo' && ( |
| 128 | + <Box margin={{ left: 'l' }}> |
| 129 | + <Toggle |
| 130 | + checked={longHeader} |
| 131 | + onChange={({ detail }) => setUrlParams({ longHeader: detail.checked })} |
| 132 | + disabled={!activeDrawerId} |
| 133 | + > |
| 134 | + Long Header |
| 135 | + </Toggle> |
| 136 | + </Box> |
| 137 | + )} |
| 138 | + |
| 139 | + {activeDrawerId === 'sample-demo' && ( |
| 140 | + <Toggle checked={hasFooter} onChange={({ detail }) => setUrlParams({ hasFooter: detail.checked })}> |
| 141 | + Has Footer |
| 142 | + </Toggle> |
| 143 | + )} |
| 144 | + |
| 145 | + {hasFooter && activeDrawerId === 'sample-demo' && ( |
| 146 | + <Box margin={{ left: 'l' }}> |
| 147 | + <Toggle checked={longFooter} onChange={({ detail }) => setUrlParams({ longFooter: detail.checked })}> |
| 148 | + Long Footer |
| 149 | + </Toggle> |
| 150 | + </Box> |
| 151 | + )} |
| 152 | + </SpaceBetween> |
| 153 | + } |
| 154 | + /> |
| 155 | + </ScreenshotArea> |
| 156 | + ); |
| 157 | +} |
0 commit comments