GithubHelp home page GithubHelp logo

huachaoliu / omi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tencent/omi

0.0 2.0 0.0 6.21 MB

Omi === Preact + Scoped CSS + Store System + Native Support in 3kb javascript.

Home Page: http://omijs.org

License: Other

JavaScript 98.12% HTML 0.51% TypeScript 1.37%

omi's Introduction

English | 简体中文

Omi Version

Omi === Preact + Scoped CSS + Store System + Native Support in 3kb javascript.

Omi

Omi has prosperous ecology and strong features through the second-hand preact developments:

  • Super tiny size and Super fast
  • Extensive React/Preact/Omi compatibility
  • Scoped CSS, made css selector simple, lazy evaluation and package with it's component
  • Each component has a update method, you can continue to use setState
  • Store system, data and logic of components management for omi without other frameworks dependencies
  • Server side render, Native Suppport, ES6+, JSX, VDOM, React DevTools, HMR ...
  • Create website with no build configuration if you use omi-cli, the same as multi-page support of create-react-app

Getting Started

Hello Omi

import { render, Component } from 'omi';

class Hello extends Component {
    render() {
        return <div> {this.props.name}</div>
    }
}

class App extends Component {
    install() {
        this.name = 'Omi'
    }

    handleClick = (e) => {
        this.name = 'Hello Omi !' 
        this.update()
    }

    style() {
        return `h3{
                    cursor:pointer;
                    color: ${Math.random() > 0.5 ? 'red' :'green'};
                }`
        }

    staticStyle() {
        return `div{
                    font-size:20px;
                }`
        }
	
    render() {
        return (
		<div>
			<Hello name={this.name}></Hello>
			<h3 onclick={this.handleClick}>Scoped css and event test! click me!</h3>
		</div>
		)
    }
}

render(<App />, 'body')

Different to preact, you need not to import { h } from 'omi'.

Tell Babel to transform JSX into Omi.h() calls:

{
    "presets": ["env", "omi"]
}

You need install env and omi preset for the config:

"babel-preset-env": "^1.6.0",
"babel-preset-omi": "^0.1.1",

Scoped CSS

What'S the different between style and staticStyle method ? For example:

render() {
        return (
		<div>
			<Hello name={this.name}></Hello>
			<Hello name={this.name}></Hello>
			<Hello name={this.name}></Hello>
		</div>
		)
    }

The style method will render three times to html head element, the staticStyle will render one times only ! When you update the component style method will rerender, but the staticStyle will not rerender.

If you want to use the scoped css but you did not want write it in your javascript, you may need to-string-loader, see the omi-cli config

{
    test: /[\\|\/]_[\S]*\.css$/,
    use: [
        'to-string-loader',
        'css-loader'
    ]
}

If your css file name is begin with _, the css content will use to-string-loader. For example:

import Omi from 'omi'
//typeof style is string
import style from './_index.css' 

class App extends Omi.Component {

  staticStyle() {
    return style
  }

  style() {
    return `
      code{
        color: ${Math.random() > 0.5 ? 'red' : 'blue'}
      }
      .app-logo{
        cursor: pointer; 
      }
    `
  }

  render() {
    return (
      <div class="app">
        <header class="app-header">
          <h1 class="app-title">Welcome to Omi</h1>
        </header>
      </div>
    )
  }
}

You can use the feature in the project created by omi-cli with no configuration.

Store

import { render, Component } from 'omi';

class Hello extends Component {
  render() {
    return <div>{this.props.name}</div>
  }
}

class App extends Component {

  handleClick = () => {
    this.$store.rename('Hello Omi !')
  }

  render() {
    return (
      <div>
        <Hello name={this.$store.name}></Hello>
        <button onclick={this.handleClick}>Click me to call this.$store.rename('Hello Omi !') </button>
      </div>
    )
  }
}

class AppStore {
  constructor(data, callbacks) {
    this.name = data.name || ''
    this.onRename = callbacks.onRename || function () { }
  }

  rename(name) {
    this.name = name
    this.onRename()
  }
}

const app = new App()
const store = new AppStore({ name: 'Omi' }, {
  onRename: () => {
    app.update()
  }
})

render(app, document.body, { store })

You can use this.$store in all components to access the data or data logic.

Lifecycle

Lifecycle method When it gets called
componentWillMount / install before the component gets mounted to the DOM
componentDidMount / installed after the component gets mounted to the DOM
componentWillUnmount / uninstall     prior to removal from the DOM                  
componentWillReceiveProps before new props get accepted
shouldComponentUpdate before render(). Return false to skip render
componentWillUpdate / beforeUpdate before render()
componentDidUpdate / afterUpdate after render()

CLI

$ npm i omi-cli -g               # install cli
$ omi init your_project_name     # init project, you can also exec 'omi init' in an empty folder
$ cd your_project_name           # please ignore this command if you executed 'omi init' in an empty folder
$ npm start                      # develop
$ npm run build                  # release

Server Side Render:

$ omi init-ssr your_project_name     # init ssr project, you can also exec 'omi init-ssr' in an empty folder
$ cd your_project_name               # please ignore this command if you executed 'omi init' in an empty folder
$ npm run dev                        # develop
$ npm run build                      # release
$ npm start                          # release 

Install

npm i omi

or get it from CDN:

Official Plugins

Links

License

MIT © dntzhang

omi's People

Contributors

xcatliu avatar yanceyou avatar pasturn avatar xcodebuild avatar nobey avatar dntzhang avatar xland avatar zhangsanshi avatar

Watchers

James Cloos avatar Huachao Liu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.