Skip to content

Commit 9ffe042

Browse files
authored
feat(resize-handle): support viewport units (#372)
1 parent 8ede729 commit 9ffe042

12 files changed

+506
-141
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "feat: support viewport units",
4+
"packageName": "@fluentui-contrib/react-resize-handle",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/react-resize-handle/src/hooks/useKeyboardHandler.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import { useEventCallback, useFluent } from '@fluentui/react-components';
2+
import type { EventHandler } from '@fluentui/react-utilities';
23
import * as React from 'react';
4+
35
import {
46
EVENTS,
57
GrowDirection,
68
ResizeHandleUpdateEventData,
79
SupportedKeys,
810
} from '../types';
9-
import type { EventHandler } from '@fluentui/react-utilities';
11+
import type { UnitHandle } from './useUnitHandle';
1012

1113
export type UseKeyboardHandlerOptions = {
1214
onValueChange: EventHandler<ResizeHandleUpdateEventData>;
1315
growDirection: GrowDirection;
16+
1417
getCurrentValue: () => number;
18+
unitHandle: UnitHandle;
1519
};
1620

17-
const DEFAULT_STEP = 20;
18-
1921
const multipliers: Record<
2022
GrowDirection,
2123
Partial<Record<SupportedKeys, number>>
@@ -49,26 +51,27 @@ function isSupportedKey(
4951
}
5052

5153
export const useKeyboardHandler = (options: UseKeyboardHandlerOptions) => {
52-
const { onValueChange, growDirection, getCurrentValue } = options;
54+
const { onValueChange, growDirection, getCurrentValue, unitHandle } = options;
5355
const { dir } = useFluent();
5456

5557
const onKeyDown = useEventCallback((event: KeyboardEvent) => {
5658
if (!isSupportedKey(growDirection, event.key)) {
5759
return;
5860
}
5961

60-
let newValue = getCurrentValue();
62+
const currentValue = getCurrentValue();
6163

6264
const multiplier = multipliers[growDirection][event.key] ?? 1;
6365
const directionMultiplier =
6466
dir === 'rtl' && ['start', 'end'].includes(growDirection) ? -1 : 1;
65-
66-
newValue += multiplier * DEFAULT_STEP * directionMultiplier;
67+
const offset =
68+
multiplier * unitHandle.getOffsetStep() * directionMultiplier;
6769

6870
onValueChange(event, {
6971
event,
70-
value: Math.round(newValue),
72+
value: unitHandle.roundValue(currentValue + offset),
7173
type: EVENTS.keyboard,
74+
unit: unitHandle.name,
7275
});
7376
});
7477

packages/react-resize-handle/src/hooks/useMouseHandler.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
import { useFluent, useEventCallback } from '@fluentui/react-components';
2-
3-
import type { EventHandler } from '@fluentui/react-utilities';
42
import {
53
getEventClientCoords,
6-
NativeTouchOrMouseEvent,
74
isMouseEvent,
85
isTouchEvent,
6+
type NativeTouchOrMouseEvent,
7+
type EventHandler,
98
} from '@fluentui/react-utilities';
109
import * as React from 'react';
10+
1111
import { EVENTS, GrowDirection, ResizeHandleUpdateEventData } from '../types';
12+
import type { UnitHandle } from './useUnitHandle';
1213

1314
export type UseMouseHandlerParams = {
1415
growDirection: GrowDirection;
1516
onValueChange: EventHandler<ResizeHandleUpdateEventData>;
1617
onDragEnd?: EventHandler<Omit<ResizeHandleUpdateEventData, 'value'>>;
1718
onDragStart?: EventHandler<Omit<ResizeHandleUpdateEventData, 'value'>>;
19+
1820
getCurrentValue: () => number;
21+
unitHandle: UnitHandle;
1922
};
2023

2124
export function useMouseHandler(params: UseMouseHandlerParams) {
2225
const { targetDocument, dir } = useFluent();
2326
const targetWindow = targetDocument?.defaultView;
2427

2528
const dragStartOriginCoords = React.useRef({ clientX: 0, clientY: 0 });
26-
const { growDirection, onValueChange, getCurrentValue } = params;
29+
const { growDirection, getCurrentValue, onValueChange, unitHandle } = params;
2730

2831
const startValue = React.useRef(0);
2932

@@ -39,21 +42,26 @@ export function useMouseHandler(params: UseMouseHandlerParams) {
3942

4043
switch (growDirection) {
4144
case 'end':
42-
newValue += deltaCoords[0] * (dir === 'rtl' ? -1 : 1);
45+
newValue += unitHandle.fromPxToValue(
46+
deltaCoords[0] * (dir === 'rtl' ? -1 : 1)
47+
);
4348
break;
4449
case 'start':
45-
newValue -= deltaCoords[0] * (dir === 'rtl' ? -1 : 1);
50+
newValue -= unitHandle.fromPxToValue(
51+
deltaCoords[0] * (dir === 'rtl' ? -1 : 1)
52+
);
4653
break;
4754
case 'up':
48-
newValue -= deltaCoords[1];
55+
newValue -= unitHandle.fromPxToValue(deltaCoords[1]);
4956
break;
5057
case 'down':
51-
newValue += deltaCoords[1];
58+
newValue += unitHandle.fromPxToValue(deltaCoords[1]);
5259
break;
5360
}
5461

5562
onValueChange(event, {
56-
value: Math.round(newValue),
63+
value: unitHandle.roundValue(newValue),
64+
unit: unitHandle.name,
5765
...(isTouchEvent(event)
5866
? { event, type: EVENTS.touch }
5967
: { event, type: EVENTS.mouse }),
@@ -95,8 +103,8 @@ export function useMouseHandler(params: UseMouseHandlerParams) {
95103
params.onDragEnd?.(
96104
event,
97105
isTouchEvent(event)
98-
? { event, type: EVENTS.touch }
99-
: { event, type: EVENTS.mouse }
106+
? { event, type: EVENTS.touch, unit: unitHandle.name }
107+
: { event, type: EVENTS.mouse, unit: unitHandle.name }
100108
);
101109
});
102110

@@ -127,8 +135,8 @@ export function useMouseHandler(params: UseMouseHandlerParams) {
127135
params.onDragStart?.(
128136
event,
129137
isTouchEvent(event)
130-
? { event, type: EVENTS.touch }
131-
: { event, type: EVENTS.mouse }
138+
? { event, type: EVENTS.touch, unit: unitHandle.name }
139+
: { event, type: EVENTS.mouse, unit: unitHandle.name }
132140
);
133141
});
134142

0 commit comments

Comments
 (0)