diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000000..3331d70496 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +# http://editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false + +[Makefile] +indent_style = tab \ No newline at end of file diff --git a/.fatherrc.ts b/.fatherrc.ts new file mode 100644 index 0000000000..f5246dcd91 --- /dev/null +++ b/.fatherrc.ts @@ -0,0 +1,16 @@ +import { IBundleOptions } from 'father-build'; + +const options: IBundleOptions = { + entry: 'src/index.tsx', + esm: 'rollup', + cjs: 'rollup', + preCommit: { + eslint: true, + prettier: true + }, + doc: { + title: '@umijs/hooks' + } +}; + +export default options; \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000000..a6bad6c9ff --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/yarn.lock +/package-lock.json +/dist +/.docz +/node_modules +/.history +/.idea +/coverage \ No newline at end of file diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000000..d1a9b85a9f --- /dev/null +++ b/.prettierignore @@ -0,0 +1,5 @@ +**/*.md +**/*.mdx +package.json +.umi +.umi-production \ No newline at end of file diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000000..3d2bd998df --- /dev/null +++ b/.prettierrc @@ -0,0 +1,11 @@ +{ + "singleQuote": true, + "trailingComma": "all", + "printWidth": 100, + "overrides": [ + { + "files": ".prettierrc", + "options": { "parser": "json" } + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index b5c902a0f6..48f74014af 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,13 @@ -# hooks +# @umi/hooks + react hooks library + +## Usage + +```sh +umi block https://github.com/https://github.com/umijs/hooks/tree/master/hooks +``` + +## LICENSE + +MIT diff --git a/package.json b/package.json new file mode 100755 index 0000000000..6eeb450bdb --- /dev/null +++ b/package.json @@ -0,0 +1,40 @@ +{ + "name": "hooks", + "version": "0.0.1", + "description": "react hooks library", + "keywords": [ + "umi hooks", + "react hooks" + ], + "main": "dist/index.js", + "module": "dist/index.esm.js", + "authors": { + "name": "brickspert", + "email": "brickspert.fjl@alipay.com" + }, + "repository": "https://github.com/umijs/hooks", + "scripts": { + "start": "npm run dev", + "dev": "father doc dev", + "build": "father build", + "test": "father test", + "cov": "father test --coverage", + "help": "father help", + "precommit": "father pre-commit", + "pub:npm": "npm run build & father publish --registry=https://www.npmjs.com", + "pub:doc": "father doc build & father doc deploy" + }, + "peerDependencies": { + "react": "^16.8.0" + }, + "devDependencies": { + "@testing-library/react-hooks": "^1.0.4", + "@types/jest": "^24.0.15", + "father": "^2.13.3", + "typescript": "^3.3.3" + }, + "engines": { + "node": ">=8.0.0" + }, + "license": "MIT" +} \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx new file mode 100644 index 0000000000..8052928686 --- /dev/null +++ b/src/index.tsx @@ -0,0 +1,3 @@ +import useTitle from './useTitle'; + +export { useTitle }; diff --git a/src/useTitle/__tests__/index.test.ts b/src/useTitle/__tests__/index.test.ts new file mode 100644 index 0000000000..fef87031a7 --- /dev/null +++ b/src/useTitle/__tests__/index.test.ts @@ -0,0 +1,16 @@ +import { renderHook } from '@testing-library/react-hooks'; +import useTitle from '../index'; + +describe('useTitle', () => { + it('should be defined', () => { + expect(useTitle).toBeDefined(); + }); + + const hook = renderHook(props => useTitle(props), { initialProps: 'My page title' }); + + it('should update document title', () => { + expect(document.title).toBe('My page title'); + hook.rerender('My other page title'); + expect(document.title).toBe('My other page title'); + }); +}); diff --git a/src/useTitle/index.mdx b/src/useTitle/index.mdx new file mode 100644 index 0000000000..89572a5a01 --- /dev/null +++ b/src/useTitle/index.mdx @@ -0,0 +1,29 @@ +--- +name: useTitle +route: / +order: -1 +sidebar: true +--- + +# useTitle + +React side-effect hook that set title of the page. + +## Usage + +### Demo +``` +import {useTitle} from '@umi/hooks'; + +const Demo = () => { + useTitle('Hello world!'); + + return null; +}; +``` + +## Reference + +``` +useTitle(title: string); +``` \ No newline at end of file diff --git a/src/useTitle/index.ts b/src/useTitle/index.ts new file mode 100644 index 0000000000..0dbc328664 --- /dev/null +++ b/src/useTitle/index.ts @@ -0,0 +1,9 @@ +import { useEffect } from 'react'; + +const useTitle = (title: string) => { + useEffect(() => { + document.title = title; + }, [title]); +}; + +export default useTitle; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000000..7ab02548a4 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "target": "esnext", + "module": "esnext", + "moduleResolution": "node", + "importHelpers": true, + "jsx": "react", + "esModuleInterop": true, + "sourceMap": true, + "baseUrl": ".", + "strict": true, + "paths": { + "@/*": ["src/*"] + }, + "allowSyntheticDefaultImports": true + } +} \ No newline at end of file