1.0.0 • Published 6 years ago

react-contraint-layout v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

react-constraint-layout

react-constraint-layout is a Javascript library that provides a constraint based solution to position HTML elements relative to each other and to the parent with zero CSS.

npm.io npm.io npm.io npm.io

You can support the project by starring it on Github. Click here to visit the projects Github page

If you like this project, you can support it by becoming a patreon here

Install

npm install --save-dev react-constraint-layout or npm i -D react-constraint-layout

Screenshots

npm.io npm.io

No, those screenshots are not Android. That's React, and the best part; they were done with zero CSS (No Bootstrap, No Flexbox, No CSS grid)

API

The library exports three UI components which form the core of the framework:

  1. ConstraintLayout
  2. ConstrainedView
  3. ConstraintGuide
ConstraintLayout

This component is the root component that positions its children relative to each other. It supports the following props:

PropTypeDescription
widthstring or number (E.g. 20px or 20)The width of the component. Not specifying will default to the full width of its parent
heightstring or number (E.g. 50px or 50)The height of the component. Since children are absolutely positioned, the height cannot be automatically estimated. Hence this is required

The <ConstraintLayout/> only supports <ConstrainedView/>, <ConstraintGuide/> and <ConstraintLayout/> as its direct children.

ConstrainedView

This component is a direct child of the <ConstraintLayout/>. This components provides constraint-related props for the positioning of itself relative to sibling views.

PropTypeDescription
idstringThe identifier for the component. This is necessary if other views will be constrained to it.
asstring or FunctionThis allows you to specify a tag name for the <Constrained /> view. You can also specify another component to be rendered (More on this). Defaults to div
widthstring or number (E.g. 20px or 20)The width of the component. Not specifying will default to 0px. A zero-width component's width will only be controlled by the horizontal constraints (if any)
heightstring or number (E.g. 50px or 50)The height of the component. Not specifying will default to 0px. A zero-height component's width will only be controlled by the vertical constraints (if any)
leftToLeftOfstring or ArrayIndicates that the left of this component is aligned to the left of another component specified by this id
leftToRightOfstring or ArrayIndicates that the left of this component is aligned to the right of another component specified by this id
rightToRightOfstring or ArrayIndicates that the right of this component is aligned to the right of another component specified by this id
rightToLeftOfstring or ArrayIndicates that the right of this component is aligned to the left of another component specified by this id
topToTopOfstring or ArrayIndicates that the top of this component is aligned to the top of another component specified by this id
topToBottomOfstring or ArrayIndicates that the top of this component is aligned to the bottom of another component specified by this id
bottomToBottomOfstring or ArrayIndicates that the bottom of this component is aligned to the bottom of another component specified by this id
bottomToTopOfstring or ArrayIndicates that the bottom of this component is aligned to the top of another component specified by this id
horizontalBiasnumber (0 to 1)The bias used to shift the component along its constraint axis if it is fully horizontally constrained
verticalBiasnumber (0 to 1)The bias used to shift the component along its constraint axis if it is fully vertically constrained

Since the props are merged, you can add any React HTML attribute (E.g. style, onClick, etc.) directly to the <ConstrainedView /> component and the framework will render them on the DOM element itself.

The value of a constraint prop (I.e. leftToLeftOf, leftToRightOf, etc.) can be set to _parent to align with the parent. As an example, we can use leftToLeftOf="_parent" to align the left of a component to the left of it's <ConstraintLayout/> parent.

If an array of ids is passed to any of the constraint props (I.e. leftToLeftOf, leftToRightOf, etc.), the solver will search the children with each array element and select the one which first matches a child. This is useful if some children are conditionally rendered. If none of the ids match any of the children, the parent reference is used for that prop

All the props of <ConstrainedView /> can be used with <ConstraintLayout /> because nesting of <ConstraintLayout /> is also supported

ConstraintGuide

This is a non-visual component that serves as a guide for other <ConstrainedView /> components to align with. For example, if you want a <ConstrainedView /> to be aligned at 20% from the left of the parent view, you would create a <ConstraintGuide /> and set its percent to 20%. The best way to think of this component is as an invisible ruler within the <ConstraintLayout />.

It supports the following props:

PropTypeDescription
idstringThe identifier for the guide. This is necessary if other views will be constrained to it.
orientationstring Can be vertical or horizontalSets the orientation of the guide
beginstring or numberThe starting point of the guideline in px. If orientation is horizontal, it will be that number of pixels from the left of the <ConstraintLayout />
endstring or numberThe starting point of the guideline in px. If orientation is horizontal, it will be that number of pixels from the right of the <ConstraintLayout />
percentnumberThe starting point of the guideline in %. If orientation is horizontal, it will be that percentage of the <ConstraintLayout /> width from the left

Examples

The examples folder contains code samples for the two user interfaces showcased in the screenshots.

Maintainers

Contributing

If you'd like to contribute to the project development, you can do so by:

  1. Clone the project from here
  2. Start storybook server by using npm run storybook
  3. Add features to project
  4. Create a PR on the project

I'll then review the pull request and if all checks out, it'll be merged to the master branch and you'll be added to the list of maintainers

Support

If you'd like to support this project, you can do so by becoming a patreon on Patreon

It would be really helpful if you can star the project on Github

Licence

MIT (c) Kwame Opare Asiedu

1.0.0

6 years ago