Code Monkey home page Code Monkey logo

yogurt-layout's Introduction

🍦 Yogurt Layout 🥛

A small JS layout computation library, to organize space in SVGs and canvases.

npm

Install

yarn add yogurt-layout

or

npm install yogurt-layout --save

Example

To obtain this result:

screenshot

Do this and then render the given <rect>s:

const layout = makeLayout({
  id: 'root',
  direction: 'column',
  width: 500,
  height: 500,
  padding: 20,
  children: [
    { id: 'title', width: '100%', height: 50 },
    {
      id: 'chart',
      width: '100%',
      height: 'auto',
      direction: 'row',
      padding: 0,
      children: [
        { id: 'left', width: 100, height: '100%' },
        {
          id: 'center-wrapper',
          width: 'auto',
          height: '100%',
          padding: [10, 20],
          direction: 'row',
          children: [{ id: 'center', width: '100%', height: '100%' }],
        },
        { id: 'right', width: 100, height: '100%' },
      ],
    },
    { id: 'legend', width: '100%', height: 150 },
  ],
})

console.log(layout)

// layout === {
//   root: { top, left, right, bottom, width, height },
//   title: { top, left, right, bottom, width, height },
//   chart: { top, left, right, bottom, width, height },
//   legend: { top, left, right, bottom, width, height },
//   ...
// }

API

The only exported function is makeLayout, which is to be called with a nested configuration of LayoutNodes. The root node LayoutNodeRoot must have numerical width and height, the nested ones can have them in pixels (integers), percentages (strings as '50%'), or 'auto' to make it auto-expanding and take the remaining space. Every LayoutNode can also have children, a direction to position them ('row' | 'column') and a padding (ssupporting CSS format as arrays). The output is an object with the ids as keys, and the LayoutBlocks { width, height, left, top } as values.

declare function makeLayout(root: LayoutNodeRoot): Dictionary<LayoutBlock>

// Where:

type LayoutNode = {
  id: string
  children?: LayoutNode[]
  width: number | Percentage | 'auto'
  height: number | Percentage | 'auto'
  direction?: 'row' | 'column'
  padding?: PaddingFormat //
}

type LayoutNodeRoot = LayoutNode & { width: number; height: number; top?: number; left?: number }

type PaddingFormat =
  | number
  | [number, number]
  | [number, number, number, number]
  | { top?: number; right?: number; bottom?: number; left?: number }

type LayoutBlock = {
  id: string
  width: number
  height: number
  top: number
  left: number
}

License

MIT © Accurat


This project was bootstrapped with urca generator.

yogurt-layout's People

Contributors

caesarsol avatar ivanross avatar

Stargazers

Filippo Elgorni avatar Alex Whiteside avatar  avatar Gabriele Lippi avatar Max Hoffmann avatar Alexandre Stahmer avatar Jin Yao avatar Thomas BAY avatar Camilo avatar Andrew Chou avatar Roman Hossain Shaon avatar David Wells avatar Vito Latrofa avatar Ayan Yenbekbay avatar Andrea avatar Mariano Viola avatar Luca Mattiazzi avatar Son avatar Malte avatar Seb Insua avatar TED Vortex (Teodor-Eugen Duțulescu) avatar Nikita avatar v1rtl avatar  avatar Gianmarco avatar Stefano Gallo avatar Ruggero Castagnola avatar Marco Fugaro avatar Ilaria Venturini avatar

Watchers

James Cloos avatar

yogurt-layout's Issues

Feat: Add `margin` and `gap` properties

We need to define a couple things:

  • Should the properties add between each other, or be exclusive?
  • How should the gap work?
    • @ivanross suggests an approach like div > * + * { margin-left: x } where it's placed between childs only if the children are >1.
  • Should gap be number | [number, number], to allow vertical/horizontal?

cc @ivanross @marcofugaro @gsimone (#1)

Feedback

Hey! 👋 Great little lib, I'm playing around with it and react-three-fiber, since laying stuff out is usually painful: https://codesandbox.io/s/blissful-lake-84hxp (<= this might explode while I work on it :D )

Here's a couple of quick things:

  1. gap and margin properties. In the example I wanted to add some gap between items, it would be great being able to do it without having to add a container
  2. direction could default to row like flex-direction does in css
  3. height and width defaults (auto probably), just to reduce boilerplate

Anyway, main message is: Kudos, this is great!

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.