Skip to content

Add option for different types of dimensions #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useState, useRef, useMemo } from "react";

export default function({ ref, onResize } = {}) {
export default function({ ref, onResize, type } = {}) {
// `defaultRef` Has to be non-conditionally declared here whether or not it'll
// be used as that's how hooks work.
// @see https://reactjs.org/docs/hooks-rules.html#explanation
Expand Down Expand Up @@ -40,9 +40,31 @@ export default function({ ref, onResize } = {}) {

const entry = entries[0];

// `Math.round` is in line with how CSS resolves sub-pixel values
const newWidth = Math.round(entry.contentRect.width);
const newHeight = Math.round(entry.contentRect.height);
let newWidth, newHeight;
switch (type) {
case "scroll":
// Actual size of content and padding, including content hidden by
// element-level scroll bars
newWidth = entry.target.scrollWidth;
newHeight = entry.target.scrollHeight;
break;
case "client":
// Displayed size of content and padding
newWidth = entry.target.clientWidth;
newHeight = entry.target.clientHeight;
break;
case "offset":
// Displayed size of content, padding, and border
newWidth = entry.target.offsetWidth;
newHeight = entry.target.offsetHeight;
break;
case "content":
default:
// Displayed size of content only
// `Math.round` is in line with how CSS resolves sub-pixel values
newWidth = Math.round(entry.contentRect.width);
newHeight = Math.round(entry.contentRect.height);
}
if (
previous.current.width !== newWidth ||
previous.current.height !== newHeight
Expand Down