Git Product home page Git Product logo

react-fit's Introduction

npm downloads CI

React-Fit

A component that aligns its child relatively to its parent while being aware where it may and may not fit.

tl;dr

  • Install by executing npm install react-fit or yarn add react-fit.
  • Import by adding import Fit from 'react-fit'.
  • Do stuff with it!
    function ElementWithChild() {
      return (
        <Parent>
          <Fit>
            <PopoverChild />
          </Fit>
        </Parent>
      );
    }

Getting started

Compatibility

Your project needs to use React 16.8 or later.

Installation

Add React-Fit to your project by executing npm install react-fit or yarn add react-fit.

How does it work?

  1. By default, the element provided to <Fit /> as a child is displayed below its parent, aligned to the left.
  2. If the element can't fit in this position and collides with bottom and/or right border of the container, <Fit /> checks if there's more space for the element on the other side(s) of the axis/axes the collision(s) has been detected on. If so, the element is moved above its parent and/or aligned to the right, depending on the collision axis.
  3. If the element still can't fit where it's placed, <Fit /> decreases the element's size. If min-width/min-height are provided, they will be respected.

Positioning the element

Vertical axis (default)

By default, the element is displayed below its parent, aligned to the left of its parent.

┌────────────┐
│   Parent   │
├────────────┴────────────┐
│                         │
│         Child           │
│                         │
└─────────────────────────┘
  • To display the element above: provide invertAxis flag.
  • To align the element to the right: provide invertSecondaryAxis flag.

Horizontal axis (mainAxis="x")

By providing mainAxis="x" to <Fit />, the element is displayed on the right of its parent, aligned to the top of its parent.

┌────────────┬─────────────────────────┐
│   Parent   │                         │
└────────────┤         Child           │
             │                         │
             └─────────────────────────┘
  • To display the element on the left: provide invertAxis flag.
  • To align the element to the bottom: provide invertSecondaryAxis flag.

Spacing

By default, React-Fit leaves 8px of space between its child and the borders of the container.

┌──────────────────────────────────────────┐
│ ┌────────────┐                           │
│ │   Parent   │                           │
│ ├────────────┴────────────┐              │
│ │                         │              │
│ │         Child           │              │
│ │                         │              │
│ └─────────────────────────┘              │
└──────────────────────────────────────────┘

If you wish to change this spacing, you can provide spacing to <Fit />. For example, if you wish for the child to touch the borders of the container, decrease the spacing by providing spacing={0} to <Fit />.

┌──────────────────────────────────────────┐
│ ┌────────────┐                           │
│ │   Parent   │                           │
│ ├────────────┴────────────┐              │
│ │                         │              │
│ │         Child           │              │
│ │      (now higher)       │              │
│ │                         │              │
└─┴─────────────────────────┴──────────────┘

You can also provide different spacing for each side by providing an object, for example spacing={{ top: 10, bottom: 20, left: 30, right: 40 }}, to <Fit />. Note: Memoize the object or define it outside render function to avoid unnecessary re-renders.

Styling

To avoid unnecessary style recalculations that may be caused by React-Fit applying the styles needed to make it work properly, the element should have absolute position, and its parent element should have relative or absolute position.

License

The MIT License.

Author

Wojciech Maj Wojciech Maj

react-fit's People

Contributors

dependabot[bot] avatar wojtekmaj avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

Forkers

rudi-kit syphini

react-fit's Issues

`<Fit />'s child needed to have its height decreased to 0px` console warning in tests

I'm using react-date-picker and while executing jest tests for components wrapping react-date-picker I see a lot of console warnings like this:

console.warn
  <Fit />'s child needed to have its height decreased to 0px

      at consoleOnDev (node_modules/react-fit/dist/umd/shared/utils.js:23:34)
      at warnOnDev (node_modules/react-fit/dist/umd/shared/utils.js:32:23)
      at shrinkToSize (node_modules/react-fit/dist/umd/Fit.js:161:28)
      at displayWhereverShrinkedFits (node_modules/react-fit/dist/umd/Fit.js:169:7)
      at alignAxis (node_modules/react-fit/dist/umd/Fit.js:183:5)
      at alignMainAxis (node_modules/react-fit/dist/umd/Fit.js:188:3)
      at alignBothAxis (node_modules/react-fit/dist/umd/Fit.js:203:3)

