-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5419 from pat270/clay-5348
feat(@clayui/core): Vertical Bar adds resizing to VerticalBar.Panel
- Loading branch information
Showing
17 changed files
with
355 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/** | ||
* SPDX-FileCopyrightText: © 2023 Liferay, Inc. <https://liferay.com> | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import {Keys} from '@clayui/shared'; | ||
import React, {useContext} from 'react'; | ||
|
||
import {VerticalBarContext} from './context'; | ||
|
||
type Props = { | ||
/** | ||
* Sets the CSS className for the component. | ||
*/ | ||
className?: string; | ||
|
||
/** | ||
* Reference to Panel | ||
*/ | ||
nodeRef?: React.RefObject<HTMLDivElement>; | ||
}; | ||
|
||
const MAIN_MOUSE_BUTTON = 0; | ||
|
||
let keyDownCounter = 0; | ||
|
||
export function Resizer({nodeRef, ...otherProps}: Props) { | ||
const {onPanelWidthChange, panelWidthMax, panelWidthMin, position} = | ||
useContext(VerticalBarContext); | ||
|
||
const positionLeft = position === 'left'; | ||
|
||
const getStartWidth = () => { | ||
return nodeRef?.current?.offsetWidth || 320; | ||
}; | ||
|
||
const decreasePanelWidth = (delta = 1, startWidth = getStartWidth()) => { | ||
const width = Math.round(startWidth - delta); | ||
|
||
if (width > panelWidthMin - 100 && width < panelWidthMin) { | ||
onPanelWidthChange(panelWidthMin); | ||
} else if (width > panelWidthMin) { | ||
onPanelWidthChange(width); | ||
} | ||
}; | ||
|
||
const increasePanelWidth = (delta = 1, startWidth = getStartWidth()) => { | ||
const width = Math.round(startWidth + delta); | ||
|
||
if (width > panelWidthMax && width < panelWidthMax + 100) { | ||
onPanelWidthChange(panelWidthMax); | ||
} else if (width < panelWidthMax) { | ||
onPanelWidthChange(width); | ||
} | ||
}; | ||
|
||
return ( | ||
<div | ||
{...otherProps} | ||
onKeyDown={(event: React.KeyboardEvent<HTMLDivElement>) => { | ||
const delta = keyDownCounter > 7 ? 10 : 1; | ||
|
||
keyDownCounter++; | ||
|
||
switch (event.key) { | ||
case Keys.Down: { | ||
decreasePanelWidth(delta); | ||
|
||
break; | ||
} | ||
case Keys.Left: { | ||
if (positionLeft) { | ||
decreasePanelWidth(delta); | ||
} else { | ||
increasePanelWidth(delta); | ||
} | ||
|
||
break; | ||
} | ||
case Keys.Right: { | ||
if (positionLeft) { | ||
increasePanelWidth(delta); | ||
} else { | ||
decreasePanelWidth(delta); | ||
} | ||
|
||
break; | ||
} | ||
case Keys.Up: { | ||
increasePanelWidth(delta); | ||
|
||
break; | ||
} | ||
default: { | ||
break; | ||
} | ||
} | ||
}} | ||
onKeyUp={() => { | ||
keyDownCounter = 0; | ||
}} | ||
onPointerDown={(event) => { | ||
const startWidth = getStartWidth(); | ||
const startXPos = event.pageX; | ||
|
||
function onResizerMove(event: any) { | ||
const delta = Math.abs(event.pageX - startXPos); | ||
|
||
if ( | ||
(event.pageX >= startXPos && positionLeft) || | ||
(event.pageX < startXPos && !positionLeft) | ||
) { | ||
increasePanelWidth(delta, startWidth); | ||
} else if ( | ||
(event.pageX < startXPos && positionLeft) || | ||
(event.pageX >= startXPos && !positionLeft) | ||
) { | ||
decreasePanelWidth(delta, startWidth); | ||
} | ||
} | ||
|
||
function removeResizerEvents() { | ||
document.removeEventListener('pointermove', onResizerMove); | ||
|
||
document.removeEventListener( | ||
'pointerup', | ||
removeResizerEvents | ||
); | ||
} | ||
|
||
if (event.button === MAIN_MOUSE_BUTTON) { | ||
document.addEventListener('pointermove', onResizerMove); | ||
|
||
document.addEventListener('pointerup', removeResizerEvents); | ||
} | ||
}} | ||
role="separator" | ||
tabIndex={0} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/clay-css/src/scss/cadmin/components/_resizer.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.c-horizontal-resizer { | ||
@include clay-css($cadmin-c-horizontal-resizer); | ||
|
||
&:hover { | ||
@include clay-css(map-get($cadmin-c-horizontal-resizer, hover)); | ||
} | ||
|
||
&:focus { | ||
@include clay-css(map-get($cadmin-c-horizontal-resizer, focus)); | ||
} | ||
} |
Oops, something went wrong.