├── .babelrc ├── .gitignore ├── .npmignore ├── README.md ├── dist └── react-singleton.js ├── example ├── .babelrc ├── components │ └── Hello.js ├── index.html ├── index.js ├── package.json └── webpack.config.js ├── package.json └── src └── react-singleton.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015", "react", "stage-1"] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | .tmp 4 | .npmignore 5 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | example 2 | node_modules 3 | .DS_Store 4 | .tmp 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-singleton 2 | 3 | ## Demo 4 | You can find example in folder [example](https://github.com/Caesor/react-singleton/tree/master/example) 5 | 6 | ## Usage with React 7 | 8 | ### 1、Install the package 9 | `npm install react-singleton --save` 10 | 11 | ### 2、Import component 12 | ``` 13 | // ES6 14 | import Singleton from 'react-singleton' 15 | // ES5 16 | var Singleton = require('react-singleton').default 17 | ``` 18 | 19 | ###3、Decorate the target component 20 | ``` 21 | class Alert extends Component {...} 22 | 23 | export default new Singleton(Alert) 24 | ``` 25 | 26 | ###4、Using as your need 27 | ``` 28 | // Example 1: 29 | 30 | import Alert from 'component/Alert' 31 | 32 | Alert.show(); 33 | // you can pass the props with a object in show method 34 | Alert.show({ 35 | title: xxx, 36 | content: xxx 37 | }) 38 | // destroy the DOM node 39 | Alert.hide(); 40 | 41 | // Example 2: 42 | 43 | import Tips from 'components/Tips' 44 | 45 | // Tips will disappear in 2 second on default. 46 | Tips.popup(); 47 | // You can change the timer as you need 48 | Tips.popup({ 49 | text: xxx 50 | }, 3000); 51 | ``` 52 | -------------------------------------------------------------------------------- /dist/react-singleton.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); 8 | 9 | var _react = require('react'); 10 | 11 | var _react2 = _interopRequireDefault(_react); 12 | 13 | var _reactDom = require('react-dom'); 14 | 15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 16 | 17 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 18 | 19 | var Singleton = function () { 20 | function Singleton(component) { 21 | _classCallCheck(this, Singleton); 22 | 23 | this.dom = null; 24 | this.component = component; 25 | this.instance = null; 26 | } 27 | 28 | _createClass(Singleton, [{ 29 | key: 'show', 30 | value: function show(option) { 31 | if (!this.dom) { 32 | this.dom = document.createElement('div'); 33 | document.body.appendChild(this.dom); 34 | } 35 | this.instance = (0, _reactDom.render)(_react2.default.createElement(this.component, option), this.dom); 36 | this.instance.setState({ 37 | show: true 38 | }); 39 | } 40 | }, { 41 | key: 'hide', 42 | value: function hide() { 43 | var _this = this; 44 | 45 | if (this.instance) { 46 | this.instance.setState({ 47 | show: false 48 | }, function () { 49 | setTimeout(function () { 50 | (0, _reactDom.unmountComponentAtNode)(_this.dom); 51 | }, 100); 52 | }); 53 | } 54 | } 55 | }]); 56 | 57 | return Singleton; 58 | }(); 59 | 60 | exports.default = Singleton; -------------------------------------------------------------------------------- /example/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015", "react", "stage-1"] 3 | } 4 | -------------------------------------------------------------------------------- /example/components/Hello.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react' 2 | import Singleton from 'react-singleton' 3 | 4 | class Hello extends Component { 5 | state = { 6 | show: true 7 | } 8 | render() { 9 | return ( 10 |
This is a hello tips
12 |