🔥 Meet Our New Project: t0ggles - Your Ultimate Project Management Tool! 🔥

Safe Areas

With the iPhone X release and further, Apple introduced so called safe areas, later implemented by Google Chrome with support in Android.

On devices with safe areas (like with top screen notch), app UI must include additional top/bottom spacing (to consider top notch and bottom bar) in portrait orientation and additional left/right spacing (to consider left/right notch) in landscape orientation.

Viewport

If your app targets full screen mobile devices, make sure you have viewport-fit=cover in your <meta name="viewport">, e.g.:

<!DOCTYPE html>
<html lang="en">
  <head>
    ...
    <!-- Make sure you have viewport-fit=cover in content -->
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, viewport-fit=cover"
    />
  </head>

  <body>
    ...
  </body>
</html>

Safe Areas

To tell our components that our app is a full screen app and we need to consider safe areas, we need to add safe-areas class to Konsta UI components parent element, preferably to the root app element.

<!-- App.svelte  -->
<!-- add "safe-areas" class to the app root element -->
<div id="my-app" class="safe-areas">...</div>

If you use Konsta UI's App component, then it can be enabled with safeAreas prop:

<!-- App.svelte -->
<script>
  import { App } from 'konsta/svelte';
</script>

<!-- enable with safeAreas prop -->
<App safeAreas theme="ios">
  <HomePage />
</App>

This is how safe-areas class defined in CSS:

:root: {
  --k-safe-area-left: 0px;
  --k-safe-area-right: 0px;
  --k-safe-area-top: 0px;
  --k-safe-area-bottom: 0px;
}

@supports (left: env(safe-area-inset-left)):  {
  .safe-areas: {
    --k-safe-area-left: env(safe-area-inset-left);
    --k-safe-area-right: env(safe-area-inset-right);
    --k-safe-area-top: env(safe-area-inset-top);
    --k-safe-area-bottom: env(safe-area-inset-bottom);
  }
}

Disable Safe Areas

It can be useful to disable safe areas on modals and side panels which are not full screen elements.

We can disable safe areas on certain elements with the following utility classes:

Class
.no-safe-areasDisable all safe areas on element
.no-safe-areas-topDisable top safe area on element
.no-safe-areas-rightDisable right safe area on element
.no-safe-areas-bottomDisable bottom safe area on element
.no-safe-areas-leftDisable left safe area on element

This is how no-safe-areas classes defined in CSS:

@supports (left: env(safe-area-inset-left)):  {
  .no-safe-areas: {
    --k-safe-area-left: 0px;
    --k-safe-area-right: 0px;
    --k-safe-area-top: 0px;
    --k-safe-area-bottom: 0px;
  }
  .no-safe-areas-top: {
    --k-safe-area-top: 0px;
  }
  .no-safe-areas-right: {
    --k-safe-area-right: 0px;
  }
  .no-safe-areas-bottom: {
    --k-safe-area-bottom: 0px;
  }
  .no-safe-areas-left: {
    --k-safe-area-left: 0px;
  }
}

Safe Spacing & Placement

We can also consider safe areas when placing elements:

ClassCSS
.top-safetop: var(--k-safe-area-top)
.top-[value]-safetop: calc([value] + var(--k-safe-area-top))
.right-saferight: var(--k-safe-area-right)
.right-[value]-saferight: calc([value] + var(--k-safe-area-right))
.bottom-safebottom: var(--k-safe-area-bottom)
.bottom-[value]-safebottom: calc([value] + var(--k-safe-area-bottom))
.left-safeleft: var(--k-safe-area-left)
.left-[value]-safeleft: calc([value] + var(--k-safe-area-left))

And same with margin and padding:

ClassCSS
.pt-safepadding-top: var(--k-safe-area-top)
.pt-[value]-safepadding-top: calc([value] + var(--k-safe-area-top))
.pr-safepadding-right: var(--k-safe-area-right)
.pr-[value]-safepadding-right: calc([value] + var(--k-safe-area-right))
.pb-safepadding-bottom: var(--k-safe-area-bottom)
.pb-[value]-safepadding-bottom: calc([value] + var(--k-safe-area-bottom))
.pl-safepadding-left: var(--k-safe-area-left)
.pl-[value]-safepadding-left: calc([value] + var(--k-safe-area-left))
ClassCSS
.mt-safemargin-top: var(--k-safe-area-top)
.mt-[value]-safemargin-top: calc([value] + var(--k-safe-area-top))
.mr-safemargin-right: var(--k-safe-area-right)
.mr-[value]-safemargin-right: calc([value] + var(--k-safe-area-right))
.mb-safemargin-bottom: var(--k-safe-area-bottom)
.mb-[value]-safemargin-bottom: calc([value] + var(--k-safe-area-bottom))
.ml-safemargin-left: var(--k-safe-area-left)
.ml-[value]-safemargin-left: calc([value] + var(--k-safe-area-left))

For example:

<!--
"left-safe" class will set:
  left: var(--k-safe-area-top);

"bottom-4-safe" class will set:
  bottom: calc(1rem + var(--k-safe-area-bottom));
-->
<div class="left-safe bottom-4-safe">...</div>;
Code licensed under MIT.
2022 © Konsta UI by nolimits4web.