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