Skip to content

Commit cb9eb3b

Browse files
committed
add types and update build
1 parent e280666 commit cb9eb3b

File tree

8 files changed

+4711
-1945
lines changed

8 files changed

+4711
-1945
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

.eslintrc

Lines changed: 0 additions & 10 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/node_modules
33
npm-debug.log*
44
yarn-error.log*
5-
5+
.vscode

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
</body>
4747
<script type="module">
48-
import observeRect from './src/index.js'
48+
import observeRect from './dist/observe-rect.esm.js'
4949
let observer = observeRect(demo, (rect) => {
5050
log.textContent = JSON.stringify(rect, null, 2)
5151
})

package.json

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,55 @@
11
{
22
"name": "@reach/observe-rect",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Observe the Rect of a DOM element.",
5+
"repository": {
6+
"type": "git",
7+
"url": "git+https://github.com/reach/observe-rect",
8+
"directory": "packages/auto-id"
9+
},
510
"scripts": {
6-
"build": "microbundle",
7-
"start": "microbundle watch",
11+
"build": "cross-env NODE_ENV=production tsdx build --format=cjs,esm,umd",
12+
"start": "cross-env NODE_ENV=production tsdx watch",
813
"serve": "serve"
914
},
1015
"files": [
11-
"src",
1216
"dist",
1317
"README.md"
1418
],
1519
"main": "dist/index.js",
16-
"umd:main": "dist/index.umd.js",
17-
"module": "dist/index.m.js",
18-
"source": "src/index.js",
19-
"repository": "https://github.com/reach/observe-rect",
20-
"author": "Ryan Florence <@ryanflorence>",
20+
"umd:main": "dist/observe-rect.umd.production.js",
21+
"module": "dist/observe-rect.esm.js",
22+
"typings": "dist/index.d.ts",
23+
"author": "React Training <[email protected]>",
2124
"license": "MIT",
2225
"dependencies": {},
2326
"devDependencies": {
27+
"@babel/core": "^7.7.7",
28+
"@types/jest": "^24.0.25",
29+
"babel-loader": "^8.0.6",
2430
"babel-preset-env": "^1.7.0",
25-
"eslint": "^6.6.0",
26-
"eslint-config-prettier": "^6.5.0",
27-
"eslint-plugin-jest": "^23.0.2",
28-
"microbundle": "^0.11.0",
29-
"serve": "^11.2.0"
31+
"cross-env": "^6.0.3",
32+
"eslint-config-prettier": "^6.9.0",
33+
"eslint-plugin-jest": "^23.3.0",
34+
"eslint": "^6.8.0",
35+
"husky": "^4.0.1",
36+
"serve": "^11.2.0",
37+
"ts-loader": "^6.2.1",
38+
"tsdx": "^0.12.1",
39+
"tslib": "^1.10.0",
40+
"typescript": "^3.7.4"
3041
},
31-
"prettier": {}
42+
"eslintConfig": {
43+
"extends": ["eslint:recommended", "prettier"],
44+
"parserOptions": {
45+
"sourceType": "module"
46+
},
47+
"env": {
48+
"es6": true,
49+
"browser": true
50+
}
51+
},
52+
"prettier": {
53+
"singleQuote": true
54+
}
3255
}

src/index.js renamed to src/index.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
let props = ["width", "height", "top", "right", "bottom", "left"];
1+
let props: (keyof Rect)[] = [
2+
'bottom',
3+
'height',
4+
'left',
5+
'right',
6+
'top',
7+
'width'
8+
];
29

3-
let rectChanged = (a = {}, b = {}) => props.some(prop => a[prop] !== b[prop]);
10+
let rectChanged = (a: PartialRect = {}, b: PartialRect = {}) =>
11+
props.some(prop => a[prop] !== b[prop]);
412

5-
let observedNodes = new Map();
6-
let rafId;
13+
let observedNodes = new Map<HTMLElement, RectProps>();
14+
let rafId: number;
715

816
let run = () => {
917
observedNodes.forEach(state => {
@@ -26,12 +34,12 @@ let run = () => {
2634
rafId = requestAnimationFrame(run);
2735
};
2836

29-
export default (node, cb) => {
37+
export default (node: HTMLElement, cb: Function) => {
3038
return {
3139
observe() {
3240
let wasEmpty = observedNodes.size === 0;
3341
if (observedNodes.has(node)) {
34-
observedNodes.get(node).callbacks.push(cb);
42+
observedNodes.get(node)!.callbacks.push(cb);
3543
} else {
3644
observedNodes.set(node, {
3745
rect: undefined,
@@ -58,3 +66,16 @@ export default (node, cb) => {
5866
}
5967
};
6068
};
69+
70+
export type Rect = Pick<
71+
DOMRect,
72+
'width' | 'height' | 'top' | 'right' | 'bottom' | 'left'
73+
>;
74+
75+
export type PartialRect = Partial<Rect>;
76+
77+
export type RectProps = {
78+
rect: Rect | undefined;
79+
hasRectChanged: boolean;
80+
callbacks: Function[];
81+
};

tsconfig.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"include": ["src", "types", "test"],
3+
"compilerOptions": {
4+
"target": "es5",
5+
"module": "esnext",
6+
"lib": ["dom", "esnext"],
7+
"importHelpers": true,
8+
"declaration": true,
9+
"sourceMap": true,
10+
"rootDir": "./",
11+
"strict": true,
12+
"noImplicitAny": true,
13+
"strictNullChecks": true,
14+
"strictFunctionTypes": true,
15+
"strictPropertyInitialization": true,
16+
"noImplicitThis": true,
17+
"alwaysStrict": true,
18+
"noUnusedLocals": true,
19+
"noUnusedParameters": true,
20+
"noImplicitReturns": true,
21+
"noFallthroughCasesInSwitch": true,
22+
"moduleResolution": "node",
23+
"baseUrl": "./",
24+
"paths": {
25+
"@": ["./"],
26+
"*": ["src/*", "node_modules/*"]
27+
},
28+
"esModuleInterop": true
29+
}
30+
}
31+

0 commit comments

Comments
 (0)