react-morpheus 0.1.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/LICENSE +21 -0
- package/README.md +42 -0
- package/dist/index.d.ts +172 -0
- package/dist/index.js +574 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shivek Khurana
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# React Morpheus
|
|
2
|
+
|
|
3
|
+
A controlled React component for morphing one UI state into another.
|
|
4
|
+
|
|
5
|
+
`Morpheus` does not manage its own open state. Keep the `expanded` boolean in
|
|
6
|
+
your app state, render your trigger wherever it belongs, and pass the same
|
|
7
|
+
source content to `collapsedContent` so Morpheus can measure and morph from it.
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
import { Morpheus, MorphAnchor } from "react-morpheus";
|
|
11
|
+
|
|
12
|
+
function Example() {
|
|
13
|
+
const [expanded, setExpanded] = useState(false);
|
|
14
|
+
const source = (
|
|
15
|
+
<button type="button" onClick={() => setExpanded(true)}>
|
|
16
|
+
Open
|
|
17
|
+
</button>
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<>
|
|
22
|
+
{source}
|
|
23
|
+
<Morpheus
|
|
24
|
+
direction="bottom"
|
|
25
|
+
anchor={MorphAnchor.TopMiddle}
|
|
26
|
+
expanded={expanded}
|
|
27
|
+
onClose={() => setExpanded(false)}
|
|
28
|
+
collapsedContent={source}
|
|
29
|
+
expandedContent={<div>Expanded content</div>}
|
|
30
|
+
/>
|
|
31
|
+
</>
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Scripts
|
|
37
|
+
|
|
38
|
+
- `bun run build` builds declarations and bundled ESM output.
|
|
39
|
+
- `bun run tag` creates an annotated git tag for the current package version.
|
|
40
|
+
- `bun run tag:push` pushes tags to `origin`.
|
|
41
|
+
- `bun run deploy:npm` builds and publishes to npm.
|
|
42
|
+
- `bun run release:npm` builds, creates the version tag, and publishes to npm.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactNode, RefObject } from 'react';
|
|
3
|
+
import { Transition } from 'framer-motion';
|
|
4
|
+
|
|
5
|
+
type MorphDirection = "top" | "right" | "bottom" | "left";
|
|
6
|
+
declare enum MorphAnchor {
|
|
7
|
+
LeftTop = "left-top",
|
|
8
|
+
LeftMiddle = "left-middle",
|
|
9
|
+
LeftBottom = "left-bottom",
|
|
10
|
+
TopMiddle = "top-middle",
|
|
11
|
+
MiddleMiddle = "middle-middle",
|
|
12
|
+
RightTop = "right-top",
|
|
13
|
+
RightMiddle = "right-middle",
|
|
14
|
+
RightBottom = "right-bottom",
|
|
15
|
+
BottomMiddle = "bottom-middle"
|
|
16
|
+
}
|
|
17
|
+
type PanelSize = {
|
|
18
|
+
width: number;
|
|
19
|
+
height: number;
|
|
20
|
+
};
|
|
21
|
+
type ContentOffset = {
|
|
22
|
+
x: number;
|
|
23
|
+
y: number;
|
|
24
|
+
};
|
|
25
|
+
type PanelPosition = {
|
|
26
|
+
left: number;
|
|
27
|
+
top: number;
|
|
28
|
+
};
|
|
29
|
+
type PanelVisualStyle = {
|
|
30
|
+
backgroundColor: string;
|
|
31
|
+
borderRadius: string;
|
|
32
|
+
};
|
|
33
|
+
type MorphSpringPreset = "balanced" | "snappy" | "smooth" | "wobbly" | "heavy";
|
|
34
|
+
type MorphMeasuredSurface = {
|
|
35
|
+
measured: boolean;
|
|
36
|
+
size: PanelSize;
|
|
37
|
+
visualStyle: PanelVisualStyle;
|
|
38
|
+
};
|
|
39
|
+
type MorphSurfaceSnapshot = {
|
|
40
|
+
size: PanelSize;
|
|
41
|
+
visualStyle: PanelVisualStyle;
|
|
42
|
+
};
|
|
43
|
+
type MorphMeasurements = {
|
|
44
|
+
collapsedRef: RefObject<HTMLDivElement | null>;
|
|
45
|
+
expandedRef: RefObject<HTMLDivElement | null>;
|
|
46
|
+
collapsed: MorphMeasuredSurface;
|
|
47
|
+
expanded: MorphMeasuredSurface;
|
|
48
|
+
ready: boolean;
|
|
49
|
+
};
|
|
50
|
+
type MorphMotionInput = {
|
|
51
|
+
expanded: boolean;
|
|
52
|
+
direction: MorphDirection;
|
|
53
|
+
anchor?: MorphAnchor | undefined;
|
|
54
|
+
spring: Transition;
|
|
55
|
+
animationEnabled: boolean;
|
|
56
|
+
collapsed: MorphMeasuredSurface;
|
|
57
|
+
expandedSurface: MorphMeasuredSurface;
|
|
58
|
+
};
|
|
59
|
+
type MorphMotionState = {
|
|
60
|
+
activeSpring: Transition;
|
|
61
|
+
anchorPosition: PanelPosition;
|
|
62
|
+
animatedSize: PanelSize;
|
|
63
|
+
collapsedLayerSize: PanelSize;
|
|
64
|
+
collapsedToExpandedScale: ContentOffset;
|
|
65
|
+
expandedLayerSize: PanelSize;
|
|
66
|
+
sourceGrowthScale: ContentOffset;
|
|
67
|
+
sourceOpacityTransition: Transition;
|
|
68
|
+
sourcePosition: PanelPosition;
|
|
69
|
+
sourceTransition: Transition;
|
|
70
|
+
targetOpacityTransition: Transition;
|
|
71
|
+
targetPosition: PanelPosition;
|
|
72
|
+
targetTransition: Transition;
|
|
73
|
+
transformOrigin: string;
|
|
74
|
+
visualStyle: PanelVisualStyle;
|
|
75
|
+
};
|
|
76
|
+
type MorphProps = {
|
|
77
|
+
direction: MorphDirection;
|
|
78
|
+
anchor?: MorphAnchor | undefined;
|
|
79
|
+
expanded: boolean;
|
|
80
|
+
onOpen?: (() => void) | undefined;
|
|
81
|
+
onClose?: (() => void) | undefined;
|
|
82
|
+
collapsedContent: ReactNode;
|
|
83
|
+
expandedContent: ReactNode;
|
|
84
|
+
className?: string;
|
|
85
|
+
overlayColor?: string;
|
|
86
|
+
overlayOpacity?: number;
|
|
87
|
+
overlayBlur?: number;
|
|
88
|
+
spring?: Transition;
|
|
89
|
+
};
|
|
90
|
+
type MorphOverlayProps = {
|
|
91
|
+
expanded: boolean;
|
|
92
|
+
onClose?: (() => void) | undefined;
|
|
93
|
+
color: string;
|
|
94
|
+
opacity: number;
|
|
95
|
+
blur: number;
|
|
96
|
+
};
|
|
97
|
+
type MorphMeasurementNodesProps = {
|
|
98
|
+
collapsedRef: RefObject<HTMLDivElement | null>;
|
|
99
|
+
expandedRef: RefObject<HTMLDivElement | null>;
|
|
100
|
+
collapsedContent: ReactNode;
|
|
101
|
+
expandedContent: ReactNode;
|
|
102
|
+
};
|
|
103
|
+
type MorphShellProps = {
|
|
104
|
+
expanded: boolean;
|
|
105
|
+
position: PanelPosition;
|
|
106
|
+
animatedSize: PanelSize;
|
|
107
|
+
visualStyle: PanelVisualStyle;
|
|
108
|
+
overflowVisible: boolean;
|
|
109
|
+
spring: Transition;
|
|
110
|
+
onMorphComplete: () => void;
|
|
111
|
+
onMorphStart: () => void;
|
|
112
|
+
children: ReactNode;
|
|
113
|
+
};
|
|
114
|
+
type MorphContentLayersProps = {
|
|
115
|
+
expanded: boolean;
|
|
116
|
+
collapsedContent: ReactNode;
|
|
117
|
+
expandedContent: ReactNode;
|
|
118
|
+
collapsedLayerSize: PanelSize;
|
|
119
|
+
expandedLayerSize: PanelSize;
|
|
120
|
+
sourcePosition: PanelPosition;
|
|
121
|
+
targetPosition: PanelPosition;
|
|
122
|
+
sourceGrowthScale: ContentOffset;
|
|
123
|
+
collapsedToExpandedScale: ContentOffset;
|
|
124
|
+
transformOrigin: string;
|
|
125
|
+
sourceTransition: Transition;
|
|
126
|
+
sourceOpacityTransition: Transition;
|
|
127
|
+
targetTransition: Transition;
|
|
128
|
+
targetOpacityTransition: Transition;
|
|
129
|
+
onOpen?: (() => void) | undefined;
|
|
130
|
+
};
|
|
131
|
+
type MorphLayerInteractivityProps = {
|
|
132
|
+
"aria-hidden": boolean;
|
|
133
|
+
inert?: boolean;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
declare function Morpheus({ direction, anchor, expanded, onOpen, onClose, collapsedContent, expandedContent, className, overlayColor, overlayOpacity, overlayBlur, spring, }: MorphProps): react.JSX.Element;
|
|
137
|
+
|
|
138
|
+
declare const defaultAnchorByDirection: Record<MorphDirection, MorphAnchor>;
|
|
139
|
+
declare const morphSpringPresets: {
|
|
140
|
+
balanced: {
|
|
141
|
+
type: "spring";
|
|
142
|
+
stiffness: number;
|
|
143
|
+
damping: number;
|
|
144
|
+
mass: number;
|
|
145
|
+
};
|
|
146
|
+
snappy: {
|
|
147
|
+
type: "spring";
|
|
148
|
+
stiffness: number;
|
|
149
|
+
damping: number;
|
|
150
|
+
mass: number;
|
|
151
|
+
};
|
|
152
|
+
smooth: {
|
|
153
|
+
type: "spring";
|
|
154
|
+
stiffness: number;
|
|
155
|
+
damping: number;
|
|
156
|
+
mass: number;
|
|
157
|
+
};
|
|
158
|
+
wobbly: {
|
|
159
|
+
type: "spring";
|
|
160
|
+
stiffness: number;
|
|
161
|
+
damping: number;
|
|
162
|
+
mass: number;
|
|
163
|
+
};
|
|
164
|
+
heavy: {
|
|
165
|
+
type: "spring";
|
|
166
|
+
stiffness: number;
|
|
167
|
+
damping: number;
|
|
168
|
+
mass: number;
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
export { type ContentOffset, MorphAnchor, type MorphContentLayersProps, type MorphDirection, type MorphLayerInteractivityProps, type MorphMeasuredSurface, type MorphMeasurementNodesProps, type MorphMeasurements, type MorphMotionInput, type MorphMotionState, type MorphOverlayProps, type MorphProps, type MorphShellProps, type MorphSpringPreset, type MorphSurfaceSnapshot, Morpheus, type PanelPosition, type PanelSize, type PanelVisualStyle, Morpheus as default, defaultAnchorByDirection, morphSpringPresets };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
// src/component.tsx
|
|
2
|
+
import { motion as motion2 } from "framer-motion";
|
|
3
|
+
import { useEffect, useState as useState2 } from "react";
|
|
4
|
+
|
|
5
|
+
// src/measurement.ts
|
|
6
|
+
import { useLayoutEffect, useRef, useState } from "react";
|
|
7
|
+
var defaultVisualStyle = {
|
|
8
|
+
backgroundColor: "transparent",
|
|
9
|
+
borderRadius: "0px"
|
|
10
|
+
};
|
|
11
|
+
function createRAFSchedule(callback) {
|
|
12
|
+
let frame = null;
|
|
13
|
+
const schedule = () => {
|
|
14
|
+
if (frame !== null) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
frame = requestAnimationFrame(() => {
|
|
18
|
+
frame = null;
|
|
19
|
+
callback();
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
const cancel = () => {
|
|
23
|
+
if (frame === null) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
cancelAnimationFrame(frame);
|
|
27
|
+
frame = null;
|
|
28
|
+
};
|
|
29
|
+
return { cancel, schedule };
|
|
30
|
+
}
|
|
31
|
+
function getMeasuredElement(element) {
|
|
32
|
+
return element.firstElementChild instanceof HTMLElement ? element.firstElementChild : element;
|
|
33
|
+
}
|
|
34
|
+
function getSurfaceVisualStyle(element) {
|
|
35
|
+
const computedStyle = getComputedStyle(element);
|
|
36
|
+
return {
|
|
37
|
+
backgroundColor: computedStyle.backgroundColor,
|
|
38
|
+
borderRadius: computedStyle.borderTopLeftRadius
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function measureSurface(element) {
|
|
42
|
+
const measuredElement = getMeasuredElement(element);
|
|
43
|
+
const width = measuredElement.offsetWidth;
|
|
44
|
+
const height = measuredElement.offsetHeight;
|
|
45
|
+
if (width === 0 || height === 0) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
size: { width, height },
|
|
50
|
+
visualStyle: getSurfaceVisualStyle(measuredElement)
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function useMeasuredSize(ref, fallbackSize) {
|
|
54
|
+
const [size, setSize] = useState(fallbackSize);
|
|
55
|
+
const [visualStyle, setVisualStyle] = useState(defaultVisualStyle);
|
|
56
|
+
const [measured, setMeasured] = useState(false);
|
|
57
|
+
useLayoutEffect(() => {
|
|
58
|
+
const element = ref.current;
|
|
59
|
+
if (!element) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const updateSize = () => {
|
|
63
|
+
const snapshot = measureSurface(element);
|
|
64
|
+
if (!snapshot) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
setMeasured(true);
|
|
68
|
+
setSize(
|
|
69
|
+
(current) => current.width === snapshot.size.width && current.height === snapshot.size.height ? current : snapshot.size
|
|
70
|
+
);
|
|
71
|
+
setVisualStyle(
|
|
72
|
+
(current) => current.backgroundColor === snapshot.visualStyle.backgroundColor && current.borderRadius === snapshot.visualStyle.borderRadius ? current : snapshot.visualStyle
|
|
73
|
+
);
|
|
74
|
+
};
|
|
75
|
+
updateSize();
|
|
76
|
+
const scheduledUpdateSize = createRAFSchedule(updateSize);
|
|
77
|
+
const observer = new ResizeObserver(scheduledUpdateSize.schedule);
|
|
78
|
+
observer.observe(element);
|
|
79
|
+
if (element.firstElementChild instanceof HTMLElement) {
|
|
80
|
+
observer.observe(element.firstElementChild);
|
|
81
|
+
}
|
|
82
|
+
return () => {
|
|
83
|
+
scheduledUpdateSize.cancel();
|
|
84
|
+
observer.disconnect();
|
|
85
|
+
};
|
|
86
|
+
}, [fallbackSize.height, fallbackSize.width, ref]);
|
|
87
|
+
return { measured, size, visualStyle };
|
|
88
|
+
}
|
|
89
|
+
function useMorphMeasurements() {
|
|
90
|
+
const collapsedRef = useRef(null);
|
|
91
|
+
const expandedRef = useRef(null);
|
|
92
|
+
const collapsed = useMeasuredSize(collapsedRef, { width: 1, height: 1 });
|
|
93
|
+
const expanded = useMeasuredSize(expandedRef, { width: 1, height: 1 });
|
|
94
|
+
return {
|
|
95
|
+
collapsedRef,
|
|
96
|
+
expandedRef,
|
|
97
|
+
collapsed,
|
|
98
|
+
expanded,
|
|
99
|
+
ready: collapsed.measured && expanded.measured
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/types.ts
|
|
104
|
+
var MorphAnchor = /* @__PURE__ */ ((MorphAnchor2) => {
|
|
105
|
+
MorphAnchor2["LeftTop"] = "left-top";
|
|
106
|
+
MorphAnchor2["LeftMiddle"] = "left-middle";
|
|
107
|
+
MorphAnchor2["LeftBottom"] = "left-bottom";
|
|
108
|
+
MorphAnchor2["TopMiddle"] = "top-middle";
|
|
109
|
+
MorphAnchor2["MiddleMiddle"] = "middle-middle";
|
|
110
|
+
MorphAnchor2["RightTop"] = "right-top";
|
|
111
|
+
MorphAnchor2["RightMiddle"] = "right-middle";
|
|
112
|
+
MorphAnchor2["RightBottom"] = "right-bottom";
|
|
113
|
+
MorphAnchor2["BottomMiddle"] = "bottom-middle";
|
|
114
|
+
return MorphAnchor2;
|
|
115
|
+
})(MorphAnchor || {});
|
|
116
|
+
|
|
117
|
+
// src/motion.ts
|
|
118
|
+
var defaultAnchorByDirection = {
|
|
119
|
+
top: "bottom-middle" /* BottomMiddle */,
|
|
120
|
+
right: "left-middle" /* LeftMiddle */,
|
|
121
|
+
bottom: "top-middle" /* TopMiddle */,
|
|
122
|
+
left: "right-middle" /* RightMiddle */
|
|
123
|
+
};
|
|
124
|
+
var anchorTransformOrigins = {
|
|
125
|
+
["left-top" /* LeftTop */]: "left top",
|
|
126
|
+
["left-middle" /* LeftMiddle */]: "left center",
|
|
127
|
+
["left-bottom" /* LeftBottom */]: "left bottom",
|
|
128
|
+
["top-middle" /* TopMiddle */]: "center top",
|
|
129
|
+
["middle-middle" /* MiddleMiddle */]: "center center",
|
|
130
|
+
["right-top" /* RightTop */]: "right top",
|
|
131
|
+
["right-middle" /* RightMiddle */]: "right center",
|
|
132
|
+
["right-bottom" /* RightBottom */]: "right bottom",
|
|
133
|
+
["bottom-middle" /* BottomMiddle */]: "center bottom"
|
|
134
|
+
};
|
|
135
|
+
var morphSpringPresets = {
|
|
136
|
+
balanced: {
|
|
137
|
+
type: "spring",
|
|
138
|
+
stiffness: 360,
|
|
139
|
+
damping: 34,
|
|
140
|
+
mass: 0.9
|
|
141
|
+
},
|
|
142
|
+
snappy: {
|
|
143
|
+
type: "spring",
|
|
144
|
+
stiffness: 520,
|
|
145
|
+
damping: 42,
|
|
146
|
+
mass: 0.7
|
|
147
|
+
},
|
|
148
|
+
smooth: {
|
|
149
|
+
type: "spring",
|
|
150
|
+
stiffness: 300,
|
|
151
|
+
damping: 32,
|
|
152
|
+
mass: 1
|
|
153
|
+
},
|
|
154
|
+
wobbly: {
|
|
155
|
+
type: "spring",
|
|
156
|
+
stiffness: 260,
|
|
157
|
+
damping: 18,
|
|
158
|
+
mass: 0.9
|
|
159
|
+
},
|
|
160
|
+
heavy: {
|
|
161
|
+
type: "spring",
|
|
162
|
+
stiffness: 220,
|
|
163
|
+
damping: 34,
|
|
164
|
+
mass: 1.6
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
var defaultPanelSpring = morphSpringPresets.balanced;
|
|
168
|
+
var contentFade = {
|
|
169
|
+
duration: 0.22,
|
|
170
|
+
ease: "easeOut"
|
|
171
|
+
};
|
|
172
|
+
var sourceContentFade = {
|
|
173
|
+
duration: 0.12,
|
|
174
|
+
ease: "easeOut"
|
|
175
|
+
};
|
|
176
|
+
var sourceReturnDelay = 0.06;
|
|
177
|
+
var sourceGrowthRatio = 2;
|
|
178
|
+
var instantTransition = { duration: 0 };
|
|
179
|
+
var createSourceContentMotion = (spring) => ({
|
|
180
|
+
opacity: sourceContentFade,
|
|
181
|
+
scaleX: spring,
|
|
182
|
+
scaleY: spring,
|
|
183
|
+
left: spring,
|
|
184
|
+
top: spring
|
|
185
|
+
});
|
|
186
|
+
var createTargetContentMotion = (spring) => ({
|
|
187
|
+
opacity: contentFade,
|
|
188
|
+
scaleX: spring,
|
|
189
|
+
scaleY: spring,
|
|
190
|
+
left: spring,
|
|
191
|
+
top: spring
|
|
192
|
+
});
|
|
193
|
+
var safeScale = (from, to) => to === 0 ? 1 : from / to;
|
|
194
|
+
function getAnchoredPanelPosition(anchor, collapsedSize, targetSize) {
|
|
195
|
+
const alignLeft = 0;
|
|
196
|
+
const alignCenterX = (collapsedSize.width - targetSize.width) / 2;
|
|
197
|
+
const alignRight = collapsedSize.width - targetSize.width;
|
|
198
|
+
const alignTop = 0;
|
|
199
|
+
const alignCenterY = (collapsedSize.height - targetSize.height) / 2;
|
|
200
|
+
const alignBottom = collapsedSize.height - targetSize.height;
|
|
201
|
+
switch (anchor) {
|
|
202
|
+
case "left-top" /* LeftTop */:
|
|
203
|
+
return { left: alignLeft, top: alignTop };
|
|
204
|
+
case "left-middle" /* LeftMiddle */:
|
|
205
|
+
return { left: alignLeft, top: alignCenterY };
|
|
206
|
+
case "left-bottom" /* LeftBottom */:
|
|
207
|
+
return { left: alignLeft, top: alignBottom };
|
|
208
|
+
case "top-middle" /* TopMiddle */:
|
|
209
|
+
return { left: alignCenterX, top: alignTop };
|
|
210
|
+
case "middle-middle" /* MiddleMiddle */:
|
|
211
|
+
return { left: alignCenterX, top: alignCenterY };
|
|
212
|
+
case "right-top" /* RightTop */:
|
|
213
|
+
return { left: alignRight, top: alignTop };
|
|
214
|
+
case "right-middle" /* RightMiddle */:
|
|
215
|
+
return { left: alignRight, top: alignCenterY };
|
|
216
|
+
case "right-bottom" /* RightBottom */:
|
|
217
|
+
return { left: alignRight, top: alignBottom };
|
|
218
|
+
case "bottom-middle" /* BottomMiddle */:
|
|
219
|
+
return { left: alignCenterX, top: alignBottom };
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function invertPosition(position) {
|
|
223
|
+
return {
|
|
224
|
+
left: -(position.left ?? 0),
|
|
225
|
+
top: -(position.top ?? 0)
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function getLayerInteractivityProps(interactive) {
|
|
229
|
+
return interactive ? { "aria-hidden": false } : { "aria-hidden": true, inert: true };
|
|
230
|
+
}
|
|
231
|
+
function getMorphMotionState({
|
|
232
|
+
expanded,
|
|
233
|
+
direction,
|
|
234
|
+
anchor,
|
|
235
|
+
spring,
|
|
236
|
+
animationEnabled,
|
|
237
|
+
collapsed,
|
|
238
|
+
expandedSurface
|
|
239
|
+
}) {
|
|
240
|
+
const collapsedLayerSize = collapsed.size;
|
|
241
|
+
const expandedLayerSize = expandedSurface.size;
|
|
242
|
+
const animatedSize = expanded ? expandedLayerSize : collapsedLayerSize;
|
|
243
|
+
const visualStyle = expanded ? expandedSurface.visualStyle : collapsed.visualStyle;
|
|
244
|
+
const panelAnchor = anchor ?? defaultAnchorByDirection[direction];
|
|
245
|
+
const sourcePanelPosition = { left: 0, top: 0 };
|
|
246
|
+
const targetPanelPosition = getAnchoredPanelPosition(
|
|
247
|
+
panelAnchor,
|
|
248
|
+
collapsedLayerSize,
|
|
249
|
+
expandedLayerSize
|
|
250
|
+
);
|
|
251
|
+
const activeSpring = animationEnabled ? spring : instantTransition;
|
|
252
|
+
return {
|
|
253
|
+
activeSpring,
|
|
254
|
+
anchorPosition: expanded ? targetPanelPosition : sourcePanelPosition,
|
|
255
|
+
animatedSize,
|
|
256
|
+
collapsedLayerSize,
|
|
257
|
+
collapsedToExpandedScale: {
|
|
258
|
+
x: safeScale(collapsedLayerSize.width, expandedLayerSize.width),
|
|
259
|
+
y: safeScale(collapsedLayerSize.height, expandedLayerSize.height)
|
|
260
|
+
},
|
|
261
|
+
expandedLayerSize,
|
|
262
|
+
sourceGrowthScale: {
|
|
263
|
+
x: sourceGrowthRatio,
|
|
264
|
+
y: sourceGrowthRatio
|
|
265
|
+
},
|
|
266
|
+
sourceOpacityTransition: {
|
|
267
|
+
...sourceContentFade,
|
|
268
|
+
delay: expanded ? 0 : sourceReturnDelay
|
|
269
|
+
},
|
|
270
|
+
sourcePosition: expanded ? invertPosition(targetPanelPosition) : sourcePanelPosition,
|
|
271
|
+
sourceTransition: createSourceContentMotion(activeSpring),
|
|
272
|
+
targetOpacityTransition: { ...contentFade, delay: expanded ? 0.03 : 0 },
|
|
273
|
+
targetPosition: expanded ? sourcePanelPosition : targetPanelPosition,
|
|
274
|
+
targetTransition: createTargetContentMotion(activeSpring),
|
|
275
|
+
transformOrigin: anchorTransformOrigins[panelAnchor],
|
|
276
|
+
visualStyle
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// src/overlay.tsx
|
|
281
|
+
import { AnimatePresence, motion } from "framer-motion";
|
|
282
|
+
import { jsx } from "react/jsx-runtime";
|
|
283
|
+
function getOverlayBackgroundColor(color, opacity) {
|
|
284
|
+
if (/^#[0-9a-f]{6}$/i.test(color)) {
|
|
285
|
+
const red = Number.parseInt(color.slice(1, 3), 16);
|
|
286
|
+
const green = Number.parseInt(color.slice(3, 5), 16);
|
|
287
|
+
const blue = Number.parseInt(color.slice(5, 7), 16);
|
|
288
|
+
return `rgb(${red} ${green} ${blue} / ${opacity})`;
|
|
289
|
+
}
|
|
290
|
+
return color;
|
|
291
|
+
}
|
|
292
|
+
function MorphOverlay({
|
|
293
|
+
expanded,
|
|
294
|
+
onClose,
|
|
295
|
+
color,
|
|
296
|
+
opacity,
|
|
297
|
+
blur
|
|
298
|
+
}) {
|
|
299
|
+
return /* @__PURE__ */ jsx(AnimatePresence, { children: expanded && onClose ? /* @__PURE__ */ jsx(
|
|
300
|
+
motion.button,
|
|
301
|
+
{
|
|
302
|
+
type: "button",
|
|
303
|
+
"aria-label": "Close morph",
|
|
304
|
+
className: "fixed inset-0 z-40 cursor-default",
|
|
305
|
+
initial: {
|
|
306
|
+
opacity: 0
|
|
307
|
+
},
|
|
308
|
+
animate: {
|
|
309
|
+
opacity: 1
|
|
310
|
+
},
|
|
311
|
+
exit: {
|
|
312
|
+
opacity: 0
|
|
313
|
+
},
|
|
314
|
+
transition: { duration: 0.18, ease: "easeOut" },
|
|
315
|
+
style: {
|
|
316
|
+
backdropFilter: `blur(${blur}px)`,
|
|
317
|
+
backgroundColor: getOverlayBackgroundColor(color, opacity),
|
|
318
|
+
WebkitBackdropFilter: `blur(${blur}px)`
|
|
319
|
+
},
|
|
320
|
+
onClick: onClose
|
|
321
|
+
}
|
|
322
|
+
) : null });
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/component.tsx
|
|
326
|
+
import { Fragment, jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
327
|
+
var liveSurfaceContentClassName = "absolute [&>*:first-child]:!border-transparent [&>*:first-child]:!bg-transparent [&>*:first-child]:!shadow-none";
|
|
328
|
+
function MorphMeasurementNodes({
|
|
329
|
+
collapsedRef,
|
|
330
|
+
expandedRef,
|
|
331
|
+
collapsedContent,
|
|
332
|
+
expandedContent
|
|
333
|
+
}) {
|
|
334
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
335
|
+
/* @__PURE__ */ jsx2(
|
|
336
|
+
"div",
|
|
337
|
+
{
|
|
338
|
+
ref: collapsedRef,
|
|
339
|
+
"aria-hidden": "true",
|
|
340
|
+
className: "invisible absolute inset-x-0 top-0 pointer-events-none",
|
|
341
|
+
children: collapsedContent
|
|
342
|
+
}
|
|
343
|
+
),
|
|
344
|
+
/* @__PURE__ */ jsx2(
|
|
345
|
+
"div",
|
|
346
|
+
{
|
|
347
|
+
ref: expandedRef,
|
|
348
|
+
"aria-hidden": "true",
|
|
349
|
+
className: "invisible absolute inset-x-0 top-0 pointer-events-none",
|
|
350
|
+
children: expandedContent
|
|
351
|
+
}
|
|
352
|
+
)
|
|
353
|
+
] });
|
|
354
|
+
}
|
|
355
|
+
function MorphShell({
|
|
356
|
+
expanded,
|
|
357
|
+
position,
|
|
358
|
+
animatedSize,
|
|
359
|
+
visualStyle,
|
|
360
|
+
overflowVisible,
|
|
361
|
+
spring,
|
|
362
|
+
onMorphComplete,
|
|
363
|
+
onMorphStart,
|
|
364
|
+
children
|
|
365
|
+
}) {
|
|
366
|
+
return /* @__PURE__ */ jsx2(
|
|
367
|
+
motion2.div,
|
|
368
|
+
{
|
|
369
|
+
className: `absolute shadow-xs ${expanded ? "z-50" : "z-0"}`,
|
|
370
|
+
initial: false,
|
|
371
|
+
animate: {
|
|
372
|
+
width: animatedSize.width,
|
|
373
|
+
height: animatedSize.height,
|
|
374
|
+
backgroundColor: visualStyle.backgroundColor,
|
|
375
|
+
borderRadius: visualStyle.borderRadius,
|
|
376
|
+
...position
|
|
377
|
+
},
|
|
378
|
+
onAnimationComplete: onMorphComplete,
|
|
379
|
+
onAnimationStart: onMorphStart,
|
|
380
|
+
style: {
|
|
381
|
+
overflow: overflowVisible ? "visible" : "hidden"
|
|
382
|
+
},
|
|
383
|
+
transition: spring,
|
|
384
|
+
children
|
|
385
|
+
}
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
function MorphContentLayers({
|
|
389
|
+
expanded,
|
|
390
|
+
collapsedContent,
|
|
391
|
+
expandedContent,
|
|
392
|
+
collapsedLayerSize,
|
|
393
|
+
expandedLayerSize,
|
|
394
|
+
sourcePosition,
|
|
395
|
+
targetPosition,
|
|
396
|
+
sourceGrowthScale,
|
|
397
|
+
collapsedToExpandedScale,
|
|
398
|
+
transformOrigin,
|
|
399
|
+
sourceTransition,
|
|
400
|
+
sourceOpacityTransition,
|
|
401
|
+
targetTransition,
|
|
402
|
+
targetOpacityTransition,
|
|
403
|
+
onOpen
|
|
404
|
+
}) {
|
|
405
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
406
|
+
/* @__PURE__ */ jsx2(
|
|
407
|
+
motion2.div,
|
|
408
|
+
{
|
|
409
|
+
...getLayerInteractivityProps(!expanded),
|
|
410
|
+
className: liveSurfaceContentClassName,
|
|
411
|
+
initial: false,
|
|
412
|
+
animate: {
|
|
413
|
+
opacity: expanded ? 0 : 1,
|
|
414
|
+
scaleX: expanded ? sourceGrowthScale.x : 1,
|
|
415
|
+
scaleY: expanded ? sourceGrowthScale.y : 1,
|
|
416
|
+
...sourcePosition
|
|
417
|
+
},
|
|
418
|
+
transition: {
|
|
419
|
+
...sourceTransition,
|
|
420
|
+
opacity: sourceOpacityTransition
|
|
421
|
+
},
|
|
422
|
+
onClick: expanded ? void 0 : onOpen,
|
|
423
|
+
style: {
|
|
424
|
+
width: collapsedLayerSize.width,
|
|
425
|
+
height: collapsedLayerSize.height,
|
|
426
|
+
pointerEvents: expanded ? "none" : "auto",
|
|
427
|
+
transformOrigin
|
|
428
|
+
},
|
|
429
|
+
children: collapsedContent
|
|
430
|
+
}
|
|
431
|
+
),
|
|
432
|
+
/* @__PURE__ */ jsx2(
|
|
433
|
+
motion2.div,
|
|
434
|
+
{
|
|
435
|
+
...getLayerInteractivityProps(expanded),
|
|
436
|
+
className: liveSurfaceContentClassName,
|
|
437
|
+
initial: false,
|
|
438
|
+
animate: {
|
|
439
|
+
opacity: expanded ? 1 : 0,
|
|
440
|
+
scaleX: expanded ? 1 : collapsedToExpandedScale.x,
|
|
441
|
+
scaleY: expanded ? 1 : collapsedToExpandedScale.y,
|
|
442
|
+
...targetPosition
|
|
443
|
+
},
|
|
444
|
+
transition: {
|
|
445
|
+
...targetTransition,
|
|
446
|
+
opacity: targetOpacityTransition
|
|
447
|
+
},
|
|
448
|
+
style: {
|
|
449
|
+
width: expandedLayerSize.width,
|
|
450
|
+
height: expandedLayerSize.height,
|
|
451
|
+
pointerEvents: expanded ? "auto" : "none",
|
|
452
|
+
transformOrigin
|
|
453
|
+
},
|
|
454
|
+
children: expandedContent
|
|
455
|
+
}
|
|
456
|
+
)
|
|
457
|
+
] });
|
|
458
|
+
}
|
|
459
|
+
function Morpheus({
|
|
460
|
+
direction,
|
|
461
|
+
anchor,
|
|
462
|
+
expanded,
|
|
463
|
+
onOpen,
|
|
464
|
+
onClose,
|
|
465
|
+
collapsedContent,
|
|
466
|
+
expandedContent,
|
|
467
|
+
className = "relative inline-block align-top",
|
|
468
|
+
overlayColor = "#05070a",
|
|
469
|
+
overlayOpacity = 0.54,
|
|
470
|
+
overlayBlur = 0,
|
|
471
|
+
spring = defaultPanelSpring
|
|
472
|
+
}) {
|
|
473
|
+
const measurements = useMorphMeasurements();
|
|
474
|
+
const [animationEnabled, setAnimationEnabled] = useState2(false);
|
|
475
|
+
const [shellOverflowVisible, setShellOverflowVisible] = useState2(false);
|
|
476
|
+
useEffect(() => {
|
|
477
|
+
if (!measurements.ready) {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
const frame = requestAnimationFrame(() => setAnimationEnabled(true));
|
|
481
|
+
return () => cancelAnimationFrame(frame);
|
|
482
|
+
}, [measurements.ready]);
|
|
483
|
+
const motionState = getMorphMotionState({
|
|
484
|
+
expanded,
|
|
485
|
+
direction,
|
|
486
|
+
anchor,
|
|
487
|
+
spring,
|
|
488
|
+
animationEnabled,
|
|
489
|
+
collapsed: measurements.collapsed,
|
|
490
|
+
expandedSurface: measurements.expanded
|
|
491
|
+
});
|
|
492
|
+
const overflowVisible = expanded && shellOverflowVisible;
|
|
493
|
+
const shouldRenderStaticSource = !measurements.ready && !expanded;
|
|
494
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
495
|
+
/* @__PURE__ */ jsx2(
|
|
496
|
+
MorphOverlay,
|
|
497
|
+
{
|
|
498
|
+
expanded,
|
|
499
|
+
onClose,
|
|
500
|
+
color: overlayColor,
|
|
501
|
+
opacity: overlayOpacity,
|
|
502
|
+
blur: overlayBlur
|
|
503
|
+
}
|
|
504
|
+
),
|
|
505
|
+
/* @__PURE__ */ jsxs("div", { className, "aria-live": "polite", children: [
|
|
506
|
+
/* @__PURE__ */ jsx2(
|
|
507
|
+
MorphMeasurementNodes,
|
|
508
|
+
{
|
|
509
|
+
collapsedRef: measurements.collapsedRef,
|
|
510
|
+
expandedRef: measurements.expandedRef,
|
|
511
|
+
collapsedContent,
|
|
512
|
+
expandedContent
|
|
513
|
+
}
|
|
514
|
+
),
|
|
515
|
+
/* @__PURE__ */ jsx2("div", { className: "relative mx-auto block w-fit align-top", children: shouldRenderStaticSource ? /* @__PURE__ */ jsx2("div", { className: "inline-block align-top", onClick: onOpen, children: collapsedContent }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
516
|
+
/* @__PURE__ */ jsx2(
|
|
517
|
+
"div",
|
|
518
|
+
{
|
|
519
|
+
"aria-hidden": "true",
|
|
520
|
+
style: {
|
|
521
|
+
visibility: "hidden",
|
|
522
|
+
width: motionState.collapsedLayerSize.width,
|
|
523
|
+
height: motionState.collapsedLayerSize.height
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
),
|
|
527
|
+
/* @__PURE__ */ jsx2(
|
|
528
|
+
MorphShell,
|
|
529
|
+
{
|
|
530
|
+
expanded,
|
|
531
|
+
position: motionState.anchorPosition,
|
|
532
|
+
animatedSize: motionState.animatedSize,
|
|
533
|
+
visualStyle: motionState.visualStyle,
|
|
534
|
+
overflowVisible,
|
|
535
|
+
spring: motionState.activeSpring,
|
|
536
|
+
onMorphComplete: () => {
|
|
537
|
+
if (expanded) {
|
|
538
|
+
setShellOverflowVisible(true);
|
|
539
|
+
}
|
|
540
|
+
},
|
|
541
|
+
onMorphStart: () => setShellOverflowVisible(false),
|
|
542
|
+
children: /* @__PURE__ */ jsx2(
|
|
543
|
+
MorphContentLayers,
|
|
544
|
+
{
|
|
545
|
+
expanded,
|
|
546
|
+
collapsedContent,
|
|
547
|
+
expandedContent,
|
|
548
|
+
collapsedLayerSize: motionState.collapsedLayerSize,
|
|
549
|
+
expandedLayerSize: motionState.expandedLayerSize,
|
|
550
|
+
sourcePosition: motionState.sourcePosition,
|
|
551
|
+
targetPosition: motionState.targetPosition,
|
|
552
|
+
sourceGrowthScale: motionState.sourceGrowthScale,
|
|
553
|
+
collapsedToExpandedScale: motionState.collapsedToExpandedScale,
|
|
554
|
+
transformOrigin: motionState.transformOrigin,
|
|
555
|
+
sourceTransition: motionState.sourceTransition,
|
|
556
|
+
sourceOpacityTransition: motionState.sourceOpacityTransition,
|
|
557
|
+
targetTransition: motionState.targetTransition,
|
|
558
|
+
targetOpacityTransition: motionState.targetOpacityTransition,
|
|
559
|
+
onOpen
|
|
560
|
+
}
|
|
561
|
+
)
|
|
562
|
+
}
|
|
563
|
+
)
|
|
564
|
+
] }) })
|
|
565
|
+
] })
|
|
566
|
+
] });
|
|
567
|
+
}
|
|
568
|
+
export {
|
|
569
|
+
MorphAnchor,
|
|
570
|
+
Morpheus,
|
|
571
|
+
Morpheus as default,
|
|
572
|
+
defaultAnchorByDirection,
|
|
573
|
+
morphSpringPresets
|
|
574
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-morpheus",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A React component for morphing one surface into other.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"module": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsup src/index.ts --format esm --dts --external react --external react-dom --external framer-motion --clean",
|
|
24
|
+
"check": "tsc --noEmit",
|
|
25
|
+
"release:minor": "bun run scripts/release.ts minor",
|
|
26
|
+
"release:major": "bun run scripts/release.ts major"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"framer-motion": "^12.42.2",
|
|
30
|
+
"react": "^19.0.0",
|
|
31
|
+
"react-dom": "^19.0.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/bun": "^1.3.14",
|
|
35
|
+
"@types/react": "^19.2.17",
|
|
36
|
+
"@types/react-dom": "^19.2.3",
|
|
37
|
+
"framer-motion": "^12.42.2",
|
|
38
|
+
"react": "^19.2.7",
|
|
39
|
+
"react-dom": "^19.2.7",
|
|
40
|
+
"tsup": "^8.5.1",
|
|
41
|
+
"typescript": "^6.0.3"
|
|
42
|
+
}
|
|
43
|
+
}
|