@octanejs/popper 0.0.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 +30 -0
- package/UPSTREAM.md +68 -0
- package/package.json +79 -0
- package/src/components.tsrx +115 -0
- package/src/index.ts +13 -0
- package/src/types.ts +87 -0
- package/src/usePopper.ts +22 -0
- package/src/usePopper.tsrx +95 -0
- package/src/utils.ts +9 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 React Popper authors
|
|
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,30 @@
|
|
|
1
|
+
# `@octanejs/popper`
|
|
2
|
+
|
|
3
|
+
Octane binding for the complete public contract of `react-popper@2.3.0`.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pnpm add @octanejs/popper @popperjs/core
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```tsrx
|
|
10
|
+
import { Manager, Popper, Reference } from '@octanejs/popper';
|
|
11
|
+
|
|
12
|
+
export function Tooltip() {
|
|
13
|
+
return <Manager>
|
|
14
|
+
<Reference>{({ ref }) => <button ref={ref}>Anchor</button>}</Reference>
|
|
15
|
+
<Popper placement="top">
|
|
16
|
+
{({ ref, style, placement, arrowProps }) => <div
|
|
17
|
+
ref={ref}
|
|
18
|
+
style={style}
|
|
19
|
+
data-placement={placement}
|
|
20
|
+
>
|
|
21
|
+
Tooltip
|
|
22
|
+
<div ref={arrowProps.ref} style={arrowProps.style} />
|
|
23
|
+
</div>}
|
|
24
|
+
</Popper>
|
|
25
|
+
</Manager>;
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`usePopper` is also available with the same reference, popper, options, state,
|
|
30
|
+
styles, attributes, update, and force-update contract as the React package.
|
package/UPSTREAM.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Upstream provenance
|
|
2
|
+
|
|
3
|
+
`@octanejs/popper` ports the React binding from
|
|
4
|
+
[`react-popper@2.3.0`](https://github.com/floating-ui/react-popper/releases/tag/v2.3.0)
|
|
5
|
+
while reusing `@popperjs/core` unchanged.
|
|
6
|
+
|
|
7
|
+
## Immutable pin
|
|
8
|
+
|
|
9
|
+
- Package: `react-popper@2.3.0`
|
|
10
|
+
- Repository: `https://github.com/floating-ui/react-popper`
|
|
11
|
+
- Tag: `v2.3.0`
|
|
12
|
+
- Commit: `b636fa3ceee14245d670a0c438fe4343c31258e5`
|
|
13
|
+
- License: MIT
|
|
14
|
+
- Supported upstream range: exactly `2.3.0`
|
|
15
|
+
- React oracle: `react@19.2.7` and `react-dom@19.2.7`
|
|
16
|
+
- Type oracle: `@types/react@19.2.17` and `@types/react-dom@19.2.3`
|
|
17
|
+
- Pristine Jest peer aliases: `react@18.3.1` / `react-dom@18.3.1` (upstream peer is React 16–18)
|
|
18
|
+
|
|
19
|
+
The pinned tag source, test suites, snapshots, TypeScript programs, package
|
|
20
|
+
metadata, README, and license are vendored under `upstream/tag`. The published
|
|
21
|
+
npm declaration, package metadata, README, and license are vendored under
|
|
22
|
+
`upstream/npm`. `audit/upstream-inventory.json` records their SHA-256 hashes and
|
|
23
|
+
the parity audit fails closed when either authority or its case map changes.
|
|
24
|
+
|
|
25
|
+
Oracle versions above are the resolved lockfile pins used by the pristine and
|
|
26
|
+
adapted lanes. They must not silently track `catalog:default` drift; bump them
|
|
27
|
+
here and in the lockfile together when the port upgrades its React oracle.
|
|
28
|
+
|
|
29
|
+
## Host tooling adaptation
|
|
30
|
+
|
|
31
|
+
The three Jest snapshot files under `upstream/tag/src/__snapshots__/` keep the
|
|
32
|
+
pinned v2.3.0 bodies but use the current Jest snapshot guide URL
|
|
33
|
+
(`https://jestjs.io/docs/snapshot-testing`) instead of the legacy `goo.gl`
|
|
34
|
+
header. Jest 30 rejects the outdated header; the inventory checksums cover the
|
|
35
|
+
adapted files.
|
|
36
|
+
|
|
37
|
+
## Export crosswalk
|
|
38
|
+
|
|
39
|
+
| Upstream entry point or export | Octane disposition | Evidence |
|
|
40
|
+
| --- | --- | --- |
|
|
41
|
+
| `Manager` | Ported | Adapted `tests/upstream/Manager.test.tsx`; differential and public runtime suites. |
|
|
42
|
+
| `Reference` | Ported | Adapted `tests/upstream/Reference.test.tsx` (direct setter-context provider fixture). |
|
|
43
|
+
| `Popper` | Ported | Adapted `tests/upstream/Popper.test.tsx`; differential markup/placement cases. |
|
|
44
|
+
| `usePopper` | Ported | Adapted `tests/upstream/usePopper.test.tsx`. |
|
|
45
|
+
| `ManagerProps` | Ported | Public type contract (`typetests/public-api.test.ts`, ordinary package typecheck) and adapted typings programs. |
|
|
46
|
+
| `ReferenceProps` / `ReferenceChildrenProps` | Ported | Public type contract and adapted typings programs. |
|
|
47
|
+
| `PopperProps` / `PopperChildrenProps` / `PopperArrowProps` | Ported | Public type contract and adapted typings programs. |
|
|
48
|
+
| `RefHandler` | Ported | Public type contract. |
|
|
49
|
+
| `Modifier` / `StrictModifier` | Ported | Public type contract (`@ts-expect-error` negative controls for invalid options). |
|
|
50
|
+
| `ManagerReferenceNodeSetterContext` (private) | Ported for adapted tests | Re-exported from `src/components.tsrx` for upstream Reference fixtures; not part of the published `index.ts` surface. |
|
|
51
|
+
| `@popperjs/core` | Reused unchanged | Peer dependency; positioning behavior comes from the pinned core. |
|
|
52
|
+
|
|
53
|
+
## Upstream suite disposition
|
|
54
|
+
|
|
55
|
+
| Pinned artifact | Disposition | Local evidence |
|
|
56
|
+
| --- | --- | --- |
|
|
57
|
+
| `tag/src/Manager.test.js` | Adapted | `tests/upstream/Manager.test.tsx` |
|
|
58
|
+
| `tag/src/Reference.test.js` | Adapted | `tests/upstream/Reference.test.tsx` |
|
|
59
|
+
| `tag/src/Popper.test.js` | Adapted | `tests/upstream/Popper.test.tsx` |
|
|
60
|
+
| `tag/src/usePopper.test.js` | Adapted | `tests/upstream/usePopper.test.tsx` |
|
|
61
|
+
| `tag/src/__snapshots__/*.snap` | Host-tooling adapted | Inventory-checksummed snapshot bodies with Jest 30 header URL. |
|
|
62
|
+
| `tag/typings/tests/main-test.tsx` | Pristine + adapted | Pristine lane via `tsc`; adapted `typetests/main-test.tsx` via `tsrx-tsc`; structural type-parity ledger. |
|
|
63
|
+
| `tag/typings/tests/svg-test.tsx` | Pristine + adapted | Pristine lane via `tsc`; adapted `typetests/svg-test.tsx` via `tsrx-tsc`; structural type-parity ledger. |
|
|
64
|
+
| Flow typings under `tag/src/__typings__` | Out of scope | Octane publishes TypeScript; Flow programs are not part of the Octane binding contract. |
|
|
65
|
+
|
|
66
|
+
Port-authored test classifications (adapted upstream, differential, Octane-only)
|
|
67
|
+
live in `audit/test-classifications.json` and are verified by
|
|
68
|
+
`scripts/react-parity/popper-classifications-lib.mjs`.
|
package/package.json
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@octanejs/popper",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Exact Octane binding for react-popper 2.3.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=22.22.2"
|
|
9
|
+
},
|
|
10
|
+
"author": {
|
|
11
|
+
"name": "Dominic Gannaway",
|
|
12
|
+
"email": "dg@domgan.com"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/octanejs/octane.git",
|
|
20
|
+
"directory": "packages/popper"
|
|
21
|
+
},
|
|
22
|
+
"main": "./src/index.ts",
|
|
23
|
+
"module": "./src/index.ts",
|
|
24
|
+
"types": "./src/index.ts",
|
|
25
|
+
"files": [
|
|
26
|
+
"src",
|
|
27
|
+
"README.md",
|
|
28
|
+
"UPSTREAM.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"exports": {
|
|
32
|
+
".": "./src/index.ts",
|
|
33
|
+
"./package.json": "./package.json"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"react-fast-compare": "^3.2.2"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@popperjs/core": "^2.0.0",
|
|
40
|
+
"octane": "0.1.35"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@babel/core": "^7.28.0",
|
|
44
|
+
"@babel/plugin-transform-modules-commonjs": "^7.27.1",
|
|
45
|
+
"@babel/polyfill": "^7.12.1",
|
|
46
|
+
"@babel/preset-env": "^7.28.0",
|
|
47
|
+
"@babel/preset-flow": "^7.27.1",
|
|
48
|
+
"@babel/preset-react": "^7.27.1",
|
|
49
|
+
"@popperjs/core": "2.11.8",
|
|
50
|
+
"@popperjs/core-pristine": "npm:@popperjs/core@2.3.3",
|
|
51
|
+
"@testing-library/react": "16.3.2",
|
|
52
|
+
"@testing-library/react-hooks": "8.0.1",
|
|
53
|
+
"@tsrx/react": "^0.2.56",
|
|
54
|
+
"@types/react": "^19.2.17",
|
|
55
|
+
"@types/react-dom": "^19.2.3",
|
|
56
|
+
"babel-jest": "30.4.1",
|
|
57
|
+
"jest": "30.4.2",
|
|
58
|
+
"jest-environment-jsdom": "30.4.1",
|
|
59
|
+
"playwright": "^1.61.1",
|
|
60
|
+
"pretty-format": "30.4.1",
|
|
61
|
+
"react": "^19.2.7",
|
|
62
|
+
"react-18": "npm:react@18.3.1",
|
|
63
|
+
"react-dom": "^19.2.7",
|
|
64
|
+
"react-dom-18": "npm:react-dom@18.3.1",
|
|
65
|
+
"react-popper": "2.3.0",
|
|
66
|
+
"react-test-renderer-18": "npm:react-test-renderer@18.3.1",
|
|
67
|
+
"vite": "^8.1.5",
|
|
68
|
+
"vitest": "^4.1.10",
|
|
69
|
+
"warning": "^4.0.3",
|
|
70
|
+
"octane": "0.1.35"
|
|
71
|
+
},
|
|
72
|
+
"scripts": {
|
|
73
|
+
"test": "pnpm upstream:verify && pnpm test:audit && pnpm test:type-parity && pnpm typecheck && vitest run --root ../.. --project popper --project popper-ssr --project popper-hydration --project popper-differential",
|
|
74
|
+
"upstream:verify": "node audit/upstream-inventory.mjs",
|
|
75
|
+
"test:audit": "node --test tests/audit/upstream-inventory.test.mjs",
|
|
76
|
+
"test:type-parity": "node ../../scripts/react-parity/popper-types.mjs",
|
|
77
|
+
"typecheck": "tsrx-tsc --noEmit -p tsconfig.json && tsrx-tsc --noEmit -p typetests/tsconfig.adapted.json && tsrx-tsc --noEmit -p typetests/tsconfig.public-api.json && tsc --noEmit -p typetests/tsconfig.pristine.json"
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
useCallback,
|
|
4
|
+
useContext,
|
|
5
|
+
useEffect,
|
|
6
|
+
useMemo,
|
|
7
|
+
useRef,
|
|
8
|
+
useState,
|
|
9
|
+
} from 'octane';
|
|
10
|
+
import type { OctaneNode } from 'octane';
|
|
11
|
+
import { usePopper } from './usePopper';
|
|
12
|
+
import type { ManagerProps, PopperChildrenProps, PopperProps, ReferenceProps } from './types';
|
|
13
|
+
import { setRef } from './utils';
|
|
14
|
+
|
|
15
|
+
const ManagerReferenceNodeContext = createContext<Element | null>(null);
|
|
16
|
+
/** Private upstream Manager export; kept for Reference identity tests. */
|
|
17
|
+
export const ManagerReferenceNodeSetterContext = createContext<
|
|
18
|
+
((node: Element | null) => void) | undefined
|
|
19
|
+
>(undefined);
|
|
20
|
+
|
|
21
|
+
export function Manager({ children }: ManagerProps): OctaneNode {
|
|
22
|
+
const [referenceNode, setReferenceNode] = useState<Element | null>(null);
|
|
23
|
+
const hasUnmounted = useRef(false);
|
|
24
|
+
useEffect(
|
|
25
|
+
() => () => {
|
|
26
|
+
hasUnmounted.current = true;
|
|
27
|
+
},
|
|
28
|
+
[],
|
|
29
|
+
);
|
|
30
|
+
const handleSetReferenceNode = useCallback((node: Element | null) => {
|
|
31
|
+
if (!hasUnmounted.current) setReferenceNode(node);
|
|
32
|
+
}, []);
|
|
33
|
+
return <ManagerReferenceNodeContext.Provider value={referenceNode}>
|
|
34
|
+
<ManagerReferenceNodeSetterContext.Provider
|
|
35
|
+
value={handleSetReferenceNode}
|
|
36
|
+
>{children}</ManagerReferenceNodeSetterContext.Provider>
|
|
37
|
+
</ManagerReferenceNodeContext.Provider>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function Reference({ children, innerRef }: ReferenceProps): OctaneNode {
|
|
41
|
+
const setReferenceNode = useContext(ManagerReferenceNodeSetterContext);
|
|
42
|
+
const ref = useCallback((node: HTMLElement | null) => {
|
|
43
|
+
setRef(innerRef, node);
|
|
44
|
+
setReferenceNode?.(node);
|
|
45
|
+
}, [innerRef, setReferenceNode]);
|
|
46
|
+
useEffect(() => () => setRef(innerRef, null), []);
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (process.env.NODE_ENV !== 'production' && !setReferenceNode) {
|
|
49
|
+
// OCTANE DIVERGENCE[popper-dev-warning-console][runtime:baa5fd0dba1358db]
|
|
50
|
+
console.error('Warning: `Reference` should not be used outside of a `Manager` component.');
|
|
51
|
+
}
|
|
52
|
+
}, [setReferenceNode]);
|
|
53
|
+
return children({ ref });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const NOOP = () => undefined;
|
|
57
|
+
const NOOP_PROMISE = () => Promise.resolve(null);
|
|
58
|
+
const EMPTY_MODIFIERS: ReadonlyArray<never> = [];
|
|
59
|
+
|
|
60
|
+
export function Popper<Modifiers = string>({
|
|
61
|
+
placement = 'bottom',
|
|
62
|
+
strategy = 'absolute',
|
|
63
|
+
modifiers = EMPTY_MODIFIERS,
|
|
64
|
+
referenceElement,
|
|
65
|
+
onFirstUpdate,
|
|
66
|
+
innerRef,
|
|
67
|
+
children,
|
|
68
|
+
}: PopperProps<Modifiers>): OctaneNode {
|
|
69
|
+
const referenceNode = useContext(ManagerReferenceNodeContext);
|
|
70
|
+
const [popperElement, setPopperElement] = useState<HTMLElement | null>(null);
|
|
71
|
+
const [arrowElement, setArrowElement] = useState<HTMLElement | null>(null);
|
|
72
|
+
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
setRef(innerRef, popperElement);
|
|
75
|
+
}, [innerRef, popperElement]);
|
|
76
|
+
|
|
77
|
+
const options = useMemo(
|
|
78
|
+
() => ({
|
|
79
|
+
placement,
|
|
80
|
+
strategy,
|
|
81
|
+
onFirstUpdate,
|
|
82
|
+
modifiers: [
|
|
83
|
+
...modifiers,
|
|
84
|
+
{
|
|
85
|
+
name: 'arrow' as const,
|
|
86
|
+
enabled: arrowElement != null,
|
|
87
|
+
options: { element: arrowElement },
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
}),
|
|
91
|
+
[placement, strategy, onFirstUpdate, modifiers, arrowElement],
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const { state, styles, forceUpdate, update } = usePopper(
|
|
95
|
+
referenceElement || referenceNode,
|
|
96
|
+
popperElement,
|
|
97
|
+
options as any,
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const childrenProps = useMemo<PopperChildrenProps>(() => {
|
|
101
|
+
const result = {
|
|
102
|
+
ref: setPopperElement,
|
|
103
|
+
style: styles.popper,
|
|
104
|
+
placement: state?.placement ?? placement,
|
|
105
|
+
hasPopperEscaped: state?.modifiersData.hide?.hasPopperEscaped ?? null,
|
|
106
|
+
isReferenceHidden: state?.modifiersData.hide?.isReferenceHidden ?? null,
|
|
107
|
+
arrowProps: { style: styles.arrow, ref: setArrowElement },
|
|
108
|
+
forceUpdate: forceUpdate ?? NOOP,
|
|
109
|
+
update: update ?? NOOP_PROMISE,
|
|
110
|
+
};
|
|
111
|
+
return result as unknown as PopperChildrenProps;
|
|
112
|
+
}, [placement, state, styles, update, forceUpdate]);
|
|
113
|
+
|
|
114
|
+
return children(childrenProps);
|
|
115
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { Manager, Popper, Reference } from './components.tsrx';
|
|
2
|
+
export { usePopper } from './usePopper';
|
|
3
|
+
export type {
|
|
4
|
+
ManagerProps,
|
|
5
|
+
Modifier,
|
|
6
|
+
PopperArrowProps,
|
|
7
|
+
PopperChildrenProps,
|
|
8
|
+
PopperProps,
|
|
9
|
+
RefHandler,
|
|
10
|
+
ReferenceChildrenProps,
|
|
11
|
+
ReferenceProps,
|
|
12
|
+
StrictModifier,
|
|
13
|
+
} from './types';
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Instance,
|
|
3
|
+
Modifier as PopperModifier,
|
|
4
|
+
Options,
|
|
5
|
+
Placement,
|
|
6
|
+
PositioningStrategy,
|
|
7
|
+
State,
|
|
8
|
+
StrictModifiers,
|
|
9
|
+
VirtualElement,
|
|
10
|
+
} from '@popperjs/core';
|
|
11
|
+
import type { OctaneNode } from 'octane';
|
|
12
|
+
import type { CSSProperties } from 'react';
|
|
13
|
+
|
|
14
|
+
export type RefObject<T> = { current: T | null };
|
|
15
|
+
export type RefCallback<T> = (value: T | null) => void;
|
|
16
|
+
export type Ref<T> = RefCallback<T> | RefObject<T> | null | undefined;
|
|
17
|
+
export type RefHandler = (ref: HTMLElement | null) => void;
|
|
18
|
+
export type Style = CSSProperties;
|
|
19
|
+
|
|
20
|
+
// OCTANE DIVERGENCE[popper-octane-node-and-ref-types][types:popper-adapted-main]
|
|
21
|
+
// OCTANE DIVERGENCE[popper-octane-node-and-ref-types][types:popper-adapted-svg]
|
|
22
|
+
export interface ManagerProps {
|
|
23
|
+
children: OctaneNode;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ReferenceChildrenProps {
|
|
27
|
+
ref: Ref<any>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ReferenceProps {
|
|
31
|
+
children: (props: ReferenceChildrenProps) => OctaneNode;
|
|
32
|
+
innerRef?: Ref<any>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface PopperArrowProps {
|
|
36
|
+
ref: Ref<any>;
|
|
37
|
+
style: Style;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface PopperChildrenProps {
|
|
41
|
+
ref: Ref<any>;
|
|
42
|
+
style: Style;
|
|
43
|
+
placement: Placement;
|
|
44
|
+
isReferenceHidden?: boolean;
|
|
45
|
+
hasPopperEscaped?: boolean;
|
|
46
|
+
update: () => Promise<null | Partial<State>>;
|
|
47
|
+
forceUpdate: () => Partial<State>;
|
|
48
|
+
arrowProps: PopperArrowProps;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
type UnionWhere<U, M> = U extends M ? U : never;
|
|
52
|
+
type StrictModifierNames = NonNullable<StrictModifiers['name']>;
|
|
53
|
+
|
|
54
|
+
export type StrictModifier<Name extends StrictModifierNames = StrictModifierNames> = UnionWhere<
|
|
55
|
+
StrictModifiers,
|
|
56
|
+
{ name?: Name }
|
|
57
|
+
>;
|
|
58
|
+
|
|
59
|
+
export type Modifier<
|
|
60
|
+
Name,
|
|
61
|
+
ModifierOptions extends object = object,
|
|
62
|
+
> = Name extends StrictModifierNames
|
|
63
|
+
? StrictModifier<Name>
|
|
64
|
+
: Partial<PopperModifier<Name, ModifierOptions>>;
|
|
65
|
+
|
|
66
|
+
export interface PopperProps<Modifiers = string> {
|
|
67
|
+
children: (props: PopperChildrenProps) => OctaneNode;
|
|
68
|
+
innerRef?: Ref<any>;
|
|
69
|
+
modifiers?: ReadonlyArray<Modifier<Modifiers>>;
|
|
70
|
+
placement?: Placement;
|
|
71
|
+
strategy?: PositioningStrategy;
|
|
72
|
+
referenceElement?: HTMLElement | VirtualElement;
|
|
73
|
+
onFirstUpdate?: (state: Partial<State>) => void;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export type UsePopperOptions<Modifiers = string> = Omit<Partial<Options>, 'modifiers'> & {
|
|
77
|
+
createPopper?: typeof import('@popperjs/core').createPopper;
|
|
78
|
+
modifiers?: ReadonlyArray<Modifier<Modifiers>>;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export interface UsePopperResult {
|
|
82
|
+
styles: Record<string, Style>;
|
|
83
|
+
attributes: Record<string, Record<string, string> | undefined>;
|
|
84
|
+
state: State | null;
|
|
85
|
+
update: Instance['update'] | null;
|
|
86
|
+
forceUpdate: Instance['forceUpdate'] | null;
|
|
87
|
+
}
|
package/src/usePopper.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { VirtualElement } from '@popperjs/core';
|
|
2
|
+
import type { UsePopperOptions, UsePopperResult } from './types';
|
|
3
|
+
import { usePopperImpl } from './usePopper.tsrx';
|
|
4
|
+
|
|
5
|
+
export function usePopper<Modifiers = string>(
|
|
6
|
+
referenceElement?: Element | VirtualElement | null,
|
|
7
|
+
popperElement?: HTMLElement | null,
|
|
8
|
+
options?: UsePopperOptions<Modifiers>,
|
|
9
|
+
): UsePopperResult;
|
|
10
|
+
export function usePopper<Modifiers = string>(...runtimeArgs: unknown[]): UsePopperResult {
|
|
11
|
+
// The compiler appends a call-site Symbol to custom-hook calls. Strip it in
|
|
12
|
+
// this plain-TypeScript boundary before optional arguments are interpreted;
|
|
13
|
+
// the surrounding withSlot scope remains active for the compiled hook body.
|
|
14
|
+
const tail = runtimeArgs[runtimeArgs.length - 1];
|
|
15
|
+
const userArgs = typeof tail === 'symbol' ? runtimeArgs.slice(0, -1) : runtimeArgs;
|
|
16
|
+
const [referenceElement, popperElement, options] = userArgs as [
|
|
17
|
+
Element | VirtualElement | null | undefined,
|
|
18
|
+
HTMLElement | null | undefined,
|
|
19
|
+
UsePopperOptions<Modifiers> | undefined,
|
|
20
|
+
];
|
|
21
|
+
return usePopperImpl(referenceElement, popperElement, options ?? {});
|
|
22
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { createPopper as defaultCreatePopper } from '@popperjs/core';
|
|
2
|
+
import type { Modifier as CoreModifier, Options } from '@popperjs/core';
|
|
3
|
+
import { flushSync, useLayoutEffect, useMemo, useRef, useState } from 'octane';
|
|
4
|
+
import isEqual from 'react-fast-compare';
|
|
5
|
+
import type { UsePopperOptions, UsePopperResult } from './types';
|
|
6
|
+
|
|
7
|
+
const EMPTY_MODIFIERS: ReadonlyArray<never> = [];
|
|
8
|
+
|
|
9
|
+
export function usePopperImpl<Modifiers = string>(
|
|
10
|
+
referenceElement?: Element | import('@popperjs/core').VirtualElement | null,
|
|
11
|
+
popperElement?: HTMLElement | null,
|
|
12
|
+
options: UsePopperOptions<Modifiers> = {},
|
|
13
|
+
): UsePopperResult {
|
|
14
|
+
const previousOptions = useRef<Partial<Options> | null>(null);
|
|
15
|
+
const instanceRef = useRef<import('@popperjs/core').Instance | null>(null);
|
|
16
|
+
const [renderState, setRenderState] = useState<{
|
|
17
|
+
styles: UsePopperResult['styles'];
|
|
18
|
+
attributes: UsePopperResult['attributes'];
|
|
19
|
+
}>({
|
|
20
|
+
styles: {
|
|
21
|
+
popper: { position: options.strategy ?? 'absolute', left: '0', top: '0' },
|
|
22
|
+
arrow: { position: 'absolute' },
|
|
23
|
+
},
|
|
24
|
+
attributes: {},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const onFirstUpdate = options.onFirstUpdate;
|
|
28
|
+
const placement = options.placement ?? 'bottom';
|
|
29
|
+
const strategy = options.strategy ?? 'absolute';
|
|
30
|
+
const modifiers = options.modifiers ?? EMPTY_MODIFIERS;
|
|
31
|
+
const createPopper = options.createPopper ?? defaultCreatePopper;
|
|
32
|
+
|
|
33
|
+
const updateStateModifier = useMemo<CoreModifier<'updateState', Record<string, never>>>(
|
|
34
|
+
() => ({
|
|
35
|
+
name: 'updateState',
|
|
36
|
+
enabled: true,
|
|
37
|
+
phase: 'write',
|
|
38
|
+
fn: ({ state }) => {
|
|
39
|
+
const elements = Object.keys(state.elements);
|
|
40
|
+
flushSync(() => {
|
|
41
|
+
setRenderState({
|
|
42
|
+
styles: Object.fromEntries(
|
|
43
|
+
elements.map((element) => [element, state.styles[element] ?? {}]),
|
|
44
|
+
) as UsePopperResult['styles'],
|
|
45
|
+
attributes: Object.fromEntries(
|
|
46
|
+
elements.map((element) => [element, state.attributes[element]]),
|
|
47
|
+
) as UsePopperResult['attributes'],
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
requires: ['computeStyles'],
|
|
52
|
+
}),
|
|
53
|
+
[],
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const popperOptions = useMemo<Partial<Options>>(() => {
|
|
57
|
+
const nextOptions: Partial<Options> = {
|
|
58
|
+
onFirstUpdate,
|
|
59
|
+
placement,
|
|
60
|
+
strategy,
|
|
61
|
+
modifiers: [
|
|
62
|
+
...modifiers as ReadonlyArray<CoreModifier<any, any>>,
|
|
63
|
+
updateStateModifier,
|
|
64
|
+
{ name: 'applyStyles', enabled: false },
|
|
65
|
+
],
|
|
66
|
+
};
|
|
67
|
+
if (isEqual(previousOptions.current, nextOptions)) {
|
|
68
|
+
return previousOptions.current ?? nextOptions;
|
|
69
|
+
}
|
|
70
|
+
previousOptions.current = nextOptions;
|
|
71
|
+
return nextOptions;
|
|
72
|
+
}, [onFirstUpdate, placement, strategy, modifiers, updateStateModifier]);
|
|
73
|
+
|
|
74
|
+
useLayoutEffect(() => {
|
|
75
|
+
instanceRef.current?.setOptions(popperOptions);
|
|
76
|
+
}, [popperOptions]);
|
|
77
|
+
|
|
78
|
+
useLayoutEffect(() => {
|
|
79
|
+
if (referenceElement == null || popperElement == null) return;
|
|
80
|
+
const instance = createPopper(referenceElement, popperElement, popperOptions);
|
|
81
|
+
instanceRef.current = instance;
|
|
82
|
+
return () => {
|
|
83
|
+
instance.destroy();
|
|
84
|
+
if (instanceRef.current === instance) instanceRef.current = null;
|
|
85
|
+
};
|
|
86
|
+
}, [referenceElement, popperElement, createPopper]);
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
state: instanceRef.current?.state ?? null,
|
|
90
|
+
styles: renderState.styles,
|
|
91
|
+
attributes: renderState.attributes,
|
|
92
|
+
update: instanceRef.current?.update ?? null,
|
|
93
|
+
forceUpdate: instanceRef.current?.forceUpdate ?? null,
|
|
94
|
+
};
|
|
95
|
+
}
|