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

Commit 525b27f

Browse files
authored
feat: export some useful Flow types (#154)
These types may be useful to consumers to better type their own code. This commit aims to get on pair with #153
1 parent 9b5f677 commit 525b27f

File tree

10 files changed

+110
-18
lines changed

10 files changed

+110
-18
lines changed

.flowconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
[lints]
1111

1212
[options]
13+
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectError
1314

1415
[strict]

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ const Example = () => (
244244
);
245245
```
246246
247+
## Flow and TypeScript types
248+
249+
This library is built with Flow but it supports TypeScript as well.
250+
251+
You can find the exported Flow types in `src/index.js`, and the
252+
TypeScript definitions in `typings/react-popper.d.ts`.
253+
247254
## Running Locally
248255
249256
#### clone repo

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"build:clean": "rimraf dist/ && rimraf lib/",
1919
"build:es": "cross-env BABEL_ENV=es babel src --ignore '*.test.js,__mocks__' --out-dir lib/es",
2020
"build:cjs": "cross-env BABEL_ENV=cjs babel src --ignore '*.test.js,__mocks__' --out-dir lib/cjs",
21-
"build:flow": "flow-copy-source --ignore '{__mocks__/*,*.test}.js' src lib/es",
21+
"build:flow": "flow-copy-source --ignore '{__mocks__/*,*.test}.js' src lib/cjs",
2222
"demo:dev": "parcel --out-dir demo/dist demo/index.html",
2323
"demo:build": "parcel build --out-dir demo/dist demo/index.html --public-url=/react-popper",
2424
"demo:deploy": "yarn demo:build && gh-pages -d demo/dist",
@@ -62,6 +62,7 @@
6262
"create-react-context": "^0.2.1",
6363
"popper.js": "^1.14.1",
6464
"prop-types": "^15.6.1",
65+
"typed-styles": "^0.0.5",
6566
"warning": "^3.0.0"
6667
},
6768
"devDependencies": {

src/Manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const ManagerContext: Context<{
77
referenceNode?: ?HTMLElement,
88
}> = createContext({ getReferenceRef: undefined, referenceNode: undefined });
99

10-
type ManagerProps = {
10+
export type ManagerProps = {
1111
children: Node,
1212
};
1313
type ManagerState = {

src/Popper.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,36 @@ import PopperJS, {
77
type Modifiers,
88
type ReferenceObject,
99
} from 'popper.js';
10+
import type { Style } from 'typed-styles';
1011
import { ManagerContext } from './Manager';
1112
import { unwrapArray } from './utils';
1213

1314
type getRefFn = (?HTMLElement) => void;
14-
type Style = Object;
15-
1615
type ReferenceElement = ReferenceObject | HTMLElement | null;
16+
type StyleOffsets = { top: number, left: number };
17+
type StylePosition = { position: 'absolute' | 'fixed' };
1718

18-
type RenderProp = ({|
19+
export type PopperArrowProps = {
20+
ref: getRefFn,
21+
style: StyleOffsets & Style,
22+
};
23+
export type PopperChildrenProps = {|
1924
ref: getRefFn,
20-
style: Style,
21-
placement: ?Placement,
25+
style: StyleOffsets & StylePosition & Style,
26+
placement: Placement,
2227
outOfBoundaries: ?boolean,
2328
scheduleUpdate: () => void,
24-
arrowProps: {
25-
ref: getRefFn,
26-
style: Style,
27-
},
28-
|}) => Node;
29+
arrowProps: PopperArrowProps,
30+
|};
31+
export type PopperChildren = PopperChildrenProps => Node;
2932

30-
type PopperProps = {
33+
export type PopperProps = {
3134
modifiers?: Modifiers,
3235
placement?: Placement,
3336
eventsEnabled?: boolean,
3437
positionFixed?: boolean,
3538
referenceElement?: ReferenceElement,
36-
children: RenderProp,
39+
children: PopperChildren,
3740
};
3841

3942
type PopperState = {

src/Reference.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import warning from 'warning';
44
import { ManagerContext } from './Manager';
55
import { unwrapArray } from './utils';
66

7-
type ReferenceProps = {
8-
children: ({ ref: (?HTMLElement) => void }) => Node,
7+
export type ReferenceChildrenProps = { ref: (?HTMLElement) => void };
8+
export type ReferenceProps = {
9+
children: ReferenceChildrenProps => Node,
910
};
1011

1112
export default function Reference({ children }: ReferenceProps) {

src/__typings__/main-test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// @flow
2+
3+
// Please remember to update also the TypeScript test files that can
4+
// be found under `/typings/tests` please. Thanks! 🤗
5+
6+
import React from 'react';
7+
import { Manager, Reference, Popper } from '..';
8+
9+
export const Test = () => (
10+
<Manager>
11+
{/* $FlowExpectError: empty children */}
12+
<Reference />
13+
<Reference>{({ ref }) => <div ref={ref} />}</Reference>
14+
<Popper
15+
// $FlowExpectError: should be boolean
16+
eventsEnabled="foo"
17+
eventsEnabled
18+
// $FlowExpectError: should be boolean
19+
positionFixed={2}
20+
positionFixed
21+
// $FlowExpectError: enabled should be boolean, order number
22+
modifiers={{ flip: { enabled: 'bar', order: 'foo' } }}
23+
modifiers={{ flip: { enabled: false } }}
24+
>
25+
{({
26+
ref,
27+
style,
28+
placement,
29+
outOfBoundaries,
30+
scheduleUpdate,
31+
arrowProps,
32+
}) => (
33+
<div
34+
ref={ref}
35+
style={{ ...style, opacity: outOfBoundaries ? 0 : 1 }}
36+
data-placement={placement}
37+
onClick={() => scheduleUpdate()}
38+
>
39+
Popper
40+
<div ref={arrowProps.ref} style={arrowProps.style} />
41+
</div>
42+
)}
43+
</Popper>
44+
<Popper>
45+
{({ ref, style, placement }) => (
46+
<div ref={ref} style={style} data-placement={placement}>
47+
Popper
48+
</div>
49+
)}
50+
</Popper>
51+
</Manager>
52+
);

src/index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
// @flow
2+
3+
// Public components
24
import Popper, { placements } from './Popper';
35
import Manager from './Manager';
46
import Reference from './Reference';
5-
67
export { Popper, placements, Manager, Reference };
8+
9+
// Public types
10+
import type { Placement } from 'popper.js';
11+
import type { ManagerProps } from './Manager';
12+
import type { ReferenceProps, ReferenceChildrenProps } from './Reference';
13+
import type {
14+
PopperChildrenProps,
15+
PopperArrowProps,
16+
PopperProps,
17+
} from './Popper';
18+
export type {
19+
Placement,
20+
ManagerProps,
21+
ReferenceProps,
22+
ReferenceChildrenProps,
23+
PopperChildrenProps,
24+
PopperArrowProps,
25+
PopperProps,
26+
};

typings/tests/main-test.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
// Please remember to update also the Flow test files that can
2+
// be found under `/src/__typings__` please. Thanks! 🤗
3+
14
import * as React from 'react';
25
import { Manager, Reference, Popper } from '../..';
36

4-
const Test = () => (
7+
export const Test = () => (
58
<Manager>
69
<Reference>{({ ref }) => <div ref={ref} />}</Reference>
710
<Popper

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6327,6 +6327,10 @@ type-check@~0.3.2:
63276327
dependencies:
63286328
prelude-ls "~1.1.2"
63296329

6330+
typed-styles@^0.0.5:
6331+
version "0.0.5"
6332+
resolved "https://registry.yarnpkg.com/typed-styles/-/typed-styles-0.0.5.tgz#a60df245d482a9b1adf9c06c078d0f06085ed1cf"
6333+
63306334
typedarray@^0.0.6:
63316335
version "0.0.6"
63326336
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

0 commit comments

Comments
 (0)