Skip to content

Commit e5075c7

Browse files
committed
Added react events to top-level spaces and added ability to cancel resize
1 parent 9a57cd8 commit e5075c7

File tree

6 files changed

+54
-21
lines changed

6 files changed

+54
-21
lines changed

react-spaces/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-spaces",
3-
"version": "0.1.21",
3+
"version": "0.1.22-alpha.1",
44
"main": "dist/index.js",
55
"module": "dist/es/index.js",
66
"types": "dist/index.d.ts",

react-spaces/src/Fixed.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import * as React from 'react';
22
import './Styles.css';
33
import * as PropTypes from "prop-types";
44
import { SpaceContext } from './Globals/Contexts';
5-
import { ISpace } from './Globals/Types';
5+
import { ISpace, IReactEvents } from './Globals/Types';
66
import { createSpaceContext } from './Globals/ISpaceContext';
77
import { HeadStyles } from './HeadStyles';
88

9-
interface IProps {
9+
interface IProps extends IReactEvents {
1010
className?: string,
1111
style?: React.CSSProperties,
1212
width?: number,
@@ -27,7 +27,15 @@ export const Fixed : React.FC<IProps> = (props) => {
2727
return (
2828
<div
2929
className={`spaces-fixedsize-layout${props.className ? ` ${props.className}` : ``}`}
30-
style={style}>
30+
style={style}
31+
onClick={props.onClick}
32+
onMouseDown={props.onMouseDown}
33+
onMouseEnter={props.onMouseEnter}
34+
onMouseLeave={props.onMouseLeave}
35+
onMouseMove={props.onMouseMove}
36+
onTouchStart={props.onTouchStart}
37+
onTouchMove={props.onTouchMove}
38+
onTouchEnd={props.onTouchEnd}>
3139
<HeadStyles spaces={children} />
3240
<SpaceContext.Provider value={createSpaceContext(children, setChildren)}>
3341
{props.children}

react-spaces/src/Globals/Types.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,7 @@ export enum CenterType {
3434
HorizontalVertical = "horizontalVertical"
3535
}
3636

37-
export interface IPublicProps {
38-
id?: string,
39-
className?: string,
40-
style?: React.CSSProperties,
41-
scrollable?: boolean,
42-
trackSize?: boolean,
43-
centerContent?: CenterType,
44-
as?: string,
45-
children?: React.ReactNode,
46-
zIndex?: number,
37+
export interface IReactEvents {
4738
onClick?: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void,
4839
onMouseDown?: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void,
4940
onMouseEnter?: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void,
@@ -54,6 +45,18 @@ export interface IPublicProps {
5445
onTouchEnd?: (event: React.TouchEvent<HTMLElement>) => void
5546
}
5647

48+
export interface IPublicProps extends IReactEvents {
49+
id?: string,
50+
className?: string,
51+
style?: React.CSSProperties,
52+
scrollable?: boolean,
53+
trackSize?: boolean,
54+
centerContent?: CenterType,
55+
as?: string,
56+
children?: React.ReactNode,
57+
zIndex?: number
58+
}
59+
5760
export const publicProps = {
5861
id: PropTypes.string,
5962
className: PropTypes.string,
@@ -103,7 +106,7 @@ export interface IResizableProps {
103106
overlayHandle?: boolean,
104107
minimumSize?: number,
105108
maximumSize?: number,
106-
onResizeStart?: () => void,
109+
onResizeStart?: () => boolean | void,
107110
onResizeEnd?: (newSize: number) => void
108111
}
109112

react-spaces/src/Space.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const RightResizable : React.FC<IPublicProps & IAnchoredProps & IResizabl
3333
RightResizable.propTypes = {...publicProps, ...anchoredProps, ...resizableProps};
3434
export const Positioned : React.FC<IPublicProps & IResizableProps & IPositionedProps> = (props) => <SpaceInternal {...props} />
3535
RightResizable.propTypes = {...publicProps, ...resizableProps, ...positionedProps};
36+
export const Custom : React.FC<AllProps> = (props) => <SpaceInternal {...props} />
3637

3738
export const SpaceInternal : React.FC<AllProps> = React.memo((props) => {
3839

@@ -95,6 +96,13 @@ export const SpaceInternal : React.FC<AllProps> = React.memo((props) => {
9596
if (!divElementRef.current) {
9697
return;
9798
}
99+
100+
if (props.onResizeStart) {
101+
const result = props.onResizeStart();
102+
if (typeof result === "boolean" && !result) {
103+
return;
104+
}
105+
}
98106

99107
var rect = divElementRef.current.getBoundingClientRect();
100108
var size = isHorizontalSpace(props.anchor) ? rect.width : rect.height;
@@ -129,13 +137,19 @@ export const SpaceInternal : React.FC<AllProps> = React.memo((props) => {
129137
window.addEventListener('touchend', removeListener);
130138
e.preventDefault();
131139
e.stopPropagation();
132-
props.onResizeStart && props.onResizeStart();
133140
};
134141

135142
const startResize = (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
136143
if (!divElementRef.current) {
137144
return;
138145
}
146+
147+
if (props.onResizeStart) {
148+
const result = props.onResizeStart();
149+
if (typeof result === "boolean" && !result) {
150+
return;
151+
}
152+
}
139153

140154
var rect = divElementRef.current.getBoundingClientRect();
141155
var size = isHorizontalSpace(props.anchor) ? rect.width : rect.height;
@@ -170,7 +184,6 @@ export const SpaceInternal : React.FC<AllProps> = React.memo((props) => {
170184
window.addEventListener('mouseup', removeListener);
171185
e.preventDefault();
172186
e.stopPropagation();
173-
props.onResizeStart && props.onResizeStart();
174187
};
175188

176189
resizeHandle =

react-spaces/src/ViewPort.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import * as React from 'react';
22
import './Styles.css';
33
import * as PropTypes from "prop-types";
44
import { SpaceContext } from './Globals/Contexts';
5-
import { ISpace } from './Globals/Types';
5+
import { ISpace, IReactEvents } from './Globals/Types';
66
import { createSpaceContext } from './Globals/ISpaceContext';
77
import { HeadStyles } from './HeadStyles';
88

9-
interface IProps {
9+
interface IProps extends IReactEvents {
1010
className?: string,
1111
left?: number,
1212
top?: number,
@@ -25,7 +25,15 @@ export const ViewPort : React.FC<IProps> = (props) => {
2525
top: props.top || 0,
2626
right: props.right || 0,
2727
bottom: props.bottom || 0
28-
}}>
28+
}}
29+
onClick={props.onClick}
30+
onMouseDown={props.onMouseDown}
31+
onMouseEnter={props.onMouseEnter}
32+
onMouseLeave={props.onMouseLeave}
33+
onMouseMove={props.onMouseMove}
34+
onTouchStart={props.onTouchStart}
35+
onTouchMove={props.onTouchMove}
36+
onTouchEnd={props.onTouchEnd}>
2937
<HeadStyles spaces={children} />
3038
<SpaceContext.Provider value={createSpaceContext(children, setChildren)}>
3139
{props.children}

react-spaces/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export {
1414
RightResizable,
1515
TopResizable,
1616
BottomResizable,
17-
Positioned
17+
Positioned,
18+
Custom
1819
} from './Space';
1920

2021
export {

0 commit comments

Comments
 (0)