|
| 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 { Checkbox, Drawer, FormField, Header, Input, SpaceBetween } from '~components'; |
| 8 | +import AppLayout from '~components/app-layout'; |
| 9 | +import Button from '~components/button'; |
| 10 | +import SplitView from '~components/split-view'; |
| 11 | + |
| 12 | +import AppContext, { AppContextType } from '../app/app-context'; |
| 13 | +import ScreenshotArea from '../utils/screenshot-area'; |
| 14 | + |
| 15 | +interface SplitViewContentProps { |
| 16 | + longPanelContent: boolean; |
| 17 | + longMainContent: boolean; |
| 18 | + minPanelSize: number; |
| 19 | + maxPanelSize: number; |
| 20 | + minContentSize: number; |
| 21 | +} |
| 22 | +type PageContext = React.Context<AppContextType<Partial<SplitViewContentProps>>>; |
| 23 | + |
| 24 | +const SplitViewContent = ({ |
| 25 | + longPanelContent, |
| 26 | + longMainContent, |
| 27 | + minContentSize, |
| 28 | + minPanelSize, |
| 29 | + maxPanelSize, |
| 30 | +}: SplitViewContentProps) => { |
| 31 | + const [size, setSize] = useState(Math.max(200, minPanelSize)); |
| 32 | + |
| 33 | + const [_actualMaxPanelSize, ref] = useContainerQuery( |
| 34 | + entry => Math.min(entry.contentBoxWidth - minContentSize, maxPanelSize), |
| 35 | + [minContentSize, maxPanelSize] |
| 36 | + ); |
| 37 | + const actualMaxPanelSize = _actualMaxPanelSize ?? maxPanelSize; |
| 38 | + const actualSize = Math.min(size, actualMaxPanelSize); |
| 39 | + |
| 40 | + const collapsed = actualMaxPanelSize < minPanelSize; |
| 41 | + |
| 42 | + return ( |
| 43 | + <div ref={ref} style={{ height: '100%', overflow: 'hidden', backgroundColor: 'lightgrey' }}> |
| 44 | + {collapsed ? ( |
| 45 | + 'Collapsed view' |
| 46 | + ) : ( |
| 47 | + <SplitView |
| 48 | + panelSize={actualSize} |
| 49 | + minPanelSize={minPanelSize} |
| 50 | + maxPanelSize={actualMaxPanelSize} |
| 51 | + resizable={true} |
| 52 | + onResize={({ detail }) => setSize(detail.panelSize)} |
| 53 | + panelContent={ |
| 54 | + <> |
| 55 | + <Header>Panel content</Header> |
| 56 | + {new Array(longPanelContent ? 20 : 1) |
| 57 | + .fill('Lorem ipsum dolor sit amet, consectetur adipiscing elit.') |
| 58 | + .map((t, i) => ( |
| 59 | + <div key={i}>{t}</div> |
| 60 | + ))} |
| 61 | + <Button>Button</Button> |
| 62 | + </> |
| 63 | + } |
| 64 | + > |
| 65 | + <Header>Main content</Header> |
| 66 | + {new Array(longMainContent ? 200 : 1) |
| 67 | + .fill('Lorem ipsum dolor sit amet, consectetur adipiscing elit.') |
| 68 | + .map((t, i) => ( |
| 69 | + <div key={i}>{t}</div> |
| 70 | + ))} |
| 71 | + <Button>button</Button> |
| 72 | + </SplitView> |
| 73 | + )} |
| 74 | + </div> |
| 75 | + ); |
| 76 | +}; |
| 77 | + |
| 78 | +export default function SplitViewPage() { |
| 79 | + const { |
| 80 | + urlParams: { |
| 81 | + longPanelContent = false, |
| 82 | + longMainContent = false, |
| 83 | + minPanelSize = 200, |
| 84 | + maxPanelSize = 600, |
| 85 | + minContentSize = 600, |
| 86 | + }, |
| 87 | + setUrlParams, |
| 88 | + } = useContext(AppContext as PageContext); |
| 89 | + |
| 90 | + return ( |
| 91 | + <> |
| 92 | + <ScreenshotArea disableAnimations={true} gutters={false}> |
| 93 | + <AppLayout |
| 94 | + navigation={ |
| 95 | + <Drawer header="Settings"> |
| 96 | + <SpaceBetween direction="vertical" size="m"> |
| 97 | + <Checkbox |
| 98 | + checked={longMainContent} |
| 99 | + onChange={({ detail }) => setUrlParams({ longMainContent: detail.checked })} |
| 100 | + > |
| 101 | + Long main content |
| 102 | + </Checkbox> |
| 103 | + <Checkbox |
| 104 | + checked={longPanelContent} |
| 105 | + onChange={({ detail }) => setUrlParams({ longPanelContent: detail.checked })} |
| 106 | + > |
| 107 | + Long panel content |
| 108 | + </Checkbox> |
| 109 | + <FormField label="Minimum panel size"> |
| 110 | + <Input |
| 111 | + type="number" |
| 112 | + value={minPanelSize.toString()} |
| 113 | + onChange={({ detail }) => setUrlParams({ minPanelSize: detail.value ? parseInt(detail.value) : 0 })} |
| 114 | + /> |
| 115 | + </FormField> |
| 116 | + <FormField label="Maximum panel size"> |
| 117 | + <Input |
| 118 | + type="number" |
| 119 | + value={maxPanelSize.toString()} |
| 120 | + onChange={({ detail }) => |
| 121 | + setUrlParams({ maxPanelSize: detail.value ? parseInt(detail.value) : Number.MAX_SAFE_INTEGER }) |
| 122 | + } |
| 123 | + /> |
| 124 | + </FormField> |
| 125 | + <FormField label="Minimum content size"> |
| 126 | + <Input |
| 127 | + type="number" |
| 128 | + value={minContentSize.toString()} |
| 129 | + onChange={({ detail }) => |
| 130 | + setUrlParams({ minContentSize: detail.value ? parseInt(detail.value) : 0 }) |
| 131 | + } |
| 132 | + /> |
| 133 | + </FormField> |
| 134 | + </SpaceBetween> |
| 135 | + </Drawer> |
| 136 | + } |
| 137 | + drawers={[ |
| 138 | + { |
| 139 | + id: 'panel', |
| 140 | + content: ( |
| 141 | + <SplitViewContent |
| 142 | + longMainContent={longMainContent} |
| 143 | + longPanelContent={longPanelContent} |
| 144 | + minPanelSize={minPanelSize} |
| 145 | + maxPanelSize={maxPanelSize} |
| 146 | + minContentSize={minContentSize} |
| 147 | + /> |
| 148 | + ), |
| 149 | + resizable: true, |
| 150 | + defaultSize: 1000, |
| 151 | + ariaLabels: { drawerName: 'Panel' }, |
| 152 | + trigger: { iconName: 'contact' }, |
| 153 | + }, |
| 154 | + ]} |
| 155 | + /> |
| 156 | + </ScreenshotArea> |
| 157 | + </> |
| 158 | + ); |
| 159 | +} |
0 commit comments