SafeArea
On a phone the notch and the rounded corners cover the top of the screen. At the bottom the view is covered by the home bar and by the on-screen keyboard while the person types. SafeArea adds to each edge of its box a padding equal to the covered part of that edge to keep your buttons and your message box visible. Wrap the whole view in it or only the part that touches the edges of the screen.
import { useWorld } from './useWorld.js';
// The component: copy this part.
/**
* SafeArea: keeps its content clear of what covers the screen. On a phone,
* the notch, the rounded corners and the home bar cover the edges, and the
* on-screen keyboard covers the bottom while the person types. The webapp
* says how much is covered, in pixels. SafeArea leaves that much room on each side.
* @param {object} props
* @param {object} props.world - The world, from useWorld.
* @param {number} [props.padding=0] - More room on every side, in pixels, on top of the safe areas.
* @param {object} [props.style] - More style for the box, e.g. `{ minHeight: '100vh' }`.
* @param {React.ReactNode} props.children - What to keep clear.
*/
export function SafeArea({ world, padding = 0, style, children }) {
// The safe areas, on the sides where the view touches the edge of the screen (0 on a desktop).
const { insets, keyboard } = world.webapp;
// The bottom gets the larger of the keyboard and the home bar, because an open keyboard covers the home bar too.
const bottom = Math.max(insets.bottom, keyboard);
return (
<div style={{
boxSizing: 'border-box',
paddingTop: padding + insets.top,
paddingRight: padding + insets.right,
paddingBottom: padding + bottom,
paddingLeft: padding + insets.left,
transition: 'padding-bottom 150ms ease-out',
...style,
}}>
{children}
</div>
);
}
// Using it, on a fake world.
// A fake world to build with: no account, no network.
const harbour = { name: 'Harbour', now: { text: 'Three boats in, one out.' } };
export default function App() {
// While building: the fake world, in a webapp that pretends to be a phone with a notch
// and a home bar. In the webapp: useWorld()
const { world, error } = useWorld({
fake: { world: harbour },
webapp: { insets: { top: 44, right: 0, bottom: 34, left: 0 }, keyboard: 0 },
});
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
const { insets, keyboard } = world.webapp;
return (
// The tinted band is the room that SafeArea leaves. Your view is the plain box inside.
<SafeArea world={world} style={{ maxWidth: 360, borderRadius: 14, background: 'rgba(240, 165, 78, 0.3)' }}>
<div style={{ padding: 12, background: 'Canvas', display: 'grid', gap: 6 }}>
<strong>{world.name}</strong>
<span>{world.now?.text}</span>
<span style={{ fontSize: 13, opacity: 0.75 }}>
Safe areas: top {insets.top}px, right {insets.right}px, bottom {insets.bottom}px, left {insets.left}px
</span>
<span style={{ fontSize: 13, opacity: 0.75 }}>Keyboard: {keyboard}px</span>
</div>
</SafeArea>
);
}import { connectToHost } from '@unaiverse/gui-kit';
// The component: copy this part.
/**
* Keeps the content of `container` clear of what covers the screen. On a
* phone, the notch, the rounded corners and the home bar cover the edges,
* and the on-screen keyboard covers the bottom while the person types. The
* webapp says how much is covered, in pixels. This function leaves that much
* room on each side of `container` and changes it whenever the webapp says so.
* @param {HTMLElement} container - The element to keep clear (often your whole view).
* @param {object} world - The world, from connectToHost.
* @param {{padding?: number}} [options] - More room on every side, in pixels, on top of the safe areas.
* @returns {() => void} Stops following the world and gives `container` its padding back.
*/
export function safeArea(container, world, { padding = 0 } = {}) {
const before = container.style.padding;
container.style.boxSizing = 'border-box';
container.style.transition = 'padding-bottom 150ms ease-out';
// Called at once, then after every change of the world (the screen is part of it).
const stop = world.subscribe((node) => {
// The safe areas, on the sides where the view touches the edge of the screen (0 on a desktop).
const { insets, keyboard } = node.webapp;
// The bottom gets the larger of the keyboard and the home bar, because an open keyboard covers the home bar too.
const bottom = Math.max(insets.bottom, keyboard);
container.style.padding = `${padding + insets.top}px ${padding + insets.right}px ${padding + bottom}px ${padding + insets.left}px`;
});
return () => {
stop();
container.style.padding = before;
};
}
// Using it, on a fake world.
// A fake world to build with: no account, no network.
const harbour = { name: 'Harbour', now: { text: 'Three boats in, one out.' } };
// While building: the fake world, in a webapp that pretends to be a phone with a notch
// and a home bar. In the webapp: const world = await connectToHost();
const world = await connectToHost({
fake: { world: harbour },
webapp: { insets: { top: 44, right: 0, bottom: 34, left: 0 }, keyboard: 0 },
});
// The tinted band is the room that safeArea leaves. Your view is the plain box inside.
const view = document.createElement('div');
Object.assign(view.style, { maxWidth: '360px', borderRadius: '14px', background: 'rgba(240, 165, 78, 0.3)' });
const inside = document.createElement('div');
Object.assign(inside.style, { padding: '12px', background: 'Canvas', display: 'grid', gap: '6px' });
const name = document.createElement('strong');
const now = document.createElement('span');
const sides = document.createElement('span');
const keyboard = document.createElement('span');
for (const small of [sides, keyboard]) Object.assign(small.style, { fontSize: '13px', opacity: '0.75' });
inside.append(name, now, sides, keyboard);
view.append(inside);
document.body.append(view);
safeArea(view, world);
world.subscribe((node) => {
const { insets } = node.webapp;
name.textContent = node.name;
now.textContent = node.now?.text ?? '';
sides.textContent = `Safe areas: top ${insets.top}px, right ${insets.right}px, bottom ${insets.bottom}px, left ${insets.left}px`;
keyboard.textContent = `Keyboard: ${node.webapp.keyboard}px`;
});The fake webapp of the example has a notch of 44 pixels at the top and a home bar of 34 pixels at the bottom. The tinted bands are the padding that SafeArea adds.
The webapp reports the covered sizes in pixels in two fields of world.webapp:
insetsholdstop,right,bottomandleftfor the sides where the view touches the edge of the screenkeyboardis the height of the keyboard over the view (0 while it is closed and always 0 on a desktop).
Both fields reach subscribe like any other change of the world. The bottom padding follows the keyboard with a transition of 150 ms. The bottom gets the larger of insets.bottom and keyboard (an open keyboard also covers the home bar) instead of their sum (The look of the webapp and the phone screen).
Props
| Prop | Default | What it is |
|---|---|---|
world | (required) | The world, from useWorld. |
padding | 0 | Extra padding in pixels that every side adds to its safe area. |
style | none | Extra style for the box, for example { minHeight: '100vh' } or a flex column. |
children | The content to keep clear. |
Parameters
safeArea(container, world, options) subscribes to the world and recomputes the padding of container on every change.
| Parameter | What it is |
|---|---|
container | The element to keep clear (often the whole view). |
world | The world, from connectToHost. |
options.padding | Extra padding in pixels on every side. Default 0. |
It returns a function that unsubscribes from the world and restores the padding that container had before.
Change it
- A chat that fills the phone: make the box a full-height column (
height: '100vh',display: 'flex'andflexDirection: 'column'in itsstyle) with theMessageListfilling the free space (flex: 1) and theMessageBoxat the bottom. An opening keyboard then shortens the list and keeps the box right above the keyboard. - Only the bottom: when your view has its own header, keep
paddingBottomand drop the other three sides. - Test a keyboard while you build: pass
webapp: { keyboard: 300 }toconnectToHosttogether with your fake world.
See also
- Keep the message box above the keyboard: the same idea in a few lines.
- A layout for phone and desktop.
FollowTheme: the light or dark theme of the webapp.