Adaptive layouts
Lay a screen out for whatever size the app has: a phone folded or unfolded, turned on its side, or sharing the screen with another app.
Lynx 4 lays the page out again whenever the app's size changes, as long as the host app tells it the new size. The apps ng build --platform makes do, on iOS and Android, and send the size to your code too. So most of a screen adapts by itself, and the rest takes a signal.
Lengths that follow the size
- Prefer
%and flex layout for a screen's columns: they follow the parent, so a component works the same in a sheet or a split view. rpxscales with the window, so a750rpxbox is as wide as the window on a phone's outer screen and on its inner one. A 16px margin written as32rpxdoubles when the phone unfolds. Keeprpxfor things meant to be a share of the window.
Change the screen with LynxViewport
When the layout itself should change, such as showing a list and its details side by side once there's room, read LynxViewport:
width()andheight()are the page's size, whichvw,vhand a page-wide%measure against.screenWidth()andscreenHeight()are the window's, whichrpxmeasures against. In an app fromng build --platformthe two are the same, except with"edgeToEdge": falseand, on Android, while the keyboard is open.orientation()is'portrait'or'landscape': the page's shape, not the way the phone is held. An unfolded phone held upright can be either.- The values arrive before Lynx lays the page out at the new size, so the screen changes in one step.
LynxSystemInfo's screenWidth and screenHeight are the screen's size when the page started, and don't follow a fold. Use LynxViewport instead.
@media rules
Lynx 4 supports CSS media queries, and the build turns them on. A @media (min-width: 600px) rule, or Tailwind's md: and the other breakpoints, applies on iOS and Android, against the page's size, and again when the size changes:
- Lynx measures the page:
widthandheightare the LynxView's size in CSS pixels, asLynxViewport.width()andheight()are. Lynx's media queries page lists the other features it reads, such asorientationand ranges like(width >= 600px). - Don't toggle
displaywith one. On Lynx 4.1, adisplayset in a media rule doesn't always apply, at launch or after the size changes, though colors, sizes and opacity do. Show or hide a part of the screen with@ifandLynxViewportinstead:
@media (prefers-color-scheme: dark) and Tailwind's dark: on their own are turned into rules the page's appearance switches, so they follow LynxTheme. A query that asks for a color scheme and something else, such as (prefers-color-scheme: dark) and (min-width: 600px), is left to Lynx, which answers the color scheme from the app's appearance in the apps ng build --platform makes: see Dark mode.
What changes the size
Through all of these the app keeps running: the screen isn't rebuilt, and its state stays.
In your own native app
A native app that embeds the app does what Lynx's foldable guide describes whenever its window or LynxView changes size, in this order:
- The window's size, which
rpxfollows:updateScreenMetricsWithWidth:height:on iOS, in points, orupdateScreenMetrics(width, height)on Android, in pixels. On its own it lays nothing out. - The LynxView's size:
updateViewportWithPreferredLayoutWidth:preferredLayoutHeight:on iOS, orupdateViewport(widthMeasureSpec, heightMeasureSpec)on Android, which a LynxView also calls itself when it's measured. - The global props
screenWidth,screenHeight,viewportWidthandviewportHeight, in CSS pixels, whichLynxViewportreads.
Without the global props, LynxViewport still follows the page's size, a moment later: from the window's resize event, once Lynx has laid the page out.
