首頁 >web前端 >js教程 >QuickUI:輕量級前端框架

QuickUI:輕量級前端框架

Mary-Kate Olsen
Mary-Kate Olsen原創
2024-12-26 08:23:09395瀏覽

QuickUI: Lightweight Frontend Framework

GitHub

(原名PDQuickUI,從0.6.0版本開始更名為QuickUI)

QuickUI是一個源自PDRenderKit的前端渲染框架,專注於增強前端框架功能。

透過整合虛擬DOM,重寫渲染邏輯,提高渲染效率,實現更快的資料觀察和自動更新。

該專案從 PDRenderKit 中刪除了原型擴展,以確保相容性和效能,使其適合複雜的應用程式。

它提供模組和非模組版本,並將 PDRenderKit 中的授權從 GPL-3.0 更改為 MIT。

特徵

  • 清晰的架構:將UI與資料邏輯分離,使其更易於維護。
  • 程式碼簡潔:減少冗餘程式碼,增強可讀性。
  • 自動渲染:自動監控資料變化與更新,盡量減少手動操作。
  • 輕量級:在小於 20kb 的檔案大小內保持完整功能。

安裝

  • 從 npm 安裝

    npm i @pardnchiu/quickui
    
  • 包含來自 CDN

    • 直接包含QuickUI

      <!-- Version 0.6.0 and above -->
      <script src="https://cdn.jsdelivr.net/npm/@pardnchiu/quickui@[VERSION]/dist/QuickUI.js"></script>
      
      <!-- Version 0.5.4 and below -->
      <script src="https://cdn.jsdelivr.net/npm/pdquickui@[VERSION]/dist/PDQuickUI.js"></script>
      
    • 模組版本

      // Version 0.6.0 and above
      import { QUI } from "https://cdn.jsdelivr.net/npm/@pardnchiu/quickui@[VERSION]/dist/QuickUI.esm.js";
      
      // Version 0.5.4 and below
      import { QUI } from "https://cdn.jsdelivr.net/npm/pdquickui@[VERSION]/dist/PDQuickUI.module.js";
      

用法

  • 初始化 QUI

    const app = new QUI({
        id: "", // Specify rendering element
        data: {
            // Custom DATA
        },
        event: {
            // Custom EVENT
        },
        when: {
            before_render: function () {
                // Stop rendering
            },
            rendered: function () {
                // Rendered
            },
            before_update: function () {
                // Stop updating
            },
            updated: function () {
                // Updated
            },
            before_destroy: function () {
                // Stop destruction
            },
            destroyed: function () {
                // Destroyed
            }
        }
    });
    

概述

自動渲染:偵測到資料變更時自動重新載入。

屬性概述

