Files
yoga/README.md

79 lines
3.4 KiB
Markdown
Raw Normal View History

2014-10-29 08:54:30 -07:00
Attention: This project is still very experimental and not used in any production application!
2014-04-06 18:41:56 -07:00
css-layout
==========
This project implements a subset of CSS including flexbox and the box model using pure JavaScript. The goal is to have a small standalone library to layout elements. It doesn't rely on the DOM at all.
2014-04-06 18:43:46 -07:00
In order to make sure that the code is correct, it is developped using TDD where each commit adds a unit test and the associated code to make it work. All the unit tests are tested against Chrome's implementation of CSS.
2014-04-16 17:35:25 -07:00
2014-10-29 08:58:36 -07:00
The JavaScript version has been implemented in a way that can be easily transpiled to C and Java via regexes. The layout function doesn't do any allocation nor uses any of the dynamic aspect of JavaScript. The tests are also transpiled to make sure that the implementations are correct everywhere.
2014-04-16 17:35:25 -07:00
Supported Attributes
--------------------
Name | Value
----:|------
2014-04-27 12:17:59 -07:00
width, height | positive number
2014-04-16 17:35:25 -07:00
left, right, top, bottom | number
margin, marginLeft, marginRight, marginTop, marginBottom | number
2014-04-27 12:17:59 -07:00
padding, paddingLeft, paddingRight, paddingTop, paddingBottom | positive number
borderWidth, borderLeftWidth, borderRightWidth, borderTopWidth, borderBottomWidth | positive number
2014-04-16 17:35:25 -07:00
flexDirection | 'column', 'row'
justifyContent | 'flex-start', 'center', 'flex-end', 'space-between', 'space-around'
alignItems, alignSelf | 'flex-start', 'center', 'flex-end', 'stretch'
2014-09-11 10:47:43 -07:00
flex | positive number
2014-04-16 17:35:25 -07:00
position | 'relative', 'absolute'
- `inherit` value is not implemented because it's a way to disambiguate between multiple colliding rules. This should be done in a pre-processing step, not in the actual layout algorithm.
2014-04-16 17:46:07 -07:00
2014-04-16 17:35:25 -07:00
Usage
-----
2014-10-08 09:35:44 -07:00
A single function `computeLayout` is exposed and
2014-04-16 17:35:25 -07:00
- takes a tree of nodes: `{ style: { ... }, children: [ nodes ] }`
2014-04-16 17:46:07 -07:00
- returns a tree of rectanges: `{ width: ..., height: ..., top: ..., left: ..., children: [ rects ] }`
2014-04-16 17:35:25 -07:00
2014-10-08 09:35:44 -07:00
For example,
2014-04-16 17:35:25 -07:00
```javascript
computeLayout(
{style: {height: 100, justifyContent: 'flex-end'}, children: [
{style: {marginTop: 10}}
]}
)
// =>
{width: 0, height: 100, top: 0, left: 0, children: [
{width: 0, height: 0, top: 100, left: 0}
]}
```
Default values
--------------
2014-09-11 10:47:43 -07:00
Since we are only using flexbox, we can use defaults that are much more sensible. This is the configuration to use in order to get the same behavior using the DOM and CSS. You can try those default settings with the [following JSFiddle](http://jsfiddle.net/vjeux/y11txxv9/).
2014-04-16 17:35:25 -07:00
```css
2014-09-11 10:47:43 -07:00
div, span {
2014-04-16 17:35:25 -07:00
box-sizing: border-box;
position: relative;
2014-04-22 11:53:56 -07:00
border: 0 solid black;
2014-04-16 17:35:25 -07:00
margin: 0;
padding: 0;
2014-10-08 09:35:44 -07:00
2014-04-16 17:35:25 -07:00
display: flex;
flex-direction: column;
2014-10-08 09:35:44 -07:00
align-items: stretch;
2014-04-16 17:35:25 -07:00
flex-shrink: 0;
}
```
- `box-sizing: border-box` is the most convenient way to express the relation between `width` and `borderWidth`.
- Everything is `display: flex` by default. All the behaviors of `block` and `inline-block` can be expressed in term of `flex` but not the opposite.
- All the flex elements are oriented from top to bottom, left to right and do not shrink. This is how things are laid out using the default CSS settings and what you'd expect.
- Everything is `position: relative`. This makes `position: absolute` target the direct parent and not some parent which is either `relative` or `absolute`. If you want to position an element relative to something else, you should move it in the DOM instead of relying of CSS. It also makes `top, left, right, bottom` do something when not specifying `position: absolute`.