├── .babelrc ├── .gitignore ├── README.md ├── index.jsx └── package.json /.babelrc: -------------------------------------------------------------------------------- 1 | { "presets": ["es2015"] } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .idea/ 3 | .ipr 4 | .iws 5 | *~ 6 | ~* 7 | *.diff 8 | *.patch 9 | *.bak 10 | .DS_Store 11 | Thumbs.db 12 | .project 13 | .*proj 14 | .svn/ 15 | *.swp 16 | *.swo 17 | *.log 18 | *.sublime-project 19 | *.sublime-workspace 20 | node_modules/ 21 | dist/ 22 | tmp/ 23 | .spm-build 24 | .buildpath 25 | .settings 26 | .yml 27 | _site 28 | main.js -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### enquire-js 2 | 3 | 避免服务端渲染报错。 4 | 5 | ### enquireScreen(cb, query): object(handler); 6 | 7 | | name |type |default |description | 8 | |------------|----------------|---------|----------------| 9 | | cb | func | null | 回调接口, cb(bool), bool 为 true 为 mobile | 10 | | query | string | `only screen and (max-width: 767.99px)` | css 的场景判断 | 11 | 12 | ### unenquireScrenn(handler, query); 13 | 14 | | name |type |default |description | 15 | |------------|----------------|---------|----------------| 16 | | handler | object | null | 需要卸载的 `enquireScreen` 返回 handler; | 17 | | query | string | `only screen and (max-width: 767.99px)` | css 的场景判断 | -------------------------------------------------------------------------------- /index.jsx: -------------------------------------------------------------------------------- 1 | let enquireJs; 2 | if (typeof window !== 'undefined') { 3 | const matchMediaPolyfill = mediaQuery => { 4 | return { 5 | media: mediaQuery, 6 | matches: false, 7 | addListener() { 8 | }, 9 | removeListener() { 10 | }, 11 | }; 12 | }; 13 | window.matchMedia = window.matchMedia || matchMediaPolyfill; 14 | enquireJs = require('enquire.js'); 15 | } 16 | 17 | const mobileQuery = 'only screen and (max-width: 767.99px)'; 18 | 19 | export function enquireScreen(cb, query = mobileQuery) { 20 | if (!enquireJs) { 21 | return; 22 | } 23 | 24 | const handler = { 25 | match: () => { 26 | cb && cb(true); 27 | }, 28 | unmatch: () => { 29 | cb && cb(); 30 | }, 31 | }; 32 | enquireJs.register(query, handler); 33 | return handler; 34 | } 35 | 36 | export function unenquireScreen(handler, query = mobileQuery) { 37 | if (!enquireJs) { 38 | return; 39 | } 40 | enquireJs.unregister(query, handler); 41 | } 42 | 43 | export default enquireJs; 44 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "enquire-js", 3 | "version": "0.2.1", 4 | "description": "avoid server-side rendering errors.", 5 | "main": "main.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "prepublish": "babel index.jsx --out-file main.js" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/jljsj/enquire-js.git" 13 | }, 14 | "author": "155259966@qq.com", 15 | "license": "MIT", 16 | "bugs": { 17 | "url": "https://github.com/jljsj/enquire-js/issues" 18 | }, 19 | "homepage": "https://github.com/jljsj/enquire-js#readme", 20 | "dependencies": { 21 | "enquire.js": "^2.1.6" 22 | }, 23 | "devDependencies": { 24 | "babel-cli": "^6.6.5", 25 | "babel-preset-es2015": "^6.6.0" 26 | } 27 | } 28 | --------------------------------------------------------------------------------