npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

troika-flex-layout

v0.49.1

Published

Utility for calculating flexbox layouts in a web worker

Downloads

164

Readme

troika-flex-layout

This package provides a convenient utility for calculating flexbox layouts in JavaScript, for use in building user interfaces. It is used in the Troika framework for laying out UI elements in WebGL scenes, but has no dependencies on that framework so it can just as easily be used on its own in other projects.

Behind the scenes it uses an ASM-compiled version of the Yoga library to calculate the flexbox layout, and does so within a web worker to prevent main thread frame drops.

For measurement of text nodes it uses the troika-three-text library. You will probably get the best results if you also use troika-three-text for rendering the text post-layout, but that's not strictly required.

Usage

import { requestFlexLayout } from 'troika-flex-layout'

// Describe your layout style tree, with a unique id for each node:
const styleTree = {
  id: 'root',
  width: 100,
  height: 100,
  alignItems: 'center',
  justifyContent: 'center',
  children: [
    {
      id: 'child',
      width: '50%',
      height: '50%'
    }
  ]
}

// Initiate a layout request with a callback function:
requestFlexLayout(styleTree, results => {
  // The results are a mapping of node ids to layout boxes:
  // {
  //   root: { left: 0, top: 0, width: 100, height: 100 },
  //   child: { left: 25, top: 25, width: 50, height: 50 }
  // }
})

Each node in the style tree accepts a number of properties related to the flexbox layout as well as some properties controlling the text. See the complete list of accepted style properties. All properties are optional except the id which is needed to identify that node in the layout results. For allowed values and defaults of the flexbox-related properties, consult the Yoga documentation. For the text-related properties see the troika-three-text documentation.

This package does not...

  • ...manage a style tree's changes over time. It's the responsibility of external framework code to manage a style tree, detect changes, and initiate layout requests as appropriate to respond to those changes.

  • ...render anything. Again, other code is required to take the layout results and apply them to some application-specific rendering objects.

For an example of framework code that performs both these functions, see the troika-3d-ui package.