@lattice-ui/popper 0.1.1 → 0.3.1
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/LICENSE +7 -0
- package/package.json +8 -4
- package/src/compute.ts +0 -116
- package/src/index.ts +0 -3
- package/src/observers.ts +0 -57
- package/src/types.ts +0 -32
- package/src/usePopper.ts +0 -130
- package/tsconfig.json +0 -16
- package/tsconfig.typecheck.json +0 -25
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2026 astra-void
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lattice-ui/popper",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "out/init.luau",
|
|
6
6
|
"types": "out/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"out",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
7
11
|
"dependencies": {
|
|
8
|
-
"@lattice-ui/core": "0.
|
|
12
|
+
"@lattice-ui/core": "0.3.1"
|
|
9
13
|
},
|
|
10
14
|
"devDependencies": {
|
|
11
15
|
"@rbxts/react": "17.3.7-ts.1",
|
|
@@ -17,7 +21,7 @@
|
|
|
17
21
|
},
|
|
18
22
|
"scripts": {
|
|
19
23
|
"build": "rbxtsc -p tsconfig.json",
|
|
20
|
-
"
|
|
21
|
-
"
|
|
24
|
+
"typecheck": "tsc -p tsconfig.typecheck.json",
|
|
25
|
+
"watch": "rbxtsc -p tsconfig.json -w"
|
|
22
26
|
}
|
|
23
27
|
}
|
package/src/compute.ts
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import type { ComputePopperInput, ComputePopperResult, PopperPlacement } from "./types";
|
|
2
|
-
|
|
3
|
-
type XY = {
|
|
4
|
-
x: number;
|
|
5
|
-
y: number;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
function getPositionForPlacement(
|
|
9
|
-
placement: PopperPlacement,
|
|
10
|
-
anchorPosition: Vector2,
|
|
11
|
-
anchorSize: Vector2,
|
|
12
|
-
contentSize: Vector2,
|
|
13
|
-
offset: Vector2,
|
|
14
|
-
out: XY,
|
|
15
|
-
): XY {
|
|
16
|
-
if (placement === "top") {
|
|
17
|
-
out.x = anchorPosition.X + anchorSize.X / 2 - contentSize.X / 2 + offset.X;
|
|
18
|
-
out.y = anchorPosition.Y - contentSize.Y + offset.Y;
|
|
19
|
-
return out;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (placement === "left") {
|
|
23
|
-
out.x = anchorPosition.X - contentSize.X + offset.X;
|
|
24
|
-
out.y = anchorPosition.Y + anchorSize.Y / 2 - contentSize.Y / 2 + offset.Y;
|
|
25
|
-
return out;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (placement === "right") {
|
|
29
|
-
out.x = anchorPosition.X + anchorSize.X + offset.X;
|
|
30
|
-
out.y = anchorPosition.Y + anchorSize.Y / 2 - contentSize.Y / 2 + offset.Y;
|
|
31
|
-
return out;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
out.x = anchorPosition.X + anchorSize.X / 2 - contentSize.X / 2 + offset.X;
|
|
35
|
-
out.y = anchorPosition.Y + anchorSize.Y + offset.Y;
|
|
36
|
-
return out;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function overflowsViewport(
|
|
40
|
-
positionX: number,
|
|
41
|
-
positionY: number,
|
|
42
|
-
contentSize: Vector2,
|
|
43
|
-
viewportSize: Vector2,
|
|
44
|
-
padding: number,
|
|
45
|
-
) {
|
|
46
|
-
const minX = padding;
|
|
47
|
-
const minY = padding;
|
|
48
|
-
const maxX = viewportSize.X - contentSize.X - padding;
|
|
49
|
-
const maxY = viewportSize.Y - contentSize.Y - padding;
|
|
50
|
-
return positionX < minX || positionX > maxX || positionY < minY || positionY > maxY;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function clampToViewport(
|
|
54
|
-
positionX: number,
|
|
55
|
-
positionY: number,
|
|
56
|
-
contentSize: Vector2,
|
|
57
|
-
viewportSize: Vector2,
|
|
58
|
-
padding: number,
|
|
59
|
-
out: XY,
|
|
60
|
-
): XY {
|
|
61
|
-
const minX = padding;
|
|
62
|
-
const minY = padding;
|
|
63
|
-
const maxX = math.max(minX, viewportSize.X - contentSize.X - padding);
|
|
64
|
-
const maxY = math.max(minY, viewportSize.Y - contentSize.Y - padding);
|
|
65
|
-
|
|
66
|
-
out.x = math.clamp(positionX, minX, maxX);
|
|
67
|
-
out.y = math.clamp(positionY, minY, maxY);
|
|
68
|
-
return out;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function computePopper(input: ComputePopperInput): ComputePopperResult {
|
|
72
|
-
const placement = input.placement ?? "bottom";
|
|
73
|
-
const offset = input.offset ?? new Vector2(0, 0);
|
|
74
|
-
const padding = input.padding ?? 8;
|
|
75
|
-
|
|
76
|
-
const primary = getPositionForPlacement(
|
|
77
|
-
placement,
|
|
78
|
-
input.anchorPosition,
|
|
79
|
-
input.anchorSize,
|
|
80
|
-
input.contentSize,
|
|
81
|
-
offset,
|
|
82
|
-
{ x: 0, y: 0 },
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
const fallbackPlacement: PopperPlacement =
|
|
86
|
-
placement === "top" ? "bottom" : placement === "bottom" ? "top" : placement === "left" ? "right" : "left";
|
|
87
|
-
|
|
88
|
-
let resolvedPlacement = placement;
|
|
89
|
-
let resolvedX = primary.x;
|
|
90
|
-
let resolvedY = primary.y;
|
|
91
|
-
|
|
92
|
-
if (overflowsViewport(primary.x, primary.y, input.contentSize, input.viewportSize, padding)) {
|
|
93
|
-
const fallback = getPositionForPlacement(
|
|
94
|
-
fallbackPlacement,
|
|
95
|
-
input.anchorPosition,
|
|
96
|
-
input.anchorSize,
|
|
97
|
-
input.contentSize,
|
|
98
|
-
offset,
|
|
99
|
-
{ x: 0, y: 0 },
|
|
100
|
-
);
|
|
101
|
-
|
|
102
|
-
if (!overflowsViewport(fallback.x, fallback.y, input.contentSize, input.viewportSize, padding)) {
|
|
103
|
-
resolvedPlacement = fallbackPlacement;
|
|
104
|
-
resolvedX = fallback.x;
|
|
105
|
-
resolvedY = fallback.y;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const clamped = clampToViewport(resolvedX, resolvedY, input.contentSize, input.viewportSize, padding, { x: 0, y: 0 });
|
|
110
|
-
|
|
111
|
-
return {
|
|
112
|
-
position: UDim2.fromOffset(clamped.x, clamped.y),
|
|
113
|
-
anchorPoint: new Vector2(0, 0),
|
|
114
|
-
placement: resolvedPlacement,
|
|
115
|
-
};
|
|
116
|
-
}
|
package/src/index.ts
DELETED
package/src/observers.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
const WorkspaceService = game.GetService("Workspace");
|
|
2
|
-
|
|
3
|
-
export type ObserverUnsubscribe = () => void;
|
|
4
|
-
|
|
5
|
-
function subscribeGuiObjectLayout(guiObject: GuiObject, onChange: () => void): ObserverUnsubscribe {
|
|
6
|
-
const positionConnection = guiObject.GetPropertyChangedSignal("AbsolutePosition").Connect(() => {
|
|
7
|
-
onChange();
|
|
8
|
-
});
|
|
9
|
-
const sizeConnection = guiObject.GetPropertyChangedSignal("AbsoluteSize").Connect(() => {
|
|
10
|
-
onChange();
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
return () => {
|
|
14
|
-
positionConnection.Disconnect();
|
|
15
|
-
sizeConnection.Disconnect();
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function subscribeAnchor(anchor: GuiObject, onChange: () => void): ObserverUnsubscribe {
|
|
20
|
-
return subscribeGuiObjectLayout(anchor, onChange);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function subscribeContent(content: GuiObject, onChange: () => void): ObserverUnsubscribe {
|
|
24
|
-
return subscribeGuiObjectLayout(content, onChange);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function subscribeViewport(onChange: () => void): ObserverUnsubscribe {
|
|
28
|
-
let viewportConnection: RBXScriptConnection | undefined;
|
|
29
|
-
|
|
30
|
-
const reconnectViewportConnection = () => {
|
|
31
|
-
if (viewportConnection) {
|
|
32
|
-
viewportConnection.Disconnect();
|
|
33
|
-
viewportConnection = undefined;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const currentCamera = WorkspaceService.CurrentCamera;
|
|
37
|
-
if (currentCamera) {
|
|
38
|
-
viewportConnection = currentCamera.GetPropertyChangedSignal("ViewportSize").Connect(() => {
|
|
39
|
-
onChange();
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
reconnectViewportConnection();
|
|
45
|
-
const cameraConnection = WorkspaceService.GetPropertyChangedSignal("CurrentCamera").Connect(() => {
|
|
46
|
-
reconnectViewportConnection();
|
|
47
|
-
onChange();
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
return () => {
|
|
51
|
-
if (viewportConnection) {
|
|
52
|
-
viewportConnection.Disconnect();
|
|
53
|
-
viewportConnection = undefined;
|
|
54
|
-
}
|
|
55
|
-
cameraConnection.Disconnect();
|
|
56
|
-
};
|
|
57
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type React from "@rbxts/react";
|
|
2
|
-
|
|
3
|
-
export type PopperPlacement = "top" | "bottom" | "left" | "right";
|
|
4
|
-
|
|
5
|
-
export type ComputePopperInput = {
|
|
6
|
-
anchorPosition: Vector2;
|
|
7
|
-
anchorSize: Vector2;
|
|
8
|
-
contentSize: Vector2;
|
|
9
|
-
viewportSize: Vector2;
|
|
10
|
-
placement?: PopperPlacement;
|
|
11
|
-
offset?: Vector2;
|
|
12
|
-
padding?: number;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export type ComputePopperResult = {
|
|
16
|
-
position: UDim2;
|
|
17
|
-
anchorPoint: Vector2;
|
|
18
|
-
placement: PopperPlacement;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export type UsePopperOptions = {
|
|
22
|
-
anchorRef: React.RefObject<GuiObject> | React.MutableRefObject<GuiObject | undefined>;
|
|
23
|
-
contentRef: React.RefObject<GuiObject> | React.MutableRefObject<GuiObject | undefined>;
|
|
24
|
-
placement?: PopperPlacement;
|
|
25
|
-
offset?: Vector2;
|
|
26
|
-
padding?: number;
|
|
27
|
-
enabled?: boolean;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export type UsePopperResult = ComputePopperResult & {
|
|
31
|
-
update: () => void;
|
|
32
|
-
};
|
package/src/usePopper.ts
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import { React } from "@lattice-ui/core";
|
|
2
|
-
import { computePopper } from "./compute";
|
|
3
|
-
import { subscribeAnchor, subscribeContent, subscribeViewport } from "./observers";
|
|
4
|
-
import type { ComputePopperResult, UsePopperOptions, UsePopperResult } from "./types";
|
|
5
|
-
|
|
6
|
-
const WorkspaceService = game.GetService("Workspace");
|
|
7
|
-
const RunService = game.GetService("RunService");
|
|
8
|
-
|
|
9
|
-
function readGuiRef(ref: UsePopperOptions["anchorRef"] | UsePopperOptions["contentRef"]): GuiObject | undefined {
|
|
10
|
-
return ref.current;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function getDefaultComputedResult(placement: UsePopperOptions["placement"]): ComputePopperResult {
|
|
14
|
-
return {
|
|
15
|
-
anchorPoint: new Vector2(0, 0),
|
|
16
|
-
placement: placement ?? "bottom",
|
|
17
|
-
position: UDim2.fromOffset(0, 0),
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function areResultsEqual(a: ComputePopperResult, b: ComputePopperResult) {
|
|
22
|
-
return (
|
|
23
|
-
a.placement === b.placement &&
|
|
24
|
-
a.anchorPoint.X === b.anchorPoint.X &&
|
|
25
|
-
a.anchorPoint.Y === b.anchorPoint.Y &&
|
|
26
|
-
a.position.X.Scale === b.position.X.Scale &&
|
|
27
|
-
a.position.X.Offset === b.position.X.Offset &&
|
|
28
|
-
a.position.Y.Scale === b.position.Y.Scale &&
|
|
29
|
-
a.position.Y.Offset === b.position.Y.Offset
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function usePopper(options: UsePopperOptions): UsePopperResult {
|
|
34
|
-
const enabled = options.enabled ?? true;
|
|
35
|
-
const [computedResult, setComputedResult] = React.useState<ComputePopperResult>(() =>
|
|
36
|
-
getDefaultComputedResult(options.placement),
|
|
37
|
-
);
|
|
38
|
-
|
|
39
|
-
const update = React.useCallback(() => {
|
|
40
|
-
if (!enabled) {
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const anchor = readGuiRef(options.anchorRef);
|
|
45
|
-
const content = readGuiRef(options.contentRef);
|
|
46
|
-
if (!anchor || !content) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const viewportSize = WorkspaceService.CurrentCamera?.ViewportSize ?? new Vector2(1920, 1080);
|
|
51
|
-
const nextResult = computePopper({
|
|
52
|
-
anchorPosition: anchor.AbsolutePosition,
|
|
53
|
-
anchorSize: anchor.AbsoluteSize,
|
|
54
|
-
contentSize: content.AbsoluteSize,
|
|
55
|
-
offset: options.offset,
|
|
56
|
-
padding: options.padding,
|
|
57
|
-
placement: options.placement,
|
|
58
|
-
viewportSize,
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
setComputedResult((currentResult) => (areResultsEqual(currentResult, nextResult) ? currentResult : nextResult));
|
|
62
|
-
}, [enabled, options.anchorRef, options.contentRef, options.offset, options.padding, options.placement]);
|
|
63
|
-
|
|
64
|
-
React.useEffect(() => {
|
|
65
|
-
update();
|
|
66
|
-
}, [update]);
|
|
67
|
-
|
|
68
|
-
React.useEffect(() => {
|
|
69
|
-
if (!enabled) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
let disconnectAnchor: (() => void) | undefined;
|
|
74
|
-
let disconnectContent: (() => void) | undefined;
|
|
75
|
-
let disconnectViewport: (() => void) | undefined;
|
|
76
|
-
let waitForRefsConnection: RBXScriptConnection | undefined;
|
|
77
|
-
let attached = false;
|
|
78
|
-
|
|
79
|
-
const attachObservers = () => {
|
|
80
|
-
if (attached) {
|
|
81
|
-
return true;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const anchor = readGuiRef(options.anchorRef);
|
|
85
|
-
const content = readGuiRef(options.contentRef);
|
|
86
|
-
if (!anchor || !content) {
|
|
87
|
-
return false;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
disconnectAnchor = subscribeAnchor(anchor, update);
|
|
91
|
-
disconnectContent = subscribeContent(content, update);
|
|
92
|
-
disconnectViewport = subscribeViewport(update);
|
|
93
|
-
attached = true;
|
|
94
|
-
return true;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
if (!attachObservers()) {
|
|
98
|
-
waitForRefsConnection = RunService.Heartbeat.Connect(() => {
|
|
99
|
-
if (attachObservers()) {
|
|
100
|
-
if (waitForRefsConnection) {
|
|
101
|
-
waitForRefsConnection.Disconnect();
|
|
102
|
-
waitForRefsConnection = undefined;
|
|
103
|
-
}
|
|
104
|
-
update();
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
} else {
|
|
108
|
-
update();
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return () => {
|
|
112
|
-
if (waitForRefsConnection) {
|
|
113
|
-
waitForRefsConnection.Disconnect();
|
|
114
|
-
waitForRefsConnection = undefined;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
disconnectAnchor?.();
|
|
118
|
-
disconnectContent?.();
|
|
119
|
-
disconnectViewport?.();
|
|
120
|
-
};
|
|
121
|
-
}, [enabled, options.anchorRef, options.contentRef, update]);
|
|
122
|
-
|
|
123
|
-
return React.useMemo(
|
|
124
|
-
() => ({
|
|
125
|
-
...computedResult,
|
|
126
|
-
update,
|
|
127
|
-
}),
|
|
128
|
-
[computedResult, update],
|
|
129
|
-
);
|
|
130
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.base.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"rootDir": "src",
|
|
5
|
-
"outDir": "out",
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"typeRoots": [
|
|
8
|
-
"./node_modules/@rbxts",
|
|
9
|
-
"../../node_modules/@rbxts",
|
|
10
|
-
"./node_modules/@lattice-ui",
|
|
11
|
-
"../../node_modules/@lattice-ui"
|
|
12
|
-
],
|
|
13
|
-
"types": ["types", "compiler-types"]
|
|
14
|
-
},
|
|
15
|
-
"include": ["src"]
|
|
16
|
-
}
|
package/tsconfig.typecheck.json
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"noEmit": true,
|
|
5
|
-
"baseUrl": "..",
|
|
6
|
-
"rootDir": "..",
|
|
7
|
-
"paths": {
|
|
8
|
-
"@lattice-ui/checkbox": ["checkbox/src/index.ts"],
|
|
9
|
-
"@lattice-ui/core": ["core/src/index.ts"],
|
|
10
|
-
"@lattice-ui/dialog": ["dialog/src/index.ts"],
|
|
11
|
-
"@lattice-ui/focus": ["focus/src/index.ts"],
|
|
12
|
-
"@lattice-ui/layer": ["layer/src/index.ts"],
|
|
13
|
-
"@lattice-ui/menu": ["menu/src/index.ts"],
|
|
14
|
-
"@lattice-ui/popover": ["popover/src/index.ts"],
|
|
15
|
-
"@lattice-ui/popper": ["popper/src/index.ts"],
|
|
16
|
-
"@lattice-ui/radio-group": ["radio-group/src/index.ts"],
|
|
17
|
-
"@lattice-ui/style": ["style/src/index.ts"],
|
|
18
|
-
"@lattice-ui/switch": ["switch/src/index.ts"],
|
|
19
|
-
"@lattice-ui/system": ["system/src/index.ts"],
|
|
20
|
-
"@lattice-ui/tabs": ["tabs/src/index.ts"],
|
|
21
|
-
"@lattice-ui/toggle-group": ["toggle-group/src/index.ts"],
|
|
22
|
-
"@lattice-ui/tooltip": ["tooltip/src/index.ts"]
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|