Safe Area
On mobile browsers, content can sometimes be obscured by system UI such as the status bar or home indicator. The Apps in Toss SDK provides a function that calculates the Safe Area inset values for the screen in pixels to prevent this situation.
Especially on devices like the iPhone X and later, or some Android devices, system UI often covers content in web apps that use the full screen. Using the function below, you can easily adjust the insets so that content is displayed safely.
SafeAreaInsets
When the screen mode changes safe area You can check the values. You can retrieve safe area information when the screen changes, or subscribe to changes. Using this feature, you can implement a stable UI even in various device environments.
SafeAreaInsets.get(): of the current screen modesafe arearetrieves the values.SafeAreaInsets.subscribe(): Every time the screen mode changessafe areasubscribes to value changes.
Example
import { SafeAreaInsets } from '@apps-in-toss/web-framework';
import { useEffect, useState } from 'react';
interface SafeAreaInsets {
top: number;
bottom: number;
left: number;
right: number;
}
function Page() {
const [safeAreaInsetsValue, setSafeAreaInsetsValue] = useState<SafeAreaInsets>(() => SafeAreaInsets.get());
// Top padding for the navigation bar: safeAreaInsetsValue.top
// Right padding for the navigation bar: safeAreaInsetsValue.right + 10
useEffect(() => {
const cleanup = SafeAreaInsets.subscribe({
onEvent: (insets) => {
setSafeAreaInsetsValue(insets);
},
});
return () => cleanup();
}, []);
// ...
}getSafeAreaInsets
The returned object includes the following properties.
top: the space that must be reserved so it isn't covered by the top status bar.bottom: the space that must be reserved so it isn't covered by the bottom home indicator.
This value is often used when building UI for the entire screen (viewport). For example, you can add spacing to a fixed bottom button or keep the top header from overlapping the status bar.
Example of applying bottom padding
This is an example of adding padding so a fixed bottom button doesn't overlap the home indicator.
Example of applying top padding
This is an example of adding padding so a fixed top header doesn't overlap the status bar.
useSafeAreaInsets
It calculates the screen's Safe Area inset values in pixels so that you can prevent content from being obscured by system UI such as the status bar or home indicator in mobile browsers. useSafeAreaInsets can be used when developing with React Native.
Last updated
Was this helpful?