@ark-ui/react 0.0.0-rc-20230119065628 → 0.0.0-rc-20230119080906
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.
|
@@ -5,6 +5,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
5
5
|
const popover = require('@zag-js/popover');
|
|
6
6
|
const react = require('@zag-js/react');
|
|
7
7
|
const React = require('react');
|
|
8
|
+
const useLatestRef = require('../use-latest-ref.cjs');
|
|
8
9
|
|
|
9
10
|
function _interopNamespaceDefault(e) {
|
|
10
11
|
const n = Object.create(null, { [Symbol.toStringTag]: { value: 'Module' } });
|
|
@@ -26,12 +27,25 @@ function _interopNamespaceDefault(e) {
|
|
|
26
27
|
const popover__namespace = /*#__PURE__*/_interopNamespaceDefault(popover);
|
|
27
28
|
|
|
28
29
|
const usePopover = (props) => {
|
|
30
|
+
const { isOpen, ...restProps } = props;
|
|
29
31
|
const context = {
|
|
30
32
|
id: React.useId(),
|
|
31
|
-
...
|
|
33
|
+
...restProps
|
|
32
34
|
};
|
|
33
35
|
const [state, send] = react.useMachine(popover__namespace.machine(context), { context });
|
|
34
|
-
|
|
36
|
+
const api = popover__namespace.connect(state, send, react.normalizeProps);
|
|
37
|
+
const apiRef = useLatestRef.useLatestRef(api);
|
|
38
|
+
React.useEffect(() => {
|
|
39
|
+
if (isOpen == null) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (isOpen && !state.matches("open")) {
|
|
43
|
+
apiRef.current.open();
|
|
44
|
+
} else if (!isOpen && !state.matches("closed")) {
|
|
45
|
+
apiRef.current.close();
|
|
46
|
+
}
|
|
47
|
+
}, [isOpen, state, apiRef]);
|
|
48
|
+
return api;
|
|
35
49
|
};
|
|
36
50
|
|
|
37
51
|
exports.usePopover = usePopover;
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import type { HTMLAttributes, DetailedHTMLProps, ButtonHTMLAttributes } from 'react';
|
|
2
2
|
import * as popover from '@zag-js/popover';
|
|
3
3
|
import type { Optional } from '../types';
|
|
4
|
-
export declare type UsePopoverProps = Optional<popover.Context, 'id'
|
|
4
|
+
export declare type UsePopoverProps = Optional<popover.Context, 'id'> & {
|
|
5
|
+
/**
|
|
6
|
+
* Control the open state of the popover.
|
|
7
|
+
*
|
|
8
|
+
* @default false
|
|
9
|
+
*/
|
|
10
|
+
isOpen?: boolean;
|
|
11
|
+
};
|
|
5
12
|
export declare const usePopover: (props: UsePopoverProps) => {
|
|
6
13
|
portalled: boolean;
|
|
7
14
|
isOpen: boolean;
|
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
import * as popover from '@zag-js/popover';
|
|
2
2
|
import { useMachine, normalizeProps } from '@zag-js/react';
|
|
3
|
-
import { useId } from 'react';
|
|
3
|
+
import { useId, useEffect } from 'react';
|
|
4
|
+
import { useLatestRef } from '../use-latest-ref.mjs';
|
|
4
5
|
|
|
5
6
|
const usePopover = (props) => {
|
|
7
|
+
const { isOpen, ...restProps } = props;
|
|
6
8
|
const context = {
|
|
7
9
|
id: useId(),
|
|
8
|
-
...
|
|
10
|
+
...restProps
|
|
9
11
|
};
|
|
10
12
|
const [state, send] = useMachine(popover.machine(context), { context });
|
|
11
|
-
|
|
13
|
+
const api = popover.connect(state, send, normalizeProps);
|
|
14
|
+
const apiRef = useLatestRef(api);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (isOpen == null) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (isOpen && !state.matches("open")) {
|
|
20
|
+
apiRef.current.open();
|
|
21
|
+
} else if (!isOpen && !state.matches("closed")) {
|
|
22
|
+
apiRef.current.close();
|
|
23
|
+
}
|
|
24
|
+
}, [isOpen, state, apiRef]);
|
|
25
|
+
return api;
|
|
12
26
|
};
|
|
13
27
|
|
|
14
28
|
export { usePopover };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const React = require('react');
|
|
6
|
+
|
|
7
|
+
function useLatestRef(value) {
|
|
8
|
+
const ref = React.useRef(value);
|
|
9
|
+
ref.current = value;
|
|
10
|
+
return ref;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
exports.useLatestRef = useLatestRef;
|