Facebook Announces Yoga
Facebook Announces Yoga
Zone Leader John Vester examines the cross-platform layout engine that has been recently announced by the team at Facebook.
Join the DZone community and get the full member experience.Join For Free
The development team at Facebook recently released Yoga. I know you might be tempted to stop reading this post and immediately search "Facebook Yoga" to cut to the chase. But be warned that the results will more likely resemble groups with pages on Facebook focused on physical, mental, and spiritual practices than anything programming related.
Let me save you from one of those internet searches that don't provide exactly the results you are expecting.
What is Facebook Yoga?
Emil Sjölander, a software engineer at Facebook, recently announced Yoga, calling it a "cross-platform layout engine" for application design. With the varied approaches for layouts often differing across application platforms (Android, iOS, web-based), teams developing across multiple platforms are often duplicating efforts to meet their current needs.
"Today, layout is handled differently on each platform, through systems like Auto Layout on iOS, composable containers on Android, and various CSS-based approaches on the web. Having such a diverse set of layout systems makes it hard for teams building products to share solutions across platforms and increases the amount of time developers spend fixing platform-specific layout bugs."
- Emil Sjölander (Facebook)
Yoga introduces a cross-platform library (Java for Android, Objective-C for UIKit and C# for .NET) that can be used in products like React Native, Components for Android and Oculus. The team is continuing to extend their reach by migrating some views in Instagram as well as ComponentKit.
As one might expect, Yoga is OSS too, which can be found here at GitHub.
How Does It Work?
Using Java as an (Android) example, a new YogaNode (called root) can be established with some initial properties being set:
YogaNode root = new YogaNode(); root.setWidth(500); root.setHeight(300); root.setAlignItems(CENTER); root.setJustifyContent(CENTER); root.setPadding(ALL, 20);
From there, nodes for a text box and image can be configured as well:
YogaNode text = new YogaNode(); text.setWidth(200); text.setHeight(25); YogaNode image = new YogaNode(); image.setWidth(50); image.setHeight(50); image.setPositionType(ABSOLUTE); image.setPosition(END, 20); image.setPosition(TOP, 20);
Finally, the text and image nodes can be attached to the root node:
root.addChildAt(text, 0); root.addChildAt(image, 1);
When displayed, the end-result will appear as follows:
Facebook Yoga is intended to reduce the effort of designing layouts across multiple platforms while fostering the goal of creating an optimal layout that can be shared and optimized. Emil Sjölander has even stated that "sharing a layout implementation across platforms has also enabled us to optimize performance once and see the gains across many product and platforms."
Software engineering teams that support different platforms should consider using Facebook Yoga to standardize on the layout aspect of their applications. In doing so, these teams can work together to build a reusable layout that can be applied to all the platforms that Yoga supports. Since Yoga is part of the open source community, team members can give back and contribute to the project where improvements or optimizations are found.
What are your thoughts? Will Yoga be part of your application design in 2017?
Have a really great day!
Opinions expressed by DZone contributors are their own.