Skip to content

Commit fb55734

Browse files
committed
Add examples
1 parent 0f5b871 commit fb55734

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

readme.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Useful React Hooks
1818
* [`useEventListener`](#event-hook) - Hook for binding to an hander to DOM event
1919
* [`useFavicon`](#favicon-hook) - Hook to set a favicon
2020
* [`useTitle`](#title-hook) - Hook to manipulate the page title
21+
* [`useMetaTag`](#meta-tag-hook) - Hook to manipulate meta tags
2122
* [`useScript`](#script-hook) - Hook to add JavaScript to the page
2223
* [`useStyles`](#style-hook) - Hook to add CSS to the page
2324
* [`useStyleSheet`](#stylesheet-hook) - Hook to add a css file to the page
@@ -281,6 +282,26 @@ export default () => {
281282
}
282283
```
283284
285+
## Meta Tag Hook
286+
287+
```jsx
288+
import React, { useEffect } from 'react'
289+
import { useMetaTag } from '@brightleaf/react-hooks'
290+
291+
export default () => {
292+
const [metaValue, setMetaValue] = useMetaTag('description', 'Brightleaf JS React Hooks')
293+
// <meta name="description" content="Brightleaf JS React Hooks">
294+
setMetaValue('Awesome React Hooks from Brightleaf JS')
295+
// <meta name="description" content="Awesome React Hooks from Brightleaf JS">
296+
useMetaTag(
297+
'og:title',
298+
'Brightleaf JS React Hooks',
299+
'property'
300+
)
301+
// <meta property="og:title" content="Brightleaf JS React Hooks">
302+
return <div>The Page</div>
303+
}
304+
```
284305
# NES Hook
285306
286307
Client hook for connecting to an [NES](https://github.com/hapijs/nes) instance

working/hooks/event-example.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import React, { useState } from 'react'
2-
import { useFavicon, useEventListener, useTitle } from '@brightleaf/react-hooks'
2+
import {
3+
useFavicon,
4+
useEventListener,
5+
useTitle,
6+
useCustomEvent,
7+
} from '@brightleaf/react-hooks'
38
import Home from '../app/home'
9+
import Example from './samples/event.md'
410

511
export default () => {
612
useTitle('useEventListener example from @brightleaf/react-hooks')
@@ -9,7 +15,13 @@ export default () => {
915
const onScroll = e => {
1016
setState(window.scrollY)
1117
}
18+
const onBrightleafEvent = e => {
19+
console.log('on brightleaf event', e)
20+
}
1221
useEventListener('scroll', onScroll)
22+
const fireEvent = useCustomEvent('brightleafEvent')
23+
useEventListener('brightleafEvent', onBrightleafEvent)
24+
1325
return (
1426
<div className="App content">
1527
<h2>Scroll the page</h2>
@@ -20,9 +32,17 @@ export default () => {
2032
left: 10,
2133
background: '#CCCCCC',
2234
}}
35+
onClick={e => {
36+
fireEvent({
37+
payload: 'nothing',
38+
})
39+
}}
2340
>
2441
{state} scroll position
2542
</span>
43+
<br />
44+
<div className="content" dangerouslySetInnerHTML={{ __html: Example }} />
45+
<br />
2646
<Home></Home>
2747
</div>
2848
)

working/hooks/online-status.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import React from 'react'
2-
import {
3-
useOnlineStatus,
4-
useTitle,
5-
useMediaQuery,
6-
} from '@brightleaf/react-hooks'
2+
import { useOnlineStatus, useTitle, useMetaTag } from '@brightleaf/react-hooks'
73
export default () => {
84
useTitle('useOnlineStatus example from @brightleaf/react-hooks')
95
const { offline, online } = useOnlineStatus()
10-
6+
useMetaTag(
7+
'description',
8+
'useOnlineStatus hook example from @brightleaf/react-hooks'
9+
)
1110
return (
1211
<div className="App content">
1312
<h3>

working/hooks/samples/event.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Event Hooks Example
2+
3+
```jsx
4+
import React, { useState } from 'react'
5+
import { useEventListener, useTitle, useCustomEvent } from '@brightleaf/react-hooks'
6+
7+
export default () => {
8+
9+
const [state, setState] = useState(window.scrollY)
10+
const onScroll = e => {
11+
setState(window.scrollY)
12+
}
13+
const onBrightleafEvent = e => {
14+
console.info('on brightleaf event', e)
15+
}
16+
useEventListener('scroll', onScroll)
17+
const fireEvent = useCustomEvent('brightleafEvent')
18+
useEventListener('brightleafEvent', onBrightleafEvent)
19+
20+
return (
21+
<div className="App content">
22+
<h2>Scroll the page</h2>
23+
<span
24+
style={{
25+
position: 'fixed',
26+
top: 10,
27+
left: 10,
28+
background: '#CCCCCC',
29+
}}
30+
onClick={e => {
31+
fireEvent({
32+
payload: 'nothing',
33+
})
34+
}}
35+
>
36+
{state} scroll position
37+
</span>
38+
<div>...page content</div>
39+
</div>
40+
)
41+
}
42+
```

0 commit comments

Comments
 (0)