Skip to content
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

feat: add prop keepSizePrecision, to keep precision on with and height #155

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions src/SingleObserver/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface SingleObserverProps extends ResizeObserverProps {
}

export default function SingleObserver(props: SingleObserverProps) {
const { children, disabled } = props;
const { children, disabled, keepSizePrecision } = props;
const elementRef = React.useRef<Element>(null);
const wrapperRef = React.useRef<DomWrapper>(null);

Expand Down Expand Up @@ -55,16 +55,16 @@ export default function SingleObserver(props: SingleObserverProps) {
* In most case we just care about element size,
* let's use `boundary` instead of `contentRect` here to avoid shaking.
*/
const fixedWidth = Math.floor(width);
const fixedHeight = Math.floor(height);
const processedWidth = keepSizePrecision ? width : Math.floor(width);
const processedHeight = keepSizePrecision ? height : Math.floor(height);

if (
sizeRef.current.width !== fixedWidth ||
sizeRef.current.height !== fixedHeight ||
sizeRef.current.width !== processedWidth ||
sizeRef.current.height !== processedHeight ||
sizeRef.current.offsetWidth !== offsetWidth ||
sizeRef.current.offsetHeight !== offsetHeight
) {
const size = { width: fixedWidth, height: fixedHeight, offsetWidth, offsetHeight };
const size = { width: processedWidth, height: processedHeight, offsetWidth, offsetHeight };
sizeRef.current = size;

// IE is strange, right?
Expand Down
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface ResizeObserverProps {
disabled?: boolean;
/** Trigger if element resized. Will always trigger when first time render. */
onResize?: OnResize;
/** Keep precision on width and height */
keepSizePrecision?: boolean;
}

function ResizeObserver(props: ResizeObserverProps) {
Expand Down