Onsen UI: Hybrid App & PWA Framework

2.12.8 · active · verified Tue Apr 21

Onsen UI is an open-source, framework-agnostic HTML5 UI framework designed for creating native-feeling Progressive Web Apps (PWAs) and hybrid mobile applications. Built on Web Components, it provides a rich set of pre-built UI components that automatically adapt to both iOS (Flat) and Android (Material) design guidelines from a single codebase. It is currently at version 2.12.8, with an active release cadence focusing on bug fixes, performance improvements, and minor new features within the 2.x major line. Key differentiators include its pure JavaScript/Web Component foundation, allowing usage without any JavaScript framework, alongside official binding packages for popular frameworks like React, Angular, and Vue. It emphasizes ease of use, optimized animations for mobile, and seamless integration with Cordova/PhoneGap.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a minimal Onsen UI application using direct HTML and JavaScript, defining a main page with a toolbar and a button. It includes the necessary CSS and JavaScript files from a CDN, and uses `ons.ready()` to ensure the framework is initialized before interacting with components or displaying notifications.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
  <title>Onsen UI Quickstart</title>
  <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsenui.css">
  <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsen-css-components.css">
  <script src="https://unpkg.com/onsenui/js/onsenui.min.js"></script>
  <script type="text/javascript">
    // Ensure Onsen UI is ready before interacting with its components or global 'ons' object
    ons.ready(function() {
      console.log('Onsen UI is ready!');

      // Example: Pushing a new page programmatically
      document.addEventListener('init', function(event) {
        var page = event.target;

        if (page.id === 'main-page') {
          var button = page.querySelector('#push-button');
          button.onclick = function() {
            // The navigator exists if declared in HTML
            ons.notification.alert('Hello from Onsen UI!');
          };
        }
      });

      // Another way to push a page, e.g., after an event or data load
      // const navigator = document.querySelector('ons-navigator');
      // if (navigator) {
      //   navigator.pushPage('next-page.html', { animation: 'lift' });
      // }
    });
  </script>
</head>
<body>
  <ons-navigator swipeable id="app-navigator" page="main-page.html"></ons-navigator>

  <template id="main-page.html">
    <ons-page id="main-page">
      <ons-toolbar>
        <div class="center">Home</div>
      </ons-toolbar>

      <div class="content">
        <section style="margin: 16px; text-align: center;">
          <p>Welcome to Onsen UI!</p>
          <ons-button id="push-button">Say Hello</ons-button>
        </section>
      </div>
    </ons-page>
  </template>
</body>
</html>

view raw JSON →