@pip-it-up/react 0.1.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 +21 -0
- package/README.md +65 -0
- package/dist/index.d.mts +47 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +294 -0
- package/dist/index.mjs +252 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Saurabh Shakya and contributors
|
|
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,65 @@
|
|
|
1
|
+
# @pip-it-up/react
|
|
2
|
+
|
|
3
|
+
React bindings for the `pip-it-up` engine.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @pip-it-up/react @pip-it-up/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Components
|
|
12
|
+
|
|
13
|
+
### `<PipWrapper>`
|
|
14
|
+
|
|
15
|
+
Wraps the content you want to move into the PiP window.
|
|
16
|
+
|
|
17
|
+
#### Uncontrolled (Default)
|
|
18
|
+
```tsx
|
|
19
|
+
<PipWrapper>
|
|
20
|
+
<div>Content to move</div>
|
|
21
|
+
<PipTrigger>Toggle</PipTrigger>
|
|
22
|
+
</PipWrapper>
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
#### Controlled
|
|
26
|
+
```tsx
|
|
27
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
28
|
+
|
|
29
|
+
<PipWrapper open={isOpen} onOpenChange={setIsOpen}>
|
|
30
|
+
<div>Content to move</div>
|
|
31
|
+
</PipWrapper>
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
#### Props
|
|
35
|
+
Supports all `PipOptions` from `@pip-it-up/core` (`mode`, `copyStyles`, `fallback`, etc.), plus:
|
|
36
|
+
- `open`: Controlled boolean state.
|
|
37
|
+
- `onOpenChange`: Callback for controlled state.
|
|
38
|
+
- `defaultOpen`: Initial uncontrolled state.
|
|
39
|
+
|
|
40
|
+
### `<PipTrigger>`
|
|
41
|
+
|
|
42
|
+
A button that toggles the PiP window.
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
<PipTrigger asChild>
|
|
46
|
+
<button className="my-custom-btn">Open PiP</button>
|
|
47
|
+
</PipTrigger>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Hooks
|
|
51
|
+
|
|
52
|
+
### `usePip()`
|
|
53
|
+
Returns the context state.
|
|
54
|
+
```tsx
|
|
55
|
+
const { isOpen, pipWindow, instance } = usePip();
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### `useIsPipSupported()`
|
|
59
|
+
Returns boolean if the browser natively supports Document PiP.
|
|
60
|
+
```tsx
|
|
61
|
+
const isSupported = useIsPipSupported();
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Next.js / SSR
|
|
65
|
+
Because the Document PiP API is browser-only, ensure components interacting with it are rendered on the client (`"use client"`).
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import React__default, { ElementType, ReactNode } from 'react';
|
|
3
|
+
import { PipOptions, PipInstance, PipState } from '@pip-it-up/core';
|
|
4
|
+
|
|
5
|
+
interface PipWrapperProps extends PipOptions {
|
|
6
|
+
open?: boolean;
|
|
7
|
+
defaultOpen?: boolean;
|
|
8
|
+
onOpenChange?: (open: boolean) => void;
|
|
9
|
+
as?: ElementType;
|
|
10
|
+
originAs?: ElementType;
|
|
11
|
+
children?: ReactNode;
|
|
12
|
+
}
|
|
13
|
+
declare const PipWrapper: React__default.ForwardRefExoticComponent<PipWrapperProps & React__default.RefAttributes<HTMLElement>>;
|
|
14
|
+
|
|
15
|
+
interface PipTriggerProps extends React__default.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
16
|
+
pipId?: string;
|
|
17
|
+
asChild?: boolean;
|
|
18
|
+
openLabel?: string;
|
|
19
|
+
closeLabel?: string;
|
|
20
|
+
renderOpen?: React__default.ReactNode;
|
|
21
|
+
renderClose?: React__default.ReactNode;
|
|
22
|
+
renderUnsupported?: React__default.ReactNode | null;
|
|
23
|
+
}
|
|
24
|
+
declare const PipTrigger: React__default.ForwardRefExoticComponent<PipTriggerProps & React__default.RefAttributes<HTMLElement>>;
|
|
25
|
+
|
|
26
|
+
declare function usePip<T extends HTMLElement = HTMLDivElement>(options?: PipOptions): {
|
|
27
|
+
contentRef: React.RefObject<T>;
|
|
28
|
+
originRef: React.RefObject<T>;
|
|
29
|
+
open: () => Promise<void>;
|
|
30
|
+
close: () => void;
|
|
31
|
+
toggle: () => Promise<void>;
|
|
32
|
+
isOpen: boolean;
|
|
33
|
+
isSupported: boolean;
|
|
34
|
+
pipWindow: Window | null;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
interface PipContextValue<T = unknown> {
|
|
38
|
+
instance: PipInstance;
|
|
39
|
+
state: PipState;
|
|
40
|
+
}
|
|
41
|
+
declare const PipContext: React.Context<PipContextValue<any> | null>;
|
|
42
|
+
|
|
43
|
+
declare function usePipContext<T = unknown>(): PipContextValue<T>;
|
|
44
|
+
|
|
45
|
+
declare const useIsPipSupported: () => boolean;
|
|
46
|
+
|
|
47
|
+
export { PipContext, type PipContextValue, PipTrigger, type PipTriggerProps, PipWrapper, type PipWrapperProps, useIsPipSupported, usePip, usePipContext };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import React__default, { ElementType, ReactNode } from 'react';
|
|
3
|
+
import { PipOptions, PipInstance, PipState } from '@pip-it-up/core';
|
|
4
|
+
|
|
5
|
+
interface PipWrapperProps extends PipOptions {
|
|
6
|
+
open?: boolean;
|
|
7
|
+
defaultOpen?: boolean;
|
|
8
|
+
onOpenChange?: (open: boolean) => void;
|
|
9
|
+
as?: ElementType;
|
|
10
|
+
originAs?: ElementType;
|
|
11
|
+
children?: ReactNode;
|
|
12
|
+
}
|
|
13
|
+
declare const PipWrapper: React__default.ForwardRefExoticComponent<PipWrapperProps & React__default.RefAttributes<HTMLElement>>;
|
|
14
|
+
|
|
15
|
+
interface PipTriggerProps extends React__default.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
16
|
+
pipId?: string;
|
|
17
|
+
asChild?: boolean;
|
|
18
|
+
openLabel?: string;
|
|
19
|
+
closeLabel?: string;
|
|
20
|
+
renderOpen?: React__default.ReactNode;
|
|
21
|
+
renderClose?: React__default.ReactNode;
|
|
22
|
+
renderUnsupported?: React__default.ReactNode | null;
|
|
23
|
+
}
|
|
24
|
+
declare const PipTrigger: React__default.ForwardRefExoticComponent<PipTriggerProps & React__default.RefAttributes<HTMLElement>>;
|
|
25
|
+
|
|
26
|
+
declare function usePip<T extends HTMLElement = HTMLDivElement>(options?: PipOptions): {
|
|
27
|
+
contentRef: React.RefObject<T>;
|
|
28
|
+
originRef: React.RefObject<T>;
|
|
29
|
+
open: () => Promise<void>;
|
|
30
|
+
close: () => void;
|
|
31
|
+
toggle: () => Promise<void>;
|
|
32
|
+
isOpen: boolean;
|
|
33
|
+
isSupported: boolean;
|
|
34
|
+
pipWindow: Window | null;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
interface PipContextValue<T = unknown> {
|
|
38
|
+
instance: PipInstance;
|
|
39
|
+
state: PipState;
|
|
40
|
+
}
|
|
41
|
+
declare const PipContext: React.Context<PipContextValue<any> | null>;
|
|
42
|
+
|
|
43
|
+
declare function usePipContext<T = unknown>(): PipContextValue<T>;
|
|
44
|
+
|
|
45
|
+
declare const useIsPipSupported: () => boolean;
|
|
46
|
+
|
|
47
|
+
export { PipContext, type PipContextValue, PipTrigger, type PipTriggerProps, PipWrapper, type PipWrapperProps, useIsPipSupported, usePip, usePipContext };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
PipContext: () => PipContext,
|
|
34
|
+
PipTrigger: () => PipTrigger,
|
|
35
|
+
PipWrapper: () => PipWrapper,
|
|
36
|
+
useIsPipSupported: () => useIsPipSupported,
|
|
37
|
+
usePip: () => usePip,
|
|
38
|
+
usePipContext: () => usePipContext
|
|
39
|
+
});
|
|
40
|
+
module.exports = __toCommonJS(index_exports);
|
|
41
|
+
|
|
42
|
+
// src/PipWrapper.tsx
|
|
43
|
+
var import_react2 = require("react");
|
|
44
|
+
var import_core = require("@pip-it-up/core");
|
|
45
|
+
|
|
46
|
+
// src/PipContext.tsx
|
|
47
|
+
var import_react = require("react");
|
|
48
|
+
var PipContext = (0, import_react.createContext)(null);
|
|
49
|
+
|
|
50
|
+
// src/PipPortal.tsx
|
|
51
|
+
var import_react_dom = require("react-dom");
|
|
52
|
+
var PipPortal = ({ children, pipWindow }) => {
|
|
53
|
+
return (0, import_react_dom.createPortal)(children, pipWindow.document.body);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// src/PipWrapper.tsx
|
|
57
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
58
|
+
var emptyServerState = { isOpen: false, isSupported: false, pipWindow: null };
|
|
59
|
+
var getServerState = () => emptyServerState;
|
|
60
|
+
var PipWrapper = (0, import_react2.forwardRef)((props, ref) => {
|
|
61
|
+
const {
|
|
62
|
+
open: controlledOpen,
|
|
63
|
+
defaultOpen = false,
|
|
64
|
+
onOpenChange,
|
|
65
|
+
as: Component = "div",
|
|
66
|
+
originAs: OriginComponent = "div",
|
|
67
|
+
children,
|
|
68
|
+
...coreOptions
|
|
69
|
+
} = props;
|
|
70
|
+
const contentRef = (0, import_react2.useRef)(null);
|
|
71
|
+
const originRef = (0, import_react2.useRef)(null);
|
|
72
|
+
const instanceRef = (0, import_react2.useRef)(null);
|
|
73
|
+
(0, import_react2.useImperativeHandle)(ref, () => originRef.current);
|
|
74
|
+
if (!instanceRef.current) {
|
|
75
|
+
instanceRef.current = (0, import_core.createPip)(coreOptions);
|
|
76
|
+
}
|
|
77
|
+
const instance = instanceRef.current;
|
|
78
|
+
(0, import_react2.useEffect)(() => {
|
|
79
|
+
instance.updateElements({
|
|
80
|
+
contentEl: contentRef.current || void 0,
|
|
81
|
+
originEl: originRef.current || void 0
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
(0, import_react2.useEffect)(() => {
|
|
85
|
+
return () => {
|
|
86
|
+
instance.destroy();
|
|
87
|
+
};
|
|
88
|
+
}, [instance]);
|
|
89
|
+
const state = (0, import_react2.useSyncExternalStore)(
|
|
90
|
+
instance.subscribe,
|
|
91
|
+
instance.getState,
|
|
92
|
+
getServerState
|
|
93
|
+
);
|
|
94
|
+
const isControlled = controlledOpen !== void 0;
|
|
95
|
+
const prevOpenRef = (0, import_react2.useRef)(state.isOpen);
|
|
96
|
+
(0, import_react2.useEffect)(() => {
|
|
97
|
+
if (state.isOpen !== prevOpenRef.current) {
|
|
98
|
+
if (onOpenChange) {
|
|
99
|
+
onOpenChange(state.isOpen);
|
|
100
|
+
}
|
|
101
|
+
prevOpenRef.current = state.isOpen;
|
|
102
|
+
}
|
|
103
|
+
}, [state.isOpen, onOpenChange]);
|
|
104
|
+
(0, import_react2.useEffect)(() => {
|
|
105
|
+
if (isControlled) {
|
|
106
|
+
if (controlledOpen && !state.isOpen) {
|
|
107
|
+
instance.open({ contentEl: contentRef.current || void 0, originEl: originRef.current || void 0 });
|
|
108
|
+
} else if (!controlledOpen && state.isOpen) {
|
|
109
|
+
instance.close();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}, [controlledOpen, isControlled, state.isOpen, instance]);
|
|
113
|
+
const defaultOpenHandled = (0, import_react2.useRef)(false);
|
|
114
|
+
(0, import_react2.useEffect)(() => {
|
|
115
|
+
if (!isControlled && defaultOpen && !defaultOpenHandled.current) {
|
|
116
|
+
defaultOpenHandled.current = true;
|
|
117
|
+
instance.open({ contentEl: contentRef.current || void 0, originEl: originRef.current || void 0 });
|
|
118
|
+
}
|
|
119
|
+
}, [defaultOpen, isControlled, instance]);
|
|
120
|
+
const mode = coreOptions.mode || "move";
|
|
121
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PipContext.Provider, { value: { instance, state }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(OriginComponent, { ref: originRef, children: mode === "portal" && state.isOpen && state.pipWindow ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PipPortal, { pipWindow: state.pipWindow, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, { ref: contentRef, children }) }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, { ref: contentRef, children }) }) });
|
|
122
|
+
});
|
|
123
|
+
PipWrapper.displayName = "PipWrapper";
|
|
124
|
+
|
|
125
|
+
// src/PipTrigger.tsx
|
|
126
|
+
var import_react5 = require("react");
|
|
127
|
+
var import_core3 = require("@pip-it-up/core");
|
|
128
|
+
|
|
129
|
+
// src/useIsPipSupported.ts
|
|
130
|
+
var import_react3 = require("react");
|
|
131
|
+
var import_core2 = require("@pip-it-up/core");
|
|
132
|
+
var useIsPipSupported = () => {
|
|
133
|
+
const [supported, setSupported] = (0, import_react3.useState)(false);
|
|
134
|
+
(0, import_react3.useEffect)(() => {
|
|
135
|
+
setSupported((0, import_core2.isSupported)());
|
|
136
|
+
}, []);
|
|
137
|
+
return supported;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// src/Slot.tsx
|
|
141
|
+
var import_react4 = __toESM(require("react"));
|
|
142
|
+
var Slot = (0, import_react4.forwardRef)((props, ref) => {
|
|
143
|
+
const { children, ...slotProps } = props;
|
|
144
|
+
if (import_react4.default.isValidElement(children)) {
|
|
145
|
+
return import_react4.default.cloneElement(children, {
|
|
146
|
+
...slotProps,
|
|
147
|
+
...children.props,
|
|
148
|
+
style: {
|
|
149
|
+
...slotProps.style,
|
|
150
|
+
...children.props.style
|
|
151
|
+
},
|
|
152
|
+
className: [slotProps.className, children.props.className].filter(Boolean).join(" "),
|
|
153
|
+
ref: (node) => {
|
|
154
|
+
if (typeof ref === "function") ref(node);
|
|
155
|
+
else if (ref) ref.current = node;
|
|
156
|
+
const childRef = children.ref;
|
|
157
|
+
if (typeof childRef === "function") childRef(node);
|
|
158
|
+
else if (childRef) childRef.current = node;
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
if (import_react4.default.Children.count(children) > 1) {
|
|
163
|
+
import_react4.default.Children.only(null);
|
|
164
|
+
}
|
|
165
|
+
return null;
|
|
166
|
+
});
|
|
167
|
+
Slot.displayName = "Slot";
|
|
168
|
+
|
|
169
|
+
// src/PipTrigger.tsx
|
|
170
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
171
|
+
var emptySubscribe = () => () => {
|
|
172
|
+
};
|
|
173
|
+
var emptyServerState2 = { isOpen: false, isSupported: false, pipWindow: null };
|
|
174
|
+
var emptyGetState = () => emptyServerState2;
|
|
175
|
+
var PipTrigger = (0, import_react5.forwardRef)((props, ref) => {
|
|
176
|
+
const {
|
|
177
|
+
pipId,
|
|
178
|
+
asChild,
|
|
179
|
+
openLabel = "\u2197 Pop out",
|
|
180
|
+
closeLabel = "\u22A0 Close",
|
|
181
|
+
renderOpen,
|
|
182
|
+
renderClose,
|
|
183
|
+
renderUnsupported = null,
|
|
184
|
+
onClick,
|
|
185
|
+
children,
|
|
186
|
+
...rest
|
|
187
|
+
} = props;
|
|
188
|
+
const isSupported2 = useIsPipSupported();
|
|
189
|
+
const context = (0, import_react5.useContext)(PipContext);
|
|
190
|
+
const [registryInstance, setRegistryInstance] = (0, import_react5.useState)(pipId ? (0, import_core3.getPip)(pipId) : null);
|
|
191
|
+
(0, import_react5.useEffect)(() => {
|
|
192
|
+
if (pipId) {
|
|
193
|
+
setRegistryInstance((0, import_core3.getPip)(pipId));
|
|
194
|
+
const unsub = (0, import_core3.subscribeRegistry)(pipId, () => {
|
|
195
|
+
setRegistryInstance((0, import_core3.getPip)(pipId));
|
|
196
|
+
});
|
|
197
|
+
return unsub;
|
|
198
|
+
}
|
|
199
|
+
}, [pipId]);
|
|
200
|
+
const instance = pipId ? registryInstance : context?.instance;
|
|
201
|
+
const subscribe = instance?.subscribe || emptySubscribe;
|
|
202
|
+
const getState = instance?.getState || emptyGetState;
|
|
203
|
+
const state = (0, import_react5.useSyncExternalStore)(
|
|
204
|
+
subscribe,
|
|
205
|
+
getState,
|
|
206
|
+
emptyGetState
|
|
207
|
+
);
|
|
208
|
+
const [mounted, setMounted] = (0, import_react5.useState)(false);
|
|
209
|
+
(0, import_react5.useEffect)(() => setMounted(true), []);
|
|
210
|
+
if (!instance && pipId) {
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
if (!mounted) {
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
if (!isSupported2) {
|
|
217
|
+
if (renderUnsupported === null) return null;
|
|
218
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: renderUnsupported });
|
|
219
|
+
}
|
|
220
|
+
const handleClick = async (e) => {
|
|
221
|
+
if (onClick) onClick(e);
|
|
222
|
+
if (instance) {
|
|
223
|
+
await instance.toggle();
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
const isOpen = state.isOpen;
|
|
227
|
+
const content = isOpen ? renderClose ?? closeLabel : renderOpen ?? openLabel;
|
|
228
|
+
const Comp = asChild ? Slot : "button";
|
|
229
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
230
|
+
Comp,
|
|
231
|
+
{
|
|
232
|
+
ref,
|
|
233
|
+
onClick: handleClick,
|
|
234
|
+
...asChild ? {} : { type: "button" },
|
|
235
|
+
...rest,
|
|
236
|
+
children: asChild ? children : content
|
|
237
|
+
}
|
|
238
|
+
);
|
|
239
|
+
});
|
|
240
|
+
PipTrigger.displayName = "PipTrigger";
|
|
241
|
+
|
|
242
|
+
// src/usePip.ts
|
|
243
|
+
var import_react6 = require("react");
|
|
244
|
+
var import_core4 = require("@pip-it-up/core");
|
|
245
|
+
var emptyServerState3 = { isOpen: false, isSupported: false, pipWindow: null };
|
|
246
|
+
var emptyGetState2 = () => emptyServerState3;
|
|
247
|
+
function usePip(options = {}) {
|
|
248
|
+
const contentRef = (0, import_react6.useRef)(null);
|
|
249
|
+
const originRef = (0, import_react6.useRef)(null);
|
|
250
|
+
const instanceRef = (0, import_react6.useRef)(null);
|
|
251
|
+
if (!instanceRef.current) {
|
|
252
|
+
instanceRef.current = (0, import_core4.createPip)(options);
|
|
253
|
+
}
|
|
254
|
+
const instance = instanceRef.current;
|
|
255
|
+
(0, import_react6.useEffect)(() => {
|
|
256
|
+
return () => {
|
|
257
|
+
instance.destroy();
|
|
258
|
+
};
|
|
259
|
+
}, [instance]);
|
|
260
|
+
const state = (0, import_react6.useSyncExternalStore)(
|
|
261
|
+
instance.subscribe,
|
|
262
|
+
instance.getState,
|
|
263
|
+
emptyGetState2
|
|
264
|
+
);
|
|
265
|
+
return {
|
|
266
|
+
contentRef,
|
|
267
|
+
originRef,
|
|
268
|
+
open: () => instance.open({ contentEl: contentRef.current || void 0, originEl: originRef.current || void 0 }),
|
|
269
|
+
close: instance.close,
|
|
270
|
+
toggle: () => instance.toggle({ contentEl: contentRef.current || void 0, originEl: originRef.current || void 0 }),
|
|
271
|
+
isOpen: state.isOpen,
|
|
272
|
+
isSupported: state.isSupported,
|
|
273
|
+
pipWindow: state.pipWindow
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// src/usePipContext.ts
|
|
278
|
+
var import_react7 = require("react");
|
|
279
|
+
function usePipContext() {
|
|
280
|
+
const context = (0, import_react7.useContext)(PipContext);
|
|
281
|
+
if (!context) {
|
|
282
|
+
throw new Error("usePipContext must be used within a <PipWrapper>");
|
|
283
|
+
}
|
|
284
|
+
return context;
|
|
285
|
+
}
|
|
286
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
287
|
+
0 && (module.exports = {
|
|
288
|
+
PipContext,
|
|
289
|
+
PipTrigger,
|
|
290
|
+
PipWrapper,
|
|
291
|
+
useIsPipSupported,
|
|
292
|
+
usePip,
|
|
293
|
+
usePipContext
|
|
294
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// src/PipWrapper.tsx
|
|
2
|
+
import { forwardRef, useEffect, useRef, useSyncExternalStore, useImperativeHandle } from "react";
|
|
3
|
+
import { createPip } from "@pip-it-up/core";
|
|
4
|
+
|
|
5
|
+
// src/PipContext.tsx
|
|
6
|
+
import { createContext } from "react";
|
|
7
|
+
var PipContext = createContext(null);
|
|
8
|
+
|
|
9
|
+
// src/PipPortal.tsx
|
|
10
|
+
import { createPortal } from "react-dom";
|
|
11
|
+
var PipPortal = ({ children, pipWindow }) => {
|
|
12
|
+
return createPortal(children, pipWindow.document.body);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// src/PipWrapper.tsx
|
|
16
|
+
import { jsx } from "react/jsx-runtime";
|
|
17
|
+
var emptyServerState = { isOpen: false, isSupported: false, pipWindow: null };
|
|
18
|
+
var getServerState = () => emptyServerState;
|
|
19
|
+
var PipWrapper = forwardRef((props, ref) => {
|
|
20
|
+
const {
|
|
21
|
+
open: controlledOpen,
|
|
22
|
+
defaultOpen = false,
|
|
23
|
+
onOpenChange,
|
|
24
|
+
as: Component = "div",
|
|
25
|
+
originAs: OriginComponent = "div",
|
|
26
|
+
children,
|
|
27
|
+
...coreOptions
|
|
28
|
+
} = props;
|
|
29
|
+
const contentRef = useRef(null);
|
|
30
|
+
const originRef = useRef(null);
|
|
31
|
+
const instanceRef = useRef(null);
|
|
32
|
+
useImperativeHandle(ref, () => originRef.current);
|
|
33
|
+
if (!instanceRef.current) {
|
|
34
|
+
instanceRef.current = createPip(coreOptions);
|
|
35
|
+
}
|
|
36
|
+
const instance = instanceRef.current;
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
instance.updateElements({
|
|
39
|
+
contentEl: contentRef.current || void 0,
|
|
40
|
+
originEl: originRef.current || void 0
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
return () => {
|
|
45
|
+
instance.destroy();
|
|
46
|
+
};
|
|
47
|
+
}, [instance]);
|
|
48
|
+
const state = useSyncExternalStore(
|
|
49
|
+
instance.subscribe,
|
|
50
|
+
instance.getState,
|
|
51
|
+
getServerState
|
|
52
|
+
);
|
|
53
|
+
const isControlled = controlledOpen !== void 0;
|
|
54
|
+
const prevOpenRef = useRef(state.isOpen);
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
if (state.isOpen !== prevOpenRef.current) {
|
|
57
|
+
if (onOpenChange) {
|
|
58
|
+
onOpenChange(state.isOpen);
|
|
59
|
+
}
|
|
60
|
+
prevOpenRef.current = state.isOpen;
|
|
61
|
+
}
|
|
62
|
+
}, [state.isOpen, onOpenChange]);
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
if (isControlled) {
|
|
65
|
+
if (controlledOpen && !state.isOpen) {
|
|
66
|
+
instance.open({ contentEl: contentRef.current || void 0, originEl: originRef.current || void 0 });
|
|
67
|
+
} else if (!controlledOpen && state.isOpen) {
|
|
68
|
+
instance.close();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}, [controlledOpen, isControlled, state.isOpen, instance]);
|
|
72
|
+
const defaultOpenHandled = useRef(false);
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (!isControlled && defaultOpen && !defaultOpenHandled.current) {
|
|
75
|
+
defaultOpenHandled.current = true;
|
|
76
|
+
instance.open({ contentEl: contentRef.current || void 0, originEl: originRef.current || void 0 });
|
|
77
|
+
}
|
|
78
|
+
}, [defaultOpen, isControlled, instance]);
|
|
79
|
+
const mode = coreOptions.mode || "move";
|
|
80
|
+
return /* @__PURE__ */ jsx(PipContext.Provider, { value: { instance, state }, children: /* @__PURE__ */ jsx(OriginComponent, { ref: originRef, children: mode === "portal" && state.isOpen && state.pipWindow ? /* @__PURE__ */ jsx(PipPortal, { pipWindow: state.pipWindow, children: /* @__PURE__ */ jsx(Component, { ref: contentRef, children }) }) : /* @__PURE__ */ jsx(Component, { ref: contentRef, children }) }) });
|
|
81
|
+
});
|
|
82
|
+
PipWrapper.displayName = "PipWrapper";
|
|
83
|
+
|
|
84
|
+
// src/PipTrigger.tsx
|
|
85
|
+
import { forwardRef as forwardRef3, useEffect as useEffect3, useState as useState2, useSyncExternalStore as useSyncExternalStore2, useContext } from "react";
|
|
86
|
+
import { getPip, subscribeRegistry } from "@pip-it-up/core";
|
|
87
|
+
|
|
88
|
+
// src/useIsPipSupported.ts
|
|
89
|
+
import { useState, useEffect as useEffect2 } from "react";
|
|
90
|
+
import { isSupported } from "@pip-it-up/core";
|
|
91
|
+
var useIsPipSupported = () => {
|
|
92
|
+
const [supported, setSupported] = useState(false);
|
|
93
|
+
useEffect2(() => {
|
|
94
|
+
setSupported(isSupported());
|
|
95
|
+
}, []);
|
|
96
|
+
return supported;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// src/Slot.tsx
|
|
100
|
+
import React2, { forwardRef as forwardRef2 } from "react";
|
|
101
|
+
var Slot = forwardRef2((props, ref) => {
|
|
102
|
+
const { children, ...slotProps } = props;
|
|
103
|
+
if (React2.isValidElement(children)) {
|
|
104
|
+
return React2.cloneElement(children, {
|
|
105
|
+
...slotProps,
|
|
106
|
+
...children.props,
|
|
107
|
+
style: {
|
|
108
|
+
...slotProps.style,
|
|
109
|
+
...children.props.style
|
|
110
|
+
},
|
|
111
|
+
className: [slotProps.className, children.props.className].filter(Boolean).join(" "),
|
|
112
|
+
ref: (node) => {
|
|
113
|
+
if (typeof ref === "function") ref(node);
|
|
114
|
+
else if (ref) ref.current = node;
|
|
115
|
+
const childRef = children.ref;
|
|
116
|
+
if (typeof childRef === "function") childRef(node);
|
|
117
|
+
else if (childRef) childRef.current = node;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
if (React2.Children.count(children) > 1) {
|
|
122
|
+
React2.Children.only(null);
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
});
|
|
126
|
+
Slot.displayName = "Slot";
|
|
127
|
+
|
|
128
|
+
// src/PipTrigger.tsx
|
|
129
|
+
import { Fragment, jsx as jsx2 } from "react/jsx-runtime";
|
|
130
|
+
var emptySubscribe = () => () => {
|
|
131
|
+
};
|
|
132
|
+
var emptyServerState2 = { isOpen: false, isSupported: false, pipWindow: null };
|
|
133
|
+
var emptyGetState = () => emptyServerState2;
|
|
134
|
+
var PipTrigger = forwardRef3((props, ref) => {
|
|
135
|
+
const {
|
|
136
|
+
pipId,
|
|
137
|
+
asChild,
|
|
138
|
+
openLabel = "\u2197 Pop out",
|
|
139
|
+
closeLabel = "\u22A0 Close",
|
|
140
|
+
renderOpen,
|
|
141
|
+
renderClose,
|
|
142
|
+
renderUnsupported = null,
|
|
143
|
+
onClick,
|
|
144
|
+
children,
|
|
145
|
+
...rest
|
|
146
|
+
} = props;
|
|
147
|
+
const isSupported2 = useIsPipSupported();
|
|
148
|
+
const context = useContext(PipContext);
|
|
149
|
+
const [registryInstance, setRegistryInstance] = useState2(pipId ? getPip(pipId) : null);
|
|
150
|
+
useEffect3(() => {
|
|
151
|
+
if (pipId) {
|
|
152
|
+
setRegistryInstance(getPip(pipId));
|
|
153
|
+
const unsub = subscribeRegistry(pipId, () => {
|
|
154
|
+
setRegistryInstance(getPip(pipId));
|
|
155
|
+
});
|
|
156
|
+
return unsub;
|
|
157
|
+
}
|
|
158
|
+
}, [pipId]);
|
|
159
|
+
const instance = pipId ? registryInstance : context?.instance;
|
|
160
|
+
const subscribe = instance?.subscribe || emptySubscribe;
|
|
161
|
+
const getState = instance?.getState || emptyGetState;
|
|
162
|
+
const state = useSyncExternalStore2(
|
|
163
|
+
subscribe,
|
|
164
|
+
getState,
|
|
165
|
+
emptyGetState
|
|
166
|
+
);
|
|
167
|
+
const [mounted, setMounted] = useState2(false);
|
|
168
|
+
useEffect3(() => setMounted(true), []);
|
|
169
|
+
if (!instance && pipId) {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
if (!mounted) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
if (!isSupported2) {
|
|
176
|
+
if (renderUnsupported === null) return null;
|
|
177
|
+
return /* @__PURE__ */ jsx2(Fragment, { children: renderUnsupported });
|
|
178
|
+
}
|
|
179
|
+
const handleClick = async (e) => {
|
|
180
|
+
if (onClick) onClick(e);
|
|
181
|
+
if (instance) {
|
|
182
|
+
await instance.toggle();
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
const isOpen = state.isOpen;
|
|
186
|
+
const content = isOpen ? renderClose ?? closeLabel : renderOpen ?? openLabel;
|
|
187
|
+
const Comp = asChild ? Slot : "button";
|
|
188
|
+
return /* @__PURE__ */ jsx2(
|
|
189
|
+
Comp,
|
|
190
|
+
{
|
|
191
|
+
ref,
|
|
192
|
+
onClick: handleClick,
|
|
193
|
+
...asChild ? {} : { type: "button" },
|
|
194
|
+
...rest,
|
|
195
|
+
children: asChild ? children : content
|
|
196
|
+
}
|
|
197
|
+
);
|
|
198
|
+
});
|
|
199
|
+
PipTrigger.displayName = "PipTrigger";
|
|
200
|
+
|
|
201
|
+
// src/usePip.ts
|
|
202
|
+
import { useRef as useRef2, useEffect as useEffect4, useSyncExternalStore as useSyncExternalStore3 } from "react";
|
|
203
|
+
import { createPip as createPip2 } from "@pip-it-up/core";
|
|
204
|
+
var emptyServerState3 = { isOpen: false, isSupported: false, pipWindow: null };
|
|
205
|
+
var emptyGetState2 = () => emptyServerState3;
|
|
206
|
+
function usePip(options = {}) {
|
|
207
|
+
const contentRef = useRef2(null);
|
|
208
|
+
const originRef = useRef2(null);
|
|
209
|
+
const instanceRef = useRef2(null);
|
|
210
|
+
if (!instanceRef.current) {
|
|
211
|
+
instanceRef.current = createPip2(options);
|
|
212
|
+
}
|
|
213
|
+
const instance = instanceRef.current;
|
|
214
|
+
useEffect4(() => {
|
|
215
|
+
return () => {
|
|
216
|
+
instance.destroy();
|
|
217
|
+
};
|
|
218
|
+
}, [instance]);
|
|
219
|
+
const state = useSyncExternalStore3(
|
|
220
|
+
instance.subscribe,
|
|
221
|
+
instance.getState,
|
|
222
|
+
emptyGetState2
|
|
223
|
+
);
|
|
224
|
+
return {
|
|
225
|
+
contentRef,
|
|
226
|
+
originRef,
|
|
227
|
+
open: () => instance.open({ contentEl: contentRef.current || void 0, originEl: originRef.current || void 0 }),
|
|
228
|
+
close: instance.close,
|
|
229
|
+
toggle: () => instance.toggle({ contentEl: contentRef.current || void 0, originEl: originRef.current || void 0 }),
|
|
230
|
+
isOpen: state.isOpen,
|
|
231
|
+
isSupported: state.isSupported,
|
|
232
|
+
pipWindow: state.pipWindow
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// src/usePipContext.ts
|
|
237
|
+
import { useContext as useContext2 } from "react";
|
|
238
|
+
function usePipContext() {
|
|
239
|
+
const context = useContext2(PipContext);
|
|
240
|
+
if (!context) {
|
|
241
|
+
throw new Error("usePipContext must be used within a <PipWrapper>");
|
|
242
|
+
}
|
|
243
|
+
return context;
|
|
244
|
+
}
|
|
245
|
+
export {
|
|
246
|
+
PipContext,
|
|
247
|
+
PipTrigger,
|
|
248
|
+
PipWrapper,
|
|
249
|
+
useIsPipSupported,
|
|
250
|
+
usePip,
|
|
251
|
+
usePipContext
|
|
252
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pip-it-up/react",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/Shakya47/pip-it-up.git",
|
|
7
|
+
"directory": "packages/react"
|
|
8
|
+
},
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/Shakya47/pip-it-up/issues"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/Shakya47/pip-it-up/tree/main/packages/react#readme",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"module": "./dist/index.mjs",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"require": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public",
|
|
25
|
+
"provenance": true
|
|
26
|
+
},
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@pip-it-up/core": "0.1.1"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": ">=17",
|
|
36
|
+
"react-dom": ">=17"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
40
|
+
"@testing-library/react": "^16.3.2",
|
|
41
|
+
"@testing-library/user-event": "^14.6.1",
|
|
42
|
+
"@types/react": "^18.2.0",
|
|
43
|
+
"@types/react-dom": "^18.2.0",
|
|
44
|
+
"react": "^18.2.0",
|
|
45
|
+
"react-dom": "^18.2.0",
|
|
46
|
+
"typescript": "^5.4.0",
|
|
47
|
+
"vitest": "^1.4.0"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsup",
|
|
51
|
+
"dev": "tsup --watch",
|
|
52
|
+
"lint": "eslint \"src/**/*.{ts,tsx}\" \"tests/**/*.{ts,tsx}\"",
|
|
53
|
+
"typecheck": "tsc --noEmit",
|
|
54
|
+
"test": "vitest run",
|
|
55
|
+
"clean": "rm -rf dist"
|
|
56
|
+
}
|
|
57
|
+
}
|