ColorUI CSS Library

repository·master·Indexed 11 days ago

https://github.com/weilanwl/coloruicss

A CSS library for rapid UI development targeting UniApp and native mini-programs. It provides utility classes, a theme color palette (ColorList), and pre-built components such as the cu-custom navigation bar to streamline mobile interface creation.

Tokens
5.5K
Snippets
20
Records
21
Agent score
96%

What's inside ColorUI

  1. Install ColorUI for UniApp

    master

    To use ColorUI in your UniApp project, follow these steps:

    1. Download the source code and unzip it.
    2. Copy the /colorui folder from the source root to your project's root directory.
    3. Import the required CSS files in your App.vue file.

    This allows you to use ColorUI components and utility classes throughout your application.

    /* In App.vue */
    <style>
        @import "colorui/main.css";
        @import "colorui/icon.css";
        @import "app.css"; /* Your project css */
        ....
    </style>
  2. Setup ColorUI in a UniApp project

    master

    To use ColorUI in a UniApp project, follow these steps:

    1. Download the source code and extract it to find the /Colorui-UniApp folder.
    2. Copy the /colorui folder from the extracted directory into your project's root directory.
    3. Import the required CSS files in your App.vue file.

    Note: Ensure you import main.css and icon.css before your own project's CSS.

    <style>
    @import "colorui/main.css";
    @import "colorui/icon.css";
    @import "app.css"; /* Your project css */
    ....
    </style>
  3. Use the cu-custom component in UniApp

    master

    The cu-custom component provides a customizable navigation bar.

    1. Configure System Info

    In App.vue, use onLaunch to capture system information (like status bar height) and attach it to the Vue prototype. This is necessary for the component to position itself correctly across different platforms (Android, iOS, WeChat, Alipay).

    2. Disable System Navigation Bar

    In pages.json, set navigationStyle to custom globally:

    "globalStyle": {
      "navigationStyle": "custom"
    }

    3. Register the Component

    In main.js, import and register the component:

    import cuCustom from './colorui/components/cu-custom.vue'
    Vue.component('cu-custom', cuCustom)

    4. Usage

    Use the component in your .vue pages. You can pass background classes and control the back button via props, and use slots for text content.

    Props:

    ParameterDescriptionTypeDefault
    bgColorBackground color class nameString''
    isBackWhether to enable the back buttonBooleanfalse
    bgImagePath to background imageString''

    Slots:

    SlotDescription
    backTextText displayed for the back button
    contentThe middle area (title/content)
    rightRight-side area (limited space in mini-programs)
    <cu-custom bgColor="bg-gradual-blue" :isBack="true">
      <block slot="backText">返回</block>
      <block slot="content">导航栏</block>
    </cu-custom>
  4. Configure Custom Navigation Bar

    master

    ColorUI provides a way to implement custom navigation bars by disabling the system default and using the cu-custom component.

    1. Disable System Navigation

    In your pages.json, set navigationStyle to custom within globalStyle.

    2. Initialize System Information

    You must capture system information (like status bar height) in App.vue and attach it to the Vue prototype so the custom component can calculate correct offsets for different platforms (WeChat, Alipay, etc.).

    3. Register the Component

    In main.js, import and register the cu-custom component globally.

    4. Use the Component

    Use the <cu-custom> component in your page files. You can customize the background, back button behavior, and content via props and slots.

    // pages.json
    "globalStyle": {
        "navigationStyle": "custom"
    }
    // main.js
    import cuCustom from './colorui/components/cu-custom.vue'
    Vue.component('cu-custom', cuCustom)
    <!-- page.vue -->
    <cu-custom bgColor="bg-gradual-blue" :isBack="true">
        <block slot="backText">返回</block>
        <block slot="content">导航栏</block>
    </cu-custom>
  5. Setup ColorUI in a Native Mini-Program

    master

    To use ColorUI in a native mini-program (e.g., WeChat Mini Program):

    1. Integration

    • Existing Project: Download the source, extract /demo, and copy the /colorui folder to your project root. Import main.wxss and icon.wxss in App.wxss.
    • New Project: Download the source, extract /template, and rename the /template folder to your project name. Import this into your development tools.

    2. Configure Navigation

    In App.json, disable the system navigation bar and register the component globally:

    "window": {
      "navigationStyle": "custom"
    },
    "usingComponents": {
      "cu-custom":"/colorui/components/cu-custom"
    }

    3. Capture System Info

    In App.js, use onLaunch to store StatusBar and CustomBar dimensions in globalData to ensure the custom bar aligns with the system's menu button area.

    @import "colorui/main.wxss";
    @import "colorui/icon.wxss";
    @import "app.css"; /* Your project css */
    ....
  6. Access global data via globalData

    master

    The globalData object in app.js is used to store application-wide state that can be accessed from any page or component within the project.

    Commonly used properties in this template include:

    • userInfo: Stores the user's profile information.
    • StatusBar: Stores the system status bar height.
    • Custom: Stores the bounding rect of the menu button (capsule).
    • CustomBar: Stores the calculated height for custom navigation bars.
    // Accessing global data in a page
    const app = getApp();
    console.log(app.globalData.userInfo);
  7. Integrate ColorUI into UniApp

    master

    To use ColorUI in a UniApp project, you must import the core CSS files into your global stylesheet (typically App.vue). This makes the ColorUI utility classes and icons available throughout your entire application.

    In your App.vue file, add the following imports within the <style> block:

    @import "colorui/main.css";
    @import "colorui/icon.css";
    <style>
    	@import "colorui/main.css";
    	@import "colorui/icon.css";
    </style>
  8. Initialize global data and system layout in app.js

    master

    In a WeChat Mini Program environment, the app.js file uses the App() constructor to initialize global state. The demo implementation uses onLaunch to perform two main tasks:

    1. Cloud Initialization: If wx.cloud is available, it initializes the cloud environment with traceUser: true.
    2. System Layout Calculation: It uses wx.getSystemInfo and wx.getMenuButtonBoundingClientRect to calculate dimensions for custom navigation bars. It stores these in globalData to ensure consistent UI rendering across pages.

    Key properties stored in globalData:

    • StatusBar: The height of the status bar.
    • Custom: The bounding client rect of the menu button (capsule).
    • CustomBar: The calculated height/offset required for a custom navigation bar.

    Additionally, globalData contains a ColorList array which serves as a reference for the theme's color palette.

    App({
      onLaunch: function() {
        if (wx.cloud) {
          wx.cloud.init({
            traceUser: true
          })
        }
        wx.getSystemInfo({
          success: e => {
            this.globalData.StatusBar = e.statusBarHeight;
            let capsule = wx.getMenuButtonBoundingClientRect();
            if (capsule) {
              this.globalData.Custom = capsule;
              this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
            } else {
              this.globalData.CustomBar = e.statusBarHeight + 50;
            }
          }
        })
      },
      globalData: {
        ColorList: [
          { title: '嫣红', name: 'red', color: '#e54d42' },
          // ... other colors
        ]
      }
    })
  9. Initialize application state in app.js

    master

    The app.js file serves as the entry point for the application. You can use the onLaunch lifecycle hook to perform initialization tasks such as managing local storage, handling user login via wx.login, and retrieving user information.

    To handle asynchronous user data (like userInfo) that might arrive after a page has already loaded, you can implement a callback pattern by attaching a function to this.userInfoReadyCallback within the App instance.

    App({
      onLaunch: function() {
        // Example: Handling async user info with a callback
        wx.getUserInfo({
          success: res => {
            this.globalData.userInfo = res.userInfo;
            if (this.userInfoReadyCallback) {
              this.userInfoReadyCallback(res);
            }
          }
        });
      },
      globalData: {
        userInfo: null
      }
    })
  10. Use the cu-custom component in Native Mini-Programs

    master

    The cu-custom component is available for native mini-programs via the usingComponents configuration.

    Props:

    ParameterDescriptionTypeDefault
    bgColorBackground color class nameString''
    isBackWhether to enable the back buttonBooleanfalse
    isCustomWhether to enable the left capsuleBooleanfalse
    bgImagePath to background imageString''

    Slots:

    SlotDescription
    backTextText displayed for the back button
    contentThe middle area (title/content)
    rightRight-side area (limited space in mini-programs)
    <cu-custom bgColor="bg-gradual-pink" isBack="{{true}}">
      <view slot="backText">返回</view>
      <view slot="content">导航栏</view>
    </cu-custom>
  11. Configure project settings in project.config.json

    master

    The project.config.json file defines the configuration for the WeChat Mini Program project. The setting object controls several compiler and environment features:

    • urlCheck: Enables/disables URL checking.
    • es6: Enables/disables ES6 support.
    • postcss: Enables/disables PostCSS processing.
    • minified: Enables/disables code minification.
    • newFeature: Enables/disables experimental or new features.
    • autoAudits: Enables/disables automatic audits.
    {
    	"setting": {
    		"urlCheck": true,
    		"es6": true,
    		"postcss": true,
    		"minified": true,
    		"newFeature": true,
    		"autoAudits": false
    	}
    }
  12. Configure WeChat Mini Program project settings

    master

    The project.config.json file is used to configure the WeChat Mini Program environment for the ColorUI demo project. Key settings include enabling ES6 support, PostCSS, and minification to ensure the UI components render correctly and performantly.

    Note: This configuration is specific to the WeChat developer environment and is used to define how the project is compiled and simulated.

    {
    	"setting": {
    		"urlCheck": true,
    		"es6": true,
    		"postcss": true,
    		"minified": true,
    		"newFeature": true
    	},
    	"compileType": "miniprogram",
    	"libVersion": "2.6.4",
    	"simulatorType": "wechat"
    }