|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import React, { useContext, useState } from 'react'; |
| 4 | + |
| 5 | +import { Box, Checkbox, Drawer, FormField, Header, Input, SegmentedControl, SpaceBetween } from '~components'; |
| 6 | +import AppLayout from '~components/app-layout'; |
| 7 | +import Button from '~components/button'; |
| 8 | +import { I18nProvider } from '~components/i18n'; |
| 9 | +import messages from '~components/i18n/messages/all.en'; |
| 10 | +import PanelLayout, { PanelLayoutProps } from '~components/panel-layout'; |
| 11 | + |
| 12 | +import AppContext, { AppContextType } from '../app/app-context'; |
| 13 | +import labels from '../app-layout/utils/labels'; |
| 14 | +import ScreenshotArea from '../utils/screenshot-area'; |
| 15 | + |
| 16 | +interface PanelLayoutContentProps { |
| 17 | + longPanelContent: boolean; |
| 18 | + longMainContent: boolean; |
| 19 | + buttons: boolean; |
| 20 | + minPanelSize: number; |
| 21 | + maxPanelSize: number; |
| 22 | + minContentSize: number; |
| 23 | + display: PanelLayoutProps.Display; |
| 24 | + panelPosition: PanelLayoutProps.PanelPosition; |
| 25 | +} |
| 26 | +type PageContext = React.Context<AppContextType<Partial<PanelLayoutContentProps>>>; |
| 27 | + |
| 28 | +const PanelLayoutContent = ({ |
| 29 | + longPanelContent, |
| 30 | + longMainContent, |
| 31 | + buttons, |
| 32 | + minContentSize, |
| 33 | + minPanelSize, |
| 34 | + maxPanelSize, |
| 35 | + display, |
| 36 | + panelPosition, |
| 37 | +}: PanelLayoutContentProps) => { |
| 38 | + const [size, setSize] = useState(Math.max(200, minPanelSize)); |
| 39 | + const [actualMaxPanelSize, setActualMaxPanelSize] = useState(size); |
| 40 | + |
| 41 | + const actualSize = Math.min(size, actualMaxPanelSize); |
| 42 | + const collapsed = actualMaxPanelSize < minPanelSize; |
| 43 | + |
| 44 | + return ( |
| 45 | + <PanelLayout |
| 46 | + panelSize={actualSize} |
| 47 | + minPanelSize={minPanelSize} |
| 48 | + maxPanelSize={actualMaxPanelSize} |
| 49 | + resizable={true} |
| 50 | + onPanelResize={({ detail }) => setSize(detail.panelSize)} |
| 51 | + onLayoutChange={({ detail }) => setActualMaxPanelSize(Math.min(detail.totalSize - minContentSize, maxPanelSize))} |
| 52 | + display={display === 'all' && collapsed ? 'main-only' : display} |
| 53 | + panelPosition={panelPosition} |
| 54 | + mainFocusable={longMainContent && !buttons ? { ariaLabel: 'Main content' } : undefined} |
| 55 | + panelFocusable={longPanelContent && !buttons ? { ariaLabel: 'Panel content' } : undefined} |
| 56 | + panelContent={ |
| 57 | + <Box padding="m"> |
| 58 | + <Header>Panel content</Header> |
| 59 | + {new Array(longPanelContent ? 20 : 1) |
| 60 | + .fill('Lorem ipsum dolor sit amet, consectetur adipiscing elit.') |
| 61 | + .map((t, i) => ( |
| 62 | + <div key={i}>{t}</div> |
| 63 | + ))} |
| 64 | + {buttons && <Button>Button</Button>} |
| 65 | + </Box> |
| 66 | + } |
| 67 | + mainContent={ |
| 68 | + <Box padding="m"> |
| 69 | + <Header>Main content{collapsed && ' [collapsed]'}</Header> |
| 70 | + {new Array(longMainContent ? 200 : 1) |
| 71 | + .fill('Lorem ipsum dolor sit amet, consectetur adipiscing elit.') |
| 72 | + .map((t, i) => ( |
| 73 | + <div key={i}>{t}</div> |
| 74 | + ))} |
| 75 | + {buttons && <Button>button</Button>} |
| 76 | + </Box> |
| 77 | + } |
| 78 | + /> |
| 79 | + ); |
| 80 | +}; |
| 81 | + |
| 82 | +export default function PanelLayoutPage() { |
| 83 | + const { |
| 84 | + urlParams: { |
| 85 | + longPanelContent = false, |
| 86 | + longMainContent = false, |
| 87 | + buttons = true, |
| 88 | + minPanelSize = 200, |
| 89 | + maxPanelSize = 600, |
| 90 | + minContentSize = 600, |
| 91 | + display = 'all', |
| 92 | + panelPosition = 'side-start', |
| 93 | + }, |
| 94 | + setUrlParams, |
| 95 | + } = useContext(AppContext as PageContext); |
| 96 | + |
| 97 | + return ( |
| 98 | + <I18nProvider messages={[messages]} locale="en"> |
| 99 | + <ScreenshotArea disableAnimations={true} gutters={false}> |
| 100 | + <AppLayout |
| 101 | + ariaLabels={labels} |
| 102 | + navigation={ |
| 103 | + <Drawer header="Settings"> |
| 104 | + <SpaceBetween direction="vertical" size="m"> |
| 105 | + <Checkbox |
| 106 | + checked={longMainContent} |
| 107 | + onChange={({ detail }) => setUrlParams({ longMainContent: detail.checked })} |
| 108 | + > |
| 109 | + Long main content |
| 110 | + </Checkbox> |
| 111 | + <Checkbox |
| 112 | + checked={longPanelContent} |
| 113 | + onChange={({ detail }) => setUrlParams({ longPanelContent: detail.checked })} |
| 114 | + > |
| 115 | + Long panel content |
| 116 | + </Checkbox> |
| 117 | + <Checkbox checked={buttons} onChange={({ detail }) => setUrlParams({ buttons: detail.checked })}> |
| 118 | + Include interactive content |
| 119 | + </Checkbox> |
| 120 | + <FormField label="Minimum panel size"> |
| 121 | + <Input |
| 122 | + type="number" |
| 123 | + value={minPanelSize.toString()} |
| 124 | + onChange={({ detail }) => setUrlParams({ minPanelSize: detail.value ? parseInt(detail.value) : 0 })} |
| 125 | + /> |
| 126 | + </FormField> |
| 127 | + <FormField label="Maximum panel size"> |
| 128 | + <Input |
| 129 | + type="number" |
| 130 | + value={maxPanelSize.toString()} |
| 131 | + onChange={({ detail }) => |
| 132 | + setUrlParams({ maxPanelSize: detail.value ? parseInt(detail.value) : Number.MAX_SAFE_INTEGER }) |
| 133 | + } |
| 134 | + /> |
| 135 | + </FormField> |
| 136 | + <FormField label="Minimum content size"> |
| 137 | + <Input |
| 138 | + type="number" |
| 139 | + value={minContentSize.toString()} |
| 140 | + onChange={({ detail }) => |
| 141 | + setUrlParams({ minContentSize: detail.value ? parseInt(detail.value) : 0 }) |
| 142 | + } |
| 143 | + /> |
| 144 | + </FormField> |
| 145 | + <FormField label="Panel position"> |
| 146 | + <SegmentedControl |
| 147 | + options={[ |
| 148 | + { id: 'side-start', text: 'side-start' }, |
| 149 | + { id: 'side-end', text: 'side-end' }, |
| 150 | + ]} |
| 151 | + selectedId={panelPosition} |
| 152 | + onChange={({ detail }) => setUrlParams({ panelPosition: detail.selectedId as any })} |
| 153 | + /> |
| 154 | + </FormField> |
| 155 | + <FormField label="Display"> |
| 156 | + <SegmentedControl |
| 157 | + options={[ |
| 158 | + { id: 'all', text: 'all' }, |
| 159 | + { id: 'panel-only', text: 'panel-only' }, |
| 160 | + { id: 'main-only', text: 'main-only' }, |
| 161 | + ]} |
| 162 | + selectedId={display} |
| 163 | + onChange={({ detail }) => setUrlParams({ display: detail.selectedId as any })} |
| 164 | + /> |
| 165 | + </FormField> |
| 166 | + </SpaceBetween> |
| 167 | + </Drawer> |
| 168 | + } |
| 169 | + content={<Header variant="h1">Panel layout in drawer demo</Header>} |
| 170 | + drawers={[ |
| 171 | + { |
| 172 | + id: 'panel', |
| 173 | + content: ( |
| 174 | + <PanelLayoutContent |
| 175 | + longMainContent={longMainContent} |
| 176 | + longPanelContent={longPanelContent} |
| 177 | + buttons={buttons} |
| 178 | + minPanelSize={minPanelSize} |
| 179 | + maxPanelSize={maxPanelSize} |
| 180 | + minContentSize={minContentSize} |
| 181 | + display={display} |
| 182 | + panelPosition={panelPosition} |
| 183 | + /> |
| 184 | + ), |
| 185 | + resizable: true, |
| 186 | + defaultSize: 1000, |
| 187 | + ariaLabels: { |
| 188 | + drawerName: 'Panel', |
| 189 | + triggerButton: 'Open panel', |
| 190 | + closeButton: 'Close panel', |
| 191 | + resizeHandle: 'Resize drawer', |
| 192 | + }, |
| 193 | + trigger: { iconName: 'contact' }, |
| 194 | + }, |
| 195 | + ]} |
| 196 | + /> |
| 197 | + </ScreenshotArea> |
| 198 | + </I18nProvider> |
| 199 | + ); |
| 200 | +} |
0 commit comments