Cross Platform Detection

Overview

NexaUI menyediakan sistem deteksi platform yang memungkinkan aplikasi web Anda beradaptasi secara otomatis dengan berbagai perangkat. Sistem ini mendeteksi jenis perangkat (mobile, tablet, desktop) dan menyajikan tampilan yang sesuai.

Breakpoints

Sistem menggunakan breakpoints berikut untuk menentukan jenis perangkat:


const MOBILE_MAX_WIDTH = 768px;   // Mobile devices
const TABLET_MAX_WIDTH = 1024px;  // Tablet devices
> 1024px                         // Desktop devices

Template Structure

Struktur folder untuk template responsif:


public/
  ├── pages/
  │   ├── home/
  │   │   ├── index.html   # Desktop version
  │   │   ├── mobile.html  # Mobile version
  │   │   └── tablet.html  # Tablet version
  │   └── about/
  │       ├── index.html
  │       ├── mobile.html
  │       └── tablet.html

Device Detection

Sistem mendeteksi perangkat menggunakan kombinasi:

  • Ukuran layar (width & height)
  • Breakpoints responsif
  • Real-time resize detection

// Device detection code
detectDeviceType() {
    const screenInfo = {
        width: window.innerWidth,
        height: window.innerHeight
    };

    let deviceType = "desktop";
    if (screenInfo.width <= 768) {
        deviceType = "mobile";
    } else if (screenInfo.width <= 1024) {
        deviceType = "tablet";
    }

    return {
        type: deviceType,
        screen: screenInfo
    };
}

Template Variables

Variabel yang tersedia dalam template:


{$deviceInfo}        // Informasi lengkap perangkat
{$isMobile}         // true jika perangkat mobile
{$isTablet}         // true jika perangkat tablet
{$isDesktop}        // true jika perangkat desktop
{$screenWidth}      // Lebar layar dalam pixel
{$screenHeight}     // Tinggi layar dalam pixel

Conditional Rendering

Contoh penggunaan dalam template:


{if $isMobile}
    
{elseif $isTablet}
    
{else}
    
{/if}

{if $screenWidth <= 768}
    
{/if}

Real-time Adaptation

Fitur adaptasi real-time:

  • Deteksi perubahan ukuran layar secara otomatis
  • Debouncing untuk optimasi performa
  • Reload konten otomatis saat ukuran berubah
  • Mempertahankan state aplikasi saat beradaptasi

Best Practices

  • Selalu sediakan versi desktop (index.html) sebagai fallback
  • Gunakan media queries CSS untuk penyesuaian style tambahan
  • Test pada berbagai perangkat dan ukuran layar
  • Optimalkan gambar dan aset untuk setiap jenis perangkat
  • Pertimbangkan kecepatan koneksi pada perangkat mobile

Example Implementation



{if $isMobile}
{else}
{/if}
{if $screenWidth <= 768} Mobile version {else} Desktop version {/if}