GithubHelp home page GithubHelp logo

react-autolayout's Introduction

React-Autolayout

Auto layout for React using Apple's Visual Format Language. React-Autolayout is a wrapper around the AutoLayout.js library.

NPM: npm install react-autolayout

Bower: bower install react-autolayout

Script Tag: <script src="path/to/react-autolayout/build/react-autolayout.js"></script> (Module exposed as ReactAutoLayout)

API

Sample Usage

class Demo extends React.Component {

  query(constraints) {
    if('demo' in constraints){
      if(constraints.demo.page.width < 600){
        return 'narrow';
      } else {
        return 'default';
      }
    }
    return 'default';
  }

  render() {
    return (
      <AutoLayout name="demo" query={this.query}
        layout={[
          { 
            name: 'default',
            constrainTo: 'viewport',
            format: ['|~[page(90%)]~|',
                    'V:|-15%-[page]-150-|']
          },
          { 
            name: 'narrow',
            constrainTo: 'viewport',
            format: ['|~[page(60%)]~|',
                    'V:|-15%-[page]-150-|']
          }
        ]}>
        <div viewKey="page" >
          ...
        </div>
      </AutoLayout>
    );
  }
}

<AutoLayout /> props

name: string

The name prop defines a region that will have auto layout applied and is used to identify the region. It is required and must be unique.

layout: array[object]

layout holds an array of layout configuration objects. The different configurations get applied in response to query changes (see next). Each configuration object must have the following 3 properties:

  • name: string is used to identify the configuration.
  • constrainTo: string | array specifies which view the layout region should be contrained to. There are three possible values:
    • 'viewport' contrain to the window
    • '${name}.${viewKey}' constrain to another view
    • [width, height] constrain to a fixed width and height in px. Width and height are specified as a number without the px suffix
  • format: array[string] takes an array of string specified by the Visual Format Language. Please refer to AutoLayout.js documentation for usage.

query: function -> string

query(constraints, currentFormat){
    if('demo' in constraints){
      if(constraints.demo.page.width < 600){
        return 'narrow';
      } else {
        return 'default';
      }
    }
    return 'default';
}

The query prop takes a function that returns a string representing the name of the layout to be applied. It can be thought of as a custom media query in which you specify the break points based on the criteria you determine.

The query function will receive two parameters.

The first is constraints. constraints holds the current state of all autolayout objects. You are able to reference view width and height to determine break points. The shape of constraints is ${name}.${viewKey}.width|height

The second is currentFormat. This is a string that stores the format currently applied to the region.

htmlTag: string

This is an optional prop that allows you to specify the type of html element to use for the region.

child element props

<AutoLayout>
    <div viewKey="child1" >
      ...
    </div>
    <div viewKey="child2" >
      ...
    </div>
    <div viewKey="child3" >
      ...
    </div>
</AutoLayout>

Child elements define the view to be laid out. You can have as many child views as required. Child elements cannot be React Components and must be the first level element within AutoLayout. A view has the following props:

  • viewKey: string The viewKey is used within the Visual Format to identify the view. viewKey's must be unique within a Component and is a required prop.
  • formatStyle: object is an optional prop that specifies styles to be applied to certain layouts. You do not need to specify every layout, only the ones that you would like a style aplied to. The format style is then merge with any existing styles set on the view.

An example of a formatStyle prop within a view is:

<div 
    viewKey="child2" 
    formatStyle={{
        narrow: {
            backgroundColor: 'black',
            zIndex: 10
        }
    }}>
    <div>child2 Text</div>
</div>

Caveats

  • Setting the following styles on child elements have no effect as they are overridden. Default values are displayed.
    • top: dynamically set
    • left: dynamically set
    • width: dynamically set
    • height: dynamically set
    • margin: 0
    • padding: 0
    • position: absolute
    • transform: dynamically set
  • box-sizing: border-box; is the default
  • Visual Format Z property is not available

ToDo

  • More Example Code
  • Tests

Author

Frank Panetta - Follow @fattenap

License

The MIT License (MIT)

Copyright (c) 2015-2016 Frank Panetta

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

react-autolayout's People

Contributors

fattenap avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

react-autolayout's Issues

Real-world problems & Seed

@fattenap
Hi, I was thinking about something similar for Seed some months ago - a combination of writing / recording layout constraints or expected behaviors for values (extra/inter)polation in GUI designer or text IDE.

What problems did you encounter while writing / using this lib?
I can imagine that JS was slow for more complex websites or it makes almost impossible to prerender website / use SSR because of not using CSS; or something else?

Documentation

Hello,

I want to take some time to try this out, what is the current state of development ? Is it usable ?

Also regarding the documentation, there are 2 things that I don't quite understand :

  • In the query, the condition constraints.demo.page.width < 600 references the viewkey of the layout that is currently being "evaluated". If it is the somewhat equivalent to media queries, I would have expected the condition to be set on the viewport...?
  • I also don' understand the "Caveat" part. Could you be a little more explicit about what would not work ?

Thank you for the work, I really find autolayout interesting and it's great to be able to combine it with react...

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.