Coder Social home page Coder Social logo

flexbox.js's Introduction

Flexbox.js

Build Status NPM Version

Javascript flexbox layouting engine

Flexbox.js is a javascript implementation for 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

Property type CSS equivalent Notes
x number Offset
funcX (parentW: number, parentH: number) When set, overrules the x property and (re)calculates it from the parent dimensions
y number
funcY (parentW: number, parentH: number)
w number Size
funcW (parentW: number, parentH: number)
h number
funcH (parentW: number, parentH: number)
visible true,false Invisible elements are ignored
flex FlexContainer See below
flexItem FlexItem See below
skipInLayout boolean See 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

Method Description
hasFlexLayout() : boolean Returns true iff this is an enabled flex container and/or item
addChild(child: FlexTarget)
addChildAt(child: FlexTarget, index: number)
removeChildAt(index: number)
toString(): string Returns a string representation
getChildren() : FlexTarget[]
update() : void Updates the layout in this branch. Should only be called on the root node.
getLayoutX() : number The layout result
getLayoutY() : number The layout result
getLayoutW() : number The layout result
getLayoutH() : number The layout result
onChangedLayout() : void Event that is called when the layout results are changed.

FlexContainer properties

Property type CSS equivalent Notes
enabled true,false
direction 'row','row-reverse','column','column-reverse' flex-direction
wrap true,false flex-wrap wrap-reverse is not supported
alignItems 'flex-start','flex-end','center','stretch' align-items baseline 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
padding number padding in pixels
paddingTop number padding-top
paddingLeft number padding-left
paddingBottom number padding-bottom
paddingRight number padding-right

FlexItem properties

Property type CSS equivalent Notes
enabled true,false If disabled, this item will not affect the flex layout and will be positioned absolutely
grow number flex-grow
shrink number flex-shrink The default value is 0 (in CSS it defaults to 1)
alignSelf 'flex-start','flex-end','center','stretch' align-self
order Not supported
flex-basis Not supported (behavior is always as flex-basis:auto)
minWidth number min-width in pixels
maxWidth number max-width in pixels
minHeight number min-height in pixels
maxHeight number max-height in pixels
margin number margin in pixels
marginTop number margin-top
marginLeft number margin-left
marginBottom number margin-bottom
marginRight number margin-right

flexbox.js's People

Contributors

basvanmeurs avatar robbinbaauw avatar dependabot[bot] avatar

Stargazers

CuberQAQ avatar Christer Sandberg avatar Tomer Shalev avatar James avatar Alexander Milevski avatar Sjoerd de Jong avatar Sandalots avatar Abdullah Atta avatar  avatar TheMe avatar Keishi Hashmimic avatar Gennaro Landolfi avatar Krtolica Vujadin avatar Agony avatar Liad Idan avatar Muhammad Athif avatar  avatar 彭科 avatar TianYi Wen avatar Andrew Prifer avatar Kaid Wong avatar 吴泽康 avatar 伊撒尔 avatar Klint avatar 湫曗 avatar bansky avatar Kipp Ashford avatar Zachary Witt avatar  avatar Toan Tran avatar Winston Fassett avatar alwayrun avatar ferrinweb avatar Paul Young avatar

Watchers

 avatar James Cloos avatar  avatar Tim Anema avatar

Forkers

redlove

flexbox.js's Issues

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.