Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add useTitle hook #490

Merged
merged 11 commits into from
Jul 10, 2020
66 changes: 12 additions & 54 deletions packages/hooks/package.json
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,56 +1,14 @@
{
"name": "ahooks",
"version": "2.1.0",
"description": "react hooks library",
"keywords": [
"ahooks",
"umi hooks",
"react hooks"
],
"main": "./lib/index.js",
"module": "./es/index.js",
"types": "./lib/index.d.ts",
"unpkg": "dist/ahooks.js",
"authors": {
"name": "brickspert",
"email": "[email protected]"
},
"publishConfig": {
"registry": "https://registry.npmjs.com/"
},
"repository": "https://github.com/alibaba/hooks",
"homepage": "https://github.com/alibaba/hooks",
"scripts": {
"build": "gulp && webpack-cli"
},
"files": [
"dist",
"lib",
"es",
"package.json",
"README.md"
],
"dependencies": {
"@ahooksjs/use-request": "^2.0.1",
"intersection-observer": "^0.7.0",
"lodash.debounce": "^4.0.8",
"lodash.isequal": "^4.5.0",
"lodash.throttle": "^4.1.1",
"resize-observer-polyfill": "^1.5.1",
"screenfull": "^5.0.0"
},
"peerDependencies": {
"react": "^16.8.6"
},
"devDependencies": {
"@alifd/next": "^1.20.6",
"antd": "^3.26.6",
"react-drag-listview": "^0.1.6",
"umi-request": "^1.0.8"
},
"engines": {
"node": ">=8.0.0"
},
"license": "MIT",
"gitHead": "561ebf0c820fa5fd6f1a8e83d6a8de3474254921"
"dependencies": {
chenbin92 marked this conversation as resolved.
Show resolved Hide resolved
"@alifd/next": "^1.20.6",
"antd": "^3.26.6",
"intersection-observer": "^0.7.0",
"lodash.debounce": "^4.0.8",
"lodash.isequal": "^4.5.0",
"lodash.throttle": "^4.1.1",
"react-drag-listview": "^0.1.6",
"resize-observer-polyfill": "^1.5.1",
"screenfull": "^5.0.0",
"umi-request": "^1.0.8"
}
}
15 changes: 15 additions & 0 deletions packages/hooks/src/useTitle/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { renderHook } from '@testing-library/react-hooks';
import useTitle from '../index';

const setUp = (title: string = 'Page Title') => renderHook(() => useTitle(title));

describe('useTitle', () => {
it('should be defined', () => {
expect(useTitle).toBeDefined();
});

it('test on methods', async () => {
const { result } = setUp();
expect(result.current).toBeUndefined();
});
});
20 changes: 20 additions & 0 deletions packages/hooks/src/useTitle/demo/demo1.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* title: Default usage
* desc: sets title of the page.
*
* title.zh-CN: 默认用法
* desc.zh-CN: 设置页面标题
*/

import React from 'react';
import { useTitle } from 'ahooks';

export default () => {
useTitle('Page Title');

return (
<div>
<p>sets title of the page.</p>
</div>
);
};
33 changes: 33 additions & 0 deletions packages/hooks/src/useTitle/index.en-US.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: useBoolean
chenbin92 marked this conversation as resolved.
Show resolved Hide resolved
nav:
title: Hooks
path: /hooks
group:
title: State
path: /state
order: 11
legacy: /state/use-boolean
---

# useBoolean
chenbin92 marked this conversation as resolved.
Show resolved Hide resolved

A hook that sets title of the page.

## Examples

### Default usage

<code src="./demo/demo1.tsx" />

## API

```javascript
useTitle(value: string);
```

### Params

| Property | Description | Type | Default |
|---------|----------------------------------------------|------------------------|--------|
| value | set a title value | string | |
25 changes: 25 additions & 0 deletions packages/hooks/src/useTitle/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useEffect, useRef } from 'react';

export interface Options {
retainOnUnmount?: boolean;
}

const DEFAULT_OPTIONS: Options = {
retainOnUnmount: false,
};

export default function useTitle(title: string, options: Options = DEFAULT_OPTIONS) {
const titleRef = useRef(document.title);

useEffect(() => {
document.title = title;
}, [title]);

useEffect(() => {
return () => {
if (options && !options.retainOnUnmount) {
chenbin92 marked this conversation as resolved.
Show resolved Hide resolved
document.title = titleRef.current;
}
};
}, []);
}
31 changes: 31 additions & 0 deletions packages/hooks/src/useTitle/index.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: useTitle
nav:
title: Hooks
path: /hooks
group:
title: State
path: /state
order: 11
legacy: /zh-CN/state/use-title
---

# useTitle

用于设置页面标题的 Hook。

## 代码演示

<code src="./demo/demo1.tsx" />

## API

```javascript
useTitle(value: string);
```

### Params

| 参数 | 说明 | 类型 | 默认值 |
|---------|----------------------------------------------|------------------------|--------|
| value | 页面标题 | string | |
awmleer marked this conversation as resolved.
Show resolved Hide resolved