Skip to content

Commit

Permalink
(add) : Add support for eslint (airbnb style guide).
Browse files Browse the repository at this point in the history
  • Loading branch information
Gautam-Arora24 committed Oct 20, 2021
1 parent b4d415b commit 917ece0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
23 changes: 23 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"env": {
"browser": true,
"es6": true
},
"extends": ["plugin:react/recommended", "airbnb", "prettier"],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": ["react"],
"rules": {
"no-restricted-syntax": "off",
"no-use-before-define": ["error", { "functions": false }]
}
}
4 changes: 2 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"singleQuote": true,
"jsxSingleQuote": true,
"semi": false,
"semi": true,
"tabWidth": 2,
"bracketSpacing": true,
"jsxBracketSameLine": false,
"arrowParens": "always",
"trailingComma": "none"
"trailingComma": "es5"
}
44 changes: 23 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
import { useState, useEffect, useRef } from 'react'
import { useState, useEffect, useRef } from 'react';

const useResizeObserver = (option = 'contentRect') => {
const ref = useRef(null)
const [height, setHeight] = useState()
const [width, setWidth] = useState()
const ref = useRef(null);
const [height, setHeight] = useState();
const [width, setWidth] = useState();

useEffect(() => {
if (ref.current) {
const observer = new ResizeObserver((entries) => {
handleResize(entries)
})
observer.observe(ref.current)
handleResize(entries);
});
observer.observe(ref.current);

// Callback fired when component is unmounted
return () => {
observer.disconnect()
}
observer.disconnect();
};
}
})
// Added this return for eslint rule -> no-consisten-return
return undefined;
});

const handleResize = (entries) => {
for (let entry of entries) {
function handleResize(entries) {
for (const entry of entries) {
if (
option === 'borderBoxSize' &&
entry.borderBoxSize &&
entry.borderBoxSize.length > 0
) {
setHeight(entry.borderBoxSize[0].blockSize)
setWidth(entry.borderBoxSize[0].inlineSize)
setHeight(entry.borderBoxSize[0].blockSize);
setWidth(entry.borderBoxSize[0].inlineSize);
} else if (
option === 'contentBoxSize' &&
entry.contentBoxSize &&
entry.contentBoxSize.length > 0
) {
setHeight(entry.contentBoxSize[0].blockSize)
setWidth(entry.contentBoxSize[0].inlineSize)
setHeight(entry.contentBoxSize[0].blockSize);
setWidth(entry.contentBoxSize[0].inlineSize);
} else {
setHeight(entry.contentRect.height)
setWidth(entry.contentRect.width)
setHeight(entry.contentRect.height);
setWidth(entry.contentRect.width);
}
}
}

return [ref, width, height]
}
return [ref, width, height];
};

export default useResizeObserver
export default useResizeObserver;

0 comments on commit 917ece0

Please sign in to comment.