2021-02-12 18:53:46 -08:00
|
|
|
/*
|
|
|
|
Copyright (c) Microsoft Corporation.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the 'License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import './splitView.css';
|
|
|
|
import * as React from 'react';
|
|
|
|
|
|
|
|
export interface SplitViewProps {
|
|
|
|
sidebarSize: number,
|
2021-02-22 15:35:38 -08:00
|
|
|
sidebarHidden?: boolean
|
2021-02-12 18:53:46 -08:00
|
|
|
}
|
|
|
|
|
2021-02-19 07:25:08 -08:00
|
|
|
const kMinSidebarSize = 50;
|
|
|
|
|
2021-02-12 18:53:46 -08:00
|
|
|
export const SplitView: React.FC<SplitViewProps> = ({
|
|
|
|
sidebarSize,
|
2021-02-22 15:35:38 -08:00
|
|
|
sidebarHidden,
|
2021-02-12 18:53:46 -08:00
|
|
|
children
|
|
|
|
}) => {
|
2021-02-19 07:25:08 -08:00
|
|
|
let [size, setSize] = React.useState<number>(Math.max(kMinSidebarSize, sidebarSize));
|
|
|
|
const [resizing, setResizing] = React.useState<{ offsetY: number, size: number } | null>(null);
|
2021-02-12 18:53:46 -08:00
|
|
|
|
|
|
|
const childrenArray = React.Children.toArray(children);
|
2021-02-22 15:35:38 -08:00
|
|
|
document.body.style.userSelect = resizing ? 'none' : 'inherit';
|
2021-02-12 18:53:46 -08:00
|
|
|
return <div className='split-view'>
|
|
|
|
<div className='split-view-main'>{childrenArray[0]}</div>
|
2021-02-22 15:35:38 -08:00
|
|
|
{ !sidebarHidden && <div style={{flexBasis: size}} className='split-view-sidebar'>{childrenArray[1]}</div> }
|
|
|
|
{ !sidebarHidden && <div
|
2021-02-19 07:25:08 -08:00
|
|
|
style={{bottom: resizing ? 0 : size - 4, top: resizing ? 0 : undefined, height: resizing ? 'initial' : 8 }}
|
2021-02-12 18:53:46 -08:00
|
|
|
className='split-view-resizer'
|
2021-02-19 07:25:08 -08:00
|
|
|
onMouseDown={event => setResizing({ offsetY: event.clientY, size })}
|
2021-02-12 18:53:46 -08:00
|
|
|
onMouseUp={() => setResizing(null)}
|
2021-02-19 07:25:08 -08:00
|
|
|
onMouseMove={event => {
|
2021-02-22 15:35:38 -08:00
|
|
|
if (!event.buttons)
|
|
|
|
setResizing(null);
|
|
|
|
else if (resizing)
|
2021-02-19 07:25:08 -08:00
|
|
|
setSize(Math.max(kMinSidebarSize, resizing.size - event.clientY + resizing.offsetY));
|
|
|
|
}}
|
2021-02-22 15:35:38 -08:00
|
|
|
></div> }
|
2021-02-12 18:53:46 -08:00
|
|
|
</div>;
|
|
|
|
};
|