Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0759cdc

Browse files
ricokahlerjscottsmith
authored andcommitted
add typings!
1 parent 8b23a86 commit 0759cdc

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed

‎__tests__/typingTests/tsconfig.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"jsx": "react",
4+
"strict": true
5+
}
6+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// this file doesn't actually run with the test runner. it is just used to
2+
// verify the typings work as expected
3+
import * as React from 'react';
4+
import { withController, WithControllerInjectedProps } from '../../src';
5+
6+
describe('typescript tests: withController', () => {
7+
it("has an error because the `TestProps` don't include `parallaxController`", () => {
8+
interface TestProps {
9+
foo: string;
10+
}
11+
12+
function TestComponent({ }: TestProps) {
13+
return null;
14+
}
15+
16+
// expect TS error here
17+
withController(TestComponent);
18+
});
19+
20+
it("doesn't error if `TestProps` include `parallaxController`", () => {
21+
interface TestProps extends WithControllerInjectedProps {
22+
foo: string;
23+
}
24+
25+
function TestComponent({ }: TestProps) {
26+
return null;
27+
}
28+
29+
// should _not_ have an error
30+
withController(TestComponent);
31+
});
32+
33+
it('removes the `parallaxController` prop from the wrapped component', () => {
34+
interface TestProps extends WithControllerInjectedProps {
35+
foo: string;
36+
}
37+
38+
function TestComponent({ foo }: TestProps) {
39+
return null;
40+
}
41+
42+
const Enhanced = withController(TestComponent);
43+
44+
// should _not_ have an error because the prop `parallaxController` was removed
45+
const x = <Enhanced foo="test" />;
46+
});
47+
});

‎package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
"@storybook/addon-links": "^5.0.6",
7474
"@storybook/addons": "^5.0.6",
7575
"@storybook/react": "^5.0.6",
76+
"@types/jest": "^24.0.11",
77+
"@types/react": "^16.8.10",
7678
"babel-core": "^7.0.0-bridge.0",
7779
"babel-eslint": "^10.0.1",
7880
"babel-jest": "^23.4.2",

‎src/index.d.ts‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import * as React from 'react';
2+
3+
// ========================
4+
// === ParallaxProvider ===
5+
// ========================
6+
export interface ParallaxProviderProps {
7+
/**
8+
* Optionally pass the scroll axis for setting horizontal/vertical scrolling. One of vertical or
9+
* horizontal
10+
*/
11+
scrollAxis?: 'vertical' | 'horizontal';
12+
/**
13+
* Optionally set the container that has overflow and will contain parallax elements. Defaults
14+
* to the HTML body
15+
*/
16+
scrollContainer?: any;
17+
}
18+
export const ParallaxProvider: React.ComponentType<ParallaxProviderProps>;
19+
20+
// ================
21+
// === Parallax ===
22+
// ================
23+
export interface ParallaxProps {
24+
/**
25+
* Offsets on x-axis in % or px. If no unit is passed percent is assumed. Percent is based on
26+
* the elements width.
27+
*/
28+
x?: Array<string | number>;
29+
/**
30+
* Offsets on y-axis in % or px. If no unit is passed percent is assumed. Percent is based on
31+
* the elements width.
32+
*/
33+
y?: Array<string | number>;
34+
/**
35+
* Optionally pass additional class names to be added to the outermost parallax element.
36+
*/
37+
className?: string;
38+
/**
39+
* Disables parallax effects on individual elements when true.
40+
*/
41+
disabled?: boolean;
42+
/**
43+
* Optionally pass a style object to be added to the innermost parallax element.
44+
*/
45+
styleInner?: any;
46+
/**
47+
* Optionally pass a style object to be added to the outermost parallax element.
48+
*/
49+
styleOuter?: any;
50+
/**
51+
* Optionally pass an element tag name to be applied to the innermost parallax element.
52+
*/
53+
tagInner?: any;
54+
/**
55+
* Optionally pass an element tag name to be applied to the outermost parallax element.
56+
*/
57+
tagOuter?: any;
58+
}
59+
export const Parallax: React.ComponentType<ParallaxProps>;
60+
61+
// =======================
62+
// === Parallax Banner ===
63+
// =======================
64+
export interface BannerLayer {
65+
/**
66+
* A value from `-1` to `1` that represents the vertical offset to be applied to the current
67+
* layer, `0.1` would equal a `10%` offset on the top and bottom.
68+
*/
69+
amount: number;
70+
/**
71+
* Custom layer children provided as a React element, for example `<Video />`
72+
*/
73+
children: any;
74+
/**
75+
* Indicate if the layer should be expanded with negative top/bottom margins so the edges will
76+
* never be visible.
77+
*/
78+
expanded?: boolean;
79+
/**
80+
* Image source that will be applied as a CSS background image on the layer.
81+
*/
82+
image?: string;
83+
}
84+
85+
export interface ParallaxBannerProps {
86+
/**
87+
* Optionally pass additional class names to be added to the outermost parallax banner element.
88+
*/
89+
className?: string;
90+
/**
91+
* Determines if the internal parallax layers will have offsets applied.
92+
*/
93+
disabled?: boolean;
94+
/**
95+
* A required Array of Objects with layer properties: `[{ amount: 0.1, image: 'foo.jpg' }]`.
96+
*/
97+
layers: BannerLayer[];
98+
/**
99+
* Optionally pass a style object to be added to the outermost parallax banner element.
100+
*/
101+
style?: any;
102+
}
103+
export const ParallaxBanner: React.ComponentType<ParallaxBannerProps>;
104+
105+
export interface ParallaxController {
106+
update(): void;
107+
destroy(): void;
108+
}
109+
export interface WithControllerInjectedProps {
110+
parallaxController: ParallaxController;
111+
}
112+
113+
// helper to remove props from a type
114+
type RemoveProps<T, U extends keyof T> = Pick<T, Exclude<keyof T, U>>;
115+
116+
export function withController<P extends WithControllerInjectedProps>(
117+
Component: React.ComponentType<P>
118+
): React.ComponentType<RemoveProps<P, 'parallaxController'>>;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /