Coder Social home page Coder Social logo

react-component / resize-observer Goto Github PK

View Code? Open in Web Editor NEW
170.0 6.0 38.0 4.23 MB

👓 Resize observer for React

Home Page: https://resize-observer.react-component.now.sh

License: MIT License

JavaScript 46.13% TypeScript 45.19% Less 8.68%
ant-design antd react react-component

resize-observer's Introduction

rc-resize-observer

NPM version dumi build status Codecov npm download

Resize observer for React.

Live Demo

https://react-component.github.io/resize-observer/

Install

rc-resize-observer

Usage

import ResizeObserver from 'rc-resize-observer';
import { render } from 'react-dom';

render(
  <ResizeObserver
    onResize={() => {
      console.log('resized!');
    }}
  >
    <textarea />
  </ResizeObserver>,
  mountNode,
);

API

Property Type Default Description
disabled boolean false
onResize ({ width, height }) => void - Trigger when child node resized

Development

npm install
npm start

License

rc-resize-observer is released under the MIT license.

resize-observer's People

Contributors

afc163 avatar cyjake avatar dependabot-preview[bot] avatar dependabot[bot] avatar hengkx avatar lgtm-com[bot] avatar xrkffgg avatar zombiej avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

resize-observer's Issues

包裹一个空的React片段 onResize 不执行 这个问题导致 procomponents 的QueryFilter onResize执行有问题

import '../assets/index.less';
import React from 'react';
import ResizeObserver, { ResizeObserverProps } from '../src';

function Com(props) {
  return <>{props.children}</>;
}

export default function App() {
  const [times, setTimes] = React.useState(0);
  const [disabled, setDisabled] = React.useState(false);
  const textareaRef = React.useRef<HTMLTextAreaElement>(null);

  React.useEffect(() => {
    console.log('Ref:', textareaRef.current);
  }, []);

  const onResize: ResizeObserverProps['onResize'] = ({
    width,
    height,
    offsetHeight,
    offsetWidth,
  }) => {
    setTimes(prevTimes => prevTimes + 1);
    console.log(
      'Resize:',
      '\n',
      'BoundingBox',
      width,
      height,
      '\n',
      'Offset',
      offsetWidth,
      offsetHeight,
    );
  };

  return (
    <React.StrictMode>
      <div style={{ transform: 'scale(1.1)', transformOrigin: '0% 0%' }}>
        <div>
          <label>
            <input
              type="checkbox"
              onChange={() => {
                setDisabled(!disabled);
              }}
              checked={disabled}
            />{' '}
            Disabled Observe
          </label>
          {' >>> '}
          <span>Resize times: {times}</span>
        </div>
        <ResizeObserver onResize={onResize} disabled={disabled}>
          <Com>
            <textarea ref={textareaRef} placeholder="I'm a textarea!" />;
          </Com>
        </ResizeObserver>
      </div>
    </React.StrictMode>
  );
}

resizeobserver loop limit exceeded

it's caused by popover'destroyTooltipOnhide property when I click a popover serveral times with chrome devtools closed(devtools open can not trigger)
I used an ant design popver
I found it uses rc-tooltip
and rc-tooltip uses ->trigger ->popup->motion
I don't know what problems it is
but it caused resizeobserver loop limit exceeded

sorry for my poor english

Collection的onBatchResize会闭包state

可在repo内example/collection.tsx文件的onBatchResize函数内添加console.log({ size1 }),观看demo效果。当多次发生resize时,打印的size1始终是第一次渲染时的size1。

原因应该是src/SingleObserver/index.tsx内,onInternalResize用useCallback做了持久化,如果onCollectionResize发生了更新则无法通知onInternalResize做出更新。因此应该加一个

const onCollectionResizeRef = React.useRef(onCollectionResize); onCollectionResizeRef.current = onCollectionResize;

之类的改造;

或者src/Collection.tsx对onBatchResize用ref包装,onResize依赖传空数组做成持久化。

文本节点通过observe监听报类型错误

<ResizeObserver onResize={onResize} disabled={disabled}>
          <Wrapper>
            11
            <textarea ref={textareaRef} placeholder="I'm a textarea!" />
          </Wrapper>
</ResizeObserver>

如果传入文本节点 通过findDOMNode获取到的是nodeType为3的节点,通过调用observe(currentElement, onInternalResize); 传入类型不匹配导致报错,还有就是如果传入() => <div>11</div> 函数渲染的话,通过findDOMNode找不到被监听的函数

我从antd里的Affix中发现的问题 最后定位到这了。

Hooks support

const { width, height } = useResizeObserver({ disabled });

Maximum update depth exceeded

After updating ant.d from v4 to v5 we started to get errors "Maximum update depth exceeded". most of the times these errors comes from our e2e tests written in cypress, but sometimes you can catch it just by clicking select field inside/outside.

I can see that not some time ago this PR was made that had changes in useEffect.
#172

And SingleObserver components is first one in crashing stack trace. Maybe they are connected?

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.