1.0.17 • Published 4 years ago

flexbox.js v1.0.17

Weekly downloads
3
License
Apache-2.0
Repository
github
Last release
4 years ago

Flexbox.js

Build Status NPM Version

Javascript flexbox layouting engine

Flexbox.js is a layout engine based on the flexbox CSS3 web layout model.

It allows a layout tree to be constructed. This tree consists of nodes on which flex properties may be specified. The layout can be calculated by invoking the update() method on the flex root. After updating the layout, the layout results will be stored in x, y, w, h coordinates for the tree nodes.

Usage

This module was designed to be used by other frameworks that require some form of layouting. It was originally developed for the tree2d framework.

This package contains the FlexTarget class, which can be used directly to construct a layout tree. Usually this will be sufficient. In other cases, where a deeper integration is required, it is also possible to provide your own implementation of FlexTarget by implementing the FlexSubject interface. The latter is used in the tree2d framework to be able to implement advanced performance optimizations.

Create flex tree

import { FlexTarget } from 'flexbox.js';
const root = new FlexTarget();
root.flex.enabled = true;
root.flex.direction = "column";
root.flex.alignItems = "stretch";
const item = new FlexTarget();
item.w = 100;
item.h = 100;
root.addChild(item);
const item2 = new FlexTarget();
item2.w = 50;
item2.h = 50;
root.addChild(item2);

Update layout

You can perform a layout update calling the update() method on the root FlexTarget:

root.update();

After such an update all layout coordinates and sizes will be updated.

Mutations

root.flex.direction = "row";
item.marginLeft = 20;
root.update();

After one or more layout properties have been mutated a layout update is required. The engine keeps track of all changes and will only update the parts that actually need to be updated.

FlexTarget properties

PropertytypeCSS equivalentNotes
xnumberOffset
funcX(parentW: number, parentH: number)When set, overrules the x property and (re)calculates it from the parent dimensions
ynumber
funcY(parentW: number, parentH: number)
wnumberSize
funcW(parentW: number, parentH: number)
hnumber
funcH(parentW: number, parentH: number)
visibletrue,falseInvisible elements are ignored
flexFlexContainerSee below
flexItemFlexItemSee below
skipInLayoutbooleanSee below

Offsets

The x and y properties act as relative positions to the positions calculated by the flexbox layout engine.

Fit to contents

When the width or height is set to the number 0, it will fit to the contents in that direction.

Relative size

Relative functions may be set for x, y, w and h. In that case, the property will be recalculated from the parent's layout width or height. This allows the user to use relative and calculated sizes.

Skipping

The skipInLayout property can be used to skip the node while layouting. In this mode, flex behaves as if this skipped node was replaced by it's own children. This means that if the node itself was a flex item of a flex container, it's children will now become flex items of that flex container.

It also affects relative layout function arguments (w or h). If the parent has skipInLayout set to true, the grandparent's width and height will be used.

This feature was built to enable another framework to expose a flex layoutable tree that's not be necessarily one-to-one with the layout tree.

FlexTarget methods

MethodDescription
hasFlexLayout() : booleanReturns true iff this is an enabled flex container and/or item
addChild(child: FlexTarget)
addChildAt(child: FlexTarget, index: number)
removeChildAt(index: number)
toString(): stringReturns a string representation
getChildren() : FlexTarget[]
update() : voidUpdates the layout in this branch. Should only be called on the root node.
getLayoutX() : numberThe layout result
getLayoutY() : numberThe layout result
getLayoutW() : numberThe layout result
getLayoutH() : numberThe layout result
onChangedLayout() : voidEvent that is called when the layout results are changed.

FlexContainer properties

PropertytypeCSS equivalentNotes
enabledtrue,false
direction'row','row-reverse','column','column-reverse'flex-direction
wraptrue,falseflex-wrapwrap-reverse is not supported
alignItems'flex-start','flex-end','center','stretch'align-itemsbaseline not supported
alignContent'flex-start','flex-end','center','space-between','space-around','space-evenly','stretch'align-content
justifyContent'flex-start','flex-end','center','space-between','space-around','space-evenly'justify-content
paddingnumberpaddingin pixels
paddingTopnumberpadding-top
paddingLeftnumberpadding-left
paddingBottomnumberpadding-bottom
paddingRightnumberpadding-right

FlexItem properties

PropertytypeCSS equivalentNotes
enabledtrue,falseIf disabled, this item will not affect the flex layout and will be positioned absolutely
grownumberflex-grow
shrinknumberflex-shrinkThe default value is 0 (in CSS it defaults to 1)
alignSelf'flex-start','flex-end','center','stretch'align-self
orderNot supported
flex-basisNot supported (behavior is always as flex-basis:auto)
minWidthnumbermin-widthin pixels
maxWidthnumbermax-widthin pixels
minHeightnumbermin-heightin pixels
maxHeightnumbermax-heightin pixels
marginnumbermarginin pixels
marginTopnumbermargin-top
marginLeftnumbermargin-left
marginBottomnumbermargin-bottom
marginRightnumbermargin-right
1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago