Code Monkey home page Code Monkey logo

react-detectable-overflow's Introduction

React Detectable Overflow

npm version

A React hook and component detecting overflow state.

Install

npm install react-detectable-overflow

or

yarn add react-detectable-overflow

Example

Hook useOverflowDetector

import * as React from 'react';
import { useOverflowDetector } from 'react-detectable-overflow';

const SampleComponent = () => {
  const { ref, overflow } = useOverflowDetector({});

  return (
    <div
      ref={ref}
      style={{
        textOverflow: 'ellipsis',
        whiteSpace: 'nowrap',
        overflow: 'hidden',
        width: '120px',
        backgroundColor: overflow ? 'red' : 'green',
      }}
    >
      This is a sample text.
    </div>
  );
};

Class DetectableOverflow

import * as React from 'react';
import DetectableOverflow from 'react-detectable-overflow';

const SampleComponent = () => {
  const [overflow, setOverflow] = useState(false);

  return (
    <DetectableOverflow
      onChange={setOverflow}
      style={{
        textOverflow: 'ellipsis',
        whiteSpace: 'nowrap',
        overflow: 'hidden',
        width: '120px',
        backgroundColor: overflow ? '#F9E9CF' : '#BCF2E7',
      }}
    >
      This is a sample text.
    </DetectableOverflow>
  );
};

Caution

Be careful when the size of children content depends on overflow state. The following code perhaps causes the infinite loop of changing overflow state.

import * as React from 'react';
import { useOverflowDetector } from 'react-detectable-overflow';

// DO NOT WRITE LIKE THIS!
const SampleComponent = () => {
  const { ref, overflow } = useOverflowDetector({});

  return <div ref={ref}>{overflow ? 'short' : 'loooooooooooooooooooooooooooooooooooooong'}</div>;
};

License

This package is released under the MIT License, see LICENSE

Changelog

0.8.0

  • Add properties handleHeight and handleWidth

0.7.0

  • Add useOverflowDetector

0.4.0

  • BREAKING CHANGE: Support vertical overflow detection

react-detectable-overflow's People

Contributors

dependabot[bot] avatar h-kanazawa avatar kw510 avatar revmischa avatar syth-1 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

Watchers

 avatar  avatar

react-detectable-overflow's Issues

findDOMNode is deprecated in StrictMode

Hi there!

I've noticed this warning in the console while using react-detectable-overflow:

Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of ChildWrapper which is inside StrictMode. Instead, add a ref directly to the element you want to reference. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-find-node

This is actually a warning shown from the package react-resize-detector that this library uses. As stated in the README, a ref is needed to not use the deprecated findDOMNode.

I believe that a ref is needed in the children that is received and then passed using targetRef to the ReactResizeDetector component over here:

return React.createElement(
tag,
props,
<ReactResizeDetector handleWidth onResize={this.updateState} />,
this.props.children,
);

Am I right? I'm fine with submitting a PR with this fix.

By the way, thank you for this great library!

overflow triggered 1 time

add the ability to trigger overflow 1 time so that when resizing it does not trigger again, I need to detect overflow 1 time

Add support for detecting overflowStart and overflowEnd

Hi, this looks pretty nice already, thank you!
I would like to add some shadow left or right / top or bottom when the element's content is overflowing. Could you add some more specific states and return values for this use case?

No output

I have a table in an absolutely positioned div and the div is set to overflow-y. The vertical scroll bar on the div appears when the table is too long for the div.

Do I wrap the div in DetectableOverflow or should I replace the div with DetectableOverflow?

Either way I'm getting no output from the component.

Chrome zoom out -> false detection

Hi, I found a problem with false detection on Chrome (version 91.0.4472.124).
When I zoom out to 90% on a Demo, component triggers as overflowed.
image

I was trying to debug a problem, looks like bad rounding -> scrollWidth is smaller than offsetWidth. I changed a state detection like this and it seems to work fine for my case but don't know if its a right solution.
const newState = this.ref.current.offsetWidth < this.ref.current.scrollWidth || this.ref.current.offsetHeight < this.ref.current.scrollHeight;

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.