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

Commit

Permalink
feat: export some useful Flow types
Browse files Browse the repository at this point in the history
These types may be useful to consumers to better type their own code.

This commit aims to get on pair with #153
  • Loading branch information
Federico Zivolo committed May 1, 2018
1 parent c8c9cb9 commit cf6c558
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 15 deletions.
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
[lints]

[options]
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectError

[strict]
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ const Example = () => (
);
```
## Flow and TypeScript types
This library is built with Flow but it supports TypeScript as well.
You can find the exported Flow types in `src/index.js`, and the
TypeScript definitions in `typings/react-popper.d.ts`.
## Running Locally
#### clone repo
Expand Down
2 changes: 1 addition & 1 deletion src/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const ManagerContext: Context<{
referenceNode?: ?HTMLElement,
}> = createContext({ getReferenceRef: undefined, referenceNode: undefined });

type ManagerProps = {
export type ManagerProps = {
children: Node,
};
type ManagerState = {
Expand Down
22 changes: 12 additions & 10 deletions src/Popper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,31 @@ import { ManagerContext } from './Manager';
import { unwrapArray } from './utils';

type getRefFn = (?HTMLElement) => void;
type Style = Object;
type Style = { [string]: any };

type ReferenceElement = ReferenceObject | HTMLElement | null;

type RenderProp = ({|
export type PopperArrowProps = {
ref: getRefFn,
style: Style,
placement: ?Placement,
};
export type PopperChildrenProps = {|
ref: getRefFn,
style: Style,
placement: Placement,
outOfBoundaries: ?boolean,
scheduleUpdate: () => void,
arrowProps: {
ref: getRefFn,
style: Style,
},
|}) => Node;
arrowProps: PopperArrowProps,
|};
export type PopperChildren = PopperChildrenProps => Node;

type PopperProps = {
export type PopperProps = {
modifiers?: Modifiers,
placement?: Placement,
eventsEnabled?: boolean,
positionFixed?: boolean,
referenceElement?: ReferenceElement,
children: RenderProp,
children: PopperChildren,
};

type PopperState = {
Expand Down
5 changes: 3 additions & 2 deletions src/Reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import warning from 'warning';
import { ManagerContext } from './Manager';
import { unwrapArray } from './utils';

type ReferenceProps = {
children: ({ ref: (?HTMLElement) => void }) => Node,
export type ReferenceChildrenProps = { ref: (?HTMLElement) => void };
export type ReferenceProps = {
children: ReferenceChildrenProps => Node,
};

export default function Reference({ children }: ReferenceProps) {
Expand Down
52 changes: 52 additions & 0 deletions src/__typings__/main-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// @flow

// Please remember to update also the TypeScript test files that can
// be found under `/typings/tests` please. Thanks! 🤗

import React from 'react';
import { Manager, Reference, Popper } from '..';

export const Test = () => (
<Manager>
{/* $FlowExpectError: empty children */}
<Reference />
<Reference>{({ ref }) => <div ref={ref} />}</Reference>
<Popper
// $FlowExpectError: should be boolean
eventsEnabled="foo"
eventsEnabled
// $FlowExpectError: should be boolean
positionFixed={2}
positionFixed
// $FlowExpectError: enabled should be boolean, order number
modifiers={{ flip: { enabled: 'bar', order: 'foo' } }}
modifiers={{ flip: { enabled: false } }}
>
{({
ref,
style,
placement,
outOfBoundaries,
scheduleUpdate,
arrowProps,
}) => (
<div
ref={ref}
style={{ ...style, opacity: outOfBoundaries ? 0 : 1 }}
data-placement={placement}
onClick={() => scheduleUpdate()}
>
Popper
<div ref={arrowProps.ref} style={arrowProps.style} />
</div>
)}
</Popper>
<Popper>
{({ ref, style, placement }) => (
<div ref={ref} style={style} data-placement={placement}>
Popper
</div>
)}
</Popper>
</Manager>
);
22 changes: 21 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
// @flow

// Public components
import Popper, { placements } from './Popper';
import Manager from './Manager';
import Reference from './Reference';

export { Popper, placements, Manager, Reference };

// Public types
import type { Placement } from 'popper.js';
import type { ManagerProps } from './Manager';
import type { ReferenceProps, ReferenceChildrenProps } from './Reference';
import type {
PopperChildrenProps,
PopperArrowProps,
PopperProps,
} from './Popper';
export type {
Placement,
ManagerProps,
ReferenceProps,
ReferenceChildrenProps,
PopperChildrenProps,
PopperArrowProps,
PopperProps,
};
5 changes: 4 additions & 1 deletion typings/tests/main-test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Please remember to update also the Flow test files that can
// be found under `/src/__typings__` please. Thanks! 🤗

import * as React from 'react';
import { Manager, Reference, Popper } from '../..';

const Test = () => (
export const Test = () => (
<Manager>
<Reference>{({ ref }) => <div ref={ref} />}</Reference>
<Popper
Expand Down

0 comments on commit cf6c558

Please sign in to comment.