While I understand that those warnings might be important in the context of a browser, I don't find them to be useful when executed in the context of a node process.

Is there a way to silence them?

TypeError: CSS.supports is not a function in test

I'm using react-date-picker, after updating to the latest version I got the error in tests.

For tests, I'm using react-create-library with jest.

my packege.json is:

"dependencies": {
    "@reach/router": "^1.2.1",
    "jwt-decode": "^2.2.0",
    "keyboardjs": "^2.5.1",
    "password-validator": "^4.1.1",
    "react": "^16.8.6",
    "react-app-polyfill": "^0.2.0",
    "react-aria-menubutton": "^6.1.0",
    "react-date-picker": "^7.5.0",
    "react-dom": "^16.8.6",
    "react-flexbox-grid": "^2.1.2",
    "react-icons": "^3.5.0",
    "react-intl": "^2.8.0",
    "react-modal": "^3.8.1",
    "react-scripts": "2.1.8",
    "react-transition-group": "^2.7.1",
    "react-use": "^7.3.1",
    "rebound": "^0.1.0",
    "rxjs": "^6.4.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "storybook": "start-storybook -p 9001 -c .storybook",
    "storybook-build": "build-storybook -c .storybook -o .out"
  },
  "devDependencies": {
    "@babel/runtime": "^7.4.2",
    "@sambego/storybook-state": "^1.3.4",
    "@storybook/addon-a11y": "^5.0.6",
    "@storybook/addon-actions": "^5.0.6",
    "@storybook/addon-info": "^5.0.6",
    "@storybook/addon-knobs": "^5.0.6",
    "@storybook/addon-links": "^5.0.6",
    "@storybook/addon-viewport": "^5.0.6",
    "@storybook/addons": "^5.0.6",
    "@storybook/client-logger": "^5.0.6",
    "@storybook/react": "^5.0.6",
    "@types/jest": "^24.0.11",
    "@types/jwt-decode": "^2.2.1",
    "@types/node": "^11.12.2",
    "@types/reach__router": "^1.2.3",
    "@types/react": "^16.8.10",
    "@types/react-aria-menubutton": "^6.2.0",
    "@types/react-dom": "^16.8.3",
    "@types/react-intl": "^2.3.17",
    "@types/react-modal": "^3.8.1",
    "@types/react-test-renderer": "^16.8.1",
    "@types/storybook__addon-actions": "^3.4.2",
    "@types/storybook__addon-info": "^4.1.1",
    "@types/storybook__addon-knobs": "^4.0.4",
    "@types/storybook__react": "^4.0.1",
    "awesome-typescript-loader": "^5.2.1",
    "babel-loader": "^8.0.5",
    "babel-polyfill": "^6.26.0",
    "cssom": "^0.3.4",
    "hyperform": "^0.9.23",
    "jest-dom": "^3.1.3",
    "jest-fetch-mock": "^2.1.1",
    "jest-localstorage-mock": "^2.4.0",
    "prettier": "^1.16.4",
    "react-docgen-typescript-loader": "^3.0.1",
    "react-testing-library": "^6.0.3",
    "storybook-addon-intl": "^2.4.1",
    "stylelint": "^9.10.1",
    "stylelint-order": "^2.1.0",
    "tslint": "^5.14.0",
    "tslint-config-prettier": "^1.18.0",
    "tslint-plugin-prettier": "^2.0.1",
    "tslint-react": "^4.0.0",
    "tslint-react-hooks": "^2.0.0",
    "typescript": "^3.4.1",
    "xhr-mock": "^2.4.1"
  },

My temporary fix:
global.CSS = { supports: jest.fn() };

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.