Attribute Description
{{value}} Inserts text into HTML tags and automatically updates with data changes.
:path Used with the temp tag to load HTML fragments from external files into the current page.
:html Replaces the element's innerHTML with text.
:for Supports formats like item in items, (item, index) in items, (key, value) in object. Iterates over data collections to generate corresponding HTML elements.
:if
:else-if
:elif
:else
Displays or hides elements based on specified conditions, enabling branching logic.
:model Binds data to form elements (e.g., input), updating data automatically when input changes.
:hide Hides elements based on specific conditions.
:animation Specifies transition effects for elements, such as fade-in or expand, to enhance user experience.
:mask Controls block loading animations, supporting `true
:[attr] Sets element attributes, such as ID, class, image source, etc.
Examples: :id/:class/:src/:alt/:href...
:[css] Sets element CSS, such as margin, padding, etc. Examples: :background-color, :opacity, :margin, :top, :position...
@[event] Adds event listeners that trigger specified actions upon activation.
Examples: @click/@input/@mousedown...

文字替換

{{價值}}

  • index.html

    npm i @pardnchiu/quickui
    
  • 結果

    <!-- Version 0.6.0 and above -->
    <script src="https://cdn.jsdelivr.net/npm/@pardnchiu/quickui@[VERSION]/dist/QuickUI.js"></script>
    
    <!-- Version 0.5.4 and below -->
    <script src="https://cdn.jsdelivr.net/npm/pdquickui@[VERSION]/dist/PDQuickUI.js"></script>
    

:html

  • index.html

    // Version 0.6.0 and above
    import { QUI } from "https://cdn.jsdelivr.net/npm/@pardnchiu/quickui@[VERSION]/dist/QuickUI.esm.js";
    
    // Version 0.5.4 and below
    import { QUI } from "https://cdn.jsdelivr.net/npm/pdquickui@[VERSION]/dist/PDQuickUI.module.js";
    
  • 結果

    const app = new QUI({
        id: "", // Specify rendering element
        data: {
            // Custom DATA
        },
        event: {
            // Custom EVENT
        },
        when: {
            before_render: function () {
                // Stop rendering
            },
            rendered: function () {
                // Rendered
            },
            before_update: function () {
                // Stop updating
            },
            updated: function () {
                // Updated
            },
            before_destroy: function () {
                // Stop destruction
            },
            destroyed: function () {
                // Destroyed
            }
        }
    });
    

插入區塊

> [!注意]
>確保在測試時停用瀏覽器中的本機檔案限製或使用即時伺服器。

:小路

  • test.html

    <h1>{{ title }}</h1>
    
        const app = new QUI({
            id: "app",
            data: {
                title: "test"
            }
        });
    
    
  • index.html

        <h1>test</h1>
    
    
  • 結果

        const app = new QUI({
            id: "app",
            data: {
                html: "<b>innerHtml</b>"
            }
        });
    
    

循環渲染

:為了

  • index.html

            <b>innerHtml</b>
    
    
  • 結果

    <h1>path heading</h1>
    <p>path content</p>
    
  • 結果

        const app = new QUI({
            id: "app"
        });
    
    

條件渲染

  • index.html

        <h1>path heading</h1>
        <p>path content</p>
    
    
  • 結果:標題 = 1

        <ul>
            <li>{{ item }} {{ CALC(index + 1) }}</li>
        </ul>
    
        const app = new QUI({
            id: "app",
            data: {
                ary: ["test1", "test2", "test3"]
            }
        });
    
    
  • 結果:heading = null && isH2 = true

        <li>
    

Nest loop

  • index.html

      <li> {{ key }}: {{ val.name }}
      • {{ item.name }}
        • {{ CALC(index1 + 1) }}. {{ item1.name }} - ${{ item1.price }}
    const app = new QUI({ id: "app", data: { obj: { food: { name: "Food", ary: [ { name: 'Snacks', ary1: [ { name: 'Potato Chips', price: 10 }, { name: 'Chocolate', price: 8 } ] }, { name: 'Beverages', ary1: [ { name: 'Juice', price: 5 }, { name: 'Tea', price: 3 } ] } ] }, home: { name: 'Home', ary: [ { name: 'Furniture', ary1: [ { name: 'Sofa', price: 300 }, { name: 'Table', price: 150 } ] }, { name: 'Decorations', ary1: [ { name: 'Picture Frame', price: 20 }, { name: 'Vase', price: 15 } ] } ] } } } });
  • 結果:標題 = 3 && isH2 = null

    <ul>
        <li>food: Food
            <ul>
                <li>Snacks
                    <ul>
                        <li>1. Potato Chips - </li>
                        <li>2. Chocolate - </li>
                    </ul>
                    </li>
                <li>Beverages
                    <ul>
                        <li>1. Juice - </li>
                        <li>2. Tea - </li>
                    </ul>
                </li>
            </ul>
        </li>
        <li>home: Home
            <ul>
                <li>Furniture
                    <ul>
                        <li>1. Sofa - 0</li>
                        <li>2. Table - 0</li>
                    </ul>
                </li>
                <li>Decorations
                    <ul>
                        <li>1. Picture Frame - </li>
                        <li>2. Vase - </li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
    
    
  • 結果:heading = null && isH2 = null

        <h1>{{ title }} {{ heading }}</h1>
        <h2>{{ title }} {{ heading }}</h2>
        <h3>{{ title }} {{ heading }}</h3>
        <h4>{{ title }} {{ heading }}</h4>
    
        const app = new QUI({
            id: "app",
            data: {
                heading: [Number|null],
                isH2: [Boolean|null],
                title: "test"
            }
        });
    
    

模板渲染

  • index.html

        <h1>test 1</h1>
    
    
  • 結果

        <h2>test </h2>
    
    

綁定

    <h3>test 3</h3>

活動

    <h4>test </h4>

CSS

> [!注意]
>支援使用:[CSS屬性]進行簡單設置,直接將資料綁定到樣式屬性。

  • index.html

        const test = new QUI({
            id: "app",
            data: {
                hint: "hint 123",
                title: "test 123"
            },
            render: () => {
                return `
                    "{{ hint }}",
                    h1 {
                        style: "background: red;", 
                        children: [ 
                            "{{ title }}"
                        ]
                    }`
            }
        })
    
    
  • 結果:

        hint 123
        <h1>test 123</h1>
    
    

功能

長度()

  • index.html

    
        test
    
    
        const app = new QUI({
            id: "app",
            data: {
                password: null,
            },
            event: {
                show: function(e){
                    alert("Password:", app.data.password);
                }
            }
        });
    
    
  • 結果

        test
    
    
        const app = new QUI({
            id: "app",
            event: {
                test: function(e){
                    alert(e.target.innerText + " clicked");
                }
            }
        });
    
    

計算()

  • index.html

        test
    
        const app = new QUI({
            id: "app",
            data: {
                width: "100px",
                color: "red"
            }
        });
    
    
  • 結果

        test
    
    

上() / 下()

  • index.html

        <p>Total: {{ LENGTH(array) }}</p>
    
        const app = new QUI({
            id: "app",
            data: {
                array: [1, 2, 3, 4]
            }
        });
    
    
  • 結果

        <p>Total: 4</p>
    
    

日期(數字,格式)

  • index.html

        <p>calc: {{ CALC(num * 10) }}</p>
    
        const app = new QUI({
            id: "app",
            data: {
                num: 1
            }
        });
    
    
  • 結果

        <p>calc: 10</p>
    
    

延遲載入

:延遲載入

  • index.html

        <p>{{ UPPER(test1) }} {{ LOWER(test2) }}</p>
    
        const app = new QUI({
            id: "app",
            data: {
                test1: "upper",
                test2: "LOWER"
            }
        });
    
    
  • 結果

        <p>UPPER lower</p>
    
    

SVG 替換

  • 測試.svg

        <p>{{ DATE(now, YYYY-MM-DD hh:mm:ss) }}</p>
    
        const app = new QUI({
            id: "app",
            data: {
                now: Math.floor(Date.now() / 1000)
            }
        });
    
    
  • index.html

        <p>2024-08-17 03:40:47</p>
    
    
  • 結果

        <img>
    
        const app = new QUI({
            id: "app",
            data: {
                image: "test.jpg"
            },
            option: {
                lazyload: true // Enable image lazy loading: true|false (default: true)
            }
        });
    
    

i18n

> [!注意]
>如果format是對象,則直接配置多語言內容。
>如果格式是字串,則透過 fetch 動態載入語言檔案。

  • en.json

        <img src="test.jpg">
    
    
  • index.html

    
    
  • 結果 i18nLang = zh

        const app = new QUI({
            id: "app",
            data: {
                svg: "test.svg",
            },
            option: {
                svg: true  // Enable SVG file transformation: true|false (default: true)
            }
        });
    
    
  • 結果 i18nLang = en

    
    

生命週期掛鉤

{
    "greeting": "Hello",
    "username": "Username"
}

資料擷取

npm i @pardnchiu/quickui

創作者

邱敬幃 Pardn Chiu

執照

此項目已取得專有許可證許可。

您只能根據最終使用者授權協議 (EULA) 中指定的條款使用、安裝和執行此軟體。


©️ 2024 邱敬幃 Pardn Chiu

以上是QuickUI:輕量級前端框架的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn