@lenso/primitives 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/resize-handle/index.d.ts +43 -0
- package/dist/resize-handle/index.js +179 -0
- package/dist/sidebar/index.js +1 -1
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @lenso/primitives
|
|
2
2
|
|
|
3
|
-
`@lenso/primitives
|
|
3
|
+
`@lenso/primitives` contains headless, style-free Product Primitives for Lenso UI. The public surface includes the composable Sidebar model and a layout-independent Resize Handle:
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
6
|
pnpm add @lenso/primitives @base-ui/react react
|
|
@@ -22,6 +22,8 @@ export function AppShell() {
|
|
|
22
22
|
|
|
23
23
|
`Sidebar` supports controlled and uncontrolled state, independent left and right panels, nested Roots, stable targeting, keyboard dismissal, and focus restoration. Its public API exposes behavior and semantic DOM only; it does not impose StyleX, theme tokens, default CSS, an animation runtime, persistence, or routing.
|
|
24
24
|
|
|
25
|
+
`ResizeHandle` provides bounded pointer and keyboard resizing, pointer capture, optional collapse toggling, and WAI-ARIA window-splitter semantics. Consumers keep ownership of pane layout and persistence.
|
|
26
|
+
|
|
25
27
|
Use `@lenso/ui/sidebar` when you want the Lenso UI visual adapter. The registry also includes an editable Sidebar Recipe for Consumers that need structural ownership.
|
|
26
28
|
|
|
27
29
|
Source and browser tests live under `packages/primitives/src/sidebar`. See the repository [architecture guide](../../docs/architecture.md) for the dependency boundary and the [contributing guide](../../CONTRIBUTING.md) for changes and release verification.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { useRender } from "@base-ui/react/use-render";
|
|
3
|
+
export type ResizeHandleOrientation = "horizontal" | "vertical";
|
|
4
|
+
export type ResizeHandleChangeReason = "keyboard" | "pointer";
|
|
5
|
+
export type ResizeHandleInputEvent = React.KeyboardEvent<HTMLElement> | React.PointerEvent<HTMLElement>;
|
|
6
|
+
export interface ResizeHandleChangeDetails {
|
|
7
|
+
delta: number;
|
|
8
|
+
event: ResizeHandleInputEvent;
|
|
9
|
+
reason: ResizeHandleChangeReason;
|
|
10
|
+
value: number;
|
|
11
|
+
}
|
|
12
|
+
export type ResizeHandleState = {
|
|
13
|
+
disabled: boolean;
|
|
14
|
+
dragging: boolean;
|
|
15
|
+
orientation: ResizeHandleOrientation;
|
|
16
|
+
value: number;
|
|
17
|
+
};
|
|
18
|
+
type ResizeHandleElementProps = Omit<useRender.ComponentProps<"div", ResizeHandleState>, "aria-controls" | "aria-disabled" | "aria-orientation" | "aria-valuemax" | "aria-valuemin" | "aria-valuenow" | "className" | "ref" | "role" | "tabIndex">;
|
|
19
|
+
export type ResizeHandleProps = ResizeHandleElementProps & {
|
|
20
|
+
"aria-label"?: string;
|
|
21
|
+
"aria-labelledby"?: string;
|
|
22
|
+
"aria-controls": string;
|
|
23
|
+
className?: string | ((state: ResizeHandleState) => string | undefined);
|
|
24
|
+
disabled?: boolean;
|
|
25
|
+
inverted?: boolean;
|
|
26
|
+
max: number;
|
|
27
|
+
min: number;
|
|
28
|
+
onCollapseToggle?: (event: React.KeyboardEvent<HTMLElement> | React.MouseEvent<HTMLElement>) => void;
|
|
29
|
+
onValueChange: (value: number, details: ResizeHandleChangeDetails) => void;
|
|
30
|
+
onValueCommit?: (value: number, details: ResizeHandleChangeDetails) => void;
|
|
31
|
+
orientation?: ResizeHandleOrientation;
|
|
32
|
+
ref?: React.Ref<HTMLElement>;
|
|
33
|
+
step?: number;
|
|
34
|
+
value: number;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* A layout-independent, focusable window splitter.
|
|
38
|
+
*
|
|
39
|
+
* Consumers own pane layout and persistence. The handle owns pointer capture,
|
|
40
|
+
* keyboard adjustment, bounded values, and WAI-ARIA separator semantics.
|
|
41
|
+
*/
|
|
42
|
+
export declare function ResizeHandle({ className, disabled, inverted, max, min, onCollapseToggle, onValueChange, onValueCommit, orientation, ref, render, step, value, ...props }: ResizeHandleProps): React.ReactElement<unknown, string | React.JSXElementConstructor<any>>;
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { mergeProps } from "@base-ui/react/merge-props";
|
|
4
|
+
import { useRender } from "@base-ui/react/use-render";
|
|
5
|
+
//#region src/resize-handle/index.tsx
|
|
6
|
+
function clamp(value, min, max) {
|
|
7
|
+
return Math.min(max, Math.max(min, value));
|
|
8
|
+
}
|
|
9
|
+
function coordinate(event, orientation) {
|
|
10
|
+
return orientation === "vertical" ? event.clientX : event.clientY;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A layout-independent, focusable window splitter.
|
|
14
|
+
*
|
|
15
|
+
* Consumers own pane layout and persistence. The handle owns pointer capture,
|
|
16
|
+
* keyboard adjustment, bounded values, and WAI-ARIA separator semantics.
|
|
17
|
+
*/
|
|
18
|
+
function ResizeHandle({ className, disabled = false, inverted = false, max, min, onCollapseToggle, onValueChange, onValueCommit, orientation = "vertical", ref, render, step = 16, value, ...props }) {
|
|
19
|
+
const elementRef = React.useRef(null);
|
|
20
|
+
const dragRef = React.useRef(null);
|
|
21
|
+
const restoreDocumentRef = React.useRef(null);
|
|
22
|
+
const suppressClickRef = React.useRef(false);
|
|
23
|
+
const [dragging, setDragging] = React.useState(false);
|
|
24
|
+
const lowerBound = Math.min(min, max);
|
|
25
|
+
const upperBound = Math.max(min, max);
|
|
26
|
+
const currentValue = clamp(value, lowerBound, upperBound);
|
|
27
|
+
const normalizedStep = Math.max(1, Math.abs(step));
|
|
28
|
+
const state = React.useMemo(() => ({
|
|
29
|
+
disabled,
|
|
30
|
+
dragging,
|
|
31
|
+
orientation,
|
|
32
|
+
value: currentValue
|
|
33
|
+
}), [
|
|
34
|
+
currentValue,
|
|
35
|
+
disabled,
|
|
36
|
+
dragging,
|
|
37
|
+
orientation
|
|
38
|
+
]);
|
|
39
|
+
const restoreDocumentInteraction = React.useCallback(() => {
|
|
40
|
+
restoreDocumentRef.current?.();
|
|
41
|
+
restoreDocumentRef.current = null;
|
|
42
|
+
}, []);
|
|
43
|
+
React.useEffect(() => restoreDocumentInteraction, [restoreDocumentInteraction]);
|
|
44
|
+
function createDetails(nextValue, previousValue, reason, event) {
|
|
45
|
+
return {
|
|
46
|
+
delta: nextValue - previousValue,
|
|
47
|
+
event,
|
|
48
|
+
reason,
|
|
49
|
+
value: nextValue
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function changeValue(nextValue, previousValue, reason, event, commit) {
|
|
53
|
+
const boundedValue = clamp(nextValue, lowerBound, upperBound);
|
|
54
|
+
const details = createDetails(boundedValue, previousValue, reason, event);
|
|
55
|
+
if (boundedValue !== previousValue) onValueChange(boundedValue, details);
|
|
56
|
+
if (commit) onValueCommit?.(boundedValue, details);
|
|
57
|
+
return boundedValue;
|
|
58
|
+
}
|
|
59
|
+
function finishPointerDrag(event) {
|
|
60
|
+
const drag = dragRef.current;
|
|
61
|
+
if (!drag || drag.pointerId !== event.pointerId) return;
|
|
62
|
+
dragRef.current = null;
|
|
63
|
+
suppressClickRef.current = drag.moved;
|
|
64
|
+
setDragging(false);
|
|
65
|
+
restoreDocumentInteraction();
|
|
66
|
+
const details = createDetails(drag.lastValue, drag.startValue, "pointer", event);
|
|
67
|
+
onValueCommit?.(drag.lastValue, details);
|
|
68
|
+
if (drag.target.hasPointerCapture(event.pointerId)) drag.target.releasePointerCapture(event.pointerId);
|
|
69
|
+
}
|
|
70
|
+
const internalProps = {
|
|
71
|
+
onClick(event) {
|
|
72
|
+
if (disabled) return;
|
|
73
|
+
if (suppressClickRef.current) {
|
|
74
|
+
suppressClickRef.current = false;
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
onCollapseToggle?.(event);
|
|
78
|
+
},
|
|
79
|
+
onKeyDown(event) {
|
|
80
|
+
if (disabled) return;
|
|
81
|
+
let nextValue;
|
|
82
|
+
const direction = inverted ? -1 : 1;
|
|
83
|
+
if (orientation === "vertical") {
|
|
84
|
+
if (event.key === "ArrowLeft") nextValue = currentValue - normalizedStep * direction;
|
|
85
|
+
if (event.key === "ArrowRight") nextValue = currentValue + normalizedStep * direction;
|
|
86
|
+
} else {
|
|
87
|
+
if (event.key === "ArrowUp") nextValue = currentValue - normalizedStep * direction;
|
|
88
|
+
if (event.key === "ArrowDown") nextValue = currentValue + normalizedStep * direction;
|
|
89
|
+
}
|
|
90
|
+
if (event.key === "Home") nextValue = lowerBound;
|
|
91
|
+
if (event.key === "End") nextValue = upperBound;
|
|
92
|
+
if (event.key === "Enter" && onCollapseToggle) {
|
|
93
|
+
event.preventDefault();
|
|
94
|
+
onCollapseToggle(event);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (nextValue === void 0) return;
|
|
98
|
+
event.preventDefault();
|
|
99
|
+
changeValue(nextValue, currentValue, "keyboard", event, true);
|
|
100
|
+
},
|
|
101
|
+
onLostPointerCapture(event) {
|
|
102
|
+
finishPointerDrag(event);
|
|
103
|
+
},
|
|
104
|
+
onPointerCancel(event) {
|
|
105
|
+
finishPointerDrag(event);
|
|
106
|
+
},
|
|
107
|
+
onPointerDown(event) {
|
|
108
|
+
if (disabled || event.button !== 0) return;
|
|
109
|
+
event.preventDefault();
|
|
110
|
+
event.currentTarget.focus({ preventScroll: true });
|
|
111
|
+
dragRef.current = {
|
|
112
|
+
lastValue: currentValue,
|
|
113
|
+
moved: false,
|
|
114
|
+
pointerId: event.pointerId,
|
|
115
|
+
startCoordinate: coordinate(event, orientation),
|
|
116
|
+
startValue: currentValue,
|
|
117
|
+
target: event.currentTarget
|
|
118
|
+
};
|
|
119
|
+
setDragging(true);
|
|
120
|
+
try {
|
|
121
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
122
|
+
} catch {}
|
|
123
|
+
const body = event.currentTarget.ownerDocument.body;
|
|
124
|
+
const previousCursor = body.style.cursor;
|
|
125
|
+
const previousUserSelect = body.style.userSelect;
|
|
126
|
+
body.style.cursor = orientation === "vertical" ? "col-resize" : "row-resize";
|
|
127
|
+
body.style.userSelect = "none";
|
|
128
|
+
restoreDocumentRef.current = () => {
|
|
129
|
+
body.style.cursor = previousCursor;
|
|
130
|
+
body.style.userSelect = previousUserSelect;
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
onPointerMove(event) {
|
|
134
|
+
const drag = dragRef.current;
|
|
135
|
+
if (!drag || drag.pointerId !== event.pointerId) return;
|
|
136
|
+
event.preventDefault();
|
|
137
|
+
const physicalDelta = coordinate(event, orientation) - drag.startCoordinate;
|
|
138
|
+
drag.moved ||= Math.abs(physicalDelta) >= 3;
|
|
139
|
+
const valueDelta = physicalDelta * (inverted ? -1 : 1);
|
|
140
|
+
const nextValue = clamp(drag.startValue + valueDelta, lowerBound, upperBound);
|
|
141
|
+
if (nextValue === drag.lastValue) return;
|
|
142
|
+
const previousValue = drag.lastValue;
|
|
143
|
+
drag.lastValue = nextValue;
|
|
144
|
+
changeValue(nextValue, previousValue, "pointer", event, false);
|
|
145
|
+
},
|
|
146
|
+
onPointerUp(event) {
|
|
147
|
+
finishPointerDrag(event);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
const protectedProps = {
|
|
151
|
+
"aria-controls": props["aria-controls"],
|
|
152
|
+
"aria-disabled": disabled || void 0,
|
|
153
|
+
"aria-orientation": orientation,
|
|
154
|
+
"aria-valuemax": upperBound,
|
|
155
|
+
"aria-valuemin": lowerBound,
|
|
156
|
+
"aria-valuenow": currentValue,
|
|
157
|
+
role: "separator",
|
|
158
|
+
tabIndex: disabled ? void 0 : 0
|
|
159
|
+
};
|
|
160
|
+
return useRender({
|
|
161
|
+
defaultTagName: "div",
|
|
162
|
+
props: {
|
|
163
|
+
...mergeProps(internalProps, props, protectedProps),
|
|
164
|
+
className: typeof className === "function" ? className(state) : className,
|
|
165
|
+
"data-slot": "resize-handle"
|
|
166
|
+
},
|
|
167
|
+
ref: ref ? [elementRef, ref] : elementRef,
|
|
168
|
+
render,
|
|
169
|
+
state,
|
|
170
|
+
stateAttributesMapping: {
|
|
171
|
+
disabled: (isDisabled) => isDisabled ? { "data-disabled": "" } : null,
|
|
172
|
+
dragging: (isDragging) => isDragging ? { "data-dragging": "" } : null,
|
|
173
|
+
orientation: (currentOrientation) => ({ "data-orientation": currentOrientation }),
|
|
174
|
+
value: () => null
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
//#endregion
|
|
179
|
+
export { ResizeHandle };
|
package/dist/sidebar/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import * as React from "react";
|
|
3
|
-
import { Button } from "@base-ui/react/button";
|
|
4
3
|
import { mergeProps } from "@base-ui/react/merge-props";
|
|
5
4
|
import { useRender } from "@base-ui/react/use-render";
|
|
5
|
+
import { Button } from "@base-ui/react/button";
|
|
6
6
|
import { jsx } from "react/jsx-runtime";
|
|
7
7
|
//#region src/sidebar/index.tsx
|
|
8
8
|
const SidebarContext = React.createContext(null);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lenso/primitives",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Headless product primitives for Lenso UI.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -14,6 +14,10 @@
|
|
|
14
14
|
],
|
|
15
15
|
"type": "module",
|
|
16
16
|
"exports": {
|
|
17
|
+
"./resize-handle": {
|
|
18
|
+
"types": "./dist/resize-handle/index.d.ts",
|
|
19
|
+
"import": "./dist/resize-handle/index.js"
|
|
20
|
+
},
|
|
17
21
|
"./sidebar": {
|
|
18
22
|
"types": "./dist/sidebar/index.d.ts",
|
|
19
23
|
"import": "./dist/sidebar/index.js"
|