Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit f501928

Browse files
committed
fix: add usePopper TS types
1 parent 8e27d86 commit f501928

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

typings/react-popper.d.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import * as React from "react";
2-
import * as PopperJS from "@popperjs/core";
1+
import * as React from 'react';
2+
import * as PopperJS from '@popperjs/core';
33

44
interface ManagerProps {
55
children: React.ReactNode;
66
}
7-
export class Manager extends React.Component<ManagerProps, {}> { }
7+
export class Manager extends React.Component<ManagerProps, {}> {}
88

99
interface ReferenceChildrenProps {
1010
// React refs are supposed to be contravariant (allows a more general type to be passed rather than a more specific one)
@@ -17,7 +17,7 @@ interface ReferenceProps {
1717
children: (props: ReferenceChildrenProps) => React.ReactNode;
1818
innerRef?: React.Ref<any>;
1919
}
20-
export class Reference extends React.Component<ReferenceProps, {}> { }
20+
export class Reference extends React.Component<ReferenceProps, {}> {}
2121

2222
export interface PopperArrowProps {
2323
ref: React.Ref<any>;
@@ -44,6 +44,20 @@ export interface PopperProps {
4444
placement?: PopperJS.Placement;
4545
strategy?: PopperJS.PositioningStrategy;
4646
referenceElement?: HTMLElement | PopperJS.VirtualElement;
47-
onFirstUpdate?: (state: Partial<PopperJS.State>) => void
47+
onFirstUpdate?: (state: Partial<PopperJS.State>) => void;
4848
}
49-
export class Popper extends React.Component<PopperProps, {}> { }
49+
export class Popper extends React.Component<PopperProps, {}> {}
50+
51+
export function usePopper(
52+
referenceElement?: Element | null,
53+
popperElement?: HTMLElement | null,
54+
options?: Partial<PopperJS.Options> & {
55+
createPopper?: typeof PopperJS.createPopper;
56+
}
57+
): {
58+
styles: { [key: string]: React.CSSProperties };
59+
attributes: { [key: string]: { [key: string]: string } };
60+
state: PopperJS.State;
61+
update: Pick<PopperJS.Instance, 'update'>;
62+
forceUpdate: Pick<PopperJS.Instance, 'forceUpdate'>;
63+
};

typings/tests/main-test.tsx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// be found under `/src/__typings__` please. Thanks! 🤗
33

44
import * as React from 'react';
5-
import { Manager, Reference, Popper } from '../..';
5+
import { Manager, Reference, Popper, usePopper } from '../..';
66

77
export const Test = () => (
88
<Manager>
@@ -23,7 +23,10 @@ export const Test = () => (
2323
}) => (
2424
<div
2525
ref={ref}
26-
style={{ ...style, opacity: (isReferenceHidden || hasPopperEscaped) ? 0 : 1 }}
26+
style={{
27+
...style,
28+
opacity: isReferenceHidden || hasPopperEscaped ? 0 : 1,
29+
}}
2730
data-placement={placement}
2831
onClick={() => update()}
2932
>
@@ -41,3 +44,32 @@ export const Test = () => (
4144
</Popper>
4245
</Manager>
4346
);
47+
48+
const HookTest = () => {
49+
const [
50+
referenceElement,
51+
setReferenceElement,
52+
] = React.useState<Element | null>(null);
53+
const [popperElement, setPopperElement] = React.useState<HTMLElement | null>(
54+
null
55+
);
56+
const [arrowElement, setArrowElement] = React.useState<HTMLElement | null>(
57+
null
58+
);
59+
const { styles, attributes } = usePopper(referenceElement, popperElement, {
60+
modifiers: [{ name: 'arrow', options: { element: arrowElement } }],
61+
});
62+
63+
return (
64+
<>
65+
<button type="button" ref={setReferenceElement}>
66+
Reference element
67+
</button>
68+
69+
<div ref={setPopperElement} style={styles.popper} {...attributes.popper}>
70+
Popper element
71+
<div ref={setArrowElement} style={styles.arrow} />
72+
</div>
73+
</>
74+
);
75+
};

0 commit comments

Comments
 (0)