0.8.0 • Published 1 month ago

react-detectable-overflow v0.8.0

Weekly downloads
770
License
MIT
Repository
github
Last release
1 month ago

React Detectable Overflow

npm version

A React hook and component detecting overflow state.

Demo

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
0.8.0

1 month ago

0.7.2

2 months ago

0.6.3

1 year ago

0.7.1

1 year ago

0.7.0

1 year ago

0.6.2

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago